0

我有两个注释。

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OmegaExtra {
    String value() default "";
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface OmegaExtraModel {

    String value() default "";

}

我有一些课。像例子

class Map {

@OmegaExtra
String param1;

@OmegaExtra
String param2;

@OmegaExtra
String param3;

}

这是我的活动课。

@OmegaActivity
    public class ShareFilesActivity extends AppCompatActivity {

    @OmegaExtra
    String value;

    @OmegaExtraModel
    Map map;

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_share_files);
            AppOmegaIntentBuilder.inject(this);
        }

    }

找到所有@OmegaExtra 元素对我来说不是问题。但是我怎么能找到子元素

@OmegaExtraModel
Map map;

我的意思是我应该在@OmegaExtraModel 中找到所有像@OmegaExtra 一样注释的元素。

@Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(OmegaActivity.class));
for(Element element: elements) {
List<? extends Element> subList = element.getEnclosedElements();
     for (Element subElement : subList) {
     OmegaExtra annotation = subElement.getAnnotation(OmegaExtraModel.class);
     }
}
4

1 回答 1

1

尝试这个:

List<? extends Element> enclosedElements = ((DeclaredType) subElement.asType()).asElement().getEnclosedElements();
 for (Element childElement : enclosedElements) {
 OmegaExtra annotation = element.getAnnotation(OmegaExtra.class);
 if(annotation != null) {
 ....
 }
 }
于 2018-01-15T11:37:06.903 回答