views:

118

answers:

6
+1  Q: 

class at runtime

Is there a way to create Java classes @ at runtime (classes methods n variables), with using Java reflection API

A: 

Sure there is. You need a java.lang.Class instance initially, for the target class you wish to create. Depending on your structure, this might either be passed in by a caller (if they're supplying the concrete class they want created), or you can statically access the class variable (e.g. MyFooImpl.class).

The simplest way is to call Class.newInstance(). This invokes the default, no-arg constructor (assuming there is one for the class; if not it throws an exception).

If you need to invoke a particular constructor with some argument, you need to call Class.getConstructor() to get a Constructor instance, which you can then call newInstance on.

In all cases you'll need to deal with reflection exceptions that you wouldn't get if invoking the constructor directly.

Big edit: I assume your question was about creating instances of a class via reflection. However I'm beginning to think that you're asking about defining new classes through at runtime. If so, then reflection won't help you here - you'd need to invoke a compiler programatically, which I believe can be done but I'm not 100% on the details. I think you'd also have to go through some hoops to get the ClassLoader to pick up your new class too.

Andrzej Doyle
Yeah but this is for existing class to create objects from it, I want to create a class.
MMRUser
+4  A: 

You can't do that using reflection. You need a bytecode manipulation library, like Jakarta BCEL.

Massimiliano Fliri
Can you add some examples
MMRUser
There is a nice example inside the tutorial. Take a look: http://jakarta.apache.org/bcel/manual.html#HelloWorldBuilder .
Massimiliano Fliri
A: 

You can create the source code string and compile it to an class file using Janino.

Markus Lausberg
+2  A: 

The standard Java API provides a set of static methods, that allows you to dynamically create a class that implements one (or many) interfaces. Those methods are part of the class java.lang.reflect.Proxy.

barjak
A: 

As people have already mentioned, there's no way of creating new classes at runtime using reflection. One library that I know is used by different mocking libraries and the likes is cglib.

abyx
+1  A: 

What do you require this for?

Interpreting the question in a very loose manor I can think of four likely options.

If you have a class that you add something too you might find that Aspect-oriented programming is what you are really after.

If you have an interface that you want to dynamically implement (as posted by barjak) what you want is java.lang.reflect.Proxy. This does not let create "code" at runtime but rather allows you link existing code to to a interface.

Finally (at three I know) you have actually building random classes at runtime. This you will need something like cglib or BCEL. While there are cases when this is required it is IMO rare.

One other option is that you don't really need runtime but rather build time. In this case you might be able to use annotations and apt (Java 5) / Processor (Java 6).

mlk
+1 - was just about to say the same thing, generating byte code should only really be needed in very few situations.
Nick Holt
I'd also add the scripting APIs (http://java.sun.com/javase/6/docs/api/) added in 1.6 but ultimately your decision is likely to be guided by how the behaviour of the 'class' is going to be defined.
Nick Holt
I'd forgotten about the scripting API but yes that is a good point.
mlk