Question

I'm looking for a solution to create an interface in runtime. I don't really know if this is possible in anyway.

Problem:

I've got a OSGi service which publishes a Map<String,String> where the key defines an action for this service. I want to publish this service directly as Hessianservice with spring but for this, I need to delcare an interface. Now I would like to create this interface at runtime.

Was it helpful?

Solution

It's possible to create interfaces dynamically for example by generating it with a bytecode manipulation library such as ASM. But it won't be possible to use that interface, because no code can be compiled against it (unless you generate dynamically also the code which uses it).

What is it that you are trying to do?

OTHER TIPS

You can't really do that (unless you involve byte-code maniuplation/creation and I don't think that's the best path).

What good would a dynamically created interface do if you have nothing that could access that interface?

Or in other words: nothing can compile against a dynamically created interface (since it doesn't exist at compile-time, obviously). So who would be using it?

Picked the following answer from another question. The example actually writes a new class, so may be this will help you.

JDK6 has a Java compiler API. However, it's not necessarily very easy to use.

A quick google pulled up this example usage.

Interfaces and classes exist solely to help compilers find possible bugs. If you want to make this interface at runtime, you have no compiler, co it won't find you the bugs, so why do you need this interface?

In such a situation just publish implementation of some generic interface, which can look like: interface GenericInterface { Object invokeMethod(String name, Object... arguments); } That's the only interface you need, and you can create it at compile time! Only implementations of it you may need to create at runtime, eg with java.lang.reflect.Proxy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top