Commits

Jan Lahoda committed 23af879

Ability to process pattern that is an expression statement - till now, this was incorrectly parsed as expression.

Comments (0)

Files changed (6)

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

 
 import com.sun.source.tree.AnnotationTree;
 import com.sun.source.tree.AssignmentTree;
+import com.sun.source.tree.BlockTree;
+import com.sun.source.tree.CompilationUnitTree;
 import com.sun.source.tree.ErroneousTree;
 import com.sun.source.tree.ExpressionStatementTree;
 import com.sun.source.tree.ExpressionTree;
 import com.sun.source.tree.IdentifierTree;
 import com.sun.source.tree.LiteralTree;
 import com.sun.source.tree.MemberSelectTree;
+import com.sun.source.tree.MethodTree;
 import com.sun.source.tree.NewArrayTree;
 import com.sun.source.tree.Scope;
 import com.sun.source.tree.Tree;
 import com.sun.source.tree.Tree.Kind;
 import com.sun.source.util.SourcePositions;
 import com.sun.source.util.TreePath;
+import com.sun.source.util.TreePathScanner;
+import com.sun.tools.javac.api.JavacTaskImpl;
 import com.sun.tools.javac.util.Context;
+import com.sun.tools.javac.util.Log;
+import java.io.File;
+import java.io.IOException;
 import java.util.Collection;
 import java.util.Iterator;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 import javax.lang.model.element.Element;
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.type.TypeKind;
 import javax.lang.model.type.TypeMirror;
+import javax.tools.JavaFileObject;
+import org.netbeans.api.annotations.common.CheckForNull;
 import org.netbeans.api.annotations.common.NonNull;
 import org.netbeans.api.java.classpath.ClassPath;
 import org.netbeans.api.java.source.CompilationInfo;
 import org.netbeans.modules.jackpot30.spi.HintDescription;
 import org.netbeans.modules.jackpot30.spi.HintProvider;
 import org.netbeans.modules.java.source.JavaSourceAccessor;
+import org.netbeans.modules.java.source.parsing.FileObjects;
 import org.netbeans.modules.java.source.pretty.ImportAnalysis2;
 import org.netbeans.modules.java.source.transform.ImmutableTreeTranslator;
 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
+import org.openide.util.Exceptions;
 import org.openide.util.Lookup;
 import org.openide.util.NbCollections;
 
     }
     
     public static Tree parseAndAttribute(CompilationInfo info, String pattern, Scope scope) {
-        Tree patternTree = info.getTreeUtilities().parseExpression(pattern, new SourcePositions[1]);
+        Tree patternTree = !isStatement(pattern) ? info.getTreeUtilities().parseExpression(pattern, new SourcePositions[1]) : null;
         boolean expression = true;
 
-        if (patternTree.getKind() == Kind.ERRONEOUS || (patternTree.getKind() == Kind.IDENTIFIER && ((IdentifierTree) patternTree).getName().contentEquals("<error>"))) { //TODO: <error>...
+        if (patternTree == null || patternTree.getKind() == Kind.ERRONEOUS || (patternTree.getKind() == Kind.IDENTIFIER && ((IdentifierTree) patternTree).getName().contentEquals("<error>"))) { //TODO: <error>...
             patternTree = info.getTreeUtilities().parseStatement(pattern, new SourcePositions[1]);
             expression = false;
         }
         return type == null || type.getKind() == TypeKind.ERROR;
     }
 
+    private static boolean isStatement(String pattern) {
+        return pattern.trim().endsWith(";");
+    }
+    
     private static final class FixTree extends ImmutableTreeTranslator {
 
         @Override
 
     }
 
-    public static CharSequence getWildcardTreeName(@NonNull Tree t) {
+    public static @CheckForNull CharSequence getWildcardTreeName(@NonNull Tree t) {
         if (t.getKind() == Kind.EXPRESSION_STATEMENT && ((ExpressionStatementTree) t).getExpression().getKind() == Kind.IDENTIFIER) {
             IdentifierTree identTree = (IdentifierTree) ((ExpressionStatementTree) t).getExpression();
             
         return null;
     }
 
+    private static long inc;
+
+    public static Scope constructScope(CompilationInfo info, Map<String, TypeMirror> constraints) {
+        StringBuilder clazz = new StringBuilder();
+
+        clazz.append("package $; public class $" + (inc++) + "{");
+
+        for (Entry<String, TypeMirror> e : constraints.entrySet()) {
+            if (e.getValue() != null) {
+                clazz.append("private ");
+                clazz.append(e.getValue().toString()); //XXX
+                clazz.append(" ");
+                clazz.append(e.getKey());
+                clazz.append(";\n");
+            }
+        }
+
+        clazz.append("private void test() {\n");
+        clazz.append("}\n");
+        clazz.append("}\n");
+
+        JavacTaskImpl jti = JavaSourceAccessor.getINSTANCE().getJavacTask(info);
+        Context context = jti.getContext();
+
+        Log.instance(context).nerrors = 0;
+
+        JavaFileObject jfo = FileObjects.memoryFileObject("$", "$", new File("/tmp/t.java").toURI(), System.currentTimeMillis(), clazz.toString());
+
+        try {
+            Iterable<? extends CompilationUnitTree> parsed = jti.parse(jfo);
+            CompilationUnitTree cut = parsed.iterator().next();
+
+            jti.analyze(jti.enter(parsed));
+
+            return new ScannerImpl().scan(cut, info);
+        } catch (IOException ex) {
+            Exceptions.printStackTrace(ex);
+            return null;
+        }
+    }
+
+    private static final class ScannerImpl extends TreePathScanner<Scope, CompilationInfo> {
+
+        @Override
+        public Scope visitBlock(BlockTree node, CompilationInfo p) {
+            return p.getTrees().getScope(getCurrentPath());
+        }
+
+        @Override
+        public Scope visitMethod(MethodTree node, CompilationInfo p) {
+            if (node.getReturnType() == null) {
+                return null;
+            }
+            return super.visitMethod(node, p);
+        }
+
+        @Override
+        public Scope reduce(Scope r1, Scope r2) {
+            return r1 != null ? r1 : r2;
+        }
+
+    }
+
+//    private static Scope constructScope2(CompilationInfo info, Map<String, TypeMirror> constraints) {
+//        JavacScope s = (JavacScope) info.getTrees().getScope(new TreePath(info.getCompilationUnit()));
+//        Env<AttrContext> env = s.getEnv();
+//
+//        env = env.dup(env.tree);
+//
+//        env.info.
+//    }
+
 }

api/src/org/netbeans/modules/jackpot30/impl/pm/Pattern.java

 
 package org.netbeans.modules.jackpot30.impl.pm;
 
-import com.sun.source.tree.BlockTree;
-import com.sun.source.tree.CompilationUnitTree;
-import com.sun.source.tree.MemberSelectTree;
-import com.sun.source.tree.MethodTree;
 import com.sun.source.tree.Scope;
 import com.sun.source.tree.Tree;
 import com.sun.source.util.SourcePositions;
 import com.sun.source.util.TreePath;
-import com.sun.source.util.TreePathScanner;
-import com.sun.tools.javac.api.JavacScope;
-import com.sun.tools.javac.api.JavacTaskImpl;
-import com.sun.tools.javac.comp.AttrContext;
-import com.sun.tools.javac.comp.Env;
-import com.sun.tools.javac.util.Context;
-import com.sun.tools.javac.util.Log;
-import java.io.File;
-import java.io.IOException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.regex.Matcher;
-import javax.lang.model.element.Element;
-import javax.lang.model.element.ElementKind;
 import javax.lang.model.element.TypeElement;
-import javax.lang.model.type.TypeKind;
 import javax.lang.model.type.TypeMirror;
-import javax.tools.JavaFileObject;
 import org.netbeans.api.java.source.CompilationInfo;
 import org.netbeans.modules.jackpot30.impl.Utilities;
 import org.netbeans.modules.jackpot30.impl.pm.CopyFinder.Pair;
-import org.netbeans.modules.java.source.JavaSourceAccessor;
-import org.netbeans.modules.java.source.parsing.FileObjects;
-import org.openide.util.Exceptions;
 
 /**XXX: cancelability!
  *
         return filtered.toString();
     }
 
-    private static long inc;
-
-    private static Scope constructScope(CompilationInfo info, Map<String, TypeMirror> constraints) {
-        StringBuilder clazz = new StringBuilder();
-
-        clazz.append("package $; public class $" + (inc++) + "{");
-
-        for (Entry<String, TypeMirror> e : constraints.entrySet()) {
-            if (e.getValue() != null) {
-                clazz.append("private ");
-                clazz.append(e.getValue().toString()); //XXX
-                clazz.append(" ");
-                clazz.append(e.getKey());
-                clazz.append(";\n");
-            }
-        }
-
-        clazz.append("private void test() {\n");
-        clazz.append("}\n");
-        clazz.append("}\n");
-
-        JavacTaskImpl jti = JavaSourceAccessor.getINSTANCE().getJavacTask(info);
-        Context context = jti.getContext();
-
-        Log.instance(context).nerrors = 0;
-
-        JavaFileObject jfo = FileObjects.memoryFileObject("$", "$", new File("/tmp/t.java").toURI(), System.currentTimeMillis(), clazz.toString());
-
-        try {
-            Iterable<? extends CompilationUnitTree> parsed = jti.parse(jfo);
-            CompilationUnitTree cut = parsed.iterator().next();
-
-            jti.analyze(jti.enter(parsed));
-
-            return new ScannerImpl().scan(cut, info);
-        } catch (IOException ex) {
-            Exceptions.printStackTrace(ex);
-            return null;
-        }
-    }
-    
-//    private static Scope constructScope2(CompilationInfo info, Map<String, TypeMirror> constraints) {
-//        JavacScope s = (JavacScope) info.getTrees().getScope(new TreePath(info.getCompilationUnit()));
-//        Env<AttrContext> env = s.getEnv();
-//
-//        env = env.dup(env.tree);
-//
-//        env.info.
-//    }
-    
     public static Tree parseAndAttribute(CompilationInfo info, String pattern, Map<String, TypeMirror> constraints, Scope[] scope) {
-        scope[0] = constructScope(info, constraints);
+        scope[0] = Utilities.constructScope(info, constraints);
 
         if (scope[0] == null) {
             return null;
         return Utilities.parseAndAttribute(info, pattern, scope[0]);
     }
 
-    private static final class ScannerImpl extends TreePathScanner<Scope, CompilationInfo> {
-
-        @Override
-        public Scope visitBlock(BlockTree node, CompilationInfo p) {
-            return p.getTrees().getScope(getCurrentPath());
-        }
-
-        @Override
-        public Scope visitMethod(MethodTree node, CompilationInfo p) {
-            if (node.getReturnType() == null) {
-                return null;
-            }
-            return super.visitMethod(node, p);
-        }
-
-        @Override
-        public Scope reduce(Scope r1, Scope r2) {
-            return r1 != null ? r1 : r2;
-        }
-
-    }
-    
 }

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

 import org.netbeans.api.java.source.TestUtilities;
 import org.netbeans.api.lexer.Language;
 import org.netbeans.junit.NbTestCase;
+import org.netbeans.modules.java.source.TreeLoader;
 import org.openide.cookies.EditorCookie;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
         super(name);
     }
 
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        SourceUtilsTestUtil.prepareTest(new String[0], new Object[0]);
+        TreeLoader.DISABLE_CONFINEMENT_TEST = true;
+    }
+
     protected void prepareTest(String fileName, String code) throws Exception {
         clearWorkDir();
 

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

+package org.netbeans.modules.jackpot30.impl;
+
+import com.sun.source.tree.Scope;
+import com.sun.source.tree.Tree;
+import com.sun.source.tree.Tree.Kind;
+import java.util.Collections;
+
+/**
+ *
+ * @author lahvac
+ */
+public class UtilitiesTest extends TestBase {
+
+    public UtilitiesTest(String name) {
+        super(name);
+    }
+
+    public void testParseAndAttributeExpressionStatement() throws Exception {
+        prepareTest("test/Test.java", "package test; public class Test{}");
+
+        Scope s = Utilities.constructScope(info, Collections.singletonMap("$1", info.getTreeUtilities().parseType("int", info.getTopLevelElements().get(0))));
+        Tree result = Utilities.parseAndAttribute(info, "$1 = 1;", s);
+
+        assertTrue(result.getKind().name(), result.getKind() == Kind.EXPRESSION_STATEMENT);
+    }
+
+}

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

 import java.util.HashMap;
 import java.util.Map;
 import java.util.Map.Entry;
-import org.netbeans.api.java.source.SourceUtilsTestUtil;
 import org.netbeans.modules.jackpot30.impl.TestBase;
 import org.netbeans.modules.java.hints.infrastructure.Pair;
-import org.netbeans.modules.java.source.TreeLoader;
 
 /**
  *
         super(name);
     }
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        SourceUtilsTestUtil.prepareTest(new String[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object[0]);
-        TreeLoader.DISABLE_CONFINEMENT_TEST = true;
-    }
-
     public void testSimple1() throws Exception {
         performVariablesTest("package test; public class Test {public void test() {int i = |1 + 2|;}}", "$1+$2",
                              new Pair<String, String>("$1", "1"),

api/test/unit/src/org/netbeans/modules/jackpot30/spi/JavaFixTest.java

 import javax.lang.model.element.ExecutableElement;
 import javax.lang.model.element.TypeElement;
 import javax.lang.model.util.ElementFilter;
-import org.netbeans.api.java.source.SourceUtilsTestUtil;
 import org.netbeans.modules.jackpot30.impl.TestBase;
-import org.netbeans.modules.java.source.TreeLoader;
 import org.openide.modules.SpecificationVersion;
 
 /**
         super(name);
     }
 
-    @Override
-    protected void setUp() throws Exception {
-        super.setUp();
-        SourceUtilsTestUtil.prepareTest(new String[] {"org/netbeans/modules/java/editor/resources/layer.xml"}, new Object[0]);
-        TreeLoader.DISABLE_CONFINEMENT_TEST = true;
-    }
-    
     public void testSimple() throws Exception {
         SpecificationVersion v = computeSpecVersion("/**\n" +
                                                     " * @since 1.5\n" +