You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@netbeans.apache.org by GitBox <gi...@apache.org> on 2018/09/12 10:45:55 UTC

[GitHub] geertjanw closed pull request #650: [NETBEANS-1075]-Adding var hints for enhanced-for-loop

geertjanw closed pull request #650: [NETBEANS-1075]-Adding var hints for enhanced-for-loop
URL: https://github.com/apache/incubator-netbeans/pull/650
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/java/java.hints/manifest.mf b/java/java.hints/manifest.mf
index 0d7fdd5abe..4299859da0 100644
--- a/java/java.hints/manifest.mf
+++ b/java/java.hints/manifest.mf
@@ -1,6 +1,6 @@
 Manifest-version: 1.0
 OpenIDE-Module: org.netbeans.modules.java.hints/1
-OpenIDE-Module-Implementation-Version: 16
+OpenIDE-Module-Implementation-Version: 17
 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/hints/resources/Bundle.properties
 OpenIDE-Module-Layer: org/netbeans/modules/java/hints/resources/layer.xml
 AutoUpdate-Show-In-Client: false
diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHint.java b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHint.java
index 827bf39951..c9e537d7ec 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHint.java
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHint.java
@@ -41,6 +41,16 @@
 import javax.lang.model.element.ElementKind;
 import javax.lang.model.type.TypeMirror;
 import org.netbeans.modules.java.hints.errors.Utilities;
+import com.sun.source.tree.EnhancedForLoopTree;
+import com.sun.source.tree.StatementTree;
+import com.sun.tools.javac.code.Type;
+import com.sun.tools.javac.code.Type.ArrayType;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ExecutableElement;
+import javax.lang.model.type.DeclaredType;
+import javax.lang.model.type.TypeKind;
+import org.netbeans.modules.java.hints.suggestions.ExpandEnhancedForLoop;
+import org.netbeans.spi.java.hints.TriggerPatterns;
 
 /**
  * Hint will convert explicit type of local variable to 'var'. Supported: JDK 10
@@ -57,8 +67,10 @@
         "compiler.err.generic.array.creation" //NOI18N
     };
 
-    @TriggerPattern("$mods$ $type $var = $init") //NOI18N
-
+    @TriggerPatterns({
+        @TriggerPattern("$mods$ $type $var = $init"), //NOI18N
+        @TriggerPattern("for ($type $var : $expression) { $stmts$; }") //NOI18N
+    })
     public static ErrorDescription computeExplicitToVarType(HintContext ctx) {
         if (!preConditionChecker(ctx)) {
             return null;
@@ -121,6 +133,22 @@ protected void performRewrite(TransformationContext tc) throws Exception {
                         initializerTree
                 );
                 wc.rewrite(oldVariableTree, newVariableTree);
+            } else if (statementPath.getLeaf().getKind() == Tree.Kind.ENHANCED_FOR_LOOP) {
+                EnhancedForLoopTree elfTree = (EnhancedForLoopTree) statementPath.getLeaf();
+                ExpressionTree expTree = elfTree.getExpression();
+                VariableTree vtt = elfTree.getVariable();
+                if (expTree == null || vtt == null) {
+                    return;
+                }
+                VariableTree newVariableTree = make.Variable(
+                        vtt.getModifiers(),
+                        vtt.getName(),
+                        make.Type("var"),
+                        null
+                );
+                StatementTree statement = ((EnhancedForLoopTree) statementPath.getLeaf()).getStatement();
+                EnhancedForLoopTree newElfTree = make.EnhancedForLoop(newVariableTree, expTree, statement);
+                wc.rewrite(elfTree, newElfTree);
             }
         }
     }
@@ -134,15 +162,9 @@ private static boolean preConditionChecker(HintContext ctx) {
 
         CompilationInfo info = ctx.getInfo();
 
-        // hint will be enable only for JDK-10 or above.
-        if (info.getSourceVersion().compareTo(SourceVersion.RELEASE_9) < 1) {
-            return false;
-        }
-
         TreePath treePath = ctx.getPath();
 
-        // variable should have local scope
-        if (info.getTrees().getElement(treePath).getKind() != ElementKind.LOCAL_VARIABLE) {
+        if (!ConvertVarToExplicitType.isVariableValidForVarHint(ctx)) {
             return false;
         }
 
@@ -161,7 +183,8 @@ private static boolean preConditionChecker(HintContext ctx) {
     private static boolean isValidVarType(HintContext ctx) {
         TreePath treePath = ctx.getPath();
         TreePath initTreePath = ctx.getVariables().get("$init");  //NOI18N
-        
+        TreePath expressionTreePath = ctx.getVariables().get("$expression"); //NOI18N
+        TreePath typeTreePath = ctx.getVariables().get("$type"); //NOI18N
         if (initTreePath != null) {
             Tree.Kind kind = initTreePath.getLeaf().getKind();
             switch (kind) {
@@ -184,17 +207,35 @@ private static boolean isValidVarType(HintContext ctx) {
                 default:
                     break;
             }
+            // variable initializer type should be same as variable type.
+            TypeMirror initTypeMirror = ctx.getInfo().getTrees().getTypeMirror(initTreePath);
+            TypeMirror variableTypeMirror = ctx.getInfo().getTrees().getElement(treePath).asType();
+            if ((!Utilities.isValidType(initTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(variableTypeMirror, Utilities.resolveCapturedType(ctx.getInfo(), initTypeMirror)))) {
+                return false;
+            }
+            return true;
+        } else if (expressionTreePath != null) {
+            ExecutableElement iterator = ExpandEnhancedForLoop.findIterable(ctx.getInfo());
+            TypeMirror expTypeMirror = ctx.getInfo().getTrees().getTypeMirror(expressionTreePath);
+            TypeMirror typeTypeMirror = ctx.getInfo().getTrees().getTypeMirror(typeTreePath);
+            if (expTypeMirror.getKind() == TypeKind.DECLARED) {
+                DeclaredType dt = (DeclaredType) expTypeMirror;
+                if (dt.getTypeArguments().size() > 0) {
+                    TypeMirror paramType = dt.getTypeArguments().get(0);
+                    if ((!Utilities.isValidType(typeTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(typeTypeMirror, paramType))) {
+                        return false;
+                    }
+                }
+            } else {
+                ArrayType arrayTypeExp = (ArrayType) Utilities.resolveCapturedType(ctx.getInfo(), expTypeMirror);
+                Type arrayTypeExpType = arrayTypeExp.getComponentType();
+                if ((!Utilities.isValidType(typeTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(typeTypeMirror, arrayTypeExpType))) {
+                    return false;
+                }
+            }
+            return (iterator != null);
         } else {
             return false;
         }
-        // variable initializer type should be same as variable type.
-        TypeMirror initTypeMirror = ctx.getInfo().getTrees().getTypeMirror(initTreePath);
-        TypeMirror variableTypeMirror = ctx.getInfo().getTrees().getElement(treePath).asType();
-
-        if ((!Utilities.isValidType(initTypeMirror)) || (!ctx.getInfo().getTypes().isSameType(variableTypeMirror, Utilities.resolveCapturedType(ctx.getInfo(), initTypeMirror)))) {
-            return false;
-        }
-                
-        return true;
     }
 }
diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitType.java b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitType.java
index 9e6f9ffa3b..151b7b6149 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitType.java
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitType.java
@@ -40,6 +40,13 @@
 import org.netbeans.spi.java.hints.JavaFix.TransformationContext;
 import org.netbeans.spi.java.hints.TriggerPattern;
 import org.openide.util.NbBundle.Messages;
+import com.sun.source.tree.EnhancedForLoopTree;
+import com.sun.source.tree.ExpressionTree;
+import com.sun.source.tree.StatementTree;
+import javax.lang.model.element.Element;
+import javax.lang.model.element.ExecutableElement;
+import org.netbeans.modules.java.hints.suggestions.ExpandEnhancedForLoop;
+import org.netbeans.spi.java.hints.TriggerPatterns;
 
 /**
  * Hint to convert type in local variable declaration from 'var' to explicit
@@ -51,7 +58,10 @@
 @Messages("MSG_ConvertibleToExplicitType=Convert var to explicit type")
 public class ConvertVarToExplicitType {
 
-    @TriggerPattern("$mods$ $type $var = $init") //NOI18N
+    @TriggerPatterns({
+        @TriggerPattern("$mods$ $type $var = $init"), //NOI18N
+        @TriggerPattern("for ($type $var : $expression) { $stmts$; }") //NOI18N
+    })
     public static ErrorDescription convertVarToExplicitType(HintContext ctx) {
 
         if (!isLocalVarType(ctx)) {
@@ -104,6 +114,24 @@ protected void performRewrite(TransformationContext tc) throws Exception {
                         oldVariableTree.getInitializer()
                 );
                 wc.rewrite(oldVariableTree, newVariableTree);
+            } else if (statementPath.getLeaf().getKind() == Tree.Kind.ENHANCED_FOR_LOOP) {
+                EnhancedForLoopTree elfTree = (EnhancedForLoopTree) statementPath.getLeaf();
+                ExpressionTree expTree = elfTree.getExpression();
+                VariableTree vtt = elfTree.getVariable();
+                String elfTreeVariable = elfTree.getVariable().getType().toString();
+                if (expTree == null) {
+                    return;
+                }
+                //VariableTree with null ExpressionTree as no initialization required
+                VariableTree newVariableTree = make.Variable(
+                        vtt.getModifiers(),
+                        vtt.getName(),
+                        make.Type(elfTreeVariable),
+                        null
+                );
+                StatementTree statement = ((EnhancedForLoopTree) statementPath.getLeaf()).getStatement();
+                EnhancedForLoopTree newElfTree = make.EnhancedForLoop(newVariableTree, expTree, statement);
+                wc.rewrite(elfTree, newElfTree);
             }
         }
 
@@ -118,42 +146,68 @@ private static boolean isLocalVarType(HintContext ctx) {
 
         CompilationInfo info = ctx.getInfo();
         
-        if (info.getSourceVersion().compareTo(SourceVersion.RELEASE_9) < 1) {
-            return false;
-        }        
-
         TreePath treePath = ctx.getPath();
 
-        // should be local variable
-        if (info.getTrees().getElement(treePath).getKind() != ElementKind.LOCAL_VARIABLE) {
+        if (!isVariableValidForVarHint(ctx)) { 
             return false;
         }
 
         // variable declaration of type 'var'
         return info.getTreeUtilities().isVarType(treePath);
     }
+    
+    protected static boolean isVariableValidForVarHint(HintContext ctx) {
+        CompilationInfo info = ctx.getInfo();
+        TreePath treePath = ctx.getPath();
+        // hint will be enable only for JDK-10 or above.
+        if (info.getSourceVersion().compareTo(SourceVersion.RELEASE_9) < 1) {
+            return false;
+        }
+         if (treePath.getLeaf().getKind() == Tree.Kind.ENHANCED_FOR_LOOP) {
+            EnhancedForLoopTree efl = (EnhancedForLoopTree) treePath.getLeaf();
+            TypeMirror expressionType = ctx.getInfo().getTrees().getTypeMirror(new TreePath(treePath, efl.getExpression()));
+            if (!Utilities.isValidType(expressionType)) {
+                return false;
+            }
+        } else {
+            Element treePathELement = info.getTrees().getElement(treePath);
+            // should be local variable
+            if (treePathELement != null && (treePathELement.getKind() != ElementKind.LOCAL_VARIABLE && treePathELement.getKind() != ElementKind.RESOURCE_VARIABLE)) {
+                return false;
+            }
+        }
+        return true;
+    }
 
     //filter anonymous class and intersection types
     private static boolean isValidType(HintContext ctx) {
         TreePath treePath = ctx.getPath();
-        TypeMirror variableTypeMirror = ctx.getInfo().getTrees().getElement(treePath).asType();
-
-        if (Utilities.isAnonymousType(variableTypeMirror)) {
-            return false;
-        } else if (variableTypeMirror.getKind() == TypeKind.DECLARED) {
-            DeclaredType dt = (DeclaredType) variableTypeMirror;
-            if (dt.getTypeArguments().size() > 0) {
-                for (TypeMirror paramType : dt.getTypeArguments()) {
-                    if (Utilities.isAnonymousType(paramType)) {
-                        return false;
+        TreePath initTreePath = ctx.getVariables().get("$init");  //NOI18N
+        TreePath expressionTreePath = ctx.getVariables().get("$expression"); //NOI18N
+        if (initTreePath != null) {
+            TypeMirror variableTypeMirror = ctx.getInfo().getTrees().getElement(treePath).asType();
+            if (Utilities.isAnonymousType(variableTypeMirror)) {
+                return false;
+            } else if (variableTypeMirror.getKind() == TypeKind.DECLARED) {
+                DeclaredType dt = (DeclaredType) variableTypeMirror;
+                if (dt.getTypeArguments().size() > 0) {
+                    for (TypeMirror paramType : dt.getTypeArguments()) {
+                        if (Utilities.isAnonymousType(paramType)) {
+                            return false;
+                        }
                     }
                 }
             }
-        }
 
-        if (!Utilities.isValidType(variableTypeMirror) ||(variableTypeMirror.getKind() == TypeKind.INTERSECTION)) {
+            if (!Utilities.isValidType(variableTypeMirror) || (variableTypeMirror.getKind() == TypeKind.INTERSECTION)) {
+                return false;
+            }
+            return true;
+        } else if (expressionTreePath != null) {
+            ExecutableElement iterator = ExpandEnhancedForLoop.findIterable(ctx.getInfo());
+            return (iterator != null);
+        } else {
             return false;
         }
-        return true;
     }
 }
diff --git a/java/java.hints/src/org/netbeans/modules/java/hints/suggestions/ExpandEnhancedForLoop.java b/java/java.hints/src/org/netbeans/modules/java/hints/suggestions/ExpandEnhancedForLoop.java
index 5649308b27..e7c2620dd9 100644
--- a/java/java.hints/src/org/netbeans/modules/java/hints/suggestions/ExpandEnhancedForLoop.java
+++ b/java/java.hints/src/org/netbeans/modules/java/hints/suggestions/ExpandEnhancedForLoop.java
@@ -96,7 +96,7 @@ public static ErrorDescription run(HintContext ctx) {
         
     }
 
-    private static ExecutableElement findIterable(CompilationInfo info) {
+    public static ExecutableElement findIterable(CompilationInfo info) {
         TypeElement iterable = info.getElements().getTypeElement("java.lang.Iterable");
 
         if (iterable == null) {
diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHintTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHintTest.java
index 4ea28140ac..a9a2168851 100644
--- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHintTest.java
+++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHintTest.java
@@ -136,6 +136,122 @@ public void testArrayInitializerVar() throws Exception {
                 .assertNotContainsWarnings(VAR_CONV_DESC);
 
     }
+    
+    @Test
+    public void testConvertIntToVarTypeInEnhancedForLoop() throws Exception {
+        HintTest.create().setCaretMarker('^')
+                .input("package test;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        int[] offInt = {1, 2, 3};\n"
+                        + "        for (int x : offInt)^ {\n"
+                        + "            \n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n")
+                .sourceLevel("1.10")
+                .run(ConvertToVarHint.class)
+                .findWarning("4:8-4:30:" + VAR_CONV_WARNING)
+                .applyFix()
+                .assertCompilable()
+                .assertVerbatimOutput("package test;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        int[] offInt = {1, 2, 3};\n"
+                        + "        for (var x : offInt) {\n"
+                        + "            \n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n");
+    }
+     @Test
+    public void testConvertStringtoVarTypeInEnhancedForLoop() throws Exception {
+        HintTest.create().setCaretMarker('^')
+                .input("package test;\n"
+                        + "import java.util.List;\n"
+                        + "import java.util.ArrayList;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        List<String> offStr = new ArrayList<>();\n"
+                        + "        offStr.add(\"a\");\n"
+                        + "        for (String x : offStr)^ {\n"
+                        + "            \n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n")
+                .sourceLevel("1.10")
+                .run(ConvertToVarHint.class)
+                .findWarning("7:8-7:33:" + VAR_CONV_WARNING)
+                .applyFix()
+                .assertCompilable()
+                .assertVerbatimOutput("package test;\n"
+                        + "import java.util.List;\n"
+                        + "import java.util.ArrayList;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        List<String> offStr = new ArrayList<>();\n"
+                        + "        offStr.add(\"a\");\n"
+                        + "        for (var x : offStr) {\n"
+                        + "            \n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n");
+    }
+    
+    @Test
+    public void testWrongMatchForVarEnhancedForLoop() throws Exception {
+        HintTest.create()
+                .setCaretMarker('^')
+                .input("package test;\n"
+                        + "import java.util.List;\n"
+                        + "import java.util.ArrayList;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        List<String> offStr = new ArrayList<>();\n"
+                        + "        offStr.add(\"a\");\n"
+                        + "        for (Object x : offStr)^ {\n"
+                        + "            \n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n")
+                .sourceLevel("1.10")
+                .run(ConvertToVarHint.class)
+                .assertNotContainsWarnings(VAR_CONV_DESC);
+    }
+    
+    @Test
+    public void testConvertStringtoVarTypeInEnhancedForLoopWithVarDeclaration() throws Exception {
+        HintTest.create().setCaretMarker('^')
+                .input("package test;\n"
+                        + "import java.util.List;\n"
+                        + "import java.util.ArrayList;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        List<String> offStr = new ArrayList<>();\n"
+                        + "        offStr.add(\"a\");\n"
+                        + "        for (String x : offStr)^ {\n"
+                        + "            var y = 10;\n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n")
+                .sourceLevel("1.10")
+                .run(ConvertToVarHint.class)
+                .findWarning("7:8-7:33:" + VAR_CONV_WARNING)
+                .applyFix()
+                .assertCompilable()
+                .assertVerbatimOutput("package test;\n"
+                        + "import java.util.List;\n"
+                        + "import java.util.ArrayList;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        List<String> offStr = new ArrayList<>();\n"
+                        + "        offStr.add(\"a\");\n"
+                        + "        for (var x : offStr) {\n"
+                        + "            var y = 10;\n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n");
+    }
 
     @Test
     public void testAnonymusObjRefToVar() throws Exception {
diff --git a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitTypeTest.java b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitTypeTest.java
index 84cf5bd477..41c3829223 100644
--- a/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitTypeTest.java
+++ b/java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitTypeTest.java
@@ -72,6 +72,101 @@ public void testConvertVarToString() throws Exception {
                         + "    }\n"
                         + "}\n");
     }
+    
+    @Test
+    public void testConvertVartoIntTypeInEnhancedForLoop() throws Exception {
+        HintTest.create().setCaretMarker('^')
+                .input("package test;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        int[] offInt = {1, 2, 3};\n"
+                        + "        for (var x : offInt)^ {\n"
+                        + "            \n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n")
+                .sourceLevel("1.10")
+                .run(ConvertVarToExplicitType.class)
+                .findWarning("4:8-4:30:" + VAR_CONV_WARNING)
+                .applyFix()
+                .assertCompilable()
+                .assertVerbatimOutput("package test;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        int[] offInt = {1, 2, 3};\n"
+                        + "        for (int x : offInt) {\n"
+                        + "            \n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n");
+    }
+     @Test
+    public void testConvertVartoStringTypeInEnhancedForLoop() throws Exception {
+        HintTest.create().setCaretMarker('^')
+                .input("package test;\n"
+                        + "import java.util.List;\n"
+                        + "import java.util.ArrayList;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        List<String> offStr = new ArrayList<>();\n"
+                        + "        offStr.add(\"a\");\n"
+                        + "        for (var x : offStr)^ {\n"
+                        + "            \n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n")
+                .sourceLevel("1.10")
+                .run(ConvertVarToExplicitType.class)
+                .findWarning("7:8-7:30:" + VAR_CONV_WARNING)
+                .applyFix()
+                .assertCompilable()
+                .assertVerbatimOutput("package test;\n"
+                        + "import java.util.List;\n"
+                        + "import java.util.ArrayList;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        List<String> offStr = new ArrayList<>();\n"
+                        + "        offStr.add(\"a\");\n"
+                        + "        for (String x : offStr) {\n"
+                        + "            \n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n");
+    }
+    
+    @Test
+    public void testConvertVartoStringTypeInEnhancedForLoopWithVarDeclaration() throws Exception {
+        HintTest.create().setCaretMarker('^')
+                .input("package test;\n"
+                        + "import java.util.List;\n"
+                        + "import java.util.ArrayList;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        List<String> offStr = new ArrayList<>();\n"
+                        + "        offStr.add(\"a\");\n"
+                        + "        for (var x : offStr)^ {\n"
+                        + "            var y = 10;\n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n")
+                .sourceLevel("1.10")
+                .run(ConvertVarToExplicitType.class)
+                .findWarning("7:8-7:30:" + VAR_CONV_WARNING)
+                .applyFix()
+                .assertCompilable()
+                .assertVerbatimOutput("package test;\n"
+                        + "import java.util.List;\n"
+                        + "import java.util.ArrayList;\n"
+                        + "public class Test {\n"
+                        + "    void m1() {\n"
+                        + "        List<String> offStr = new ArrayList<>();\n"
+                        + "        offStr.add(\"a\");\n"
+                        + "        for (String x : offStr) {\n"
+                        + "            var y = 10;\n"
+                        + "        }\n"
+                        + "    }\n"
+                        + "}\n");
+    }
 
     @Test
     public void testVartoHashMap() throws Exception {
diff --git a/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java b/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
index ca507078f4..dabee6847b 100644
--- a/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
+++ b/java/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
@@ -1891,7 +1891,7 @@ public boolean isCompileTimeConstantExpression(TreePath expression) {
     public boolean isVarType(@NonNull TreePath path) {
         TokenSequence<JavaTokenId> tokenSequence = tokensFor(path.getLeaf());
         tokenSequence.moveStart();
-        while(tokenSequence.moveNext() && tokenSequence.token().id() != JavaTokenId.EQ){
+        while(tokenSequence.moveNext() && tokenSequence.token().id() != JavaTokenId.EQ && tokenSequence.token().id() != JavaTokenId.COLON && tokenSequence.token().id() != JavaTokenId.RPAREN && tokenSequence.token().id() != JavaTokenId.SEMICOLON){
             if(tokenSequence.token().id() == JavaTokenId.VAR){
                 return true;
             }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@netbeans.apache.org
For additional commands, e-mail: notifications-help@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists