java 动态泛型,Java动态代码生成,支持泛型

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?

解决方案

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

[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;");

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值