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/03/27 11:32:19 UTC

[GitHub] geertjanw closed pull request #453: Split into declaration and assignment issue for var type

geertjanw closed pull request #453: Split into declaration and assignment issue for var type
URL: https://github.com/apache/incubator-netbeans/pull/453
 
 
   

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.hints/src/org/netbeans/modules/java/hints/suggestions/Tiny.java b/java.hints/src/org/netbeans/modules/java/hints/suggestions/Tiny.java
index 9cd0bf660..670dbe24f 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/suggestions/Tiny.java
+++ b/java.hints/src/org/netbeans/modules/java/hints/suggestions/Tiny.java
@@ -222,6 +222,10 @@ public static ErrorDescription splitDeclaration(HintContext ctx) {
         Tree.Kind parentKind = ctx.getPath().getParentPath().getLeaf().getKind();
 
         if (parentKind != Tree.Kind.BLOCK && parentKind != Tree.Kind.CASE) return null;
+        
+        if(ctx.getInfo().getTreeUtilities().isVarType(ctx.getPath())){
+            return null; // hints discarded for var keyword
+        }      
 
         String displayName = NbBundle.getMessage(Tiny.class, "ERR_splitDeclaration");
         Fix fix = new FixImpl(ctx.getInfo(), ctx.getPath()).toEditorFix();
diff --git a/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/TinyTest.java b/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/TinyTest.java
index 94a2f0a21..c9683027c 100644
--- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/TinyTest.java
+++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/suggestions/TinyTest.java
@@ -268,6 +268,86 @@ public void testSplitDeclaration3() throws Exception {
                               "}\n");
     }
 
+    public void testSplitDeclarationForVar1() throws Exception {
+        HintTest
+                .create()
+                .setCaretMarker('|')
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "    void m1(){\n" +
+                       "        var v =| 10; \n" +
+                       "    }\n" +
+                       "}\n")
+                .sourceLevel("1.10")
+                .run(Tiny.class)
+                .assertNotContainsWarnings("ERR_splitDeclaration");
+    }
+    
+    public void testSplitDeclarationForVar2() throws Exception {
+        HintTest
+                .create()
+                .setCaretMarker('|')
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "    void m1(){\n" +
+                       "        final var i =| 10; \n" +
+                       "    }\n" +
+                       "}\n")
+                .sourceLevel("1.10")
+                .run(Tiny.class)
+                .assertNotContainsWarnings("ERR_splitDeclaration");
+    }
+    
+    public void testSplitDeclarationForVar3() throws Exception {
+        HintTest
+                .create()
+                .setCaretMarker('|')
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "    void m1(){\n" +
+                       "        final/*comment*/var x =| 1.5; \n" +
+                       "    }\n" +
+                       "}\n")
+                .sourceLevel("1.10")
+                .run(Tiny.class)
+                .assertNotContainsWarnings("ERR_splitDeclaration");
+    }
+    
+    public void testSplitDeclarationForVar4() throws Exception {
+        HintTest
+                .create()
+                .setCaretMarker('|')
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "    void m1(){\n" +
+                       "        var/*comment*/y =| 100; \n" +
+                       "    }\n" +
+                       "}\n")
+                .sourceLevel("1.10")
+                .run(Tiny.class)
+                .assertNotContainsWarnings("ERR_splitDeclaration");
+    }
+    
+    public void testSplitDeclarationForVar5() throws Exception {
+        HintTest
+                .create()
+                .setCaretMarker('|')
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "    void m1(){\n" +
+                       "        Runnable r =| new Runnable(){ \n" +
+                       "        @Override \n" +
+                       "        public void run() { \n" +
+                       "        var v = 10; \n" +
+                       "        } \n" +
+                       "      }; \n" +
+                       "    }\n" +
+                       "}\n")
+                .sourceLevel("1.10")
+                .run(Tiny.class)
+                .findWarning("3:20-3:20:hint:ERR_splitDeclaration");
+    }
+ 
     public void testFillSwitch1() throws Exception {
         HintTest
                 .create()
@@ -451,4 +531,4 @@ public void testFillSwitchTypeVar222372() throws Exception {
                 .run(Tiny.class)
                 .assertWarnings();
     }
-}
\ No newline at end of file
+}
diff --git a/java.source.base/apichanges.xml b/java.source.base/apichanges.xml
index efe4cd72d..6c3399692 100644
--- a/java.source.base/apichanges.xml
+++ b/java.source.base/apichanges.xml
@@ -25,6 +25,18 @@
     <apidef name="javasource_base">Java Source API</apidef>
 </apidefs>
 <changes>
+    <change id="TreeUtilities.isVarType">
+        <api name="javasource_base"/>
+        <summary>Check the var type variable in given tree path.</summary>
+        <version major="1" minor="2.31"/>
+        <date day="27" month="3" year="2018"/>
+        <author login="vikasprabhakar"/>
+        <compatibility addition="yes" binary="compatible" source="compatible"/>
+        <description>
+            Check the var type variable in given tree path.
+        </description>
+        <class name="TreeUtilities" package="org.netbeans.api.java.source"/>
+    </change>
     <change id="ElementHandle.createModuleElementHandle">
         <api name="javasource_base"/>
         <summary>Added a method to create an <code>ElementHandle</code> for module</summary>
diff --git a/java.source.base/nbproject/project.properties b/java.source.base/nbproject/project.properties
index a97ce2287..406c43054 100644
--- a/java.source.base/nbproject/project.properties
+++ b/java.source.base/nbproject/project.properties
@@ -23,7 +23,7 @@ javadoc.name=Java Source Base
 javadoc.title=Java Source Base
 javadoc.arch=${basedir}/arch.xml
 javadoc.apichanges=${basedir}/apichanges.xml
-spec.version.base=2.30.0
+spec.version.base=2.31.0
 test.qa-functional.cp.extra=${refactoring.java.dir}/modules/ext/nb-javac-api.jar
 test.unit.run.cp.extra=${o.n.core.dir}/core/core.jar:\
     ${o.n.core.dir}/lib/boot.jar:\
diff --git a/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java b/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
index d616476d0..01438ca8d 100644
--- a/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
+++ b/java.source.base/src/org/netbeans/api/java/source/TreeUtilities.java
@@ -1850,7 +1850,24 @@ public boolean isCompileTimeConstantExpression(TreePath expression) {
         
         return ref.paramTypes;
     }
-    
+   
+    /**Check the var type variable in given tree path {@link TreePath}.
+     * 
+     * @param path the path of tree {@link TreePath}
+     * @return the true if tree contains var keyword else return false
+     * @since 2.31.0
+     */
+    public boolean isVarType(@NonNull TreePath path) {
+        TokenSequence<JavaTokenId> tokenSequence = tokensFor(path.getLeaf());
+        tokenSequence.moveStart();
+        while(tokenSequence.moveNext() && tokenSequence.token().id() != JavaTokenId.EQ){
+            if(tokenSequence.token().id() == JavaTokenId.VAR){
+                return true;
+            }
+        }
+        return false;
+    }
+ 
     private static final class NBScope implements Scope {
 
         private final JavacScope delegate;


 

----------------------------------------------------------------
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