1 19 package org.netbeans.api.java.source.gen; 20 21 import com.sun.source.tree.AnnotationTree; 22 import com.sun.source.tree.BlockTree; 23 import com.sun.source.tree.ClassTree; 24 import com.sun.source.tree.IdentifierTree; 25 import com.sun.source.tree.MethodTree; 26 import com.sun.source.tree.ModifiersTree; 27 import com.sun.source.tree.VariableTree; 28 import java.io.File ; 29 import java.util.Collections ; 30 import java.util.EnumSet ; 31 import java.util.HashSet ; 32 import java.util.Set ; 33 import javax.lang.model.element.Modifier; 34 import org.netbeans.api.java.source.CancellableTask; 35 import org.netbeans.api.java.source.JavaSource; 36 import org.netbeans.api.java.source.JavaSource.Phase; 37 import org.netbeans.api.java.source.TestUtilities; 38 import org.netbeans.api.java.source.TreeMaker; 39 import org.netbeans.api.java.source.WorkingCopy; 40 import org.netbeans.junit.NbTestSuite; 41 import org.openide.filesystems.FileUtil; 42 43 48 public class ModifiersTest extends GeneratorTestMDRCompat { 49 50 51 public ModifiersTest(String name) { 52 super(name); 53 } 54 55 public static NbTestSuite suite() { 56 NbTestSuite suite = new NbTestSuite(); 57 suite.addTestSuite(ModifiersTest.class); 58 return suite; 69 } 70 71 74 public void testChangeToFinalLocVar() throws Exception { 75 testFile = new File (getWorkDir(), "Test.java"); 76 TestUtilities.copyStringToFile(testFile, 77 "package hierbas.del.litoral;\n\n" + 78 "import java.io.*;\n\n" + 79 "public class Test {\n" + 80 " public void taragui() {\n" + 81 " int i = 10;\n" + 82 " }\n" + 83 "}\n" 84 ); 85 String golden = 86 "package hierbas.del.litoral;\n\n" + 87 "import java.io.*;\n\n" + 88 "public class Test {\n" + 89 " public void taragui() {\n" + 90 " final int i = 10;\n" + 91 " }\n" + 92 "}\n"; 93 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 94 CancellableTask task = new CancellableTask<WorkingCopy>() { 95 96 public void run(WorkingCopy workingCopy) throws java.io.IOException { 97 workingCopy.toPhase(Phase.RESOLVED); 98 TreeMaker make = workingCopy.getTreeMaker(); 99 100 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 102 MethodTree method = (MethodTree) clazz.getMembers().get(1); 103 BlockTree block = method.getBody(); 104 VariableTree vt = (VariableTree) block.getStatements().get(0); 105 ModifiersTree mods = vt.getModifiers(); 106 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.FINAL))); 107 } 108 109 public void cancel() { 110 } 111 }; 112 testSource.runModificationTask(task).commit(); 113 String res = TestUtilities.copyFileToString(testFile); 114 System.err.println(res); 115 assertEquals(golden, res); 116 } 117 118 121 public void testAddClassAbstract() throws Exception { 122 testFile = new File (getWorkDir(), "Test.java"); 123 TestUtilities.copyStringToFile(testFile, 124 "package hierbas.del.litoral;\n\n" + 125 "import java.io.*;\n\n" + 126 "public class Test {\n" + 127 " public abstract void taragui();\n" + 128 "}\n" 129 ); 130 String golden = 131 "package hierbas.del.litoral;\n\n" + 132 "import java.io.*;\n\n" + 133 "public abstract class Test {\n" + 134 " public abstract void taragui();\n" + 135 "}\n"; 136 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 137 CancellableTask task = new CancellableTask<WorkingCopy>() { 138 139 public void run(WorkingCopy workingCopy) throws java.io.IOException { 140 workingCopy.toPhase(Phase.RESOLVED); 141 TreeMaker make = workingCopy.getTreeMaker(); 142 143 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 145 ModifiersTree mods = clazz.getModifiers(); 146 Set <Modifier> s = new HashSet <Modifier>(mods.getFlags()); 147 s.add(Modifier.ABSTRACT); 148 workingCopy.rewrite(mods, make.Modifiers(s)); 149 } 150 151 public void cancel() { 152 } 153 }; 154 testSource.runModificationTask(task).commit(); 155 String res = TestUtilities.copyFileToString(testFile); 156 System.err.println(res); 157 assertEquals(golden, res); 158 } 159 160 171 public void testMethodMods1() throws Exception { 172 testFile = new File (getWorkDir(), "Test.java"); 173 TestUtilities.copyStringToFile(testFile, 174 "package hierbas.del.litoral;\n\n" + 175 "import java.io.*;\n\n" + 176 "public class Test {\n" + 177 " void method() {\n" + 178 " }\n" + 179 "}\n" 180 ); 181 String golden = 182 "package hierbas.del.litoral;\n\n" + 183 "import java.io.*;\n\n" + 184 "public class Test {\n" + 185 " public static void method() {\n" + 186 " }\n" + 187 "}\n"; 188 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 189 CancellableTask task = new CancellableTask<WorkingCopy>() { 190 191 public void run(WorkingCopy workingCopy) throws java.io.IOException { 192 workingCopy.toPhase(Phase.RESOLVED); 193 TreeMaker make = workingCopy.getTreeMaker(); 194 195 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 197 MethodTree method = (MethodTree) clazz.getMembers().get(1); 198 ModifiersTree mods = method.getModifiers(); 199 workingCopy.rewrite(mods, make.Modifiers(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC))); 200 } 201 202 public void cancel() { 203 } 204 }; 205 testSource.runModificationTask(task).commit(); 206 String res = TestUtilities.copyFileToString(testFile); 207 System.err.println(res); 208 assertEquals(golden, res); 209 } 210 211 222 public void testMethodMods2() throws Exception { 223 testFile = new File (getWorkDir(), "Test.java"); 224 TestUtilities.copyStringToFile(testFile, 225 "package hierbas.del.litoral;\n\n" + 226 "import java.io.*;\n\n" + 227 "public class Test {\n" + 228 " public static void method() {\n" + 229 " }\n" + 230 "}\n" 231 ); 232 String golden = 233 "package hierbas.del.litoral;\n\n" + 234 "import java.io.*;\n\n" + 235 "public class Test {\n" + 236 " void method() {\n" + 237 " }\n" + 238 "}\n"; 239 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 240 CancellableTask task = new CancellableTask<WorkingCopy>() { 241 242 public void run(WorkingCopy workingCopy) throws java.io.IOException { 243 workingCopy.toPhase(Phase.RESOLVED); 244 TreeMaker make = workingCopy.getTreeMaker(); 245 246 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 248 MethodTree method = (MethodTree) clazz.getMembers().get(1); 249 ModifiersTree mods = method.getModifiers(); 250 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>emptySet())); 251 } 252 253 public void cancel() { 254 } 255 }; 256 testSource.runModificationTask(task).commit(); 257 String res = TestUtilities.copyFileToString(testFile); 258 System.err.println(res); 259 assertEquals(golden, res); 260 } 261 262 273 public void testMethodMods3() throws Exception { 274 testFile = new File (getWorkDir(), "Test.java"); 275 TestUtilities.copyStringToFile(testFile, 276 "package hierbas.del.litoral;\n\n" + 277 "import java.io.*;\n\n" + 278 "public class Test {\n" + 279 " Test() {\n" + 280 " }\n" + 281 "}\n" 282 ); 283 String golden = 284 "package hierbas.del.litoral;\n\n" + 285 "import java.io.*;\n\n" + 286 "public class Test {\n" + 287 " public Test() {\n" + 288 " }\n" + 289 "}\n"; 290 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 291 CancellableTask task = new CancellableTask<WorkingCopy>() { 292 293 public void run(WorkingCopy workingCopy) throws java.io.IOException { 294 workingCopy.toPhase(Phase.RESOLVED); 295 TreeMaker make = workingCopy.getTreeMaker(); 296 297 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 299 MethodTree method = (MethodTree) clazz.getMembers().get(0); 300 ModifiersTree mods = method.getModifiers(); 301 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC))); 302 } 303 304 public void cancel() { 305 } 306 }; 307 testSource.runModificationTask(task).commit(); 308 String res = TestUtilities.copyFileToString(testFile); 309 System.err.println(res); 310 assertEquals(golden, res); 311 } 312 313 324 public void testMethodMods4() throws Exception { 325 testFile = new File (getWorkDir(), "Test.java"); 326 TestUtilities.copyStringToFile(testFile, 327 "package hierbas.del.litoral;\n\n" + 328 "import java.io.*;\n\n" + 329 "public class Test {\n" + 330 " public Test() {\n" + 331 " }\n" + 332 "}\n" 333 ); 334 String golden = 335 "package hierbas.del.litoral;\n\n" + 336 "import java.io.*;\n\n" + 337 "public class Test {\n" + 338 " Test() {\n" + 339 " }\n" + 340 "}\n"; 341 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 342 CancellableTask task = new CancellableTask<WorkingCopy>() { 343 344 public void run(WorkingCopy workingCopy) throws java.io.IOException { 345 workingCopy.toPhase(Phase.RESOLVED); 346 TreeMaker make = workingCopy.getTreeMaker(); 347 348 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 350 MethodTree method = (MethodTree) clazz.getMembers().get(0); 351 ModifiersTree mods = method.getModifiers(); 352 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>emptySet())); 353 } 354 355 public void cancel() { 356 } 357 }; 358 testSource.runModificationTask(task).commit(); 359 String res = TestUtilities.copyFileToString(testFile); 360 System.err.println(res); 361 assertEquals(golden, res); 362 } 363 374 375 public void testMethodMods5() throws Exception { 376 testFile = new File (getWorkDir(), "Test.java"); 377 TestUtilities.copyStringToFile(testFile, 378 "package hierbas.del.litoral;\n\n" + 379 "import java.io.*;\n\n" + 380 "public class Test {\n" + 381 " public static void method() {\n" + 382 " }\n" + 383 "}\n" 384 ); 385 String golden = 386 "package hierbas.del.litoral;\n\n" + 387 "import java.io.*;\n\n" + 388 "public class Test {\n" + 389 " static void method() {\n" + 390 " }\n" + 391 "}\n"; 392 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 393 CancellableTask task = new CancellableTask<WorkingCopy>() { 394 395 public void run(WorkingCopy workingCopy) throws java.io.IOException { 396 workingCopy.toPhase(Phase.RESOLVED); 397 TreeMaker make = workingCopy.getTreeMaker(); 398 399 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 401 MethodTree method = (MethodTree) clazz.getMembers().get(1); 402 ModifiersTree mods = method.getModifiers(); 403 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.STATIC))); 404 } 405 406 public void cancel() { 407 } 408 }; 409 testSource.runModificationTask(task).commit(); 410 String res = TestUtilities.copyFileToString(testFile); 411 System.err.println(res); 412 assertEquals(golden, res); 413 } 414 415 426 public void testMethodMods6() throws Exception { 427 testFile = new File (getWorkDir(), "Test.java"); 428 TestUtilities.copyStringToFile(testFile, 429 "package hierbas.del.litoral;\n\n" + 430 "import java.io.*;\n\n" + 431 "public class Test {\n" + 432 " public Test() {\n" + 433 " }\n" + 434 "}\n" 435 ); 436 String golden = 437 "package hierbas.del.litoral;\n\n" + 438 "import java.io.*;\n\n" + 439 "public class Test {\n" + 440 " protected Test() {\n" + 441 " }\n" + 442 "}\n"; 443 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 444 CancellableTask task = new CancellableTask<WorkingCopy>() { 445 446 public void run(WorkingCopy workingCopy) throws java.io.IOException { 447 workingCopy.toPhase(Phase.RESOLVED); 448 TreeMaker make = workingCopy.getTreeMaker(); 449 450 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 452 MethodTree method = (MethodTree) clazz.getMembers().get(0); 453 ModifiersTree mods = method.getModifiers(); 454 workingCopy.rewrite(mods, make.Modifiers(Collections.<Modifier>singleton(Modifier.PROTECTED))); 455 } 456 457 public void cancel() { 458 } 459 }; 460 testSource.runModificationTask(task).commit(); 461 String res = TestUtilities.copyFileToString(testFile); 462 System.err.println(res); 463 assertEquals(golden, res); 464 } 465 466 479 public void testAnnRename() throws Exception { 480 testFile = new File (getWorkDir(), "Test.java"); 481 TestUtilities.copyStringToFile(testFile, 482 "package hierbas.del.litoral;\n\n" + 483 "import java.io.*;\n\n" + 484 "@Annotace()\n" + 485 "public class Test {\n" + 486 " public Test() {\n" + 487 " }\n" + 488 "}\n" 489 ); 490 String golden = 491 "package hierbas.del.litoral;\n\n" + 492 "import java.io.*;\n\n" + 493 "@Annotation()\n" + 494 "public class Test {\n" + 495 " public Test() {\n" + 496 " }\n" + 497 "}\n"; 498 JavaSource testSource = JavaSource.forFileObject(FileUtil.toFileObject(testFile)); 499 CancellableTask task = new CancellableTask<WorkingCopy>() { 500 501 public void run(WorkingCopy workingCopy) throws java.io.IOException { 502 workingCopy.toPhase(Phase.RESOLVED); 503 TreeMaker make = workingCopy.getTreeMaker(); 504 505 ClassTree clazz = (ClassTree) workingCopy.getCompilationUnit().getTypeDecls().get(0); 507 ModifiersTree mods = clazz.getModifiers(); 508 AnnotationTree ann = mods.getAnnotations().get(0); 509 IdentifierTree ident = (IdentifierTree) ann.getAnnotationType(); 510 workingCopy.rewrite(ident, make.Identifier("Annotation")); 511 } 512 513 public void cancel() { 514 } 515 }; 516 testSource.runModificationTask(task).commit(); 517 String res = TestUtilities.copyFileToString(testFile); 518 System.err.println(res); 519 assertEquals(golden, res); 520 } 521 522 String getGoldenPckg() { 523 return ""; 524 } 525 526 String getSourcePckg() { 527 return ""; 528 } 529 } 530 | Popular Tags |