1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.AnnotationTree; 22 import com.sun.source.tree.AssignmentTree; 23 import com.sun.source.tree.BlockTree; 24 import com.sun.source.tree.ExpressionTree; 25 import com.sun.source.tree.MethodTree; 26 import com.sun.source.tree.ModifiersTree; 27 import com.sun.source.tree.StatementTree; 28 import com.sun.source.tree.VariableTree; 29 import java.io.FileInputStream ; 30 import java.io.FileNotFoundException ; 31 import java.io.IOException ; 32 import java.util.ArrayList ; 33 import java.util.Collections ; 34 import java.util.List ; 35 import javax.lang.model.element.Modifier; 36 import org.netbeans.api.java.source.transform.Transformer; 37 import org.netbeans.junit.NbTestSuite; 38 import org.openide.filesystems.FileStateInvalidException; 39 40 44 public class AnnotationOnLocVarTest extends GeneratorTest { 45 46 47 public AnnotationOnLocVarTest(String name) { 48 super(name); 49 } 50 51 public static NbTestSuite suite() { 52 NbTestSuite suite = new NbTestSuite(AnnotationOnLocVarTest.class); 53 return suite; 54 } 55 56 protected void setUp() throws Exception { 57 super.setUp(); 58 testFile = getFile(getSourceDir(), getSourcePckg() + "AnnOnLocalVar.java"); 59 } 60 61 public void testAddAnnToLocVar() throws IOException { 62 System.err.println("testAddAnnToLocVar"); 63 process( 64 new Transformer<Void , Object >() { 65 public Void visitMethod(MethodTree node, Object p) { 66 super.visitMethod(node, p); 67 if ("<init>".contentEquals(node.getName())) { 68 BlockTree body = node.getBody(); 69 List <? extends StatementTree> statements = body.getStatements(); 70 VariableTree statement = (VariableTree) statements.get(1); 71 ModifiersTree mods = statement.getModifiers(); 73 List <AnnotationTree> anns = new ArrayList <AnnotationTree>(1); 74 List <AssignmentTree> attribs = new ArrayList <AssignmentTree>(4); 75 attribs.add(make.Assignment(make.Identifier("id"), make.Literal(Integer.valueOf(666)))); 76 attribs.add(make.Assignment(make.Identifier("synopsis"), make.Literal("fat"))); 77 attribs.add(make.Assignment(make.Identifier("engineer"), make.Literal("PaF"))); 78 attribs.add(make.Assignment(make.Identifier("date"), make.Literal("2005"))); 79 anns.add(make.Annotation(make.Identifier("AnnotationType"), attribs)); 80 model.setElement(statement, model.getElement(statement)); 81 model.setType(statement, model.getType(statement)); 82 changes.rewrite(mods, make.Modifiers(mods.getFlags(), anns)); 83 } 84 return null; 85 } 86 } 87 ); 88 assertFiles("testAddAnnToLocVar_AnnotationOnLocVarTest.pass"); 89 } 90 91 public void testAddLocVarWithAnn() throws java.io.IOException , FileStateInvalidException { 92 System.err.println("testAddLocVarWithAnn"); 93 process( 94 new Transformer<Void , Object >() { 95 public Void visitMethod(MethodTree node, Object p) { 96 super.visitMethod(node, p); 97 if ("<init>".contentEquals(node.getName())) { 98 BlockTree bt = node.getBody(); 99 List <StatementTree> statements = new ArrayList <StatementTree>(bt.getStatements()); 100 statements.remove(0); List <AnnotationTree> anns = new ArrayList <AnnotationTree>(1); 102 List <AssignmentTree> attribs = new ArrayList <AssignmentTree>(4); 103 attribs.add(make.Assignment(make.Identifier("id"), make.Literal(Integer.valueOf(777)))); 104 attribs.add(make.Assignment(make.Identifier("synopsis"), make.Literal("thin"))); 105 attribs.add(make.Assignment(make.Identifier("engineer"), make.Literal("Snoopy"))); 106 attribs.add(make.Assignment(make.Identifier("date"), make.Literal("2001"))); 107 anns.add(make.Annotation(make.Identifier("AnnotationType"), attribs)); 108 statements.add(0, make.Variable( 109 make.Modifiers(Collections.singleton(Modifier.FINAL), anns), 110 "testVar", 111 make.Identifier("java.util.List"), 112 make.NewClass( 113 null, 114 Collections.EMPTY_LIST, 115 make.Identifier("java.util.ArrayList"), 116 Collections.singletonList(make.Literal(Integer.valueOf(3))), 117 null 118 ) 119 )); 120 BlockTree njuBlock = make.Block(statements, false); 121 model.setPos(njuBlock, model.getPos(bt)); 122 changes.rewrite(bt, njuBlock); 123 } 124 return null; 125 } 126 } 127 ); 128 assertFiles("testAddLocVarWithAnn_AnnotationOnLocVarTest.pass"); 129 } 130 131 String getSourcePckg() { 132 return "org/netbeans/test/codegen/"; 133 } 134 135 String getGoldenPckg() { 136 return "org/netbeans/jmi/javamodel/codegen/AnnotationOnLocVarTest/"; 137 } 138 } 139 | Popular Tags |