You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@netbeans.apache.org by jl...@apache.org on 2018/06/08 08:56:19 UTC

[incubator-netbeans] branch master updated: [NETBEANS-774] Disable var hint for anonymous type

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

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


The following commit(s) were added to refs/heads/master by this push:
     new a1f54cf  [NETBEANS-774] Disable var hint for anonymous type
a1f54cf is described below

commit a1f54cf2765599ff5593615885ab5061bbfa2082
Author: Reema Taneja <32...@users.noreply.github.com>
AuthorDate: Fri Jun 8 01:56:07 2018 -0700

    [NETBEANS-774] Disable var hint for anonymous type
---
 .../ConvertInvalidVarToExplicitArrayType.java       |  4 ++--
 .../modules/java/hints/errors/Utilities.java        | 12 ++++++++++++
 .../java/hints/jdk/ConvertVarToExplicitType.java    | 20 ++++++++++++--------
 .../ConvertInvalidVarToExplicitArrayTypeTest.java   |  6 ++++++
 .../hints/jdk/ConvertVarToExplicitTypeTest.java     | 21 +++++++++++++++++++--
 5 files changed, 51 insertions(+), 12 deletions(-)

diff --git a/java.hints/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayType.java b/java.hints/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayType.java
index 428cb05..9568817 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayType.java
+++ b/java.hints/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayType.java
@@ -96,9 +96,9 @@ public class ConvertInvalidVarToExplicitArrayType implements ErrorRule<Void> {
 
                 TypeMirror etType = trees.getTypeMirror(new TreePath(initArrayTreePath, tree));
 
-                //skipped fix for invalid array member and for parameterized array member.
+                //skipped fix for invalid array member, anonymous class and parameterized array member.
                 if (etType == null || etType.getKind() == TypeKind.ERROR || (etType.getKind() == TypeKind.DECLARED
-                        && !((DeclaredType) etType).getTypeArguments().isEmpty())) {
+                        && !((DeclaredType) etType).getTypeArguments().isEmpty()) || Utilities.isAnonymousType(etType)) {
                     return null;
                 }
 
diff --git a/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java b/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
index 4fb6e5f..414fa5f 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
+++ b/java.hints/src/org/netbeans/modules/java/hints/errors/Utilities.java
@@ -138,6 +138,7 @@ import com.sun.tools.javac.util.JCDiagnostic;
 import com.sun.tools.javac.util.Log;
 import java.net.URI;
 import java.util.concurrent.Callable;
+import javax.lang.model.element.NestingKind;
 import javax.lang.model.type.ErrorType;
 import javax.lang.model.type.UnionType;
 import javax.tools.Diagnostic;
@@ -2995,4 +2996,15 @@ public class Utilities {
     public static boolean isModular(CompilationInfo info) {
         return getModuleInfo(info) != null;
     }
+    
+    public static boolean isAnonymousType(TypeMirror type) {
+        if (type.getKind() == TypeKind.DECLARED) {
+            DeclaredType dt = (DeclaredType) type;
+            TypeElement typeElem = (TypeElement) dt.asElement();
+            if (typeElem.getNestingKind() == NestingKind.ANONYMOUS) {
+                return true;
+            }
+        }
+        return false;
+    }
 }
diff --git a/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitType.java b/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitType.java
index 9a09945..db826e7 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitType.java
+++ b/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitType.java
@@ -19,12 +19,12 @@
 package org.netbeans.modules.java.hints.jdk;
 
 import com.sun.source.tree.CompilationUnitTree;
-import com.sun.source.tree.NewClassTree;
 import com.sun.source.tree.Tree;
 import com.sun.source.tree.VariableTree;
 import com.sun.source.util.TreePath;
 import javax.lang.model.SourceVersion;
 import javax.lang.model.element.ElementKind;
+import javax.lang.model.type.DeclaredType;
 import javax.lang.model.type.TypeKind;
 import javax.lang.model.type.TypeMirror;
 import javax.tools.Diagnostic;
@@ -151,17 +151,21 @@ public class ConvertVarToExplicitType {
     //filter anonymous class and intersection types
     private static boolean isValidType(HintContext ctx) {
         TreePath treePath = ctx.getPath();
-        TreePath initTreePath = ctx.getVariables().get("$init");  //NOI18N
+        TypeMirror variableTypeMirror = ctx.getInfo().getTrees().getElement(treePath).asType();
 
-        if (initTreePath.getLeaf().getKind() == Tree.Kind.NEW_CLASS) {
-            NewClassTree nct = ((NewClassTree) initTreePath.getLeaf());
-            if (nct.getClassBody() != null) {
-                return false;
+        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;
+                    }
+                }
             }
         }
 
-        TypeMirror variableTypeMirror = ctx.getInfo().getTrees().getElement(treePath).asType();
-
         if (!Utilities.isValidType(variableTypeMirror) ||(variableTypeMirror.getKind() == TypeKind.INTERSECTION)) {
             return false;
         }
diff --git a/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java b/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java
index a36d16f..a5d79cb 100644
--- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java
+++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/errors/ConvertInvalidVarToExplicitArrayTypeTest.java
@@ -63,6 +63,12 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase
                 "package test; public class Test {{final var j = {int1,var1,\"hello\"};}}",
                 -1);
     }
+    
+    public void testAnonymousClassTypeArray() throws Exception {
+        performAnalysisTest("test/Test.java",
+                "package test; public class Test {{var j = {new Object(){}};}}",
+                -1);
+    }
 
     public void testParameterizedElements() throws Exception {
         performAnalysisTest("test/Test.java",
diff --git a/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitTypeTest.java b/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitTypeTest.java
index 3942ee7..69bbe47 100644
--- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitTypeTest.java
+++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertVarToExplicitTypeTest.java
@@ -350,6 +350,23 @@ public class ConvertVarToExplicitTypeTest {
                         + "    }\n"
                         + "    <Z> List<Z> listOf(Z z) { return null; }\n"
                         + "}");
-    }    
-
+    } 
+    
+    @Test
+    public void testNoVarHintForAnonymousType() throws Exception {
+        HintTest.create()
+                .input("package test;\n"
+                        + "public class Test {\n"
+                        + "void v() {\n"
+                        + "  var v = get(new Object(){});\n" 
+                        + "}\n"
+                        + "\n" 
+                        + "<Z> Z get(Z z) {\n" 
+                        + "  return z; \n"
+                        + "}"
+                        + "}")                        
+                .sourceLevel("1.10")
+                .run(ConvertVarToExplicitType.class)
+                .assertNotContainsWarnings(VAR_CONV_WARNING);
+    }
 }

-- 
To stop receiving notification emails like this one, please contact
jlahoda@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