1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.*; 22 import static com.sun.source.tree.Tree.Kind.*; 23 import java.util.*; 24 import java.io.IOException ; 25 import java.util.EnumSet ; 26 import javax.lang.model.element.Modifier; 27 import javax.lang.model.type.TypeKind; 28 import org.netbeans.junit.NbTestSuite; 29 import junit.textui.TestRunner; 30 import org.netbeans.api.java.source.CancellableTask; 31 import org.netbeans.api.java.source.JavaSource; 32 import org.netbeans.api.java.source.JavaSource.Phase; 33 import org.netbeans.api.java.source.TreeMaker; 34 import org.netbeans.api.java.source.WorkingCopy; 35 36 41 public class ConstructorTest extends GeneratorTest { 42 43 44 public ConstructorTest(String name) { 45 super(name); 46 } 47 48 public static NbTestSuite suite() { 49 NbTestSuite suite = new NbTestSuite(ConstructorTest.class); 50 return suite; 51 } 52 53 protected void setUp() throws Exception { 54 super.setUp(); 55 } 56 57 public void testAddConstructor() throws IOException { 58 testFile = getFile(getSourceDir(), getSourcePckg() + "ConstructorTest.java"); 59 60 JavaSource src = getJavaSource(testFile); 61 CancellableTask task = new CancellableTask<WorkingCopy>() { 62 63 public void run(WorkingCopy workingCopy) throws IOException { 64 workingCopy.toPhase(Phase.RESOLVED); 65 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 66 TreeMaker make = workingCopy.getTreeMaker(); 67 ClassTree topLevel = (ClassTree) cut.getTypeDecls().iterator().next(); 69 for (Tree member : topLevel.getMembers()) { 70 if (CLASS == member.getKind()) { 72 73 ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC)); 74 75 List<VariableTree> arguments = new ArrayList<VariableTree>(); 76 arguments.add(make.Variable( 77 make.Modifiers(EnumSet.noneOf(Modifier.class)), 78 "a", 79 make.PrimitiveType(TypeKind.BOOLEAN), null) 80 ); 81 82 MethodTree newConstructor = make.Constructor( 83 mods, 84 Collections.<TypeParameterTree>emptyList(), 85 arguments, 86 Collections.<ExpressionTree>emptyList(), 87 make.Block(Collections.<StatementTree>emptyList(), false) 88 ); 89 ClassTree newInner = make.addClassMember((ClassTree) member, newConstructor); 90 workingCopy.rewrite(member, newInner); 91 } 92 } 93 } 94 95 public void cancel() { 96 } 97 }; 98 src.runModificationTask(task).commit(); 99 assertFiles("testAddConstructor.pass"); 100 } 101 102 public void testAddConstructor2() throws IOException { 103 testFile = getFile(getSourceDir(), getSourcePckg() + "ConstructorTest2.java"); 104 105 JavaSource src = getJavaSource(testFile); 106 CancellableTask task = new CancellableTask<WorkingCopy>() { 107 108 public void run(WorkingCopy workingCopy) throws IOException { 109 workingCopy.toPhase(Phase.RESOLVED); 110 CompilationUnitTree cut = workingCopy.getCompilationUnit(); 111 TreeMaker make = workingCopy.getTreeMaker(); 112 ClassTree topLevel = (ClassTree) cut.getTypeDecls().iterator().next(); 114 115 ModifiersTree mods = make.Modifiers(EnumSet.of(Modifier.PUBLIC)); 116 List<VariableTree> arguments = new ArrayList<VariableTree>(); 117 arguments.add(make.Variable( 118 make.Modifiers(EnumSet.noneOf(Modifier.class)), 119 "a", 120 make.PrimitiveType(TypeKind.BOOLEAN), null) 121 ); 122 MethodTree newConstructor = make.Constructor( 123 mods, 124 Collections.<TypeParameterTree>emptyList(), 125 arguments, 126 Collections.<ExpressionTree>emptyList(), 127 make.Block(Collections.<StatementTree>emptyList(), false) 128 ); 129 130 ClassTree newClass = make.addClassMember(topLevel, newConstructor); 131 workingCopy.rewrite(topLevel, newClass); 132 } 133 134 public void cancel() { 135 } 136 }; 137 src.runModificationTask(task).commit(); 138 assertFiles("testAddConstructor2.pass"); 139 } 140 141 145 public static void main(String [] args) { 146 TestRunner.run(suite()); 147 } 148 149 String getSourcePckg() { 150 return "org/netbeans/test/codegen/"; 151 } 152 153 String getGoldenPckg() { 154 return "org/netbeans/jmi/javamodel/codegen/ConstructorTest/ConstructorTest/"; 155 } 156 157 } 158 | Popular Tags |