问题
I need to extend on runtime a class with a generic signature.
For example, the class to be extended is:
public class A<T> {}
I need to get this dynamically:
public class B extends A<String> {}
I cannot use Proxy because I need to add the obtained class to the ClassLoader. I'm trying with javassist but I have no idea of how make it in the correct way.
回答1:
If you know type only at runtime, then you will have to use casting. Like:
class TokenCounterMapper
extends Mapper<Object, Object, Object, Object>{
public void map(Object key, Object value, Context context){
if (value instanceOf String){
String castedString = (String)value;
...
}
}
}
来源:https://stackoverflow.com/questions/16894571/extend-a-generic-class-dynamically-in-java