简体   繁体   English

动态地使用泛型扩展Java类

[英]Extending a java class with generics dynamically

is possible to use aspectj or javassist for extending a class with a generic class? 是否可以使用aspectj或javassist扩展具有通用类的类? for example i have a class with: 例如我有一个类:

@someAnnotation(GenerictTypeParameter.class)
public aClass{
  ...
}

so the idea is to search all classes with @someAnnotation and then extending them with another class and get this: 所以这个想法是用@someAnnotation搜索所有类,然后用另一个类扩展它们并得到:

public aClass extends anotherClass<GenericTypeParameter>{
  ...
}

You can do so using Byte Buddy , if you are willing to use another code generation library. 如果您愿意使用另一个代码生成库,则可以使用Byte Buddy进行操作。 With Byte Buddy, you can: 使用Byte Buddy,您可以:

new ByteBuddy()
  .subclass(TypeDescription.Generic.Builder
              .parameterizedType(AnotherClass.class, GenericTypeParameter.class)
              .build())
  .make();

You can simply resolve the relevant type from the annotation. 您可以简单地从注释中解析相关类型。 Byte Buddy also allows you to redefine existing types if this is what you are trying to achieve. 如果这是您要实现的目标,则Byte Buddy还允许您重新定义现有类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM