views:

739

answers:

3

Ladies & Gentlemen,

I´m new to Java, forgive me if it´s obvious, but I haven´t found much about it.

I´d like to create dynamic properties (variables) for a class at runtime (define an object that can be altered at runtime by adding or changing properties and methods).


Reason: I want to store a data model in GAE that can be extended dynamically after compilation of the app (yes, the DataStore allows that). What properties should be added are stored in the DataStore as well (It´s like using Robots to built Robots...funny).

Python allows me to add properties at Runtime. Groovy seems to allow that, too. The only thing in the "pure" Java world indicating in that direction seems to be "Dynamic Proxies".

But I couldn´t figure out yet if they do the trick.

+3  A: 

Java doesn't have the capability to dynamically add properties. Nor does it have the ability to dynamically create classes at runtime or change them at runtime. Java is strongly and statically typed. The best you can do is put such properties into a Map or similar.

Edit: Ok, apparently some clarifications are in order. The OP specifically mentioned GAE, which none of these methods will work on but I'll mention them since some seem to take exception to their absence.

The Java Compiler API (Java 6+) allows you to compile Java classes at runtime. Technically you could write out a Java source file to look exactly how you want, compile it and load it.

Java bytecode libraries can rewrite classes at runtime. This is used by such libraries as JPA (and others). You could modify classes this way.

What the OP is referring to however is a) in reference to working on GAE and b) more in the order of how Javascript allows you to modify classes or particular instances at runtime by dynamically adding, removing or change properties. Java certainly doesn't do this and specifically doesn't on the GAE.

The above is not an exception to this just like casting a class to char * in C++ so you can read private members doesn't mean C++ doesn't have private members. You're essentially bypassing the Java runtime with both of these methods even though they're part of Java.

cletus
You can of course create new Java classes at runtime and to some extent modify existing classes using byte code instrumentation.
jarnbjo
*"Nor does it have the ability to dynamically create classes at runtime"* This is certainly possible with the JSR-199 Compiler API (maybe not on GAE but it **is** possible).
Pascal Thivent
@jarnbjo, @pascal - yes, but it is a darned lot of hard work to implement!! If the OP really *needs* to do dynamic properties, he'd be better off sticking with Groovy or Python.
Stephen C
+2  A: 

Java doesn't support it. Your best bet is to store/manage in some external datastore which you can access from inside the Java code. As a basic and builtin example, you can make use of java.util.Properties API which you load on every request, or cache and reload at timed intervals, or reload programmatically. You can then store the key-value pairs in a .properties file which you just place in the classpath. Here is a Sun tutorial about the subject.

A properties file can look like

key1=value1
key2=value2
key3=value3

If you put it in the classpath, then you can load it as

Properties properties = new Properties();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
properties.load(classLoader.getResourceAsStream("file.properties"));
String key1 = properties.getProperty("key1"); // value1

Other alternatives are for example XML files (which you can access using any Java XML API) or just a database (which you can access using JDBC API).

BalusC
+1  A: 

I don't know if this is an option on GAE (I didn't checked the restrictions) and if this will suit your needs but maybe have a look at the BeanGenerator class from CGLIB (an alternative to the ugly DynaBean from BeanUtils). Quoting "Death to DynaBeans" (have a look at the post):

Not one to let my CGLIB Golden Hammer go to waste, I have checked in a BeanGenerator class into CVS. You use it like so:

BeanGenerator bg = new BeanGenerator();
bg.addProperty("foo", Double.TYPE);
bg.addProperty("bar", String.class);
Object bean = bg.create();

The generated class is an real JavaBean, which means you can use standard bean utilities. This includes all of the classes in the net.sf.cglib.beans package (BeanCopier, BeanMap, and BulkBean). Do your part to end the tyranny of DynaBeans!

Pascal Thivent