1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.ClassTree; 22 import com.sun.source.tree.ExpressionTree; 23 import com.sun.source.tree.MethodTree; 24 import com.sun.source.tree.ModifiersTree; 25 import com.sun.source.tree.StatementTree; 26 import com.sun.source.tree.TypeParameterTree; 27 import java.io.File ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 import java.util.EnumSet ; 31 import java.util.List ; 32 import javax.lang.model.element.Modifier; 33 import javax.lang.model.type.TypeKind; 34 import org.netbeans.api.java.source.TestUtilities; 35 import org.netbeans.api.java.source.transform.Transformer; 36 import org.netbeans.junit.NbTestSuite; 37 38 42 public class MethodCreationTest extends GeneratorTestMDRCompat { 43 44 45 public MethodCreationTest(String testName) { 46 super(testName); 47 } 48 49 public static NbTestSuite suite() { 50 NbTestSuite suite = new NbTestSuite(); 51 suite.addTest(new MethodCreationTest("testAddFirst")); 53 return suite; 66 } 67 68 74 public void testAddFirst() throws Exception { 75 testFile = new File (getWorkDir(), "Test.java"); 76 TestUtilities.copyStringToFile(testFile, 77 "package hierbas.del.litoral;\n\n" + 78 "import java.util.*;\n\n" + 79 "public class Test {\n" + 80 "}\n" 81 ); 82 String golden = 83 "package hierbas.del.litoral;\n\n" + 84 "import java.util.*;\n\n" + 85 "public class Test {\n\n" + 86 "public <T> void taragui(List menta, T carqueja, int dulce, boolean compuesta,\n" + 87 " boolean logrando) throws IOException {\n" + 88 "}\n" + 89 "}\n"; 90 91 process( 92 new Transformer<Void , Object >() { 93 94 public Void visitClass(ClassTree node, Object p) { 95 super.visitClass(node, p); 96 if ("Test".contentEquals(node.getSimpleName())) { 97 List parametersList = new ArrayList (5); 98 ModifiersTree mods = make.Modifiers(EnumSet.noneOf(Modifier.class)); 99 parametersList.add(make.Variable(mods, "menta", make.Identifier("List"), null)); 100 parametersList.add(make.Variable(mods, "carqueja", make.Identifier("T"), null)); 101 parametersList.add(make.Variable(mods, "dulce", make.PrimitiveType(TypeKind.INT), null)); 102 parametersList.add(make.Variable(mods, "compuesta", make.PrimitiveType(TypeKind.BOOLEAN), null)); 103 parametersList.add(make.Variable(mods, "logrando", make.PrimitiveType(TypeKind.BOOLEAN), null)); 104 MethodTree newMethod = make.Method( 105 make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)), "taragui", make.PrimitiveType(TypeKind.VOID), Collections.<TypeParameterTree>singletonList(make.TypeParameter("T", Collections.<ExpressionTree>emptyList())), parametersList, Collections.<ExpressionTree>singletonList(make.Identifier("IOException")), make.Block(Collections.<StatementTree>emptyList(), false), 112 null ); 114 ClassTree copy = make.addClassMember( 115 node, newMethod 116 ); 117 changes.rewrite(node, copy); 118 } 119 return null; 120 } 121 } 122 ); 123 String res = TestUtilities.copyFileToString(testFile); 124 System.err.println(res); 125 assertEquals(golden, res); 126 } 127 128 String getGoldenPckg() { 129 return ""; 130 } 131 132 String getSourcePckg() { 133 return ""; 134 } 135 136 } 137 | Popular Tags |