我想测试我的实用程序类 ElementUtils,但我不知道如何将类作为元素获取。在 AnnotationProcessors 中,我使用以下代码获取元素
Set<? extends Element> elements = roundEnvironment.getElementsAnnotatedWith(annotation);
但由于 RoundEnvironment 在测试中不可用,因此这不是一个选项。
有什么方法可以将类作为 javax.lang.model.element.Element 获取吗?
您可以使用 JUnit @Rule 在测试期间调用编译器运行,此时您可以使用 Types 和 Elements 实用程序类来获取 TypeMirror 和 Element 实例。
这样的规则已经创建并且是http://github.com/google/compile-testing http://github.com/google/compile-testing可以这样使用:
public @Rule CompilationRule rule = new CompilationRule();
private Elements elements;
private Types types;
@Before
public void setup() {
elements = rule.getElements();
types = rule.getTypes();
}
@Test void testStuff() {
TypeElement typeElement = elements.getTypeElement("some.fully.qualified.ClassName");
// do stuff.
}
您可以执行哪些操作(无论是在测试中还是以其他方式)TypeMirror 和 Element 实例,可以在http://github.com/google/auto http://github.com/google/auto公共子模块中的项目,其中包含用于方便管理这些 java.lang.model 类的实用程序。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)