我有两个注释。
@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;
}
这是我的Activity类。
@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);
}
}
答案 0 :(得分:1)
试试这个:
List<? extends Element> enclosedElements = ((DeclaredType) subElement.asType()).asElement().getEnclosedElements();
for (Element childElement : enclosedElements) {
OmegaExtra annotation = element.getAnnotation(OmegaExtra.class);
if(annotation != null) {
....
}
}