Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Herrmann2013-02-18 13:38:05 +0000
committerJayaprakash Arthanareeswaran2013-04-03 09:02:09 +0000
commit8cb7d70abc5895601c4d99f3457d23376eb93fb5 (patch)
tree64571df09ef6b9e34f71b1ef92269349d82a12a1
parent986a6d11b1b7e4db82dd5d830210e01550f74aa3 (diff)
downloadeclipse.jdt.core-8cb7d70abc5895601c4d99f3457d23376eb93fb5.tar.gz
eclipse.jdt.core-8cb7d70abc5895601c4d99f3457d23376eb93fb5.tar.xz
eclipse.jdt.core-8cb7d70abc5895601c4d99f3457d23376eb93fb5.zip
Bug 401017: [compiler][null] casted reference to @Nullable field
lacks a warning
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java38
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java5
2 files changed, 43 insertions, 0 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java
index 1c1f521feb..e045881fe8 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java
@@ -4973,6 +4973,44 @@ public void test_nullable_field_14a() {
"----------\n");
}
+// https://bugs.eclipse.org/401017: [compiler][null] casted reference to @Nullable field lacks a warning
+public void test_nullable_field_15() {
+ runNegativeTestWithLibs(
+ new String[] {
+ "X.java",
+ "import org.eclipse.jdt.annotation.*;\n" +
+ "public class X {\n" +
+ " @Nullable\n" +
+ " private Object nullable;\n" +
+ "\n" +
+ " public void test() {\n" +
+ " if (nullable instanceof Number) {\n" +
+ " ((Number)nullable).intValue(); // A\n" +
+ " }\n" +
+ " if (nullable != null) {\n" +
+ " nullable.toString(); // B\n" +
+ " }\n" +
+ " nullable.toString(); // C\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in X.java (at line 8)\n" +
+ " ((Number)nullable).intValue(); // A\n" +
+ " ^^^^^^^^\n" +
+ "Potential null pointer access: The field nullable is declared as @Nullable\n" +
+ "----------\n" +
+ "2. ERROR in X.java (at line 11)\n" +
+ " nullable.toString(); // B\n" +
+ " ^^^^^^^^\n" +
+ "Potential null pointer access: The field nullable is declared as @Nullable\n" +
+ "----------\n" +
+ "3. ERROR in X.java (at line 13)\n" +
+ " nullable.toString(); // C\n" +
+ " ^^^^^^^^\n" +
+ "Potential null pointer access: The field nullable is declared as @Nullable\n" +
+ "----------\n");
+}
// an enum is declared within the scope of a null-default
// https://bugs.eclipse.org/331649#c61
public void test_enum_field_01() {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java
index e57addb2c4..8f9b0e018c 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java
@@ -17,6 +17,7 @@
* bug 345305 - [compiler][null] Compiler misidentifies a case of "variable can only be null"
* bug 395002 - Self bound generic class doesn't resolve bounds properly for wildcards for certain parametrisation.
* bug 383368 - [compiler][null] syntactic null analysis for field references
+ * bug 401017 - [compiler][null] casted reference to @Nullable field lacks a warning
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.ast;
@@ -251,6 +252,10 @@ public static void checkNeedForArgumentCasts(BlockScope scope, int operator, int
}
}
+public boolean checkNPE(BlockScope scope, FlowContext flowContext, FlowInfo flowInfo) {
+ return this.expression.checkNPE(scope, flowContext, flowInfo);
+}
+
private static void checkAlternateBinding(BlockScope scope, Expression receiver, TypeBinding receiverType, MethodBinding binding, Expression[] arguments, TypeBinding[] originalArgumentTypes, TypeBinding[] alternateArgumentTypes, final InvocationSite invocationSite) {
InvocationSite fakeInvocationSite = new InvocationSite(){
public TypeBinding[] genericTypeArguments() { return null; }

Back to the top