Sae*_*umi 3 java annotation-processing
我有一个这样的界面:
interface MyInterface<V>{
}
Run Code Online (Sandbox Code Playgroud)
例如,我所有的带注释的类都以@MyAnnotation不同的方式实现了这个接口。
//first way
Class1 implement MyInterface<SomeClass>
//second way
AbstractClass<V> implement MyInterface<V>
Class2 extends AbstractClass<SomeClass>
//third way
ConcreteClass implement MyInterface<SomeClass>
Class3 extends ConcreteClass
Run Code Online (Sandbox Code Playgroud)
好吧,我有TypeElement1,2 和 3 类,我想找到类型变量的限定名称V。
我试过这个,但它返回V而不是SomeClass.
TypeElement class1 = ...
while(reachToMyInterface){
for (TypeMirror m : ((DeclaredType) class1.asType()).getTypeArguments()) {
print(m.toString()) // prints V
}
class1 = getItsSuperClass();
}
Run Code Online (Sandbox Code Playgroud)
编辑:这种方法也有同样的问题:
for (Element element : roundEnv.getElementsAnnotatedWith(Haha.class)) {
if (element instanceof TypeElement) {
TypeElement te = (TypeElement) element;
TypeElement currentType = te;
while(currentType!=null){
for (TypeMirror typeMirror : currentType .getInterfaces()) {
if (typeMirror instanceof DeclaredType) {
DeclaredType dclt = (DeclaredType) typeMirror;
for (TypeMirror argument : dclt.getTypeArguments()) {
print(argument);
}
}
}
currentType = getSuperClass(currentType);
}
}
private TypeElement getSuperClass(TypeElement typeElement) {
if (!(typeElement.getSuperclass() instanceof DeclaredType)) return null;
DeclaredType declaredAncestor = (DeclaredType) typeElement.getSuperclass();
return (TypeElement) declaredAncestor.asElement();
}
Run Code Online (Sandbox Code Playgroud)
这很痛苦,但可以做到。
使用这些类型:
@Retention(RUNTIME) @Target(TYPE) public @interface Haha {}
interface MyInterface<V>{}
@Haha public class StringImpl implements MyInterface<String> {}
Run Code Online (Sandbox Code Playgroud)
这是一个注释处理器,它为 上的@Haha注释打印“java.lang.String” StringImpl:
public class Proc extends AbstractProcessor {
@Override public Set<String> getSupportedAnnotationTypes() {
return Collections.singleton("bar.Haha");
}
@Override public SourceVersion getSupportedSourceVersion() {
return SourceVersion.RELEASE_8;
}
@Override public boolean process(final Set<? extends TypeElement> annotations,
final RoundEnvironment roundEnv) {
for (Element element : roundEnv.getElementsAnnotatedWith(Haha.class)) {
if (element instanceof TypeElement) {
TypeElement te = (TypeElement) element;
for (TypeMirror typeMirror : te.getInterfaces()) {
if (typeMirror instanceof DeclaredType) {
DeclaredType dclt = (DeclaredType) typeMirror;
for (TypeMirror argument : dclt.getTypeArguments()) {
System.out.println(argument);
}
}
}
}
}
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
到达那里有很多测试和摆弄,我用这个测试来调试这个过程:
public class ProcTest {
@Test
public void run() {
String basePath = "/path/to/src/folder/";
List<String> args = asList("Haha", "MyInterface", "StringImpl")
.stream()
.map(s -> basePath +"bar/" + s + ".java")
.collect(Collectors.toList());
args.addAll(0, asList("-processor", Proc.class.getName()));
String[] flags = args.toArray(new String[3]);
ToolProvider.getSystemJavaCompiler()
.run(System.in, System.out, System.err, flags);
}
}
Run Code Online (Sandbox Code Playgroud)
如果在调试模式下运行它,则可以在注释处理器内设置断点。这真的帮助我更好地理解了事情。