使用Generators创建类时,可以发现类型的所有子类。您可以在GWT Showcase源代码中找到此技术(请参阅full code):
JClassType cwType = null;
try {
cwType = context.getTypeOracle().getType(ContentWidget.class.getName());
} catch (NotFoundException e) {
logger.log(TreeLogger.ERROR, "Cannot find ContentWidget class", e);
throw new UnableToCompleteException();
}
JClassType[] types = cwType.getSubtypes();
我想做类似的事情,而不是扩展类(或实现接口)
public class SomeWidget extends ContentWidget { ... }
,我还可以通过注释小部件吗?
@MyAnnotation(...)
public class SomeWidget extends Widget { ... }
然后找到所有使用@MyAnnotation注释的小部件?我找不到像JAnnotationType.getAnnotatedTypes()
这样的方法,但也许我只是失明了?
注意:我能够使用reflections.getTypesAnnotatedWith(SomeAnnotation.class)
使用Google Reflections库,但我更喜欢使用GeneratorContext,特别是因为这样做效果更好在DevMode中重新加载应用程序时。
答案 0 :(得分:8)
是 - 最简单的方法是遍历所有类型,并检查它们是否有注释。您可能还有其他规则(是公开的,非抽象的),也应该在那时完成。
for (JClassType type : oracle.getTypes()) {
MyAnnotation annotation = type.getAnnotation(MyAnnotation.class);
if (annotation != null && ...) {
// handle this type
}
}
可以使用TypeOracle
从GeneratorContext
获取context.getTypeOracle()
个实例。
请注意,这只会让您访问源路径上的类型。也就是说,只有当前可用的类型基于继承的模块和正在使用的<source>
标记。