1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.BlockTree; 22 import com.sun.source.tree.ClassTree; 23 import com.sun.source.tree.ExpressionTree; 24 import com.sun.source.tree.MethodTree; 25 import com.sun.source.tree.Tree; 26 import com.sun.source.tree.TypeParameterTree; 27 import com.sun.source.tree.VariableTree; 28 import com.sun.source.util.SourcePositions; 29 import java.io.IOException ; 30 import java.util.ArrayList ; 31 import java.util.Collections ; 32 import java.util.List ; 33 import java.util.Set ; 34 import javax.lang.model.element.Element; 35 import javax.lang.model.element.Modifier; 36 import javax.lang.model.element.TypeElement; 37 import org.netbeans.api.java.source.JavaSource; 38 import org.netbeans.api.java.source.SourceUtilsTestUtil2; 39 import org.netbeans.api.java.source.WorkingCopy; 40 import org.netbeans.api.java.source.transform.Transformer; 41 import org.netbeans.junit.NbTestSuite; 42 import junit.textui.TestRunner; 43 import org.netbeans.api.java.source.CancellableTask; 44 import org.openide.filesystems.FileObject; 45 import org.openide.filesystems.FileUtil; 46 47 52 public class MethodTest2 extends GeneratorTest { 53 54 55 public MethodTest2(String name) { 56 super(name); 57 } 58 59 public static NbTestSuite suite() { 60 NbTestSuite suite = new NbTestSuite(); 61 suite.addTest(new MethodTest2("testMethodAdd")); 62 return suite; 63 } 64 65 protected void setUp() throws Exception { 66 super.setUp(); 67 testFile = getFile(getSourceDir(), getSourcePckg() + "MethodTest2.java"); 68 } 69 70 74 public void testMethodAdd() throws IOException { 75 FileObject fo = FileUtil.toFileObject(testFile); 76 JavaSource js = JavaSource.forFileObject(fo); 77 js.runModificationTask(new CancellableTask<WorkingCopy>() { 78 public void cancel() { 79 } 80 public void run(WorkingCopy wc) { 81 82 CreateMethod create = new CreateMethod(); 83 SourceUtilsTestUtil2.run(wc, create); 84 MethodTree mt = create.makeMethod(); 85 86 MethodImplGenerator add = new MethodImplGenerator(mt); 87 SourceUtilsTestUtil2.run(wc, add); 88 89 RenameImplGenerator rename = new RenameImplGenerator(add.method, "foo"); 90 SourceUtilsTestUtil2.run(wc, rename); 91 92 SetTypeGenerator setType = new SetTypeGenerator(rename.method, wc.getElements().getTypeElement("java.lang.String")); 93 SourceUtilsTestUtil2.run(wc, setType); 94 95 SourcePositions pos[] = new SourcePositions[1]; 96 BlockTree btree = (BlockTree) wc.getTreeUtilities().parseStatement("{System.out.println();}", pos); 97 98 SetBodyGenerator setBody = new SetBodyGenerator(setType.method, btree); 99 SourceUtilsTestUtil2.run(wc, setBody); 100 } 101 }).commit(); 102 103 assertFiles("testMethodAdd.pass"); 104 } 105 106 110 public static void main(String [] args) { 111 TestRunner.run(suite()); 112 } 113 114 String getSourcePckg() { 115 return "org/netbeans/test/codegen/"; 116 } 117 118 String getGoldenPckg() { 119 return "org/netbeans/jmi/javamodel/codegen/MethodTest2/MethodTest2/"; 120 } 121 122 private class SetTypeGenerator extends Transformer<Void , Object > { 123 124 public MethodTree method; 125 Element newType; 126 public SetTypeGenerator(MethodTree m, Element el) { 127 method = m; 128 this.newType = el; 129 } 130 131 public Void visitMethod(MethodTree node, Object p) { 132 super.visitMethod(node, p); 133 if (method.equals(node)) { 134 MethodTree njuMethod = make.Method( 135 node.getModifiers(), 136 node.getName().toString(), 137 make.Identifier(newType), 138 node.getTypeParameters(), 139 node.getParameters(), 140 node.getThrows(), 141 node.getBody(), 142 (ExpressionTree) node.getDefaultValue() 143 ); 144 changes.rewrite(node, njuMethod); 145 method = njuMethod; 146 } 147 return null; 148 } 149 } 150 151 private class SetBodyGenerator extends Transformer<Void , Object > { 152 153 public MethodTree method; 154 BlockTree newType; 155 public SetBodyGenerator(MethodTree m, BlockTree tree) { 156 method = m; 157 this.newType = newType; 158 newType = tree; 159 } 160 161 public Void visitMethod(MethodTree node, Object p) { 162 super.visitMethod(node, p); 163 if (method.equals(node)) { 164 MethodTree njuMethod = make.Method( 165 node.getModifiers(), 166 node.getName().toString(), 167 (ExpressionTree) node.getReturnType(), 168 node.getTypeParameters(), 169 node.getParameters(), 170 node.getThrows(), 171 newType, 172 (ExpressionTree) node.getDefaultValue() 173 ); 174 method = njuMethod; 175 changes.rewrite(node, njuMethod); 176 } 177 return null; 178 } 179 } 180 private class RenameImplGenerator extends Transformer<Void , Object > { 181 182 public MethodTree method; 183 String newName; 184 public RenameImplGenerator(MethodTree m, String newName) { 185 method = m; 186 this.newName = newName; 187 } 188 189 public Void visitMethod(MethodTree node, Object p) { 190 super.visitMethod(node, p); 191 if (method.equals(node)) { 192 MethodTree njuMethod = make.Method( 193 node.getModifiers(), 194 newName, 195 (ExpressionTree) node.getReturnType(), 196 node.getTypeParameters(), 197 node.getParameters(), 198 node.getThrows(), 199 node.getBody(), 200 (ExpressionTree) node.getDefaultValue() 201 ); 202 changes.rewrite(node, njuMethod); 203 method = njuMethod; 204 } 205 return null; 206 } 207 } 208 private class MethodImplGenerator extends Transformer<Void , Object > { 209 210 public MethodTree method; 211 public MethodImplGenerator(MethodTree m) { 212 method = m; 213 214 } 215 216 public Void visitClass(ClassTree node, Object p) { 217 super.visitClass(node, p); 218 TypeElement te = (TypeElement)model.getElement(node); 219 if (te != null) { 220 List <Tree> members = new ArrayList <Tree>(); 221 for(Tree m : node.getMembers()) 222 members.add(m); 223 members.add(method); 224 ClassTree decl = make.Class(node.getModifiers(), node.getSimpleName(), node.getTypeParameters(), node.getExtendsClause(), (List <ExpressionTree>)node.getImplementsClause(), members); 225 model.setElement(decl, te); 226 model.setType(decl, model.getType(node)); 227 changes.rewrite(node, decl); 228 } 229 return null; 230 } 231 } 232 233 private class CreateMethod extends Transformer<Void , Object > { 234 public MethodTree makeMethod() { 235 Set <Modifier> emptyModifs = Collections.emptySet(); 236 List <TypeParameterTree> emptyTpt= Collections.emptyList(); 237 List <VariableTree> emptyVt = Collections.emptyList(); 238 List <ExpressionTree> emptyEt = Collections.emptyList(); 239 return make.Method( 240 make.Modifiers(emptyModifs), 241 (CharSequence )"", 242 (ExpressionTree) null, 243 emptyTpt, 244 emptyVt, 245 emptyEt, 246 (BlockTree) null, 247 (ExpressionTree)null); 248 } 249 } 250 251 } 252 | Popular Tags |