开发者

How do I extend Java classes by Reflection?

I have two Strings:

String a="org.test.A";
String b="org.test.B";

I get a class by Reflection

Class aClass = Class.forName(a);

I want开发者_StackOverflow社区 aClass extends b, like:

Class okClass=aClass.extends(b);//how to implement this?

how to implement this?

how to get okClass?

thanks!


Apart from using a JDK dynamic proxy, which works only by interface, you can use CGLIB or javassist for extending classes at runtime.


Generally speaking you want to be able to create new classes (not objects) at runtime. You can use bytecode engineering or java compiler api for that.


You could start by reading up on dynamic proxies. Proxies do not extend classes, but they do implement interfaces which you can map on your class implementation through the invocation handler.


As said before I'd recommend to look at CGLIB but there is also javassist which is a class library for editing bytecodes...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜