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/18 04:39:06 UTC

[incubator-netbeans] 14/20: [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 release90
in repository https://gitbox.apache.org/repos/asf/incubator-netbeans.git

commit 734c489115e2d579ee85755445a7804164e0cd10
Author: rtaneja <rt...@slc15zwx.us.oracle.com>
AuthorDate: Mon May 28 01:50:05 2018 -0700

    [NETBEANS-774] Disable var hint for anonymous type
---
 .../java/hints/jdk/ConvertVarToExplicitType.java    | 16 ++++++++++++++--
 .../hints/jdk/ConvertVarToExplicitTypeTest.java     | 21 +++++++++++++++++++--
 2 files changed, 33 insertions(+), 4 deletions(-)

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..798ae0d 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
@@ -23,8 +23,11 @@ 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 com.sun.tools.javac.code.Symbol.ClassSymbol;
 import javax.lang.model.SourceVersion;
 import javax.lang.model.element.ElementKind;
+import javax.lang.model.element.TypeElement;
+import javax.lang.model.type.DeclaredType;
 import javax.lang.model.type.TypeKind;
 import javax.lang.model.type.TypeMirror;
 import javax.tools.Diagnostic;
@@ -152,16 +155,25 @@ public class ConvertVarToExplicitType {
     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;
             }
+        } else if (initTreePath.getLeaf().getKind() == Tree.Kind.METHOD_INVOCATION) {
+            //filter anonymous class type return type
+            if (variableTypeMirror.getKind() == TypeKind.DECLARED) {
+                DeclaredType dt = (DeclaredType) variableTypeMirror;
+                TypeElement typeElem = (TypeElement) dt.asElement();
+                ClassSymbol sym = (ClassSymbol) typeElem;
+                if (sym.isAnonymous()) {
+                    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/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