开发者

Java: dynamic properties

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. Groov开发者_运维技巧y 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.


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.


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).


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!


It is possible using Dynamic Proxies. It is also possible to do this on GAE.

First create the class "SomeObject" that exposes methods to get and set property values (i.e. getProperty(name) and setProperty(name, value)).

Then, create an interface "PropertyModel" that contains the methods that you would like your generated objects to have.

Call TransparentProxy.newInstance(someObjectInstance, MyPropertyModel.class) to create a dynamic proxy.

What happens is that Java will extend your object someObjectInstance with the specified interface (btw. you could specify more than one). When you call a method on the proxy object, the method invocation will be redirected to the "invoke(...)" method defined below, you'll need to modify that code to handle both getters and setters and include some exception handling etc. But in general, this is the way dynamic proxies work in Java.

public class TransparentProxy implements InvocationHandler
{
private final SomeObject someObject;

private TransparentProxy(SomeObject someObject)
{
    this.someObject = someObject;
}

public static Object newInstance(SomeObject someObject,
    Class<? extends PropertyModel> propertyModel)
{
    return Proxy.newProxyInstance(someObject.getClass().getClassLoader(),
        new Class[] { propertyModel }, new TransparentProxy(someObject));
}

public Object invoke(Object proxy, Method method, Object[] args)
    throws Throwable
{
    return this.someObject.getProperty(method.getName());
}
}


There is DynaClass library that can be used this way to dynamically create JavaBeans

Map<Object, Object> properties = new HashMap<Object, Object>();
roperties.put("title", "The Italian Job");
roperties.put("dateOfRelease", "new GregorianCalendar(1969, 0, 1).getTime()");
Object movieBean = BeanCreator.createBeanFromMap(properties);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜