How to extend Java classes with Reflection?

I have two lines:

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

I get a Reflection class

 Class aClass = Class.forName(a); 

I want aClass to extend b, for example:

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

how to implement this?

How to get okClass?

thanks!

+5
java
source share
4 answers

Besides using the JDK dynamic proxy , which works only on the interface, you can use CGLIB or javassist to extend classes at runtime.

+8
source share

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 what.

+4
source share

You can start by reading dynamic proxies . Proxies do not extend classes, but they implement interfaces that can be mapped to class implementations using call handlers.

+3
source share

As mentioned earlier, I recommend looking at CGLIB, but there is also a javassist , which is a class library for editing byte codes ...

0
source share

All Articles