|                                                                                                              1
 19  package org.netbeans.api.java.source.gen;
 20
 21  import com.sun.source.tree.*;
 22  import java.io.*;
 23  import java.util.Collections
  ; 24  import javax.lang.model.element.Modifier;
 25  import javax.lang.model.type.TypeKind;
 26  import org.netbeans.api.java.source.*;
 27  import org.netbeans.api.java.source.JavaSource.*;
 28  import org.netbeans.junit.NbTestSuite;
 29
 30
 34  public class AnonymousClassTest extends GeneratorTestMDRCompat {
 35
 36
 39      public AnonymousClassTest(String
  name) { 40          super(name);
 41      }
 42
 43
 46      public static NbTestSuite suite() {
 47          NbTestSuite suite = new NbTestSuite();
 48          suite.addTestSuite(AnonymousClassTest.class);
 49            return suite;
 52      }
 53
 54
 68      public void testAddMethodToInvocParam() throws Exception
  { 69          testFile = new File(getWorkDir(), "Test.java");
 70          TestUtilities.copyStringToFile(testFile,
 71              "package hierbas.del.litoral;\n\n" +
 72              "class Test {\n" +
 73              "    void method(Runnable r) {\n" +
 74              "        method(new Runnable() {});\n" +
 75              "    }" +
 76              "}\n");
 77          String
  golden = 78              "package hierbas.del.litoral;\n\n" +
 79              "class Test {\n" +
 80              "    void method(Runnable r) {\n" +
 81              "        method(new Runnable() {\n" +
 82              "    public void run() {\n" +
 83              "    }\n" +
 84              "});\n" +
 85              "    }}\n";
 86
 87          JavaSource src = getJavaSource(testFile);
 88          CancellableTask task = new CancellableTask<WorkingCopy>() {
 89
 90              public void run(WorkingCopy workingCopy) throws IOException {
 91                  workingCopy.toPhase(Phase.RESOLVED);
 92                  CompilationUnitTree cut = workingCopy.getCompilationUnit();
 93                  TreeMaker make = workingCopy.getTreeMaker();
 94
 95                  ClassTree testClass = (ClassTree) cut.getTypeDecls().get(0);
 96                  MethodTree method = (MethodTree) testClass.getMembers().get(1);
 97
 98                  ExpressionStatementTree est = (ExpressionStatementTree) method.getBody().getStatements().get(0);
 99                  MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
 100                 NewClassTree nct = (NewClassTree) mit.getArguments().get(0);
 101                 MethodTree m = make.Method(
 102                     make.Modifiers(Collections.<Modifier>singleton(Modifier.PUBLIC)),
 103                     "run",
 104                     make.PrimitiveType(TypeKind.VOID),
 105                     Collections.<TypeParameterTree>emptyList(),
 106                     Collections.<VariableTree>emptyList(),
 107                     Collections.<ExpressionTree>emptyList(),
 108                     make.Block(Collections.<StatementTree>emptyList(), false),
 109                     null
 110                 );
 111                 workingCopy.rewrite(nct.getClassBody(), make.addClassMember(nct.getClassBody(), m));
 112             }
 113
 114             public void cancel() {
 115             }
 116         };
 117         src.runModificationTask(task).commit();
 118         String
  res = TestUtilities.copyFileToString(testFile); 119         System.err.println(res);
 120         assertEquals(golden, res);
 121     }
 122
 123
 144     public void testAddInsideAnnWhenAnnInPar() throws Exception
  { 145         testFile = new File(getWorkDir(), "Test.java");
 146         TestUtilities.copyStringToFile(testFile,
 147             "package javaapplication1;\n" +
 148             "\n" +
 149             "public class Main {\n" +
 150             "    void m(Runnable r) {\n" +
 151             "    }\n" +
 152             "    \n" +
 153             "    void method() {\n" +
 154             "        m(new Runnable() {\n" +
 155             "            public void run() {\n" +
 156             "                Object o = null;\n" +
 157             "                String s = o;\n" +
 158             "            }\n" +
 159             "        });\n" +
 160             "    }\n" +
 161             "}\n");
 162         String
  golden = 163             "package javaapplication1;\n" +
 164             "\n" +
 165             "public class Main {\n" +
 166             "    void m(Runnable r) {\n" +
 167             "    }\n" +
 168             "    \n" +
 169             "    void method() {\n" +
 170             "        m(new Runnable() {\n" +
 171             "            public void run() {\n" +
 172             "                Object o = null;\n" +
 173             "                String s = (String) o;\n" +
 174             "            }\n" +
 175             "        });\n" +
 176             "    }\n" +
 177             "}\n";
 178
 179         JavaSource src = getJavaSource(testFile);
 180         CancellableTask task = new CancellableTask<WorkingCopy>() {
 181
 182             public void run(WorkingCopy workingCopy) throws IOException {
 183                 workingCopy.toPhase(Phase.RESOLVED);
 184                 CompilationUnitTree cut = workingCopy.getCompilationUnit();
 185                 TreeMaker make = workingCopy.getTreeMaker();
 186
 187                 ClassTree testClass = (ClassTree) cut.getTypeDecls().get(0);
 188                 MethodTree method = (MethodTree) testClass.getMembers().get(2);
 189                 BlockTree block = method.getBody();
 190                 ExpressionStatementTree est = (ExpressionStatementTree) block.getStatements().get(0);
 191                 MethodInvocationTree mit = (MethodInvocationTree) est.getExpression();
 192                 NewClassTree nct = (NewClassTree) mit.getArguments().get(0);
 193                 ClassTree clazzTree = nct.getClassBody();
 194                 method = (MethodTree) clazzTree.getMembers().get(1);
 195                 VariableTree vt = (VariableTree) method.getBody().getStatements().get(1);
 196                 ExpressionTree init = vt.getInitializer();
 197                 ExpressionTree cast = make.TypeCast(make.Identifier("String"), init);
 198                 workingCopy.rewrite(init, cast);
 199             }
 200
 201             public void cancel() {
 202             }
 203         };
 204         src.runModificationTask(task).commit();
 205         String
  res = TestUtilities.copyFileToString(testFile); 206         System.err.println(res);
 207         assertEquals(golden, res);
 208     }
 209
 210     String
  getGoldenPckg() { 211         return "";
 212     }
 213
 214     String
  getSourcePckg() { 215         return "";
 216     }
 217 }
 218
                                                                                                                                                                                                             |                                                                       
 
 
 
 
 
                                                                                   Popular Tags                                                                                                                                                                                              |