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/05/31 18:57:05 UTC

[GitHub] jlahoda closed pull request #565: Backport/cherry-pick Arunava's fix for [NETBEANS-764] to release90

jlahoda closed pull request #565: Backport/cherry-pick Arunava's fix for  [NETBEANS-764] to release90
URL: https://github.com/apache/incubator-netbeans/pull/565
 
 
   

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/jdk/ConvertToVarHint.java b/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHint.java
index 8245d9e74..c001f7d74 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHint.java
+++ b/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHint.java
@@ -157,6 +157,10 @@ private static boolean preConditionChecker(HintContext ctx) {
             return false;
         }
 
+        // hint is not applicable for compound variable declaration.
+        if (info.getTreeUtilities().isPartOfCompoundVariableDeclaration(treePath.getLeaf()))
+            return false;
+
         //  hint is not applicable for  variable declaration where type is already 'var'
         return !info.getTreeUtilities().isVarType(treePath);
     }
diff --git a/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHintTest.java b/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHintTest.java
index af3a65914..ebf199167 100644
--- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHintTest.java
+++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToVarHintTest.java
@@ -492,5 +492,38 @@ public void testConvertToVarWithDiamondOperator3() throws Exception {
                         + "    }\n"
                         + "}");
     }
+
+    @Test
+    public void testCompoundVariableDeclStatement() throws Exception {
+        HintTest.create()
+                .input("package test;\n"
+                        + "import java.util.List;\n"
+                        + "public class Test {\n"
+                        + "    void m() {\n"
+                        + "         int i =10,j=20;\n"
+                        + "    }\n"
+                        + "}")
+                .sourceLevel("1.10")
+                .run(ConvertToVarHint.class)
+                .assertNotContainsWarnings(VAR_CONV_DESC);
+
+    }
+
+    @Test
+    public void testCompoundVariableDeclStatement2() throws Exception {
+        HintTest.create()
+                .input("package test;\n"
+                        + "import java.util.List;\n"
+                        + "public class Test {\n"
+                        + "    void m() {\n"
+                        + "        final int /*comment*/l =10/*comment*/,i=20/*comment*/,j=5/*comment*/;\n"
+                        + "    }\n"
+                        + "}")
+                .sourceLevel("1.10")
+                .run(ConvertToVarHint.class)
+                .assertNotContainsWarnings(VAR_CONV_DESC);
+
+    }
+
     
 }
diff --git a/java.source.base/apichanges.xml b/java.source.base/apichanges.xml
index ae8601b2f..84c22cbf5 100644
--- a/java.source.base/apichanges.xml
+++ b/java.source.base/apichanges.xml
@@ -62,6 +62,18 @@
         </description>
         <class name="TreeUtilities" package="org.netbeans.api.java.source"/>
     </change>
+    <change id="TreeUtilities.isPartOfCompoundVariableDeclaration">
+        <api name="javasource_base"/>
+        <summary>Check the tree is the end of compound declaration.</summary>
+        <version major="1" minor="2.34"/>
+        <date day="14" month="5" year="2018"/>
+        <author login="arusinha"/>
+        <compatibility addition="yes" binary="compatible" source="compatible"/>
+        <description>
+            Check whether tree is part of compound declaration.
+        </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 8e4b7dd20..c5cf52143 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.33.0
+spec.version.base=2.34.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 b429ddaf1..b6b8d51dd 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
@@ -1883,6 +1883,50 @@ public boolean isEndOfCompoundVariableDeclaration(@NonNull Tree tree) {
         return false;
     }
  
+    /**
+     * Checks whether tree is part of compound variable declaration.
+     *
+     * @param tree tree to examine.
+     * @return true if {@code tree} is part of compound var declaration.
+     * @since 2.34.0
+     */
+    public boolean isPartOfCompoundVariableDeclaration(@NonNull Tree tree) {
+        TokenSequence<JavaTokenId> tokenSequence = tokensFor(tree);
+
+        if (tree.getKind() != Tree.Kind.VARIABLE) {
+            return false;
+        }
+
+        // If tree ends with comma then tree is part of compound variable declaration.
+        tokenSequence.moveEnd();
+        if (tokenSequence.movePrevious() && tokenSequence.token().id() == JavaTokenId.COMMA) {
+            return true;
+        }
+
+        int startPos = (int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), tree);
+        tokenSequence.moveStart();
+
+        int tokensLength = 0;
+
+        // To find out the first subtree from compound varaible declaration statement(if any).
+        while (tokenSequence.moveNext()) {
+            tokensLength += tokenSequence.token().length();
+            if (tokenSequence.token().id() == JavaTokenId.IDENTIFIER) {
+
+                Tree path = pathFor(startPos + tokensLength).getLeaf();
+                TokenSequence<JavaTokenId> TokenSeq = tokensFor(path);
+                TokenSeq.moveEnd();
+
+                if (TokenSeq.movePrevious() && TokenSeq.token().id() == JavaTokenId.COMMA) {
+                    return true;
+                }
+                break;
+            }
+        }
+
+        return false;
+    }
+
     private static final class NBScope implements Scope {
 
         private final JavacScope delegate;
diff --git a/java.source.base/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java b/java.source.base/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java
index dc882e966..7bbbf5e53 100644
--- a/java.source.base/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java
+++ b/java.source.base/test/unit/src/org/netbeans/api/java/source/TreeUtilitiesTest.java
@@ -592,4 +592,21 @@ public void testIsEndOfCompoundVariableDeclaration() throws Exception {
         assertFalse(info.getTreeUtilities().isEndOfCompoundVariableDeclaration(bt.getStatements().get(1)));
         assertTrue(info.getTreeUtilities().isEndOfCompoundVariableDeclaration(bt.getStatements().get(2)));
     }
+
+    public void testIsPartOfCompoundVariableDeclaration() throws Exception {
+        prepareTest("Test", "package test; public class Test {public Test(){int i = 10, j = 11; int k = 1;}}");
+
+        //int i = 10
+        VariableTree var1 = (VariableTree) info.getTreeUtilities().pathFor(55).getLeaf();
+        assertTrue(info.getTreeUtilities().isPartOfCompoundVariableDeclaration(var1));
+
+        //int j = 11
+        VariableTree var2 = (VariableTree) info.getTreeUtilities().pathFor(60).getLeaf();
+        assertTrue(info.getTreeUtilities().isPartOfCompoundVariableDeclaration(var2));
+
+        //int k = 1
+        VariableTree var3 = (VariableTree) info.getTreeUtilities().pathFor(71).getLeaf();
+        assertFalse(info.getTreeUtilities().isPartOfCompoundVariableDeclaration(var3));
+
+    }
 }


 

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