Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Dynamic Code Generation with support for generics

Is there any tool which provides Java dynamic code generation and that also supports generics?

Javassist for example, is the kind of tool that I need, but it does not support generics.

I wrote a small lib which uses the Java 6 Compiler API, however as far as I know it depends on JDK. Is there a way to specify another compiler? Or to ship with my application only the parts that I need to invoke with the Java Compiler API?

like image 403
halfwarp Avatar asked Aug 29 '10 21:08

halfwarp


2 Answers

It seems you can manipulate and read generic info with Javaassist. See

http://www.mail-archive.com/[email protected]/msg101222.html

[jboss-user] [Javassist user questions] - Re: Altering Generics Information of Methods using Javassist SimonRinguette Thu, 20 Dec 2007 12:22:14 -0800

I have done further reading on how this is implemented by the compiler and finally found out the answer I was looking for.

You can defenitely do that with javaassist. The key class is javassist.bytecode.SignatureAttribute.

From a CtMethod, i've obtained the methodInfo I add a Signature attribute. You can do it with something like:

CtMethod method = ....
   MethodInfo methodInfo = method.getMethodInfo();
   SignatureAttribute signatureAttribute = new 
SignatureAttribute(methodInfo.getConstPool(),
   "()Ljava/util/List<Ljava/lang/String;>;");
   methodInfo.addAttribute(signatureAttribute);

If your more interesed in reading the signature with the generics inside, you can use the methodInfo.getAttribute(SignatureAttribute.tag).

I hope this helped.

like image 93
Kiril Avatar answered Sep 18 '22 16:09

Kiril


If you are comfortable with writing bytecode then ASM is quite a good library for that kind of thing. That will let you generate a class file on the fly without having to worry about the nitty-gritty of the classfile format. You can then use a classloader to dynamically load it into your application.

like image 21
Richard Warburton Avatar answered Sep 22 '22 16:09

Richard Warburton