Commits

Jan Lahoda committed 30830a2

#1144(workaround): generalizing c.new I() {} causes an IllegalArgumentException.

Comments (0)

Files changed (2)

api/src/org/netbeans/modules/jackpot30/impl/Utilities.java

 import com.sun.source.tree.ImportTree;
 import com.sun.source.tree.LiteralTree;
 import com.sun.source.tree.MemberSelectTree;
+import com.sun.source.tree.MethodInvocationTree;
 import com.sun.source.tree.MethodTree;
 import com.sun.source.tree.ModifiersTree;
 import com.sun.source.tree.NewArrayTree;
 import com.sun.source.util.TreeScanner;
 import com.sun.source.util.Trees;
 import com.sun.tools.javac.api.JavacTaskImpl;
+import com.sun.tools.javac.code.Flags;
+import com.sun.tools.javac.tree.JCTree;
+import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
 import com.sun.tools.javac.util.Context;
 import com.sun.tools.javac.util.Log;
 import java.io.File;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
-import javax.annotation.processing.ProcessingEnvironment;
 import javax.lang.model.element.AnnotationMirror;
 import javax.lang.model.element.AnnotationValue;
 import javax.lang.model.element.AnnotationValueVisitor;
             return super.visitVariable(node, p);
         }
 
+        @Override
+        public Void visitNewClass(NewClassTree node, Void p) {
+            //XXX:
+            List<? extends ExpressionTree> arguments = node.getArguments();
+
+            if (!arguments.isEmpty() && arguments.get(0).getKind() == Kind.OTHER) {
+                tree2Variable.put(node, make.Identifier("$" + currentVariableIndex++));
+                return null;
+            }
+
+            return super.visitNewClass(node, p);
+        }
+
     }
 
     private static final class GeneralizePatternITT extends ImmutableTreeTranslator {

api/test/unit/src/org/netbeans/modules/jackpot30/impl/UtilitiesTest.java

 import com.sun.source.tree.Scope;
 import com.sun.source.tree.Tree;
 import com.sun.source.tree.Tree.Kind;
+import com.sun.source.util.TreePath;
+import com.sun.tools.javac.tree.JCTree;
 import java.util.Collections;
 import javax.lang.model.type.TypeMirror;
+import org.netbeans.modules.java.source.pretty.VeryPretty;
 
 /**
  *
         assertEquals("7h3m5s", Utilities.toHumanReadableTime(time += 7 * 60 * 60 * 1000));
     }
 
-}
+    public void testGeneralization() throws Exception {
+        performGeneralizationTest("package test;\n" +
+                                  "public class Test {\n" +
+                                  "    class Inner {\n" +
+                                  "        Inner(int i) {}\n" +
+                                  "    }\n" +
+                                  "    public static void main(String[] args) {\n" +
+                                  "        int i = 1;\n" +
+                                  "        Test c = null;\n" +
+                                  "        c.new Inner(i++) {};\n" +
+                                  "    }\n" +
+                                  "}\n",
+                                  "package test;\n" +
+                                  "public class Test {\n" +
+                                  "    class Inner {\n" +
+                                  "        Inner(int $0) { }\n" +
+                                  "    }\n" +
+                                  "    public static void main(String[] $1) {\n" +
+                                  "        int $2 = 1;\n" +
+                                  "        Test $3 = null;\n" +
+                                  "        $4;\n" + //XXX
+                                  "    }\n" +
+                                  "}\n");
+    }
+    private void performGeneralizationTest(String code, String generalized) throws Exception {
+        prepareTest("test/Test.java", code);
+
+        Tree generalizedTree = Utilities.generalizePattern(info, new TreePath(info.getCompilationUnit()));
+        VeryPretty vp = new VeryPretty(info);
+
+        vp.print((JCTree) generalizedTree);
+
+        String repr = vp.toString();
+
+        assertEquals(generalized.replaceAll("[ \n\t]+", " "),
+                     repr.replaceAll("[ \n\t]+", " "));
+    }
+
+}