You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by ge...@apache.org on 2018/03/27 11:32:20 UTC

[incubator-netbeans] branch jdk18_3 updated: Split into declaration and assignment issue for var type (#453)

This is an automated email from the ASF dual-hosted git repository.

geertjan pushed a commit to branch jdk18_3
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git


The following commit(s) were added to refs/heads/jdk18_3 by this push:
     new c8320a2  Split into declaration and assignment issue for var type (#453)
c8320a2 is described below

commit c8320a2fa3ddb7d8c211c01f3b325cfb39ba2750
Author: Vikas Prabhakar <vi...@oracle.com>
AuthorDate: Tue Mar 27 17:02:18 2018 +0530

    Split into declaration and assignment issue for var type (#453)
    
    * Split into declaration and assignment issue
    
    * Add review comment
    
    * Add space after var keyword
    
    * Add Jan comments
    
    * Add missing imports
    
    * Add JavaTokenId enum
    
    * Add anonymous method test case
    
    * Add anonymous method test case
    
    * add method in utilities class
    
    * add method in utilities class
    
    * add review request comment
    
    * add review request comment
    
    * Add api in apichanges.xml
---
 .../modules/java/hints/suggestions/Tiny.java       |  4 ++
 .../modules/java/hints/suggestions/TinyTest.java   | 82 +++++++++++++++++++++-
 java.source.base/apichanges.xml                    | 12 ++++
 java.source.base/nbproject/project.properties      |  2 +-
 .../netbeans/api/java/source/TreeUtilities.java    | 19 ++++-
 5 files changed, 116 insertions(+), 3 deletions(-)

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 9cd0bf6..670dbe2 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 class Tiny {
         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 94a2f0a..c968302 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 class TinyTest extends NbTestCase {
                               "}\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 class TinyTest extends NbTestCase {
                 .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 efe4cd7..6c33996 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 a97ce22..406c430 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 d616476..01438ca 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 final class TreeUtilities {
         
         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;

-- 
To stop receiving notification emails like this one, please contact
geertjan@apache.org.

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

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