GenerationUtilsTest.java 文件源码

java
阅读 41 收藏 0 点赞 0 评论 0

项目:incubator-netbeans 作者:
public void testCreateAnnotation() throws Exception {
    TestUtilities.copyStringToFileObject(testFO,
            "package foo;" +
            "public class TestClass {" +
            "}");
    runModificationTask(testFO, new Task<WorkingCopy>() {
        public void run(WorkingCopy copy) throws Exception {
            GenerationUtils genUtils = GenerationUtils.newInstance(copy);
            ClassTree classTree = (ClassTree)copy.getCompilationUnit().getTypeDecls().get(0);
            AnnotationTree annotationTree = genUtils.createAnnotation("java.lang.SuppressWarnings",
                    Collections.singletonList(genUtils.createAnnotationArgument(null, "unchecked")));
            ClassTree newClassTree = genUtils.addAnnotation(classTree, annotationTree);
            annotationTree = genUtils.createAnnotation("java.lang.annotation.Retention",
                    Collections.singletonList(genUtils.createAnnotationArgument(null, "java.lang.annotation.RetentionPolicy", "RUNTIME")));
            newClassTree = genUtils.addAnnotation(newClassTree, annotationTree);
            copy.rewrite(classTree, newClassTree);
        }
    }).commit();
    runUserActionTask(testFO, new Task<CompilationController>() {
        public void run(CompilationController controller) throws Exception {
            TypeElement typeElement = SourceUtils.getPublicTopLevelElement(controller);
            assertEquals(2, typeElement.getAnnotationMirrors().size());
            SuppressWarnings suppressWarnings = typeElement.getAnnotation(SuppressWarnings.class);
            assertNotNull(suppressWarnings);
            assertEquals(1, suppressWarnings.value().length);
            assertEquals("unchecked", suppressWarnings.value()[0]);
            Retention retention = typeElement.getAnnotation(Retention.class);
            assertNotNull(retention);
            assertEquals(RetentionPolicy.RUNTIME, retention.value());
        }
    });
}
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号