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:38:59 UTC

[incubator-netbeans] 07/20: [NETBEANS-481] Refactored code of ConvertInvalidVarToExplicitArrayType class

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 20332b056f14036ec25147f1e5641bff0275f5e9
Author: Arunava Sinha <ar...@oracle.com>
AuthorDate: Mon Apr 30 12:33:56 2018 +0530

    [NETBEANS-481] Refactored code of ConvertInvalidVarToExplicitArrayType class
---
 .../ConvertInvalidVarToExplicitArrayType.java      | 64 +++++-----------------
 .../ConvertInvalidVarToExplicitArrayTypeTest.java  | 17 ++++--
 2 files changed, 28 insertions(+), 53 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 dda5c70..cdc9972 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
@@ -29,6 +29,7 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 import com.sun.source.tree.VariableTree;
+import com.sun.source.util.Trees;
 import java.util.HashMap;
 import java.util.Map;
 import javax.lang.model.type.TypeKind;
@@ -41,6 +42,7 @@ import org.netbeans.modules.java.hints.spi.ErrorRule;
 import org.netbeans.spi.editor.hints.Fix;
 import org.netbeans.spi.java.hints.JavaFix;
 import org.openide.util.NbBundle.Messages;
+import javax.lang.model.util.Types;
 
 /**
  *
@@ -52,21 +54,11 @@ import org.openide.util.NbBundle.Messages;
 public class ConvertInvalidVarToExplicitArrayType implements ErrorRule<Void> {
 
     private static final Set<String> CODES;
-    private static final Map<TypeKind, Integer> NUMERIC_PRIMITIVE_TYPE_PRIORITY_MAP;
 
     static {
         Set<String> codes = new HashSet<>();
         codes.add("compiler.err.cant.infer.local.var.type"); // NOI18N
         CODES = Collections.unmodifiableSet(codes);
-
-        Map<TypeKind, Integer> map = new HashMap<>();
-        map.put(TypeKind.BYTE, 1);
-        map.put(TypeKind.SHORT, 2);
-        map.put(TypeKind.INT, 3);
-        map.put(TypeKind.LONG, 4);
-        map.put(TypeKind.FLOAT, 5);
-        map.put(TypeKind.DOUBLE, 6);
-        NUMERIC_PRIMITIVE_TYPE_PRIORITY_MAP = Collections.unmodifiableMap(map);
     }
 
     @Override
@@ -81,22 +73,20 @@ public class ConvertInvalidVarToExplicitArrayType implements ErrorRule<Void> {
             return null;
         }
 
-        TypeMirror arrayTypeMirror = null;
+        TypeMirror arrayType = null;
         if (treePath.getLeaf().getKind() == Tree.Kind.VARIABLE) {
             VariableTree oldVariableTree = (VariableTree) treePath.getLeaf();
             NewArrayTree arrayTree = (NewArrayTree) oldVariableTree.getInitializer();
             List<? extends ExpressionTree> currentValues = arrayTree.getInitializers();
-
-            TypeMirror arrayElementTypeMirror = null;
-            Integer maxTypePriority = -1;
-
-            boolean isHomoGeneousArray = true;
             TreePath initArrayTreePath = new TreePath(treePath, arrayTree);
+            Types types = compilationInfo.getTypes();
+            Trees trees = compilationInfo.getTrees();
 
             for (ExpressionTree tree : currentValues) {
 
-                arrayElementTypeMirror = compilationInfo.getTrees().getTypeMirror(new TreePath(initArrayTreePath, tree));
+                TypeMirror etType = trees.getTypeMirror(new TreePath(initArrayTreePath, tree));
 
+                //skipped fix for parameterized array member as parameterized array is not possible.
                 if (tree.getKind() == Tree.Kind.NEW_CLASS) {
                     NewClassTree nct = (NewClassTree) tree;
                     if (nct.getIdentifier().getKind() == Tree.Kind.PARAMETERIZED_TYPE) {
@@ -104,35 +94,18 @@ public class ConvertInvalidVarToExplicitArrayType implements ErrorRule<Void> {
                         return null;
                     }
                 }
-
-                if (arrayTypeMirror == null) {
-                    arrayTypeMirror = arrayElementTypeMirror;
-                }
-
-                isHomoGeneousArray = compilationInfo.getTypes().isSameType(arrayElementTypeMirror, arrayTypeMirror);
-                if (isHomoGeneousArray) {
-                    continue;
-                }
-
-                // Hint will be enabled only for primitive Numeric array initializers or for homogeneous array members.
-                if (!isHomoGeneousArray && !isPrimitiveNumeric(arrayElementTypeMirror.getKind())) {
-                    return null;
-                }
-
-                if (!isHomoGeneousArray) {
-                    Integer arrayElementPriority = NUMERIC_PRIMITIVE_TYPE_PRIORITY_MAP.get(arrayElementTypeMirror.getKind());
-                    if (maxTypePriority == -1) {
-                        maxTypePriority = NUMERIC_PRIMITIVE_TYPE_PRIORITY_MAP.get(arrayTypeMirror.getKind());
-                    }
-
-                    if (maxTypePriority < arrayElementPriority) {
-                        arrayTypeMirror = arrayElementTypeMirror;
-                        maxTypePriority = arrayElementPriority;
+                if (arrayType == null) {
+                    arrayType = etType;
+                } else if (!types.isAssignable(etType, arrayType)) {
+                    if (types.isAssignable(arrayType, etType)) {
+                        arrayType = etType;
+                    } else {
+                        return null; //the array is not sufficiently homogeneous.
                     }
                 }
             }
         }
-        return Collections.<Fix>singletonList(new FixImpl(compilationInfo, treePath, arrayTypeMirror).toEditorFix());
+        return Collections.<Fix>singletonList(new FixImpl(compilationInfo, treePath, arrayType).toEditorFix());
 
     }
 
@@ -187,11 +160,4 @@ public class ConvertInvalidVarToExplicitArrayType implements ErrorRule<Void> {
             }
         }
     }
-
-    private boolean isPrimitiveNumeric(TypeKind type) {
-        Set<TypeKind> primitiveNumbericTypes = NUMERIC_PRIMITIVE_TYPE_PRIORITY_MAP.keySet();
-
-        return primitiveNumbericTypes.contains(type);
-
-    }
 }
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 19b50ca..b357242 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
@@ -50,16 +50,18 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase
         super.tearDown();
     }
 
-    public void testArrayHetrogeneousElements() throws Exception {
-        performAnalysisTest("test/Test.java", "package test; public class Test {{var/*comment1*/ k = {1,'c'};}}", -1);
-    }
-
     public void testParameterizedElements() throws Exception {
         performAnalysisTest("test/Test.java",
                 "package test; public class Test {{final var j = {new java.util.ArrayList<String>(),new java.util.ArrayList<String>()};}}",
                 -1);
     }
 
+    public void testArrayHetrogeneousElements() throws Exception {
+        performAnalysisTest("test/Test.java",
+                "package test; public class Test {{final/*comment1*/ var/**comment2**/ j/*comment3*/ = /*comment4*/{new java.util.ArrayList(),new java.util.HashMap()};}}",
+                -1);
+    }
+
     public void testArrayObjectElementsFix() throws Exception {
         performFixTest("test/Test.java",
                 "package test; public class Test {{final/*comment1*/ var/**comment2**/ j/*comment3*/ = /*comment4*/{new java.util.ArrayList(),new java.util.ArrayList()};}}",
@@ -155,6 +157,13 @@ public class ConvertInvalidVarToExplicitArrayTypeTest extends ErrorHintsTestBase
                 "package test; public class Test {{@NotNull Object[] j = {new Object(),new Object()};}}");
     }
 
+    public void testArrayObject9ElementsFix() throws Exception {
+        performFixTest("test/Test.java",
+                "package test; public class Test {{var/*comment1*/ k = {1,'c'};}}",
+                -1, "Convert Var to Explicit Type",
+                "package test; public class Test {{int[]/*comment1*/ k = {1,'c'};}}");
+    }
+
     protected List<Fix> computeFixes(CompilationInfo info, int pos, TreePath path) {
         List<Fix> fixes = new ConvertInvalidVarToExplicitArrayType().run(info, null, pos, path, null);
         List<Fix> result = new LinkedList<Fix>();

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