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/03/29 05:17:51 UTC

[GitHub] jlahoda closed pull request #436: [NETBEANS-407] Using a special State to model instanceof instead of N���

jlahoda closed pull request #436: [NETBEANS-407] Using a special State to model instanceof instead of N…
URL: https://github.com/apache/incubator-netbeans/pull/436
 
 
   

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/bugs/NPECheck.java b/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java
index b7b5af852..993e3e351 100644
--- a/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java
+++ b/java.hints/src/org/netbeans/modules/java/hints/bugs/NPECheck.java
@@ -209,11 +209,13 @@ public static ErrorDescription unboxingConditional(HintContext ctx) {
                 break;
             case POSSIBLE_NULL_REPORT:
             case POSSIBLE_NULL:
+            case INSTANCE_OF_FALSE:
                 k = "ERR_UnboxingPotentialNullValue"; // NOI18N
                 break;
             case NOT_NULL_BE_NPE:
             case NOT_NULL:
             case NOT_NULL_HYPOTHETICAL:
+            case INSTANCE_OF_TRUE:
                 return null;
             default:
                 throw new AssertionError(s.name());
@@ -238,7 +240,7 @@ public static ErrorDescription switchExpression(HintContext ctx) {
             return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName);
         }
 
-        if (r == State.POSSIBLE_NULL_REPORT) {
+        if (r == State.POSSIBLE_NULL_REPORT || r == INSTANCE_OF_FALSE) {
             String displayName = NbBundle.getMessage(NPECheck.class, "ERR_PossiblyDereferencingNull");
             
             return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName);
@@ -259,7 +261,7 @@ public static ErrorDescription enhancedFor(HintContext ctx) {
             return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName);
         }
 
-        if (r == State.POSSIBLE_NULL_REPORT) {
+        if (r == State.POSSIBLE_NULL_REPORT || r == State.INSTANCE_OF_FALSE) {
             String displayName = NbBundle.getMessage(NPECheck.class, "ERR_PossiblyDereferencingNull");
             
             return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName);
@@ -278,7 +280,7 @@ public static ErrorDescription memberSelect(HintContext ctx) {
             return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName);
         }
 
-        if (r == State.POSSIBLE_NULL_REPORT) {
+        if (r == State.POSSIBLE_NULL_REPORT || r == State.INSTANCE_OF_FALSE) {
             String displayName = NbBundle.getMessage(NPECheck.class, "ERR_PossiblyDereferencingNull");
             
             return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), displayName);
@@ -327,6 +329,7 @@ public static boolean isSafeToDereference(CompilationInfo info, TreePath path) {
                         result.add(ErrorDescriptionFactory.forTree(ctx, mit.getArguments().get(index), NbBundle.getMessage(NPECheck.class, "ERR_NULL_TO_NON_NULL_ARG")));
                         break;
                     case POSSIBLE_NULL_REPORT:
+                    case INSTANCE_OF_FALSE:
                         result.add(ErrorDescriptionFactory.forTree(ctx, mit.getArguments().get(index), NbBundle.getMessage(NPECheck.class, "ERR_POSSIBLENULL_TO_NON_NULL_ARG")));
                         break;
                 }
@@ -455,6 +458,7 @@ public static ErrorDescription returnNull(HintContext ctx) {
                 if (expected.isNotNull()) key = "ERR_ReturningNullFromNonNull";
                 break;
             case POSSIBLE_NULL_REPORT:
+            case INSTANCE_OF_FALSE:
                 if (expected.isNotNull()) key = "ERR_ReturningPossibleNullFromNonNull";
                 break;
         }
@@ -717,7 +721,7 @@ public State visitMemberSelect(MemberSelectTree node, Void p) {
             State expr = scan(node.getExpression(), p);
             boolean wasNPE = false;
             
-            if (expr == State.NULL || expr == State.NULL_HYPOTHETICAL || expr == State.POSSIBLE_NULL || expr == State.POSSIBLE_NULL_REPORT) {
+            if (expr == State.NULL || expr == State.NULL_HYPOTHETICAL || expr == State.POSSIBLE_NULL || expr == State.POSSIBLE_NULL_REPORT || expr == State.INSTANCE_OF_FALSE) {
                 wasNPE = true;
             }
             
@@ -886,8 +890,17 @@ public State visitInstanceOf(InstanceOfTree node, Void p) {
             
             Element e = info.getTrees().getElement(new TreePath(getCurrentPath(), node.getExpression()));
 
-            if (isVariableElement(e) && (variable2State.get((VariableElement) e) == null || !variable2State.get((VariableElement) e).isNotNull()) && !not) {
-                variable2State.put((VariableElement) e, State.NOT_NULL_HYPOTHETICAL);
+            if (isVariableElement(e)) {
+                boolean setState = false;
+                State currentState = variable2State.get((VariableElement) e);
+                if (currentState == null) {
+                    setState = !getStateFromAnnotations(info, e).isNotNull();
+                } else {
+                    setState = !variable2State.get((VariableElement) e).isNotNull();
+                }
+                if (setState) {
+                    variable2State.put((VariableElement) e, not ? State.INSTANCE_OF_FALSE : State.INSTANCE_OF_TRUE);
+                }
             }
             
             return null;
@@ -1479,7 +1492,7 @@ private void mergeNonHypotheticalVariable2State(Map<VariableElement, State> orig
             for (Entry<VariableElement, State> e : backup.entrySet()) {
                 State t = e.getValue();
                 
-                if (t  != null && t != State.NOT_NULL_HYPOTHETICAL && t != NULL_HYPOTHETICAL) {
+                if (t  != null && t != State.NOT_NULL_HYPOTHETICAL && t != NULL_HYPOTHETICAL && t != INSTANCE_OF_TRUE && t != INSTANCE_OF_FALSE) {
                     variable2State.put(e.getKey(), t);
                 }
             }
@@ -1495,15 +1508,6 @@ private boolean isVariableElement(Element ve) {
             return NPECheck.isVariableElement(ctx, ve);
         }
         
-        private void clearHypothetical() {
-            for (Iterator<Entry<VariableElement, State>> it = variable2State.entrySet().iterator(); it.hasNext();) {
-                Entry<VariableElement, State> e = it.next();
-                
-                if (e.getValue() == State.NOT_NULL_HYPOTHETICAL || e.getValue() == State.NULL_HYPOTHETICAL) {
-                    it.remove();
-                }
-            }
-        }
     }
     
     static enum State {
@@ -1511,8 +1515,10 @@ private void clearHypothetical() {
         NULL_HYPOTHETICAL,
         POSSIBLE_NULL,
         POSSIBLE_NULL_REPORT,
+        INSTANCE_OF_FALSE,
         NOT_NULL,
         NOT_NULL_HYPOTHETICAL,
+        INSTANCE_OF_TRUE,
         NOT_NULL_BE_NPE;
         
         public @CheckForNull State reverse() {
@@ -1521,6 +1527,8 @@ private void clearHypothetical() {
                     return NOT_NULL;
                 case NULL_HYPOTHETICAL:
                     return NOT_NULL_HYPOTHETICAL;
+                case INSTANCE_OF_FALSE:
+                    return INSTANCE_OF_TRUE;
                 case POSSIBLE_NULL:
                 case POSSIBLE_NULL_REPORT:
                     return this;
@@ -1529,18 +1537,20 @@ private void clearHypothetical() {
                     return NULL;
                 case NOT_NULL_HYPOTHETICAL:
                     return NULL_HYPOTHETICAL;
+                case INSTANCE_OF_TRUE:
+                    return INSTANCE_OF_FALSE;
                 default: throw new IllegalStateException();
             }
         }
         
         public boolean isNotNull() {
-            return this == NOT_NULL || this == NOT_NULL_BE_NPE || this == NOT_NULL_HYPOTHETICAL;
+            return this == NOT_NULL || this == NOT_NULL_BE_NPE || this == NOT_NULL_HYPOTHETICAL || this == INSTANCE_OF_TRUE;
         }
         
         public static State collect(State s1, State s2) {
             if (s1 == s2) return s1;
             if (s1 == NULL || s2 == NULL || s1 == NULL_HYPOTHETICAL || s2 == NULL_HYPOTHETICAL) return POSSIBLE_NULL_REPORT;
-            if (s1 == POSSIBLE_NULL_REPORT || s2 == POSSIBLE_NULL_REPORT) return POSSIBLE_NULL_REPORT;
+            if (s1 == POSSIBLE_NULL_REPORT || s2 == POSSIBLE_NULL_REPORT || s2 == INSTANCE_OF_FALSE) return POSSIBLE_NULL_REPORT;
             if (s1 != null && s2 != null && s1.isNotNull() && s2.isNotNull()) return NOT_NULL;
             
             return POSSIBLE_NULL;
diff --git a/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java b/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java
index 185f9e3ae..ae143455f 100644
--- a/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java
+++ b/java.hints/test/unit/src/org/netbeans/modules/java/hints/bugs/NPECheckTest.java
@@ -1645,6 +1645,57 @@ public void testLambdaExpressionShouldntReturnNull() throws Exception {
                 .assertWarnings("17:19-17:23:verifier:ERR_ReturningNullFromNonNull");
     }
 
+    public void testNETBEANS407a() throws Exception {
+        HintTest.create()
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "  public void test(Object o) {\n" +
+                       "    boolean b1 = o instanceof Integer;\n" +
+                       "    boolean b2 = o instanceof Integer && o != null;\n" +
+                       "    System.out.println(o.toString());\n" +
+                       "  }\n" +
+                       "}")
+                .run(NPECheck.class)
+                .assertWarnings("4:41-4:50:verifier:ERR_NotNull");
+    }
+    
+    public void testNETBEANS407b() throws Exception {
+        HintTest.create()
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "  public void test(Object o) {\n" +
+                       "    boolean b = !(o instanceof Integer) && o.toString() != null;\n" +
+                       "  }\n" +
+                       "}")
+                .run(NPECheck.class)
+                .assertWarnings("3:45-3:53:verifier:Possibly Dereferencing null");
+    }
+    
+    public void testNETBEANS407c() throws Exception {
+        HintTest.create()
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "  public void test(Object o) {\n" +
+                       "    boolean b = o != null;\n" +
+                       "    System.out.println(o.toString());\n" +
+                       "  }\n" +
+                       "}")
+                .run(NPECheck.class)
+                .assertWarnings("4:25-4:33:verifier:Possibly Dereferencing null");
+    }
+    
+    public void testNETBEANS407d() throws Exception {
+        HintTest.create()
+                .input("package test;\n" +
+                       "public class Test {\n" +
+                       "  public void test(Object o) {\n" +
+                       "    boolean b = (o == null || o == \"\") && o.toString() != null;\n" +
+                       "  }\n" +
+                       "}")
+                .run(NPECheck.class)
+                .assertWarnings("3:44-3:52:verifier:Possibly Dereferencing null");
+    }
+    
     private void performAnalysisTest(String fileName, String code, String... golden) throws Exception {
         HintTest.create()
                 .input(fileName, code)


 

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