1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.AnnotationTree; 22 import com.sun.source.tree.ClassTree; 23 import com.sun.source.tree.CompilationUnitTree; 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.Tree; 28 import com.sun.source.tree.TypeParameterTree; 29 import com.sun.source.tree.VariableTree; 30 import com.sun.tools.javac.util.List; 31 import java.io.File ; 32 import java.util.Collections ; 33 import javax.lang.model.element.Modifier; 34 import javax.lang.model.element.TypeElement; 35 import javax.lang.model.type.TypeKind; 36 import org.netbeans.api.java.source.CancellableTask; 37 import org.netbeans.api.java.source.JavaSource; 38 import org.netbeans.api.java.source.JavaSource.Phase; 39 import org.netbeans.api.java.source.TreeMaker; 40 import org.netbeans.api.java.source.WorkingCopy; 41 import org.netbeans.jackpot.test.TestUtilities; 42 import org.netbeans.junit.NbTestSuite; 43 import org.openide.filesystems.FileUtil; 44 45 50 public class TwoModificationsTest extends GeneratorTest { 51 52 53 public TwoModificationsTest(String name) { 54 super(name); 55 } 56 57 public static NbTestSuite suite() { 58 NbTestSuite suite = new NbTestSuite(); 59 suite.addTestSuite(TwoModificationsTest.class); 60 return suite; 61 } 62 63 66 public void testAddAnnAndMethod() throws Exception { 67 testFile = new File (getWorkDir(), "Test.java"); 68 TestUtilities.copyStringToFile(testFile, 69 "package hierbas.del.litoral;\n\n" + 70 "import java.io.*;\n\n" + 71 "public class Test {\n" + 72 " public void taragui() {// what a ... problem\n" + 73 " }\n" + 74 "}\n" 75 ); 76 String golden = 77 "package hierbas.del.litoral;\n\n" + 78 "import java.io.*;\n\n" + 79 "@javax.jws.WebService\n" + 80 "public class Test {\n" + 81 " public void taragui() {// what a ... problem\n" + 82 " }\n\n" + 83 " public void writeExternal(final Object arg0) throws IOException {\n" + 84 " throw new UnsupportedOperationException(\"Not supported yet.\");\n" + 85 " }\n" + 86 "}\n"; 87 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 88 CancellableTask task = new CancellableTask<WorkingCopy>() { 89 90 public void run(WorkingCopy workingCopy) throws java.io.IOException { 91 workingCopy.toPhase(Phase.RESOLVED); 92 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 93 94 TreeMaker make = workingCopy.getTreeMaker(); 95 for (Tree typeDecl : cut.getTypeDecls()) { 96 if (Tree.Kind.CLASS == typeDecl.getKind()) { 98 ClassTree clazz = (ClassTree) typeDecl; 99 100 AnnotationTree wsAnnotation = make.Annotation( 101 make.Identifier("javax.jws.WebService"), 102 Collections.<ExpressionTree>emptyList() 104 ); 105 106 ClassTree modClass1 = make.Class( 107 make.addModifiersAnnotation(clazz.getModifiers(), wsAnnotation), 108 clazz.getSimpleName(), 109 clazz.getTypeParameters(), 110 clazz.getExtendsClause(), 111 (List<ExpressionTree>)clazz.getImplementsClause(), 112 clazz.getMembers()); 113 114 ModifiersTree methodModifiers = make.Modifiers( 116 Collections.<Modifier>singleton(Modifier.PUBLIC), 117 Collections.<AnnotationTree>emptyList() 118 ); 119 120 VariableTree parameter = make.Variable( 123 make.Modifiers( 124 Collections.<Modifier>singleton(Modifier.FINAL), 125 Collections.<AnnotationTree>emptyList() 126 ), 127 "arg0", make.Identifier("Object"), null ); 131 132 TypeElement element = workingCopy.getElements().getTypeElement("java.io.IOException"); 135 ExpressionTree throwsClause = make.QualIdent(element); 136 137 MethodTree newMethod = make.Method( 146 methodModifiers, "writeExternal", make.PrimitiveType(TypeKind.VOID), Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>singletonList(parameter), Collections.<ExpressionTree>singletonList(throwsClause), "{ throw new UnsupportedOperationException(\"Not supported yet.\") }", null ); 156 157 ClassTree modifiedClazz = make.addClassMember(modClass1, newMethod); 160 workingCopy.rewrite(clazz, modifiedClazz); 161 } 162 } 163 } 164 165 public void cancel() { 166 } 167 }; 168 testSource.runModificationTask(task).commit(); 169 String res = TestUtilities.copyFileToString(testFile); 170 System.err.println(res); 171 assertEquals(golden, res); 172 } 173 174 String getGoldenPckg() { 175 return ""; 176 } 177 178 String getSourcePckg() { 179 return ""; 180 } 181 182 185 public static void main(String [] args) { 186 } 188 189 } 190 | Popular Tags |