Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'org.eclipse.jdt.core.tests.compiler')
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionTest.java64
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java44
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java18
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java117
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/InitializationTests.java40
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullAnnotationTest.java146
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceImplTests.java18
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java2784
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java547
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/TestVerifier.java485
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/VerifyTests.java18
11 files changed, 1036 insertions, 3245 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionTest.java
index 816aac968..a11f6ac20 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/parser/SelectionTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2009 IBM Corporation and others.
+ * Copyright (c) 2000, 2012 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -2448,4 +2448,66 @@ public void test55() {
expectedReplacedSource,
testName);
}
+// https://bugs.eclipse.org/bugs/show_bug.cgi?id=291040
+public void test56() {
+
+ String str =
+ "class X {\n" +
+ " void foo() {\n" +
+ " new X(null) {\n" +
+ " void goo() {\n" +
+ " new X(zoo()) {\n" +
+ " void voo() {\n" +
+ " }\n" +
+ " };\n" +
+ " }\n" +
+ "\n" +
+ " Object zoo() {\n" +
+ " return null;\n" +
+ " }\n" +
+ " };\n" +
+ " }\n" +
+ "\n" +
+ " X(Object k) {\n" +
+ " }\n" +
+ "}\n";
+
+ String selection = "zoo";
+
+ String expectedCompletionNodeToString = "<SelectOnMessageSend:zoo()>";
+
+ String completionIdentifier = "zoo";
+ String expectedUnitDisplayString =
+ "class X {\n" +
+ " void foo() {\n" +
+ " new X(null) {\n" +
+ " void goo() {\n" +
+ " new X(<SelectOnMessageSend:zoo()>) {\n" +
+ " void voo() {\n" +
+ " }\n" +
+ " };\n" +
+ " }\n" +
+ " Object zoo() {\n" +
+ " }\n" +
+ " };\n" +
+ " }\n" +
+ " X(Object k) {\n" +
+ " }\n" +
+ "}\n";
+ String expectedReplacedSource = "zoo()";
+ String testName = "<select>";
+
+ int selectionStart = str.indexOf(selection);
+ int selectionEnd = str.indexOf(selection) + selection.length() - 1;
+
+ this.checkMethodParse(
+ str.toCharArray(),
+ selectionStart,
+ selectionEnd,
+ expectedCompletionNodeToString,
+ expectedUnitDisplayString,
+ completionIdentifier,
+ expectedReplacedSource,
+ testName);
+}
}
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java
index d63b9d0f2..925533c13 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java
@@ -17,6 +17,7 @@
* bug 359721 - [options] add command line option for new warning token "resource"
* bug 186342 - [compiler][null] Using annotations for null checking
* bug 365208 - [compiler][batch] command line options for annotation based null analysis
+ * bug 370639 - [compiler][resource] restore the default for resource leak warnings
*******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;
@@ -1772,10 +1773,9 @@ public void test012b(){
" nullable|nonnull|nonnullbydefault annotation types\n" +
" optionally specified using fully qualified names\n" +
" nullDereference + missing null check\n" +
- " nullFields + null analysis for fields\n" +
- " over-ann missing @Override annotation (superclass)\n" +
- " paramAssign assignment to a parameter\n" +
- " pkgDefaultMethod + attempt to override package-default method\n" +
+ " over-ann missing @Override annotation (superclass)\n" +
+ " paramAssign assignment to a parameter\n" +
+ " pkgDefaultMethod + attempt to override package-default method\n" +
" raw + usage of raw type\n" +
" resource + (pot.) unsafe usage of resource of type Closeable\n" +
//OT:
@@ -1912,7 +1912,6 @@ public void test012b(){
" <option key=\"org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally\" value=\"warning\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.forbiddenReference\" value=\"warning\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock\" value=\"warning\"/>\n" +
- " <option key=\"org.eclipse.jdt.core.compiler.problem.includeFieldsInNullAnalysis\" value=\"disabled\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts\" value=\"disabled\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod\" value=\"warning\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch\" value=\"ignore\"/>\n" +
@@ -1967,7 +1966,7 @@ public void test012b(){
" <option key=\"org.eclipse.jdt.core.compiler.problem.typeParameterHiding\" value=\"warning\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems\" value=\"enabled\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation\" value=\"warning\"/>\n" +
- " <option key=\"org.eclipse.jdt.core.compiler.problem.unclosedCloseable\" value=\"ignore\"/>\n" +
+ " <option key=\"org.eclipse.jdt.core.compiler.problem.unclosedCloseable\" value=\"warning\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock\" value=\"ignore\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.unhandledWarningToken\" value=\"warning\"/>\n" +
" <option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryElse\" value=\"ignore\"/>\n" +
@@ -12457,36 +12456,6 @@ public void test310b_warn_options() {
}
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=325342
-// -warn option - regression tests to check option nulLFields
-// Null warnings should be flagged on fields
-public void test311_warn_options() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object o;\n" +
- " void foo() {\n" +
- " if (o == null && o.toString() == \"\"){}\n" +
- " else {}\n" +
- " o.toString();\n" + // toString() call above defuses null info, so no warning here
- " }\n" +
- "}\n",
- },
- "\"" + OUTPUT_DIR + File.separator + "X.java\""
- + " -sourcepath \"" + OUTPUT_DIR + "\""
- + " -warn:null,nullFields -proc:none -d \"" + OUTPUT_DIR + "\"",
- "",
- "----------\n" +
- "1. WARNING in ---OUTPUT_DIR_PLACEHOLDER---/X.java (at line 4)\n" +
- " if (o == null && o.toString() == \"\"){}\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n" +
- "1 problem (1 warning)",
- true);
-}
-
-// https://bugs.eclipse.org/bugs/show_bug.cgi?id=325342
// -warn option - regression tests to check option nullAnnot (with args)
// Null warnings because of annotations - custom annotation types used - challenging various kinds of diagnostics
public void test312_warn_options() {
@@ -12690,7 +12659,8 @@ public void test314_warn_options() {
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=325342
// -warn option - regression tests to check option nullAnnot
// Null warnings because of annotations, global nonNullByDefault
-public void test315_warn_options() {
+// DISABLED due to dysfunctional global default after Bug 366063 - Compiler should not add synthetic @NonNull annotations
+public void _test315_warn_options() {
this.runConformTest(
new String[] {
"p/X.java",
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java
index 3d036f95d..6a306e4b6 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java
@@ -702,7 +702,6 @@ public void _test011_problem_categories() {
expectedProblemAttributes.put("MissingEnclosingInstanceForConstructorCall", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
expectedProblemAttributes.put("MissingEnumConstantCase", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("MissingOverrideAnnotation", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
- expectedProblemAttributes.put("MissingNullAnnotationType", new ProblemAttributes(CategorizedProblem.CAT_BUILDPATH));
expectedProblemAttributes.put("MissingOverrideAnnotationForInterfaceMethodImplementation", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
expectedProblemAttributes.put("MissingReturnType", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
expectedProblemAttributes.put("MissingSemiColon", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX));
@@ -730,7 +729,6 @@ public void _test011_problem_categories() {
expectedProblemAttributes.put("NonGenericConstructor", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
expectedProblemAttributes.put("NonGenericMethod", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
expectedProblemAttributes.put("NonGenericType", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
- expectedProblemAttributes.put("NonNullFieldComparisonYieldsFalse", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("NonNullLocalVariableComparisonYieldsFalse", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("NonStaticAccessToStaticField", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
expectedProblemAttributes.put("NonStaticAccessToStaticMethod", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
@@ -743,9 +741,6 @@ public void _test011_problem_categories() {
expectedProblemAttributes.put("NotVisibleField", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
expectedProblemAttributes.put("NotVisibleMethod", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
expectedProblemAttributes.put("NotVisibleType", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
- expectedProblemAttributes.put("NullFieldComparisonYieldsFalse", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
- expectedProblemAttributes.put("NullFieldInstanceofYieldsFalse", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
- expectedProblemAttributes.put("NullFieldReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("NullLocalVariableComparisonYieldsFalse", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("NullLocalVariableInstanceofYieldsFalse", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("NullLocalVariableReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
@@ -789,7 +784,6 @@ public void _test011_problem_categories() {
expectedProblemAttributes.put("PotentialHeapPollutionFromVararg", new ProblemAttributes(CategorizedProblem.CAT_UNCHECKED_RAW));
expectedProblemAttributes.put("PotentiallyUnclosedCloseable", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("PotentiallyUnclosedCloseableAtExit", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
- expectedProblemAttributes.put("PotentialNullFieldReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("PotentialNullMessageSendReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("PublicClassMustMatchFileName", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
@@ -799,13 +793,10 @@ public void _test011_problem_categories() {
expectedProblemAttributes.put("RedefinedArgument", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
expectedProblemAttributes.put("RedefinedLocal", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
expectedProblemAttributes.put("RedundantSpecificationOfTypeArguments", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
- expectedProblemAttributes.put("RedundantFieldNullAssignment", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("RedundantLocalVariableNullAssignment", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("RedundantNullAnnotation", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
- expectedProblemAttributes.put("RedundantNullCheckOnNonNullField", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("RedundantNullCheckOnNonNullLocalVariable", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("RedundantNullCheckOnNonNullMessageSend", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
- expectedProblemAttributes.put("RedundantNullCheckOnNullField", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("RedundantNullCheckOnNullLocalVariable", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
expectedProblemAttributes.put("RedundantNullDefaultAnnotation", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
expectedProblemAttributes.put("RedundantNullDefaultAnnotationPackage", new ProblemAttributes(CategorizedProblem.CAT_UNNECESSARY_CODE));
@@ -1486,7 +1477,6 @@ public void test012_compiler_problems_tuning() {
expectedProblemAttributes.put("MissingEnclosingInstance", SKIP);
expectedProblemAttributes.put("MissingEnclosingInstanceForConstructorCall", SKIP);
expectedProblemAttributes.put("MissingEnumConstantCase", new ProblemAttributes(JavaCore.COMPILER_PB_INCOMPLETE_ENUM_SWITCH));
- expectedProblemAttributes.put("MissingNullAnnotationType", SKIP);
expectedProblemAttributes.put("MissingOverrideAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION));
expectedProblemAttributes.put("MissingOverrideAnnotationForInterfaceMethodImplementation", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_OVERRIDE_ANNOTATION));
expectedProblemAttributes.put("MissingReturnType", SKIP);
@@ -1515,7 +1505,6 @@ public void test012_compiler_problems_tuning() {
expectedProblemAttributes.put("NonGenericConstructor", SKIP);
expectedProblemAttributes.put("NonGenericMethod", SKIP);
expectedProblemAttributes.put("NonGenericType", SKIP);
- expectedProblemAttributes.put("NonNullFieldComparisonYieldsFalse", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("NonNullLocalVariableComparisonYieldsFalse", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("NonStaticAccessToStaticField", new ProblemAttributes(JavaCore.COMPILER_PB_STATIC_ACCESS_RECEIVER));
expectedProblemAttributes.put("NonStaticAccessToStaticMethod", new ProblemAttributes(JavaCore.COMPILER_PB_STATIC_ACCESS_RECEIVER));
@@ -1528,9 +1517,6 @@ public void test012_compiler_problems_tuning() {
expectedProblemAttributes.put("NotVisibleField", SKIP);
expectedProblemAttributes.put("NotVisibleMethod", SKIP);
expectedProblemAttributes.put("NotVisibleType", SKIP);
- expectedProblemAttributes.put("NullFieldComparisonYieldsFalse", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
- expectedProblemAttributes.put("NullFieldInstanceofYieldsFalse", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
- expectedProblemAttributes.put("NullFieldReference", new ProblemAttributes(JavaCore.COMPILER_PB_NULL_REFERENCE));
expectedProblemAttributes.put("NullLocalVariableComparisonYieldsFalse", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("NullLocalVariableInstanceofYieldsFalse", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("NullLocalVariableReference", new ProblemAttributes(JavaCore.COMPILER_PB_NULL_REFERENCE));
@@ -1574,7 +1560,6 @@ public void test012_compiler_problems_tuning() {
expectedProblemAttributes.put("PotentialHeapPollutionFromVararg", new ProblemAttributes(JavaCore.COMPILER_PB_UNCHECKED_TYPE_OPERATION));
expectedProblemAttributes.put("PotentiallyUnclosedCloseable", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE));
expectedProblemAttributes.put("PotentiallyUnclosedCloseableAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE));
- expectedProblemAttributes.put("PotentialNullFieldReference", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE));
expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE));
expectedProblemAttributes.put("PotentialNullMessageSendReference", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE));
expectedProblemAttributes.put("PublicClassMustMatchFileName", SKIP);
@@ -1584,13 +1569,10 @@ public void test012_compiler_problems_tuning() {
expectedProblemAttributes.put("RedefinedArgument", SKIP);
expectedProblemAttributes.put("RedefinedLocal", SKIP);
expectedProblemAttributes.put("RedundantSpecificationOfTypeArguments", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_TYPE_ARGUMENTS));
- expectedProblemAttributes.put("RedundantFieldNullAssignment", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("RedundantLocalVariableNullAssignment", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("RedundantNullAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION));
- expectedProblemAttributes.put("RedundantNullCheckOnNonNullField", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("RedundantNullCheckOnNonNullLocalVariable", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("RedundantNullCheckOnNonNullMessageSend", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
- expectedProblemAttributes.put("RedundantNullCheckOnNullField", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("RedundantNullCheckOnNullLocalVariable", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_CHECK));
expectedProblemAttributes.put("RedundantNullDefaultAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION));
expectedProblemAttributes.put("RedundantNullDefaultAnnotationPackage", new ProblemAttributes(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION));
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java
index 669345c58..4e758287b 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/FlowAnalysisTest.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2012 IBM Corporation and others.
+ * Copyright (c) 2005, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -31,7 +31,7 @@ import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
public class FlowAnalysisTest extends AbstractRegressionTest {
static {
-// TESTS_NAMES = new String[] { "testInnerClassesWithFields1" };
+// TESTS_NAMES = new String[] { "testLocalClassInInitializer1" };
// TESTS_NUMBERS = new int[] { 69 };
}
public FlowAnalysisTest(String name) {
@@ -2496,119 +2496,6 @@ public void testLocalClassInInitializer2() {
"continue cannot be used outside of a loop\n" +
"----------\n");
}
-// final field in anonymous nested class
-// witness a regression during working on Bug 247564 - [compiler][null] Detecting null field reference
-public void testFinalFieldInNested1() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " void print4() {\n" +
- " for (int i=0; i<4; i++)\n" +
- " new Runnable() {\n" +
- " final String s1local;\n" +
- " public void run() {\n" +
- " s1local.toString();\n" +
- " }\n" +
- " }.run();\n" +
- " }\n" +
- "}\n"
- },
- "----------\n" +
- "1. ERROR in X.java (at line 4)\n" +
- " new Runnable() {\n" +
- " ^^^^^^^^^^\n" +
- "The blank final field s1local may not have been initialized\n" +
- "----------\n");
-}
-// witness a regression during working on Bug 247564 - [compiler][null] Detecting null field reference
-// local variable in inner class triggered IAE in StackMapFrame.addStackItem
-public void testInnerClassesWithFields1() {
- if (this.complianceLevel >= ClassFileConstants.JDK1_6) { // we're specifically interested in StackMap generation
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " int f1, f2, f3;\n" +
- " class I1 {\n" +
- " int f4;\n" +
- " String m(boolean b) {\n" +
- " String l1 = \"Hello\";\n" +
- " if (b) {\n" +
- " l1 += \" world!\";\n" +
- " } else {\n" +
- " l1 += \" test.\";\n" +
- " }\n" +
- " return l1;\n" +
- " }\n" +
- " }\n" +
- " class I2 {\n" +
- " int f5, f6, f7;\n" +
- " }\n" +
- "}\n"
- },
- "");
- }
-}
-// witness a regression during working on Bug 247564 - [compiler][null] Detecting null field reference
-// local variable in local class triggered IAE in StackMapFrame.addStackItem
-public void testInnerClassesWithFields1a() {
- if (this.complianceLevel >= ClassFileConstants.JDK1_6) { // we're specifically interested in StackMap generation
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " int f1, f2, f3;\n" +
- " void foo() {\n" +
- " class I1 {\n" +
- " int f4;\n" +
- " String m(boolean b) {\n" +
- " String l1 = \"Hello\";\n" +
- " if (b) {\n" +
- " l1 += \" world!\";\n" +
- " } else {\n" +
- " l1 += \" test.\";\n" +
- " }\n" +
- " return l1;\n" +
- " }\n" +
- " }\n" +
- " }\n" +
- " class I2 {\n" +
- " int f5, f6, f7;\n" +
- " }\n" +
- "}\n"
- },
- "");
- }
-}
-// witness a regression during working on Bug 247564 - [compiler][null] Detecting null field reference
-// final fields in parameterized nested were reported as uninitialized
-public void testInnerClassesWithFields2() {
- if (this.complianceLevel >= ClassFileConstants.JDK1_5) {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final String f1 = \"f1\";\n" +
- " static final String f2 = \"f2\";\n" +
- " String f3;\n" +
- " X() {\n" +
- " f3 = \"f3\";\n" +
- " }\n" +
- " protected abstract class I<T> {\n" +
- " final String f5, f6;\n" +
- " T f7;\n" +
- " I(T a) {\n" +
- " f5 = \"f5\";\n" +
- " f6 = \"f6\";\n" +
- " f7 = a;\n" +
- " }\n" +
- " }\n" +
- "}\n"
- },
- "");
- }
-}
public static Class testClass() {
return FlowAnalysisTest.class;
}
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/InitializationTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/InitializationTests.java
index 0309b5f19..23d941007 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/InitializationTests.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/InitializationTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2010, 2012 IBM Corporation and others.
+ * Copyright (c) 2010, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -464,44 +464,6 @@ public void testBug324178d() {
"The local variable b2 may not have been initialized\n" +
"----------\n");
}
-// https://bugs.eclipse.org/bugs/show_bug.cgi?id=247564
-// Change in the way field id's are calculated should not affect initializations
-public void testBug247564j() {
- this.runNegativeTest(
- new String[] {
- "Z.java",
- "public class Z {\n" +
- " final int field1 = 0;\n" +
- " {\n" +
- " class ZInner {\n" +
- " final int fieldz1;\n" +
- " final int fieldz2 = 0;\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. WARNING in Z.java (at line 4)\n" +
- " class ZInner {\n" +
- " ^^^^^^\n" +
- "The type ZInner is never used locally\n" +
- "----------\n" +
- "2. ERROR in Z.java (at line 4)\n" +
- " class ZInner {\n" +
- " ^^^^^^\n" +
- "The blank final field fieldz1 may not have been initialized\n" +
- "----------\n" +
- "3. WARNING in Z.java (at line 5)\n" +
- " final int fieldz1;\n" +
- " ^^^^^^^\n" +
- "The value of the field ZInner.fieldz1 is not used\n" +
- "----------\n" +
- "4. WARNING in Z.java (at line 6)\n" +
- " final int fieldz2 = 0;\n" +
- " ^^^^^^^\n" +
- "The value of the field ZInner.fieldz2 is not used\n" +
- "----------\n"
- );
-}
public static Class testClass() {
return InitializationTests.class;
}
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 1b1d69ca0..ebd5df08b 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
@@ -53,7 +53,7 @@ public NullAnnotationTest(String name) {
// Static initializer to specify tests subset using TESTS_* static variables
// All specified tests which do not belong to the class are skipped...
static {
-// TESTS_NAMES = new String[] { "test_redundant_annotation_" };
+// TESTS_NAMES = new String[] { "test_message_send_in_control_structure_02" };
// TESTS_NUMBERS = new int[] { 561 };
// TESTS_RANGE = new int[] { 1, 2049 };
}
@@ -762,6 +762,43 @@ public void test_nonnull_local_001() {
true /* shouldFlush*/);
}
+// assigning potential null to a nonnull local variable - separate decl and assignment
+public void test_nonnull_local_002() {
+ runNegativeTest(
+ new String[] {
+ "X.java",
+ "import org.eclipse.jdt.annotation.*;\n" +
+ "public class X {\n" +
+ " void foo(boolean b, Object p) {\n" +
+ " @NonNull Object o1;\n" +
+ " o1 = b ? null : new Object();\n" +
+ " @NonNull String o2;\n" +
+ " o2 = \"\";\n" +
+ " o2 = null;\n" +
+ " @NonNull Object o3;\n" +
+ " o3 = p;\n" +
+ " }\n" +
+ "}\n"},
+ "----------\n" +
+ "1. ERROR in X.java (at line 5)\n" +
+ " o1 = b ? null : new Object();\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" +
+ "----------\n" +
+ "2. ERROR in X.java (at line 8)\n" +
+ " o2 = null;\n" +
+ " ^^^^\n" +
+ "Type mismatch: required \'@NonNull String\' but the provided value is null\n" +
+ "----------\n" +
+ "3. WARNING in X.java (at line 10)\n" +
+ " o3 = p;\n" +
+ " ^\n" +
+ "Potential type mismatch: required \'@NonNull Object\' but nullness of the provided value is unknown\n" +
+ "----------\n",
+ this.LIBS,
+ true /* shouldFlush*/);
+}
+
// a method tries to tighten the type specification, super declares parameter o as @Nullable
// other parameters: s is redefined from not constrained to @Nullable which is OK
// third is redefined from not constrained to @NonNull which is bad, too
@@ -1877,15 +1914,14 @@ public void test_annotation_import_006() {
"----------\n",
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
}
-// using nullness defaulting to nonnull, missing annotation types
+// using nullness defaulting to nonnull, missing annotation types, no longer a problem
public void test_annotation_import_007() {
Map customOptions = getCompilerOptions();
customOptions.put(JavaCore.COMPILER_PB_NULL_SPECIFICATION_INSUFFICIENT_INFO, JavaCore.ERROR);
customOptions.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "org.foo.MayBeNull");
customOptions.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "org.foo.MustNotBeNull");
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
- runNegativeTest(
- true/*shouldFlushOutputDirectory*/,
+ runConformTestWithLibs(
new String[] {
"Lib.java",
"public class Lib {\n" +
@@ -1898,15 +1934,8 @@ public void test_annotation_import_007() {
" }\n" +
"}\n"
},
- this.LIBS,
customOptions,
- "----------\n" +
- "1. ERROR in Lib.java (at line 1)\n" +
- " public class Lib {\n" +
- " ^\n" +
- "Buildpath problem: the type org.foo.MustNotBeNull, which is configured as a null annotation type, cannot be resolved\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
+ "");
}
// a null annotation is illegally used on a class:
@@ -2085,11 +2114,13 @@ public void test_illegal_annotation_007() {
public void test_default_nullness_002() {
Map customOptions = getCompilerOptions();
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR);
- customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
+// This option currently does nothing:
+// customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
runNegativeTestWithLibs(
new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
+ "@NonNullByDefault\n" +
"public class X {\n" +
" Object getObject(@Nullable Object o) {\n" +
" return new Object();\n" +
@@ -2097,6 +2128,7 @@ public void test_default_nullness_002() {
"}\n",
"Y.java",
"import org.eclipse.jdt.annotation.*;\n" +
+ "@NonNullByDefault\n" +
"public class Y extends X {\n" +
" @Override\n" +
" @Nullable Object getObject(Object o) {\n" + // complain illegal return redef and inherited annot is not repeated
@@ -2107,13 +2139,13 @@ public void test_default_nullness_002() {
customOptions,
// main error:
"----------\n" +
- "1. ERROR in Y.java (at line 4)\n" +
+ "1. ERROR in Y.java (at line 5)\n" +
" @Nullable Object getObject(Object o) {\n" +
" ^^^^^^^^^^^^^^^^\n" +
"The return type is incompatible with the @NonNull return from X.getObject(Object)\n" +
"----------\n" +
// additional error:
- "2. ERROR in Y.java (at line 4)\n" +
+ "2. ERROR in Y.java (at line 5)\n" +
" @Nullable Object getObject(Object o) {\n" +
" ^^^^^^\n" +
"Illegal redefinition of parameter o, inherited method from X declares this parameter as @Nullable\n" +
@@ -2162,7 +2194,7 @@ public void test_default_nullness_003() {
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" +
"----------\n");
}
-// package level default is consumed from package-info.class
+// package level default is consumed from package-info.class, similarly for type level default
public void test_default_nullness_003a() {
Map customOptions = getCompilerOptions();
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR);
@@ -2219,7 +2251,7 @@ public void test_default_nullness_003a() {
"Type mismatch: required \'@NonNull Object\' but the provided value can be null\n" +
"----------\n");
}
-//same as test_default_nullness_003b, but default-induced annotations are combined with explicit ones (not null related)
+// same as test_default_nullness_003a, but default-induced annotations are combined with explicit ones (not null related)
public void test_default_nullness_003b() {
Map customOptions = getCompilerOptions();
runConformTestWithLibs(
@@ -2373,7 +2405,8 @@ public void test_default_nullness_006() {
"----------\n");
}
// global default nonnull, but return may be null
-public void test_default_nullness_007() {
+// DISABLED due to dysfunctional global default after Bug 366063 - Compiler should not add synthetic @NonNull annotations
+public void _test_default_nullness_007() {
Map customOptions = getCompilerOptions();
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR);
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
@@ -2828,7 +2861,8 @@ public void test_redundant_annotation_02g() {
}
// redundant default annotations - package / class / method vs global default
-public void test_redundant_annotation_03() {
+// DISABLED due to dysfunctional global default after Bug 366063 - Compiler should not add synthetic @NonNull annotations
+public void _test_redundant_annotation_03() {
Map customOptions = getCompilerOptions();
customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
runConformTestWithLibs(
@@ -2956,11 +2990,13 @@ public void test_contradictory_annotations_01() {
public void test_nonnull_var_in_constrol_structure_1() {
Map customOptions = getCompilerOptions();
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR);
- customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
+// This option currently does nothing:
+// customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
runNegativeTestWithLibs(
new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
+ "@NonNullByDefault\n" +
"public class X {\n" +
" void print4(@NonNull String s) {\n" +
" for (int i=0; i<4; i++)\n" +
@@ -2983,22 +3019,22 @@ public void test_nonnull_var_in_constrol_structure_1() {
},
customOptions,
"----------\n" +
- "1. WARNING in X.java (at line 3)\n" +
+ "1. WARNING in X.java (at line 4)\n" +
" void print4(@NonNull String s) {\n" +
" ^^^^^^^^^^^^^^^^^\n" +
"The nullness annotation is redundant with a default that applies to this location\n" +
"----------\n" +
- "2. ERROR in X.java (at line 9)\n" +
+ "2. ERROR in X.java (at line 10)\n" +
" print(s);\n" +
" ^\n" +
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" +
"----------\n" +
- "3. ERROR in X.java (at line 14)\n" +
+ "3. ERROR in X.java (at line 15)\n" +
" print(s);\n" +
" ^\n" +
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" +
"----------\n" +
- "4. WARNING in X.java (at line 16)\n" +
+ "4. WARNING in X.java (at line 17)\n" +
" void print(@NonNull String s) {\n" +
" ^^^^^^^^^^^^^^^^^\n" +
"The nullness annotation is redundant with a default that applies to this location\n" +
@@ -3008,11 +3044,13 @@ public void test_nonnull_var_in_constrol_structure_1() {
public void test_nonnull_var_in_constrol_structure_2() {
Map customOptions = getCompilerOptions();
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR);
- customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
+// This option currently does nothing:
+// customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
runNegativeTestWithLibs(
new String[] {
"X.java",
- "import org.eclipse.jdt.annotation.*;\n" +
+ "import org.eclipse.jdt.annotation.*;" +
+ "@NonNullByDefault\n" +
"public class X {\n" +
" void print4(String s) {\n" +
" try { /*empty*/ } finally {\n" +
@@ -3183,6 +3221,54 @@ public void test_message_send_in_control_structure_01() {
"Null comparison always yields false: The variable enclosingSourceType cannot be null at this location\n" +
"----------\n");
}
+// Bug 370930 - NonNull annotation not considered for enhanced for loops
+public void test_message_send_in_control_structure_02() {
+ runNegativeTestWithLibs(
+ new String[] {
+ "Bug370930.java",
+ "import org.eclipse.jdt.annotation.*;\n" +
+ "import java.util.*;\n" +
+ "public class Bug370930 {\n" +
+ " void loop(Collection<String> list) {\n" +
+ " for(@NonNull String s: list) { // warning here: insufficient info on elements\n" +
+ " expectNonNull(s); // no warning here\n" +
+ " }\n" +
+ " }\n" +
+ " \n" +
+ " void expectNonNull(@NonNull String s) {}\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. WARNING in Bug370930.java (at line 5)\n" +
+ " for(@NonNull String s: list) { // warning here: insufficient info on elements\n" +
+ " ^^^^\n" +
+ "Potential type mismatch: required \'@NonNull String\' but nullness of the provided value is unknown\n" +
+ "----------\n");
+}
+//Bug 370930 - NonNull annotation not considered for enhanced for loops
+public void test_message_send_in_control_structure_03() {
+ runNegativeTestWithLibs(
+ new String[] {
+ "Bug370930.java",
+ "import org.eclipse.jdt.annotation.*;\n" +
+ "import java.util.*;\n" +
+ "public class Bug370930 {\n" +
+ " void loop(Collection<String> list) {\n" +
+ " for(@Nullable String s: list) {\n" +
+ " expectNonNull(s); // warning here\n" +
+ " }\n" +
+ " }\n" +
+ " \n" +
+ " void expectNonNull(@NonNull String s) {}\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in Bug370930.java (at line 6)\n" +
+ " expectNonNull(s); // warning here\n" +
+ " ^\n" +
+ "Type mismatch: required \'@NonNull String\' but the provided value can be null\n" +
+ "----------\n");
+}
public void test_assignment_expression_1() {
Map customOptions = getCompilerOptions();
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR);
@@ -3217,11 +3303,13 @@ public void test_assignment_expression_1() {
public void test_nesting_1() {
Map customOptions = getCompilerOptions();
// customOptions.put(CompilerOptions.OPTION_ReportPotentialNullSpecViolation, JavaCore.ERROR);
- customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
+// This option currently does nothing:
+// customOptions.put(JavaCore.COMPILER_NONNULL_IS_DEFAULT, JavaCore.ENABLED);
runNegativeTestWithLibs(
new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
+ "@NonNullByDefault\n" +
"public class X {\n" +
" void print4(final String s1) {\n" +
" for (int i=0; i<3; i++)\n" +
@@ -3256,12 +3344,12 @@ public void test_nesting_1() {
},
customOptions,
"----------\n" +
- "1. ERROR in X.java (at line 15)\n" +
+ "1. ERROR in X.java (at line 16)\n" +
" print(s2);\n" +
" ^^\n" +
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" +
"----------\n" +
- "2. ERROR in X.java (at line 24)\n" +
+ "2. ERROR in X.java (at line 25)\n" +
" @NonNull String s3R = s3;\n" +
" ^^\n" +
"Type mismatch: required \'@NonNull String\' but the provided value can be null\n" +
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceImplTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceImplTests.java
index 2bb4c6175..95af131fd 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceImplTests.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceImplTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2005, 2011 IBM Corporation and others.
+ * Copyright (c) 2005, 2010 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -37,7 +37,6 @@ import org.eclipse.jdt.internal.compiler.impl.Constant;
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
import org.eclipse.jdt.internal.compiler.lookup.PackageBinding;
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
-import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
/**
* A tests series especially meant to validate the internals of our null
@@ -1084,21 +1083,18 @@ public FlowInfo copy() {
return copy;
}
-public void markAsDefinitelyNonNull(VariableBinding local) {
- int position = local.getAnalysisId(this.maxFieldCount);
- grow(position);
+public void markAsDefinitelyNonNull(LocalVariableBinding local) {
+ grow(local.id + this.maxFieldCount);
super.markAsDefinitelyNonNull(local);
}
-public void markAsDefinitelyNull(VariableBinding local) {
- int position = local.getAnalysisId(this.maxFieldCount);
- grow(position);
+public void markAsDefinitelyNull(LocalVariableBinding local) {
+ grow(local.id + this.maxFieldCount);
super.markAsDefinitelyNull(local);
}
-public void markAsDefinitelyUnknown(VariableBinding local) {
- int position = local.getAnalysisId(this.maxFieldCount);
- grow(position);
+public void markAsDefinitelyUnknown(LocalVariableBinding local) {
+ grow(local.id + this.maxFieldCount);
super.markAsDefinitelyUnknown(local);
}
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java
index 6e8dfa6cc..31f6af6cf 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java
@@ -49,7 +49,7 @@ public NullReferenceTest(String name) {
// Only the highest compliance level is run; add the VM argument
// -Dcompliance=1.4 (for example) to lower it if needed
static {
-// TESTS_NAMES = new String[] { "testBug247564n_3" };
+// TESTS_NAMES = new String[] { "testBug360328" };
// TESTS_NUMBERS = new int[] { 561 };
// TESTS_RANGE = new int[] { 1, 2049 };
}
@@ -72,7 +72,6 @@ protected Map getCompilerOptions() {
defaultOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.ERROR);
defaultOptions.put(CompilerOptions.OPTION_ReportRawTypeReference, CompilerOptions.IGNORE);
defaultOptions.put(CompilerOptions.OPTION_IncludeNullInfoFromAsserts, CompilerOptions.ENABLED);
- defaultOptions.put(CompilerOptions.OPTION_IncludeFieldsInNullAnalysis, CompilerOptions.ENABLED);
}
return defaultOptions;
}
@@ -110,12 +109,13 @@ public void test0002_simple_field() {
" o.toString();\n" +
" }\n" +
"}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 5)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
+ ""
+// "----------\n" +
+// "1. ERROR in X.java (at line 5)\n" +
+// " o.toString();\n" +
+// " ^\n" +
+// "The field o is likely null; it was either set to null or checked for null when last used\n" +
+// "----------\n"
);
}
@@ -346,12 +346,13 @@ public void test0014_field_with_explicit_this_access() {
" this.o.toString();\n" +
" }\n" +
"}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 5)\n" +
- " this.o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
+ ""
+// "----------\n" +
+// "1. ERROR in X.java (at line 5)\n" +
+// " this.o.toString();\n" +
+// " ^^^^^^\n" +
+// "The field o is likely null; it was either set to null or checked for null when last used\n" +
+// "----------\n"
);
}
@@ -367,12 +368,13 @@ public void test0015_field_with_explicit_this_access() {
" o.toString();\n" +
" }\n" +
"}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 5)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
+ ""
+// "----------\n" +
+// "1. ERROR in X.java (at line 5)\n" +
+// " o.toString();\n" +
+// " ^\n" +
+// "The field o is likely null; it was either set to null or checked for null when last used\n" +
+// "----------\n"
);
}
@@ -422,12 +424,13 @@ public void test0018_field_of_enclosing_object() {
" }\n" +
" }\n" +
"}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 6)\n" +
- " X.this.o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
+ ""
+// "----------\n" +
+// "1. ERROR in X.java (at line 6)\n" +
+// " X.this.o.toString();\n" +
+// " ^^^^^^^^\n" +
+// "The field o is likely null; it was either set to null or checked for null when last used\n" +
+// "----------\n"
);
}
@@ -446,12 +449,13 @@ public void test0019_field_synchronized() {
" }\n" +
" void bar() {/* */}\n" +
"}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 5)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
+ ""
+// "----------\n" +
+// "1. ERROR in X.java (at line 5)\n" +
+// " o.toString();\n" +
+// " ^\n" +
+// "The field o is likely null; it was either set to null or checked for null when last used\n" +
+// "----------\n"
);
}
@@ -3095,7 +3099,7 @@ public void test0412_while_if_nested() {
// null analysis -- while
public void test0413_while_unknown_field() {
- this.runNegativeTest(
+ this.runConformTest(
new String[] {
"X.java",
"public class X {\n" +
@@ -3107,12 +3111,7 @@ public void test0413_while_unknown_field() {
" o.toString();\n" +
" }\n" +
"}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 7)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n");
+ "");
}
// null analysis -- while
@@ -15456,2709 +15455,4 @@ public void testBug360328d() {
"",/* expected error */
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
}
-
-// null analysis -- simple case for field
-public void testBug247564a() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object o;\n" +
- " void foo() {\n" +
- " if (o == null && o.toString() == \"\"){}\n" +
- " else {}\n" +
- " o.toString();\n" + // toString() call above defuses null info, so no warning here
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 4)\n" +
- " if (o == null && o.toString() == \"\"){}\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- simple case for field
-// no redundant null check warnings should be obtained since value of field
-// may be changed in another thread.
-public void testBug247564a_1() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object o;\n" +
- " void foo() {\n" +
- " o = null;" +
- " if (o == null){}\n" +
- " if (o != null){}\n" +
- " o.toString();\n" + // warn here
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 6)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- simple case for field
-public void testBug247564a_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object o;\n" +
- " void foo() {\n" +
- " if (o == null){\n" + // o is null inside the if block
- " o.toString();\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 5)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- simple case for field
-// null info from one method should not be present in the other (for instance fields)
-public void testBug247564a_3() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object o;\n" +
- " void foo() {\n" +
- " o = null;\n" +
- " }\n" +
- " void foo1() {\n" +
- " o.toString();\n" +
- " }\n" +
- "}\n"},
- ""
- );
-}
-
-//null analysis -- simple case for field
-public void testBug247564a_4() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object o;\n" +
- " int foo() {\n" +
- " if (o != null && o.toString() == \"\") {\n" +
- " } else {\n" +
- " }\n" +
- " return o.hashCode();\n" + // the above check has shed doubts so give a warning here
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 7)\n" +
- " return o.hashCode();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- field accessed inside control structure
-// from https://bugs.eclipse.org/bugs/show_bug.cgi?id=247564#c121
-public void testBug247564a_5() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " private Object field;\n" +
- " void goo(Object var) throws Exception{\n" +
- " if (field != null) field.hashCode();\n" +
- " int i = 20;\n" +
- " while (i<10) {\n" +
- " if (field == null) { \n" +
- " field = new Object();\n" +
- " }\n" +
- " field.toString(); // Wrong outcome was: Pot. NPE\n" +
- " i--;\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- ""
- );
-}
-
-// null analysis -- simple case for static final field
-// once dereferenced, treat as non null. Consistent with local variables.
-public void testBug247564b() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o = null;\n" +
- " static final Object o1 = new Object();\n" +
- " void foo() {\n" +
- " if (o.toString() == \"\") {}\n" +
- " if (o == null) {}\n" +
- " if (o != null) {}\n" +
- " if (o1 == null) {}\n" +
- " if (o1 != null) {}\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 5)\n" +
- " if (o.toString() == \"\") {}\n" +
- " ^\n" +
- "Null pointer access: The field o can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 6)\n" +
- " if (o == null) {}\n" +
- " ^\n" +
- "Null comparison always yields false: The field o cannot be null at this location\n" +
- "----------\n" +
- "3. WARNING in X.java (at line 6)\n" +
- " if (o == null) {}\n" +
- " ^^\n" +
- "Dead code\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 7)\n" +
- " if (o != null) {}\n" +
- " ^\n" +
- "Redundant null check: The field o cannot be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 8)\n" +
- " if (o1 == null) {}\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o1 cannot be null at this location\n" +
- "----------\n" +
- "6. WARNING in X.java (at line 8)\n" +
- " if (o1 == null) {}\n" +
- " ^^\n" +
- "Dead code\n" +
- "----------\n" +
- "7. ERROR in X.java (at line 9)\n" +
- " if (o1 != null) {}\n" +
- " ^^\n" +
- "Redundant null check: The field o1 cannot be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- simple case for static final field
-public void testBug247564b_1() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o;\n" +
- " static final Object o1;\n" +
- " static {\n" +
- " o = null;\n" +
- " o1 = new Object();\n" +
- " }\n" +
- " void foo() {\n" +
- " if (o.toString() == \"\") {}\n" +
- " if (o == null) {}\n" +
- " if (o != null) {}\n" +
- " if (o1 == null) {}\n" +
- " if (o1 != null) {}\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 9)\n" +
- " if (o.toString() == \"\") {}\n" +
- " ^\n" +
- "Null pointer access: The field o can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 10)\n" +
- " if (o == null) {}\n" +
- " ^\n" +
- "Null comparison always yields false: The field o cannot be null at this location\n" +
- "----------\n" +
- "3. WARNING in X.java (at line 10)\n" +
- " if (o == null) {}\n" +
- " ^^\n" +
- "Dead code\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 11)\n" +
- " if (o != null) {}\n" +
- " ^\n" +
- "Redundant null check: The field o cannot be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 12)\n" +
- " if (o1 == null) {}\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o1 cannot be null at this location\n" +
- "----------\n" +
- "6. WARNING in X.java (at line 12)\n" +
- " if (o1 == null) {}\n" +
- " ^^\n" +
- "Dead code\n" +
- "----------\n" +
- "7. ERROR in X.java (at line 13)\n" +
- " if (o1 != null) {}\n" +
- " ^^\n" +
- "Redundant null check: The field o1 cannot be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- simple case for static final field
-public void testBug247564b_1_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64, \n" +
- "field65, field66, field67, field68, \n" +
- "field69, field70, field71, field72, \n" +
- "field73, field74, field75, field76, \n" +
- "field77, field78, field79, field80, \n" +
- "field81, field82, field83, field84, \n" +
- "field85, field86, field87, field88, \n" +
- "field89, field90, field91, field92, \n" +
- "field93, field94, field95, field96, \n" +
- "field97, field98, field99;\n" +
- " static final Object o;\n" +
- " static final Object o1;\n" +
- " static {\n" +
- " o = null;\n" +
- " o1 = new Object();\n" +
- " }\n" +
- " void foo() {\n" +
- " if (o.toString() == \"\") {}\n" +
- " if (o == null) {}\n" +
- " if (o != null) {}\n" +
- " if (o1 == null) {}\n" +
- " if (o1 != null) {}\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 35)\n" +
- " if (o.toString() == \"\") {}\n" +
- " ^\n" +
- "Null pointer access: The field o can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 36)\n" +
- " if (o == null) {}\n" +
- " ^\n" +
- "Null comparison always yields false: The field o cannot be null at this location\n" +
- "----------\n" +
- "3. WARNING in X.java (at line 36)\n" +
- " if (o == null) {}\n" +
- " ^^\n" +
- "Dead code\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 37)\n" +
- " if (o != null) {}\n" +
- " ^\n" +
- "Redundant null check: The field o cannot be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 38)\n" +
- " if (o1 == null) {}\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o1 cannot be null at this location\n" +
- "----------\n" +
- "6. WARNING in X.java (at line 38)\n" +
- " if (o1 == null) {}\n" +
- " ^^\n" +
- "Dead code\n" +
- "----------\n" +
- "7. ERROR in X.java (at line 39)\n" +
- " if (o1 != null) {}\n" +
- " ^^\n" +
- "Redundant null check: The field o1 cannot be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// checked before use
-public void testBug247564b_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o;\n" +
- " static final Object o1 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" +
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " void foo1() {\n" +
- " if (o == null) \n" +
- " return;\n" +
- " o.toString();\n" + // cant be null for sure, dont complain
- " }\n" +
- "}\n"},
- ""
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// checked before use
-public void testBug247564b_2_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64, \n" +
- "field65, field66, field67, field68, \n" +
- "field69, field70, field71, field72, \n" +
- "field73, field74, field75, field76, \n" +
- "field77, field78, field79, field80, \n" +
- "field81, field82, field83, field84, \n" +
- "field85, field86, field87, field88, \n" +
- "field89, field90, field91, field92, \n" +
- "field93, field94, field95, field96, \n" +
- "field97, field98, field99;\n" +
- " static final Object o;\n" +
- " static final Object o1 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" +
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " void foo1() {\n" +
- " if (o == null) \n" +
- " return;\n" +
- " o.toString();\n" + // cant be null for sure, dont complain
- " }\n" +
- "}\n"},
- ""
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// Check pot. NPE case for constant fields
-// Once dereferenced, treat as non null. Just like locals.
-public void testBug247564b_3() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o;\n" +
- " static final Object o1 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" +
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " void foo() {\n" +
- " if (o.toString() == \"\") {}\n" +
- " if (o == null) {}\n" +
- " if (o != null) {}\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 12)\n" +
- " if (o.toString() == \"\") {}\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 13)\n" +
- " if (o == null) {}\n" +
- " ^\n" +
- "Null comparison always yields false: The field o cannot be null at this location\n" +
- "----------\n" +
- "3. WARNING in X.java (at line 13)\n" +
- " if (o == null) {}\n" +
- " ^^\n" +
- "Dead code\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 14)\n" +
- " if (o != null) {}\n" +
- " ^\n" +
- "Redundant null check: The field o cannot be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// Check pot. NPE case for constant fields
-// Once dereferenced, treat as non null. Just like locals.
-public void testBug247564b_3_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64, \n" +
- "field65, field66, field67, field68, \n" +
- "field69, field70, field71, field72, \n" +
- "field73, field74, field75, field76, \n" +
- "field77, field78, field79, field80, \n" +
- "field81, field82, field83, field84, \n" +
- "field85, field86, field87, field88, \n" +
- "field89, field90, field91, field92, \n" +
- "field93, field94, field95, field96, \n" +
- "field97, field98, field99;\n" +
- " static final Object o;\n" +
- " static final Object o1 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" +
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " void foo() {\n" +
- " if (o.toString() == \"\") {}\n" +
- " if (o == null) {}\n" +
- " if (o != null) {}\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 38)\n" +
- " if (o.toString() == \"\") {}\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 39)\n" +
- " if (o == null) {}\n" +
- " ^\n" +
- "Null comparison always yields false: The field o cannot be null at this location\n" +
- "----------\n" +
- "3. WARNING in X.java (at line 39)\n" +
- " if (o == null) {}\n" +
- " ^^\n" +
- "Dead code\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 40)\n" +
- " if (o != null) {}\n" +
- " ^\n" +
- "Redundant null check: The field o cannot be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// checked before use
-public void testBug247564b_4() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o;\n" +
- " static final Object o1 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" +
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " void foo1() {\n" +
- " if (o == null) {\n" +
- " o.toString(); // danger\n" +
- " return;\n" +
- " }\n" +
- " o.toString(); // safe\n" +
- " }\n" +
- " void foo2() {\n" +
- " if (o != null) {\n" +
- " o.toString(); // safe (2)\n" +
- " }\n" +
- " o.toString(); // uncertain\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 13)\n" +
- " o.toString(); // danger\n" +
- " ^\n" +
- "Null pointer access: The field o can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 22)\n" +
- " o.toString(); // uncertain\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// checked before use
-public void testBug247564b_4_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64, \n" +
- "field65, field66, field67, field68, \n" +
- "field69, field70, field71, field72, \n" +
- "field73, field74, field75, field76, \n" +
- "field77, field78, field79, field80, \n" +
- "field81, field82, field83, field84, \n" +
- "field85, field86, field87, field88, \n" +
- "field89, field90, field91, field92, \n" +
- "field93, field94, field95, field96, \n" +
- "field97, field98, field99;\n" +
- " static final Object o;\n" +
- " static final Object o1 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" +
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " void foo1() {\n" +
- " if (o == null) {\n" +
- " o.toString(); // danger\n" +
- " return;\n" +
- " }\n" +
- " o.toString(); // safe\n" +
- " }\n" +
- " void foo2() {\n" +
- " if (o != null) {\n" +
- " o.toString(); // safe (2)\n" +
- " }\n" +
- " o.toString(); // uncertain\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 39)\n" +
- " o.toString(); // danger\n" +
- " ^\n" +
- "Null pointer access: The field o can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 48)\n" +
- " o.toString(); // uncertain\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// check if the resetting works properly i.e. null status for constant fields should not be
-// reset on method calls.
-public void testBug247564b_5() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o;\n" +
- " static final Object o1 = new Object();\n" +
- " static final Object o2 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" +
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " void foo1() {\n" +
- " final Object local = null;\n" +
- " if (local == null) {\n" +
- " local.toString();\n" +
- " }\n" +
- " local.toString();\n" +
- " if (o == null) {\n" + // don't know o's nullness, so silent
- " o.toString();\n" + // report NPE
- " }\n" +
- " local.toString();\n" + // try to diffuse status for o / o2
- " o.toString();\n" + // already reported NPE above. So silent. Same behaviour as 'local'
- " if (o2 == null) {\n" + // report always false null check
- " o2.toString();\n" + // dead code
- " }\n" +
- " o2.toString();" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 14)\n" +
- " if (local == null) {\n" +
- " ^^^^^\n" +
- "Redundant null check: The variable local can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 15)\n" +
- " local.toString();\n" +
- " ^^^^^\n" +
- "Null pointer access: The variable local can only be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 19)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Null pointer access: The field o can only be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 23)\n" +
- " if (o2 == null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o2 cannot be null at this location\n" +
- "----------\n" +
- "5. WARNING in X.java (at line 23)\n" +
- " if (o2 == null) {\n" +
- " o2.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n"
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// check if the resetting works properly i.e. null status for constant fields should not be
-// reset on method calls. This test is for constructors
-public void testBug247564b_6() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o;\n" +
- " static final Object o1 = new Object();\n" +
- " static final Object o2 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" +
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " public X() {\n" +
- " final Object local = null;\n" +
- " if (local == null) {\n" +
- " local.toString();\n" +
- " }\n" +
- " local.toString();\n" +
- " if (o == null) {\n" + // don't know o's nullness, so silent
- " o.toString();\n" + // report NPE
- " }\n" +
- " local.toString();\n" + // try to diffuse status for o / o2
- " o.toString();\n" + // already reported NPE above. So silent. Same behaviour as 'local'
- " if (o2 == null) {\n" + // report always false null check
- " o2.toString();\n" + // dead code
- " }\n" +
- " o2.toString();" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 14)\n" +
- " if (local == null) {\n" +
- " ^^^^^\n" +
- "Redundant null check: The variable local can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 15)\n" +
- " local.toString();\n" +
- " ^^^^^\n" +
- "Null pointer access: The variable local can only be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 19)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Null pointer access: The field o can only be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 23)\n" +
- " if (o2 == null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o2 cannot be null at this location\n" +
- "----------\n" +
- "5. WARNING in X.java (at line 23)\n" +
- " if (o2 == null) {\n" +
- " o2.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n"
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// check if the resetting works properly i.e. null status for constant fields should not be
-// reset on method calls. This test is for constructors
-public void testBug247564b_6_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64, \n" +
- "field65, field66, field67, field68, \n" +
- "field69, field70, field71, field72, \n" +
- "field73, field74, field75, field76, \n" +
- "field77, field78, field79, field80, \n" +
- "field81, field82, field83, field84, \n" +
- "field85, field86, field87, field88, \n" +
- "field89, field90, field91, field92, \n" +
- "field93, field94, field95, field96, \n" +
- "field97, field98, field99;\n" +
- " static final Object o;\n" +
- " static final Object o1 = new Object();\n" +
- " static final Object o2 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" +
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " public X() {\n" +
- " final Object local = null;\n" +
- " if (local == null) {\n" +
- " local.toString();\n" +
- " }\n" +
- " local.toString();\n" +
- " if (o == null) {\n" + // don't know o's nullness, so silent
- " o.toString();\n" + // report NPE
- " }\n" +
- " local.toString();\n" + // try to diffuse status for o / o2
- " o.toString();\n" + // already reported NPE above. So silent. Same behaviour as 'local'
- " if (o2 == null) {\n" + // report always false null check
- " o2.toString();\n" + // dead code
- " }\n" +
- " o2.toString();" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 40)\n" +
- " if (local == null) {\n" +
- " ^^^^^\n" +
- "Redundant null check: The variable local can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 41)\n" +
- " local.toString();\n" +
- " ^^^^^\n" +
- "Null pointer access: The variable local can only be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 45)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Null pointer access: The field o can only be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 49)\n" +
- " if (o2 == null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o2 cannot be null at this location\n" +
- "----------\n" +
- "5. WARNING in X.java (at line 49)\n" +
- " if (o2 == null) {\n" +
- " o2.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n"
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// check if the resetting works properly i.e. null status for constant fields should not be
-// reset on method calls. Also, null info of constant field from static block is available in methods
-public void testBug247564b_7() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o;\n" +
- " static final Object o1 = null;\n" +
- " static final Object o2 = new Object();\n" +
- " static {\n" +
- " if (o1.hashCode() == 2){\n" + // report NPE. But dereferenced here, so later it should be treated as non null
- " o = new Object();\n" +
- " } else {\n" +
- " o = null;\n" +
- " }\n" +
- " }\n" +
- " void foo1() {\n" +
- " final Object local = null;\n" +
- " if (local == null) {\n" +
- " local.toString();\n" +
- " }\n" +
- " local.toString();\n" +
- " if (o1 == null) {\n" + // can't be null, was dereferenced in static initializer
- " o1.toString();\n" + // dead
- " }\n" +
- " local.toString();\n" + // try to diffuse status for o1 / o2
- " o1.toString();\n" + // safe
- " if (o2 == null) {\n" + // report always false null check
- " o2.toString();\n" + // dead code
- " }\n" +
- " o2.toString();" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 6)\n" +
- " if (o1.hashCode() == 2){\n" +
- " ^^\n" +
- "Null pointer access: The field o1 can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 14)\n" +
- " if (local == null) {\n" +
- " ^^^^^\n" +
- "Redundant null check: The variable local can only be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 15)\n" +
- " local.toString();\n" +
- " ^^^^^\n" +
- "Null pointer access: The variable local can only be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 18)\n" +
- " if (o1 == null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o1 cannot be null at this location\n" +
- "----------\n" +
- "5. WARNING in X.java (at line 18)\n" +
- " if (o1 == null) {\n" +
- " o1.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n" +
- "6. ERROR in X.java (at line 23)\n" +
- " if (o2 == null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o2 cannot be null at this location\n" +
- "----------\n" +
- "7. WARNING in X.java (at line 23)\n" +
- " if (o2 == null) {\n" +
- " o2.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n"
- );
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// check if the resetting works properly i.e. null status for constant fields should not be
-// reset on method calls. This test is for more than 64 fields to check for extra bits.
-public void testBug247564b_8() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- false,
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64, \n" +
- "field65, field66, field67, field68, \n" +
- "field69, field70, field71, field72, \n" +
- "field73, field74, field75, field76, \n" +
- "field77, field78, field79, field80, \n" +
- "field81, field82, field83, field84, \n" +
- "field85, field86, field87, field88, \n" +
- "field89, field90, field91, field92, \n" +
- "field93, field94, field95, field96, \n" +
- "field97, field98, field99;\n" +
- "static final Object o1 = null;\n" +
- "static final Object o2 = new Object();\n" +
- " void foo1() {\n" +
- " final Object local = null;\n" +
- " if (local == null) {\n" +
- " local.toString();\n" +
- " }\n" +
- " local.toString();\n" +
- " if (o1 == null) {\n" + // report redundant null check
- " o1.toString();\n" + // report NPE
- " }\n" +
- " local.toString();\n" + // try to diffuse status for o1 / o2
- " o1.toString();\n" + // already reported NPE above. So silent. Same behaviour as 'local'
- " if (o2 == null) {\n" + // report always false null check
- " o2.toString();\n" + // dead code
- " }\n" +
- " o2.toString();" +
- " }\n" +
- "}\n"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 32)\n" +
- " if (local == null) {\n" +
- " ^^^^^\n" +
- "Redundant null check: The variable local can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 33)\n" +
- " local.toString();\n" +
- " ^^^^^\n" +
- "Null pointer access: The variable local can only be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 36)\n" +
- " if (o1 == null) {\n" +
- " ^^\n" +
- "Redundant null check: The field o1 can only be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 37)\n" +
- " o1.toString();\n" +
- " ^^\n" +
- "Null pointer access: The field o1 can only be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 41)\n" +
- " if (o2 == null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o2 cannot be null at this location\n" +
- "----------\n" +
- "6. WARNING in X.java (at line 41)\n" +
- " if (o2 == null) {\n" +
- " o2.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
-}
-
-// null analysis -- case for static final field initialized inside static block where some locals are also present
-// check if the resetting works properly
-public void testBug247564b_9() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- false,
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o1;\n" +
- " static final Object o2 = new Object();\n" +
- " static {\n" +
- " int i = 10;\n" +
- " o1 = null;\n" +
- " }\n" +
- " void foo1() {\n" +
- " final Object local = null;\n" +
- " if (local == null) {\n" +
- " local.toString();\n" +
- " }\n" +
- " local.toString();\n" +
- " if (o1 == null) {\n" + // report redundant null check
- " o1.toString();\n" + // report NPE
- " }\n" +
- " local.toString();\n" + // try to diffuse status for o1 / o2
- " o1.toString();\n" + // already reported NPE above. So silent. Same behaviour as 'local'
- " if (o2 == null) {\n" + // report always false null check
- " o2.toString();\n" + // dead code
- " }\n" +
- " o2.toString();" +
- " }\n" +
- "}\n"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 10)\n" +
- " if (local == null) {\n" +
- " ^^^^^\n" +
- "Redundant null check: The variable local can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 11)\n" +
- " local.toString();\n" +
- " ^^^^^\n" +
- "Null pointer access: The variable local can only be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 14)\n" +
- " if (o1 == null) {\n" +
- " ^^\n" +
- "Redundant null check: The field o1 can only be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 15)\n" +
- " o1.toString();\n" +
- " ^^\n" +
- "Null pointer access: The field o1 can only be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 19)\n" +
- " if (o2 == null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o2 cannot be null at this location\n" +
- "----------\n" +
- "6. WARNING in X.java (at line 19)\n" +
- " if (o2 == null) {\n" +
- " o2.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// check if the resetting works properly i.e. null status for constant fields should not be
-// reset on method calls.
-// Boundary condition: field <boundary> constant local
-public void testBug247564b_10() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- false,
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63;\n" +
- "static final Object static64 = null;\n" +
- " void foo1(Object local65) {\n" +
- " if (field63 == null) {\n" +
- " field63.toString(); // report NPE\n" +
- " }\n" +
- " if (static64 == null) { // redundant\n" +
- " static64.toString(); // report NPE\n" +
- " }\n" +
- " if (local65 == null) {\n" +
- " local65.toString(); // report NPE\n" +
- " }\n" +
- " if (field63 == null) {\n" +
- " this.toString();\n" +
- " field63.toString();\n" + // wiped by method call
- " }\n" +
- " if (static64 == null) { // always false\n" + // because we're past static64.toString()
- " this.toString(); // dead\n" +
- " static64.toString();\n" +
- " }\n" +
- " if (local65 == null) { // alwayws false\n" + // because we're past local65.toString()
- " this.toString(); // dead\n" +
- " local65.toString();\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 22)\n" +
- " field63.toString(); // report NPE\n" +
- " ^^^^^^^\n" +
- "Potential null pointer access: The field field63 may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 24)\n" +
- " if (static64 == null) { // redundant\n" +
- " ^^^^^^^^\n" +
- "Redundant null check: The field static64 can only be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 25)\n" +
- " static64.toString(); // report NPE\n" +
- " ^^^^^^^^\n" +
- "Null pointer access: The field static64 can only be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 28)\n" +
- " local65.toString(); // report NPE\n" +
- " ^^^^^^^\n" +
- "Null pointer access: The variable local65 can only be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 34)\n" +
- " if (static64 == null) { // always false\n" +
- " ^^^^^^^^\n" +
- "Null comparison always yields false: The field static64 cannot be null at this location\n" +
- "----------\n" +
- "6. WARNING in X.java (at line 34)\n" +
- " if (static64 == null) { // always false\n" +
- " this.toString(); // dead\n" +
- " static64.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n" +
- "7. ERROR in X.java (at line 38)\n" +
- " if (local65 == null) { // alwayws false\n" +
- " ^^^^^^^\n" +
- "Null comparison always yields false: The variable local65 cannot be null at this location\n" +
- "----------\n" +
- "8. WARNING in X.java (at line 38)\n" +
- " if (local65 == null) { // alwayws false\n" +
- " this.toString(); // dead\n" +
- " local65.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
-}
-
-// null analysis -- case for static final field initialized inside static block with different values
-// check if the resetting works properly i.e. null status for constant fields should not be
-// reset on method calls.
-// Boundary condition: constant field <boundary> local
-public void testBug247564b_11() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- false,
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61;\n" +
- "static final Object static62 = null;\n" +
- "Object field63;" +
- " void foo1(Object local64) {\n" +
- " if (static62 == null) { // redundant\n" +
- " static62.toString(); // report NPE\n" +
- " }\n" +
- " if (field63 == null) {\n" +
- " field63.toString(); // report NPE\n" +
- " }\n" +
- " if (local64 == null) {\n" +
- " local64.toString(); // report NPE\n" +
- " }\n" +
- " if (static62 == null) { // always false\n" + // because we're past static64.toString()
- " this.toString(); // dead\n" +
- " static62.toString();\n" +
- " }\n" +
- " if (field63 == null) {\n" +
- " this.toString(); //\n" +
- " field63.toString();\n" + // wiped by method call
- " }\n" +
- " if (local64 == null) { // alwayws false\n" + // because we're past local64.toString()
- " this.toString(); // dead\n" +
- " local64.toString();\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 21)\n" +
- " if (static62 == null) { // redundant\n" +
- " ^^^^^^^^\n" +
- "Redundant null check: The field static62 can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 22)\n" +
- " static62.toString(); // report NPE\n" +
- " ^^^^^^^^\n" +
- "Null pointer access: The field static62 can only be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 25)\n" +
- " field63.toString(); // report NPE\n" +
- " ^^^^^^^\n" +
- "Potential null pointer access: The field field63 may be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 28)\n" +
- " local64.toString(); // report NPE\n" +
- " ^^^^^^^\n" +
- "Null pointer access: The variable local64 can only be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 30)\n" +
- " if (static62 == null) { // always false\n" +
- " ^^^^^^^^\n" +
- "Null comparison always yields false: The field static62 cannot be null at this location\n" +
- "----------\n" +
- "6. WARNING in X.java (at line 30)\n" +
- " if (static62 == null) { // always false\n" +
- " this.toString(); // dead\n" +
- " static62.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n" +
- "7. ERROR in X.java (at line 38)\n" +
- " if (local64 == null) { // alwayws false\n" +
- " ^^^^^^^\n" +
- "Null comparison always yields false: The variable local64 cannot be null at this location\n" +
- "----------\n" +
- "8. WARNING in X.java (at line 38)\n" +
- " if (local64 == null) { // alwayws false\n" +
- " this.toString(); // dead\n" +
- " local64.toString();\n" +
- " }\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Dead code\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
-}
-
-// null analysis -- fields in synchronized methods
-// check that null analysis for fields in synchronized methods
-// behave as it does in ordinary methods.
-public void testBug247564c() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object o;\n" +
- " Object o1;\n" +
- " static final Object o2 = null;\n" +
- " static final Object o3 = new Object();\n" +
- " synchronized void foo() {\n" +
- " o = null;\n" +
- " if (o == null) {\n" +
- " o.toString();\n" +
- " }\n" +
- " o1 = new Object();\n" +
- " if (o1 == null) {\n" +
- " o1.toString();\n" +
- " }\n" +
- " if (o2 != null) {\n" +
- " }\n" +
- " else {\n" +
- " o2.toString();\n" +
- " }\n" +
- " if (o3 == null) {\n" +
- " }\n" +
- " else {\n" +
- " o3.toString();\n" +
- " }\n" +
- " }\n" +
- " void foo1() {\n" +
- " o.toString();\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 9)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 13)\n" +
- " o1.toString();\n" +
- " ^^\n" +
- "Potential null pointer access: The field o1 may be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 15)\n" +
- " if (o2 != null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o2 can only be null at this location\n" +
- "----------\n" +
- "4. WARNING in X.java (at line 15)\n" +
- " if (o2 != null) {\n" +
- " }\n" +
- " ^^^^^\n" +
- "Dead code\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 18)\n" +
- " o2.toString();\n" +
- " ^^\n" +
- "Null pointer access: The field o2 can only be null at this location\n" +
- "----------\n" +
- "6. ERROR in X.java (at line 20)\n" +
- " if (o3 == null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o3 cannot be null at this location\n" +
- "----------\n" +
- "7. WARNING in X.java (at line 20)\n" +
- " if (o3 == null) {\n" +
- " }\n" +
- " ^^^^^\n" +
- "Dead code\n" +
- "----------\n"
- );
-}
-
-// null analysis -- fields in synchronized methods
-// check that null analysis for fields in synchronized methods
-// behave as it does in ordinary methods. Higher no. of fields
-public void testBug247564c_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object o;\n" +
- " Object o1;\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64, \n" +
- "field65, field66, field67, field68, \n" +
- "field69, field70, field71, field72, \n" +
- "field73, field74, field75, field76, \n" +
- "field77, field78, field79, field80, \n" +
- "field81, field82, field83, field84, \n" +
- "field85, field86, field87, field88, \n" +
- "field89, field90, field91, field92, \n" +
- "field93, field94, field95, field96, \n" +
- "field97, field98, field99;\n" +
- " static final Object o2 = null;\n" +
- " static final Object o3 = new Object();\n" +
- " synchronized void foo() {\n" +
- " o = null;\n" +
- " if (o == null) {\n" +
- " o.toString();\n" +
- " }\n" +
- " o1 = new Object();\n" +
- " if (o1 == null) {\n" +
- " o1.toString();\n" +
- " }\n" +
- " if (o2 != null) {\n" +
- " }\n" +
- " else {\n" +
- " o2.toString();\n" +
- " }\n" +
- " if (o3 == null) {\n" +
- " }\n" +
- " else {\n" +
- " o3.toString();\n" +
- " }\n" +
- " }\n" +
- " void foo1() {\n" +
- " o.toString();\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 35)\n" +
- " o.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 39)\n" +
- " o1.toString();\n" +
- " ^^\n" +
- "Potential null pointer access: The field o1 may be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 41)\n" +
- " if (o2 != null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o2 can only be null at this location\n" +
- "----------\n" +
- "4. WARNING in X.java (at line 41)\n" +
- " if (o2 != null) {\n" +
- " }\n" +
- " ^^^^^\n" +
- "Dead code\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 44)\n" +
- " o2.toString();\n" +
- " ^^\n" +
- "Null pointer access: The field o2 can only be null at this location\n" +
- "----------\n" +
- "6. ERROR in X.java (at line 46)\n" +
- " if (o3 == null) {\n" +
- " ^^\n" +
- "Null comparison always yields false: The field o3 cannot be null at this location\n" +
- "----------\n" +
- "7. WARNING in X.java (at line 46)\n" +
- " if (o3 == null) {\n" +
- " }\n" +
- " ^^^^^\n" +
- "Dead code\n" +
- "----------\n"
- );
-}
-
-// null analysis -- test redundant instanceof warning for static final field
-public void testBug247564d() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " static final Object o = null;\n" +
- " static final Object o1 = new Object();\n" +
- " void foo() {\n" +
- " if (o instanceof String) {}\n" +
- " if (o1 instanceof String) {}\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 5)\n" +
- " if (o instanceof String) {}\n" +
- " ^\n" +
- "instanceof always yields false: The field o can only be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- test redundant instanceof warning for static final field. More fields
-public void testBug247564d_1() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64, \n" +
- "field65, field66, field67, field68, \n" +
- "field69, field70, field71, field72, \n" +
- "field73, field74, field75, field76, \n" +
- "field77, field78, field79, field80, \n" +
- "field81, field82, field83, field84, \n" +
- "field85, field86, field87, field88, \n" +
- "field89, field90, field91, field92, \n" +
- "field93, field94, field95, field96, \n" +
- "field97, field98, field99;\n" +
- " static final Object o = null;\n" +
- " static final Object o1 = new Object();\n" +
- " void foo() {\n" +
- " if (o instanceof String) {}\n" +
- " if (o1 instanceof String) {}\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 31)\n" +
- " if (o instanceof String) {}\n" +
- " ^\n" +
- "instanceof always yields false: The field o can only be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- test potential null ptr access warning because of static field access through object returned by method call
-public void testBug247564f() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- false,
- new String[] {
- "X.java",
- "public class X {\n" +
- " static Object o;\n" +
- " static Object o1;\n" +
- " Object o2;\n" +
- " X getX() { return new X();\n}\n" +
- " void foo() {\n" +
- " if (getX().o == null && this.o.hashCode() == 0) return;\n" +
- " if (getX().o2 == null && this.o2.hashCode() == 0) return;\n" +
- " }\n" +
- "}"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 8)\n" +
- " if (getX().o == null && this.o.hashCode() == 0) return;\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
-}
-
-// null analysis -- test potential null ptr access warning because of static field access through object returned by method call
-public void testBug247564f_1() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- false,
- new String[] {
- "X.java",
- "public class X {\n" +
- " static Object o;\n" +
- " X getX() { return new X();\n}\n" +
- " Y getY() { return new Y();\n}\n" +
- " void foo() {\n" +
- " if (getY().o == null && this.o.hashCode() == 0) return;\n" +
- " if (getX().o == null && this.o.hashCode() == 0) return;\n" +
- " }\n" +
- "}\n" +
- "class Y{\n" +
- " static Object o;\n" +
- "}\n"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 9)\n" +
- " if (getX().o == null && this.o.hashCode() == 0) return;\n" +
- " ^\n" +
- "Potential null pointer access: The field o may be null at this location\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
-}
-
-// null analysis -- test field analysis in case of more than 64 fields
-public void testBug247564g() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- false,
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64, \n" +
- "field65, field66, field67, field68, \n" +
- "field69, field70, field71, field72, \n" +
- "field73, field74, field75, field76, \n" +
- "field77, field78, field79, field80, \n" +
- "field81, field82, field83, field84, \n" +
- "field85, field86, field87, field88, \n" +
- "field89, field90, field91, field92, \n" +
- "field93, field94, field95, field96, \n" +
- "field97, field98, field99;\n" +
- "static final Object field100 = null;\n" +
- " void foo() {\n" +
- " int i = 0;" +
- " while (i<10){\n" +
- " i++;\n" +
- " if (this.field99 == null && this.field99.hashCode() == 0){}\n" +
- " this.field98 = null;\n" +
- " }\n" +
- " if (this.field98.hashCode() == 0) {}\n" +
- " this.field97 = null;\n" +
- " if (this.field97.hashCode() == 0) {}\n" +
- " if (this.field100.hashCode() == 0) {}\n" +
- " }\n" +
- "}"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 32)\n" +
- " if (this.field99 == null && this.field99.hashCode() == 0){}\n" +
- " ^^^^^^^\n" +
- "Potential null pointer access: The field field99 may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 35)\n" +
- " if (this.field98.hashCode() == 0) {}\n" +
- " ^^^^^^^\n" +
- "Potential null pointer access: The field field98 may be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 37)\n" +
- " if (this.field97.hashCode() == 0) {}\n" +
- " ^^^^^^^\n" +
- "Potential null pointer access: The field field97 may be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 38)\n" +
- " if (this.field100.hashCode() == 0) {}\n" +
- " ^^^^^^^^\n" +
- "Null pointer access: The field field100 can only be null at this location\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError);
-}
-
-// null analysis -- simple case for field for inner class
-// to make sure field id's of inner and outer classes are not same for flow analysis
-public void testBug247564h() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object o;\n" +
- " class X1 {\n" +
- " Object x;" +
- " Object x1;" +
- " Object x2;" +
- " void goo() {\n" +
- " if (o == null && x.toString() == \"\"){}\n" +
- " if (o2 == null && o2.toString() == \"\"){}\n" +
- " if (o2 == null && x2.toString() == \"\"){}\n" +
- " }\n" +
-
- " }\n" +
- " Object o1;\n" +
- " static Object o2;\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 6)\n" +
- " if (o2 == null && o2.toString() == \"\"){}\n" +
- " ^^\n" +
- "Potential null pointer access: The field o2 may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- simple case for field for inner class
-// to make sure that id's of local variables in inner classes don't conflict with those of fields.
-public void testBug247564h_1() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object field0;\n" +
- " Object field1;\n" +
- " class X1 {\n" +
- " Object field2;" +
- " Object field3;" +
- " void goo(Object var) {\n" +
- " if (var == null && field2.toString() == \"\"){}\n" +
- " if (var == null && field3.toString() == \"\"){}\n" +
- " if (field2 == null && field2.toString() == \"\"){}\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 8)\n" +
- " if (field2 == null && field2.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field2 may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- simple case for field for inner class
-// to make sure that id's of local variables in inner classes don't conflict with those of fields.
-public void testBug247564h_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " Object field0;\n" +
- " Object field1;\n" +
- " class X1 {\n" +
- " Object field2;\n" +
- " Object field3;\n" +
- " class X2 {\n" +
- " Object field4;\n" +
- " Object field5;\n" +
- " void goo(Object var) {\n" +
- " if (var == null && field4.toString() == \"\"){}\n" +
- " if (var == null && field5.toString() == \"\"){}\n" +
- " if (field3 == null && field3.toString() == \"\"){}\n" +
- " if (field3 == null && field1.toString() == \"\"){}\n" +
- " }\n" +
- " }\n" +
- " Object field22;\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 13)\n" +
- " if (field3 == null && field3.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field3 may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- simple case for static fields
-// to make sure that static field only of the current type is assigned potentially null when compared against null
-// Static fields belonging to any other class should be ignored
-// Qualified access to static fields should also work
-public void testBug247564i_1() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- true,
- new String[] {
- "X.java",
- "public class X {\n" +
- " static Object field0;\n" +
- " static Object field1;\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX() { return new X();}\n" +
- " void goo(Object var) {\n" +
- " if (Y.yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (Y.yField1 == null && this.field0.toString() == \"\"){}\n" + // no warn
- " if (Y.xiny.field0 == null && Y.xiny.field0.toString() == \"\"){}\n" + // warn, qualified access
- " if (Y.yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (Y.xiny.field1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (X.field0 == null && X.field0.toString() == \"\"){}\n" + // warn
- " if (this.field0 == null && X.field0.toString() == \"\"){}\n" + // warn
- " if (X.field0 == null && this.field0.toString() == \"\"){}\n" + // warn
- " if (X.field0 == null && getX().field0.toString() == \"\"){}\n" + // no warn (diffused by getX() call)
- " }\n" +
- "}\n" +
- "class Y{\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX(){ return new X();}\n" +
- " static Object yField1;" +
- " static X xiny;\n" +
- "}"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 9)\n" +
- " if (Y.xiny.field0 == null && Y.xiny.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 12)\n" +
- " if (X.field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 13)\n" +
- " if (this.field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 14)\n" +
- " if (X.field0 == null && this.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError
- );
-}
-
-// null analysis -- static fields accessed via MessageSend
-// to make sure that static field only of the current type is assigned potentially null when compared against null
-// Static fields belonging to any other class should be ignored
-// Qualified access to static fields should also work
-public void testBug247564i_2() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- true,
- new String[] {
- "X.java",
- "public class X {\n" +
- " static Object field0;\n" +
- " static Object field1;\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX() { return new X();}\n" +
- " void goo(Object var) {\n" +
- " if (new Y().getY().yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && this.field0.toString() == \"\"){}\n" + // no warn
- " if (new Y().getX().field0 == null && field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && this.field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && getX().field0.toString() == \"\"){}\n" + // no warn, getX() wipes out null info
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" + // warn, qualified access
- // fields from other types, don't warn
- " if (new Y().getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getX().field0 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " }\n" +
- "}\n" +
- "class Y{\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX(){ return new X();}\n" +
- " static Object yField1;\n" +
- "}"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 10)\n" +
- " if (new Y().getX().field0 == null && field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 11)\n" +
- " if (getX().field0 == null && field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 12)\n" +
- " if (getX().field0 == null && this.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 14)\n" +
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError
- );
-}
-
-// null analysis -- static fields accessed from Member type
-// Qualified access to static fields should also work
-public void testBug247564i_3() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- true,
- new String[] {
- "X.java",
- "public class X {\n" +
- " static Object field0;\n" +
- " static Object field1;\n" +
- " static Y getY(){ return new Y();}\n" +
- " static X getX() { return new X();}\n" +
- " static class XInner{\n" +
- " static Object xinnerfield;\n" +
- " XInner getXInner() { return new XInner();\n}" +
- " void goo(Object var) {\n" +
- " if (new Y().getY().yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (new Y().getX().field0 == null && field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && getX().field0.toString() == \"\"){}\n" + // no warn, getX() wipes out null info
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" + // warn, qualified access
- " if (getXInner().xinnerfield == null && xinnerfield.toString() == \"\"){}\n" + // warn
- " if (getXInner().xinnerfield == null && this.xinnerfield.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && this.xinnerfield.toString() == \"\"){}\n" + // no warn
- " if (getXInner().xinnerfield == null && field0.toString() == \"\"){}\n" + // no warn
- // fields from other types, don't warn
- " if (new Y().getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getX().field0 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- // qualified accesses
- " if (Y.yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (Y.xiny.field0 == null && Y.xiny.field0.toString() == \"\"){}\n" + // warn, qualified access
- " if (Y.yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (Y.xiny.field1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (X.field0 == null && X.field0.toString() == \"\"){}\n" + // warn
- " if (X.field0 == null && getX().field0.toString() == \"\"){}\n" + // no warn
- " }\n" +
- " }\n" +
- "}\n" +
- "class Y{\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX(){ return new X();}\n" +
- " static Object yField1;\n" +
- " static X xiny;\n" +
- "}"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 12)\n" +
- " if (new Y().getX().field0 == null && field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 13)\n" +
- " if (getX().field0 == null && field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 15)\n" +
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 16)\n" +
- " if (getXInner().xinnerfield == null && xinnerfield.toString() == \"\"){}\n" +
- " ^^^^^^^^^^^\n" +
- "Potential null pointer access: The field xinnerfield may be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 17)\n" +
- " if (getXInner().xinnerfield == null && this.xinnerfield.toString() == \"\"){}\n" +
- " ^^^^^^^^^^^\n" +
- "Potential null pointer access: The field xinnerfield may be null at this location\n" +
- "----------\n" +
- "6. ERROR in X.java (at line 24)\n" +
- " if (Y.xiny.field0 == null && Y.xiny.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "7. ERROR in X.java (at line 27)\n" +
- " if (X.field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError
- );
-}
-
-// null analysis -- static fields accessed from a local type
-// Qualified access to static fields should also work
-public void testBug247564i_4() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- true,
- new String[] {
- "X.java",
- "public class X {\n" +
- " static Object field0;\n" +
- " static Object field1;\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX() { return new X();}\n" +
- " void goo(Object var) {\n" +
- " class Local{\n" +
- " void localfoo(){\n " +
- " if (new Y().getY().yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (new Y().getX().field0 == null && field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && getX().field0.toString() == \"\"){}\n" + // no warn, getX() wipes out null info
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" + // warn, qualified access
- // fields from other types, don't warn
- " if (new Y().getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getX().field0 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- // qualified accesses
- " if (Y.yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (Y.xiny.field0 == null && Y.xiny.field0.toString() == \"\"){}\n" + // warn, qualified access
- " if (Y.yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (Y.xiny.field1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (X.field0 == null && X.field0.toString() == \"\"){}\n" + // warn
- " if (X.field0 == null && getX().field0.toString() == \"\"){}\n" + // no warn
- " }\n" +
- " }\n" +
- " }\n" +
- "}\n" +
- "class Y{\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX(){ return new X();}\n" +
- " static Object yField1;\n" +
- " static X xiny;\n" +
- "}"},
- null,
- compilerOptions,
- "----------\n" +
- "1. WARNING in X.java (at line 7)\n" +
- " class Local{\n" +
- " ^^^^^\n" +
- "The type Local is never used locally\n" +
- "----------\n" +
- "2. WARNING in X.java (at line 8)\n" +
- " void localfoo(){\n" +
- " ^^^^^^^^^^\n" +
- "The method localfoo() from the type Local is never used locally\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 11)\n" +
- " if (new Y().getX().field0 == null && field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 12)\n" +
- " if (getX().field0 == null && field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 14)\n" +
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "6. ERROR in X.java (at line 19)\n" +
- " if (Y.xiny.field0 == null && Y.xiny.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "7. ERROR in X.java (at line 22)\n" +
- " if (X.field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError
- );
-}
-
-// null analysis -- static fields from an anonymous type
-// Qualified access to static fields should also work
-public void testBug247564i_5() {
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- true,
- new String[] {
- "X.java",
- "interface Anon{}\n" +
- "public class X {\n" +
- " static Object field0;\n" +
- " static Object field1;\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX() { return new X();}\n" +
- " void goo(Object var) {\n" +
- " new Anon(){\n" +
- " void localfoo(){\n " +
- " if (new Y().getY().yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (new Y().getX().field0 == null && field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && getX().field0.toString() == \"\"){}\n" + // no warn, getX() wipes out null info
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" + // warn, qualified access
- // fields from other types, don't warn
- " if (new Y().getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getX().field0 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- // qualified accesses
- " if (Y.yField1 == null && field0.toString() == \"\"){}\n" + // no warn
- " if (Y.xiny.field0 == null && Y.xiny.field0.toString() == \"\"){}\n" + // warn, qualified access
- " if (Y.yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (Y.xiny.field1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (X.field0 == null && X.field0.toString() == \"\"){}\n" + // warn
- " if (X.field0 == null && getX().field0.toString() == \"\"){}\n" + // no warn
- " }\n" +
- " };\n" +
- " }\n" +
- "}\n" +
- "class Y{\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX(){ return new X();}\n" +
- " static Object yField1;\n" +
- " static X xiny;\n" +
- "}"},
- null,
- compilerOptions,
- "----------\n" +
- "1. WARNING in X.java (at line 9)\n" +
- " void localfoo(){\n" +
- " ^^^^^^^^^^\n" +
- "The method localfoo() from the type new Anon(){} is never used locally\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 12)\n" +
- " if (new Y().getX().field0 == null && field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 13)\n" +
- " if (getX().field0 == null && field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "4. ERROR in X.java (at line 15)\n" +
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "5. ERROR in X.java (at line 20)\n" +
- " if (Y.xiny.field0 == null && Y.xiny.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "6. ERROR in X.java (at line 23)\n" +
- " if (X.field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError
- );
-}
-// null analysis -- static fields
-// to check static field access from fieldReference and QualifiedReference when the type is parameterized
-public void testBug247564i_6() {
- if (this.complianceLevel < ClassFileConstants.JDK1_5) return;
- Map compilerOptions = getCompilerOptions();
- compilerOptions.put(CompilerOptions.OPTION_ReportNonStaticAccessToStatic, CompilerOptions.IGNORE);
- this.runNegativeTest(
- true,
- new String[] {
- "X.java",
- "public class X<T> {\n" +
- " static Object field0;\n" +
- " static Object field1;\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX() { return new X();}\n" +
- " void goo(Object var) {\n" +
- " if (new Y().getY().yField1 == null && X.field0.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && X.field0.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && this.field0.toString() == \"\"){}\n" + // no warn
- " if (new Y().getX().field0 == null && X.field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && this.field0.toString() == \"\"){}\n" + // warn
- " if (getX().field0 == null && getX().field0.toString() == \"\"){}\n" + // no warn, getX() wipes out null info
- // fields from other types, don't warn
- " if (new Y().getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getY().yField1 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " if (getX().field0 == null && Y.yField1.toString() == \"\"){}\n" + // no warn
- " }\n" +
- "}\n" +
- "class Y<K>{\n" +
- " Y getY(){ return new Y();}\n" +
- " X getX(){ return new X();}\n" +
- " static Object yField1;\n" +
- "}"},
- null,
- compilerOptions,
- "----------\n" +
- "1. ERROR in X.java (at line 10)\n" +
- " if (new Y().getX().field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 11)\n" +
- " if (getX().field0 == null && X.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n" +
- "3. ERROR in X.java (at line 12)\n" +
- " if (getX().field0 == null && this.field0.toString() == \"\"){}\n" +
- " ^^^^^^\n" +
- "Potential null pointer access: The field field0 may be null at this location\n" +
- "----------\n",
- JavacTestOptions.Excuse.EclipseWarningConfiguredAsError
- );
-}
-//null analysis -- simple case for field of parent type
-public void testBug247564j() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X extends Y {\n" +
- " private Object fieldx;\n" +
- " void goo(Object var) {\n" +
- " if (fieldx == null && fieldy.toString() == \"\"){}\n" + // don't flag fieldy, nothing known
- " }\n" +
- "}\n" +
- "class Y{\n" +
- " protected Object fieldy = null;\n" +
- "}\n" +
- ""},
- ""
- );
-}
-
-// null analysis -- simple case for field in try-finally
-public void testBug247564k() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " private Object f;\n" +
- " void goo(Object var) {\n" +
- " try {\n" +
- " int i = 10;\n" +
- " while (i<20){\n" +
- " if (i == 15) {\n" +
- " f = null;\n" +
- " break;\n" +
- " }\n" +
- " i++;\n" +
- " }\n" +
- " return;\n" +
- " } finally {\n" +
- " if (f != null && f.hashCode() == 0){}\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- ""
- );
-}
-
-// null analysis -- simple case for field in try-finally
-// presence or absence of throw should not affect the behaviour
-public void testBug247564k_1() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " private Object f;\n" +
- " void goo(Object var) throws Exception{\n" +
- " try {\n" +
- " int i = 10;\n" +
- " } catch(Exception e) {\n" +
- " f = null;\n" +
- " throw e;\n" +
- " } finally {\n" +
- " if (f != null && f.hashCode() == 0){}\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- ""
- );
-}
-
-// null analysis -- simple case for field in try-finally
-// presence or absence of method call in finally should not affect the behaviour
-public void testBug247564k_2() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " private Object f;\n" +
- " void gooCalls(){}\n" +
- " void goo(Object var) throws Exception{\n" +
- " try {\n" +
- " if (f != null) {}\n" +
- " } finally {\n" +
- " if (f != null ) {\n" +
- " gooCalls();\n" +
- " f.toString();\n" +
- " }\n" + // silent
- " }\n" +
- " }\n" +
- "}\n"},
- ""
- );
-}
-
-// null analysis -- simple case for constant field in try-catch-finally
-// presence or absence of method call in finally should not affect the behaviour
-public void testBug247564k_3() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " private static final Object f = null;\n" +
- " void gooCalls() throws NumberFormatException{}\n" +
- " void goo(Object var) throws Exception{\n" +
- " try {\n" +
- " gooCalls();\n" +
- " } catch(NumberFormatException e) {\n" +
- " if (f.hashCode() == 0){}\n" +
- " } finally {\n" +
- " gooCalls();\n" +
- " if (f.hashCode() == 0){}\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 8)\n" +
- " if (f.hashCode() == 0){}\n" +
- " ^\n" +
- "Null pointer access: The field f can only be null at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 11)\n" +
- " if (f.hashCode() == 0){}\n" +
- " ^\n" +
- "Null pointer access: The field f can only be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- potentially redundant checks against the same field
-public void testBug247564l_1() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " private Object f;\n" +
- " int foo() throws Exception{\n" +
- " if (f == null && f != null)\n" +
- " return 13;\n" +
- " return -13;\n" +
- " }\n" +
- " int goo() throws Exception{\n" +
- " if (f == null && f == null)\n" +
- " return 14;\n" +
- " return -14;\n" +
- " }\n" +
- " boolean hoo() throws Exception{\n" +
- " if (f == null)\n" +
- " return f != null;\n" +
- " return f == null;\n" +
- " }\n" +
- "}\n"},
- ""
- );
-}
-
-// null analysis -- fields assigned a def. non null value in a loop
-// comment 121
-public void testBug247564l_2() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- " private Object field;\n" +
- " void foo() throws Exception{\n" +
- " if (field != null) field.hashCode();\n" +
- " int i = 10;\n" +
- " while (i<20) {\n" +
- " if (field == null) field = new Object();\n" +
- " field.toString();\n" + // should not warn
- " i++;\n" +
- " }\n" +
- " }\n" +
- "}\n"},
- ""
- );
-}
-
-// null analysis -- checked and unchecked exceptions
-public void testBug247564m() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "class MyException extends Exception{}\n" +
- "public class X {\n" +
- " private Object f;\n" +
- " void gooCalls() throws MyException{}\n" +
- " void goo(){\n" +
- " try {\n" +
- " if (f == null) return;\n" +
- " gooCalls();\n" +
- " } catch(MyException e) {\n" + // checked Exception
- " f.toString();\n" + // silent - at gooCalls() in 'try', f is not going to be null
- " } catch(NumberFormatException e) {\n" + // unchecked Exception
- " f.toString();\n" + // could have come from anywhere, f can be null as doubted in 'try'
- " }\n" +
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. WARNING in X.java (at line 1)\n" +
- " class MyException extends Exception{}\n" +
- " ^^^^^^^^^^^\n" +
- "The serializable class MyException does not declare a static final serialVersionUID field of type long\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 12)\n" +
- " f.toString();\n" +
- " ^\n" +
- "Potential null pointer access: The field f may be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis -- check resetting of large no. of fields
-// https://bugs.eclipse.org/bugs/show_bug.cgi?id=247564#c161
-public void testBug247564n() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" +
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64; \n" +
- " static final Object o2 = new Object();\n" +
- " public X() {\n" +
- " field23 = null;\n" +
- " o2.toString();\n" +
- " if (field23.hashCode() == 0){}\n" + // don't warn
- " }\n" +
- "}\n"},
- ""
- );
-}
-// null analysis -- check resetting of large no. of fields
-// see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=369381
-public void testBug247564n_2() {
- this.runNegativeTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- "Object field0, \n" + // more than 64 fields
- "field1, field2, field3, field4, \n" +
- "field5, field6, field7, field8, \n" +
- "field9, field10, field11, field12, \n" +
- "field13, field14, field15, field16, \n" +
- "field17, field18, field19, field20, \n" +
- "field21, field22, field23, field24, \n" +
- "field25, field26, field27, field28, \n" +
- "field29, field30, field31, field32, \n" +
- "field33, field34, field35, field36, \n" +
- "field37, field38, field39, field40, \n" +
- "field41, field42, field43, field44, \n" +
- "field45, field46, field47, field48, \n" +
- "field49, field50, field51, field52, \n" +
- "field53, field54, field55, field56, \n" +
- "field57, field58, field59, field60, \n" +
- "field61, field62, field63, field64; \n" +
- " static final Object o2 = new Object();\n" +
- " public X() {\n" +
- " Object l0, l1, l2, l3, l4, l5, l6, l7, l8, l9,\n" + // more than 64 locals
- " l10, l11, l12, l13, l14, l15, l16, l17, l18, l19,\n" +
- " l20, l21, l22, l23, l24, l25, l26, l27, l28, l29,\n" +
- " l30, l31, l32, l33, l34, l35, l36, l37, l38, l39,\n" +
- " l40, l41, l42, l43, l44, l45, l46, l47, l48, l49,\n" +
- " l50, l51, l52, l53, l54, l55, l56, l57, l58, l59,\n" +
- " l60, l61, l62, l63, l64, l65, l66, l67, l68, l69;\n" +
- " l69 = null;\n" + // l69 has bits in extra[x][1]
- " o2.toString();\n" + // resetNullInfoForFields() must not reset any bits in extra[x][1]
- " l69.toString();\n" + // warn!
- " }\n" +
- "}\n"},
- "----------\n" +
- "1. ERROR in X.java (at line 30)\n" +
- " l69.toString();\n" +
- " ^^^\n" +
- "Null pointer access: The variable l69 can only be null at this location\n" +
- "----------\n"
- );
-}
-
-// null analysis - should not throw NPE
-// see also https://bugs.eclipse.org/bugs/show_bug.cgi?id=369381
-public void testBug369381() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X extends XParent{\n" +
- " public void foo(X x, int i) {\n" +
- " if (this.field0.length == i){}\n" + // declaring class of 'length' is null. Dont throw NPE
- " }\n" +
- "}\n" +
- "class XParent {\n" +
- " protected Object[] field0 = {new Object()}; \n" +
- "}\n"
- },
- ""
- );
-}
-
-// null analysis -- with large no. of fields, no AIOOBE should be thrown
-// no. of static final fields less than no. of final-only fields
-public void _testBug369381a() {
- this.runConformTest(
- new String[] {
- "X.java",
- "public class X {\n" +
- "static final Object field0 = null;\n " +
- "static final Object field1 = null;\n " +
- "static final Object field2 = null;\n " +
- "static final Object field3 = null;\n " +
- "static final Object field4 = null;\n " +
- "static final Object field5 = null;\n " +
- "static final Object field6 = null;\n " +
- "static final Object field7 = null;\n " +
- "static final Object field8 = null;\n " +
- "static final Object field9 = null;\n " +
- "static final Object field10 = null;\n" +
- "static final Object field11 = null;\n" +
- "static final Object field12 = null;\n" +
- "static final Object field13 = null;\n" +
- "static final Object field14 = null;\n" +
- "static final Object field15 = null;\n" +
- "static final Object field16 = null;\n" +
- "static final Object field17 = null;\n" +
- "static final Object field18 = null;\n" +
- "static final Object field19 = null;\n" +
- "static final Object field20 = null;\n" +
- "static final Object field21 = null;\n" +
- "static final Object field22 = null;\n" +
- "static final Object field23 = null;\n" +
- "static final Object field24 = null;\n" +
- "static final Object field25 = null;\n" +
- "static final Object field26 = null;\n" +
- "static final Object field27 = null;\n" +
- "static final Object field28 = null;\n" +
- "static final Object field29 = null;\n" +
- "static final Object field30 = null;\n" +
- "static final Object field31 = null;\n" +
- "static final Object field32 = null;\n" +
- "static final Object field33 = null;\n" +
- "static final Object field34 = null;\n" +
- "static final Object field35 = null;\n" +
- "static final Object field36 = null;\n" +
- "static final Object field37 = null;\n" +
- "static final Object field38 = null;\n" +
- "static final Object field39 = null;\n" +
- "static final Object field40 = null;\n" +
- "static final Object field41 = null;\n" +
- "static final Object field42 = null;\n" +
- "static final Object field43 = null;\n" +
- "static final Object field44 = null;\n" +
- "static final Object field45 = null;\n" +
- "static final Object field46 = null;\n" +
- "static final Object field47 = null;\n" +
- "static final Object field48 = null;\n" +
- "static final Object field49 = null;\n" +
- "static final Object field50 = null;\n" +
- "static final Object field51 = null;\n" +
- "static final Object field52 = null;\n" +
- "static final Object field53 = null;\n" +
- "static final Object field54 = null;\n" +
- "static final Object field55 = null;\n" +
- "static final Object field56 = null;\n" +
- "static final Object field57 = null;\n" +
- "static final Object field58 = null;\n" +
- "static final Object field59 = null;\n" +
- "static final Object field60 = null;\n" +
- "static final Object field61 = null;\n" +
- "static final Object field62 = null;\n" +
- "static final Object field63 = null;\n" +
- "static final Object field64 = null;\n" +
- "static final Object field65 = null;\n" +
- "static final Object field66 = null;\n" +
- "static final Object field67 = null;\n" +
- "static final Object field68 = null;\n" +
- "static final Object field69 = null;\n" +
- "final Object field00 = null; \n" +
- "final Object field01 = null; \n" +
- "final Object field02 = null; \n" +
- "final Object field03 = null; \n" +
- "final Object field04 = null; \n" +
- "final Object field05 = null; \n" +
- "final Object field06 = null; \n" +
- "final Object field07 = null; \n" +
- "final Object field08 = null; \n" +
- "final Object field09 = null; \n" +
- "final Object field010 = null;\n" +
- "final Object field011 = null;\n" +
- "final Object field012 = null;\n" +
- "final Object field013 = null;\n" +
- "final Object field014 = null;\n" +
- "final Object field015 = null;\n" +
- "final Object field016 = null;\n" +
- "final Object field017 = null;\n" +
- "final Object field018 = null;\n" +
- "final Object field019 = null;\n" +
- "final Object field020 = null;\n" +
- "final Object field021 = null;\n" +
- "final Object field022 = null;\n" +
- "final Object field023 = null;\n" +
- "final Object field024 = null;\n" +
- "final Object field025 = null;\n" +
- "final Object field026 = null;\n" +
- "final Object field027 = null;\n" +
- "final Object field028 = null;\n" +
- "final Object field029 = null;\n" +
- "final Object field030 = null;\n" +
- "final Object field031 = null;\n" +
- "final Object field032 = null;\n" +
- "final Object field033 = null;\n" +
- "final Object field034 = null;\n" +
- "final Object field035 = null;\n" +
- "final Object field036 = null;\n" +
- "final Object field037 = null;\n" +
- "final Object field038 = null;\n" +
- "final Object field039 = null;\n" +
- "final Object field040 = null;\n" +
- "final Object field041 = null;\n" +
- "final Object field042 = null;\n" +
- "final Object field043 = null;\n" +
- "final Object field044 = null;\n" +
- "final Object field045 = null;\n" +
- "final Object field046 = null;\n" +
- "final Object field047 = null;\n" +
- "final Object field048 = null;\n" +
- "final Object field049 = null;\n" +
- "final Object field050 = null;\n" +
- "final Object field051 = null;\n" +
- "final Object field052 = null;\n" +
- "final Object field053 = null;\n" +
- "final Object field054 = null;\n" +
- "final Object field055 = null;\n" +
- "final Object field056 = null;\n" +
- "final Object field057 = null;\n" +
- "final Object field058 = null;\n" +
- "final Object field059 = null;\n" +
- "final Object field060 = null;\n" +
- "final Object field061 = null;\n" +
- "final Object field062 = null;\n" +
- "final Object field063 = null;\n" +
- "final Object field064 = null;\n" +
- "final Object field065 = null;\n" +
- "final Object field066 = null;\n" +
- "final Object field067 = null;\n" +
- "final Object field068 = null;\n" +
- "final Object field069 = null;\n" +
- " public X() {\n" +
- " }\n" +
- "}\n"},
- ""
- );
-}
-}
+} \ No newline at end of file
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java
index c587eaf6c..beeb07b6f 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java
@@ -26,7 +26,7 @@ import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
public class ResourceLeakTests extends AbstractRegressionTest {
static {
-// TESTS_NAMES = new String[] { "testBug368709"};
+// TESTS_NAMES = new String[] { "test066"};
// TESTS_NUMBERS = new int[] { 50 };
// TESTS_RANGE = new int[] { 11, -1 };
}
@@ -1489,7 +1489,7 @@ public void test056y() {
" final FileReader reader23 = new FileReader(\"file\");\n" +
" provider = new ResourceProvider() {\n" +
" public FileReader provide() {\n" +
- " return reader23;\n" +
+ " return reader23;\n" + // responsibility now lies at the caller of this method
" }\n" +
" };\n" +
" }\n" +
@@ -1500,11 +1500,6 @@ public void test056y() {
" final FileReader reader31 = new FileReader(\"file\");\n" +
" ^^^^^^^^\n" +
"Potential resource leak: 'reader31' may not be closed\n" +
- "----------\n" +
- "2. WARNING in X.java (at line 17)\n" +
- " final FileReader reader23 = new FileReader(\"file\");\n" +
- " ^^^^^^^^\n" +
- "Potential resource leak: 'reader23' may not be closed\n" +
"----------\n",
null,
true,
@@ -3143,12 +3138,7 @@ public void testBug368709a() {
"}\n"
},
"----------\n" +
- "1. ERROR in X.java (at line 15)\n" +
- " return wc.open(getObjectId(), type).openStream();\n" +
- " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
- "Resource leak: \'in\' is not closed at this location\n" +
- "----------\n" +
- "2. ERROR in X.java (at line 18)\n" +
+ "1. ERROR in X.java (at line 18)\n" +
" return new ObjectStream.Filter(type, size, in);\n" +
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
"Potential resource leak: \'in\' may not be closed at this location\n" +
@@ -3189,4 +3179,535 @@ public void testBug368709b() {
true,
options);
}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// example from comment 3
+public void test064() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ this.runNegativeTest(new String[] {
+ "Test064.java",
+ "import java.io.*;\n" +
+ "public class Test064 {\n" +
+ " void foo(File outfile) {\n" +
+ " OutputStream out= System.out;\n" +
+ " if (outfile != null) {\n" +
+ " try {\n" +
+ " out = new FileOutputStream(outfile);\n" +
+ " } catch (java.io.IOException e) {\n" +
+ " throw new RuntimeException(e);\n" +
+ " }\n" +
+ " }\n" +
+ " setOutput(out);\n" +
+ " }\n" +
+ " private void setOutput(OutputStream out) { }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in Test064.java (at line 7)\n" +
+ " out = new FileOutputStream(outfile);\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Potential resource leak: \'out\' may not be closed\n" +
+ "----------\n",
+ null,
+ true,
+ options);
+}
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// example from comment 10
+// disabled, because basic null-analysis machinery doesn't support this pattern
+// see also Bug 370424 - [compiler][null] throw-catch analysis for null flow could be more precise
+public void _test065() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runConformTest(new String[] {
+ "Test065.java",
+ "import java.io.*;\n" +
+ "class MyException extends Exception{}\n" +
+ "public class Test065 {\n" +
+ " void foo(String fileName) throws IOException, MyException {\n" +
+ " FileReader fileRead = new FileReader(fileName);\n" +
+ " BufferedReader bufRead = new BufferedReader(fileRead);\n" +
+ " LineNumberReader lineReader = new LineNumberReader(bufRead);\n" +
+ " try {\n" +
+ " while (lineReader.readLine() != null) {\n" +
+ " bufRead.close();\n" +
+ " callSome(); // only this can throw MyException\n" +
+ " }\n" +
+ " } catch (MyException e) {\n" +
+ " throw e; // Pot. leak reported here\n" +
+ " }\n" +
+ " bufRead.close(); \n" +
+ " }\n" +
+ " private void callSome() throws MyException\n" +
+ " {\n" +
+ " \n" +
+ " }\n" +
+ "}\n"
+ },
+ "",
+ null,
+ true,
+ null,
+ options,
+ null);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// example from comment 11
+public void test066() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runNegativeTest(new String[] {
+ "Test066.java",
+ "import java.io.*;\n" +
+ "class MyException extends Exception{}\n" +
+ "public class Test066 {\n" +
+ " void countFileLines(String fileName) throws IOException {\n" +
+ " FileReader fileRead = new FileReader(fileName);\n" +
+ " BufferedReader bufRead = new BufferedReader(fileRead);\n" +
+ " LineNumberReader lineReader = new LineNumberReader(bufRead);\n" +
+ " while (lineReader.readLine() != null) {\n" +
+ " if (lineReader.markSupported())\n" +
+ " throw new IOException();\n" +
+ " bufRead.close();\n" +
+ " }\n" +
+ " bufRead.close();\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in Test066.java (at line 10)\n" +
+ " throw new IOException();\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Potential resource leak: \'lineReader\' may not be closed at this location\n" +
+ "----------\n",
+ null,
+ true,
+ options);
+}
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// example from comment 11 - variant with closing top-level resource
+public void test066b() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runNegativeTest(new String[] {
+ "Test066.java",
+ "import java.io.*;\n" +
+ "class MyException extends Exception{}\n" +
+ "public class Test066 {\n" +
+ " void countFileLines(String fileName) throws IOException {\n" +
+ " FileReader fileRead = new FileReader(fileName);\n" +
+ " BufferedReader bufRead = new BufferedReader(fileRead);\n" +
+ " LineNumberReader lineReader = new LineNumberReader(bufRead);\n" +
+ " while (lineReader.readLine() != null) {\n" +
+ " if (lineReader.markSupported())\n" +
+ " throw new IOException();\n" +
+ " lineReader.close();\n" +
+ " }\n" +
+ " lineReader.close();\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in Test066.java (at line 10)\n" +
+ " throw new IOException();\n" +
+ " ^^^^^^^^^^^^^^^^^^^^^^^^\n" +
+ "Potential resource leak: \'lineReader\' may not be closed at this location\n" +
+ "----------\n",
+ null,
+ true,
+ options);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// example from comment 12
+// disabled because null info after try-catch is too weak,
+// see also Bug 370424 - [compiler][null] throw-catch analysis for null flow could be more precise
+public void _test067() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runConformTest(new String[] {
+ "Test067.java",
+ "import java.io.*;\n" +
+ "public class Test067 {\n" +
+ " public void comment12() throws IOException {\n" +
+ " LineNumberReader o = null;\n" +
+ " try {\n" +
+ " o = new LineNumberReader(null); \n" +
+ " } catch (NumberFormatException e) { \n" +
+ " }\n" +
+ " }\n" +
+ "}\n"
+ },
+ "",
+ null,
+ true,
+ null,
+ options,
+ null);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// example from comment 12
+// disabled because null info after try-catch is too weak,
+// see also Bug 370424 - [compiler][null] throw-catch analysis for null flow could be more precise
+public void _test067b() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runConformTest(new String[] {
+ "Test067.java",
+ "import java.io.*;\n" +
+ "public class Test067 {\n" +
+ " public void comment12b() throws IOException {\n" +
+ " LineNumberReader o = new LineNumberReader(null);\n" +
+ " try {\n" +
+ " o.close();\n" +
+ " } catch (NumberFormatException e) {\n" +
+ " }\n" +
+ " }\n" +
+ "}\n"
+ },
+ "",
+ null,
+ true,
+ null,
+ options,
+ null);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// example from comment 13
+public void test068() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runConformTest(new String[] {
+ "Test068.java",
+ "import java.io.*;\n" +
+ "public class Test068 {\n" +
+ " class ProcessingStep extends OutputStream {\n" +
+ " public void write(int b) throws IOException {}\n" +
+ " public OutputStream getDestination() { return null; }\n" +
+ " }\n" +
+ " class ArtifactOutputStream extends OutputStream {\n" +
+ " public void write(int b) throws IOException {}\n" +
+ " }" +
+ " ArtifactOutputStream comment13(OutputStream stream) {\n" +
+ " OutputStream current = stream;\n" +
+ " while (current instanceof ProcessingStep)\n" +
+ " current = ((ProcessingStep) current).getDestination();\n" + // we previously saw a bogus warning here.
+ " if (current instanceof ArtifactOutputStream)\n" +
+ " return (ArtifactOutputStream) current;\n" +
+ " return null;\n" +
+ " }\n" +
+ "}\n"
+ },
+ "",
+ null,
+ true,
+ null,
+ options,
+ null);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// example from comment 16
+public void test069() {
+ if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // generics used
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runConformTest(new String[] {
+ "Test069.java",
+ "import java.io.*;\n" +
+ "import java.util.Collection;\n" +
+ "public class Test069 {\n" +
+ " class Profile {}\n" +
+ " class CoreException extends Exception {}\n" +
+ " void writeProfilesToStream(Collection<Profile> p, OutputStream s, String enc) {}\n" +
+ " CoreException createException(IOException ioex, String message) { return new CoreException(); }\n" +
+ " public void comment16(Collection<Profile> profiles, File file, String encoding) throws CoreException {\n" +
+ " final OutputStream stream;\n" +
+ " try {\n" +
+ " stream= new FileOutputStream(file);\n" +
+ " try {\n" +
+ " writeProfilesToStream(profiles, stream, encoding);\n" +
+ " } finally {\n" +
+ " try { stream.close(); } catch (IOException e) { /* ignore */ }\n" +
+ " }\n" +
+ " } catch (IOException e) {\n" +
+ " throw createException(e, \"message\"); // should not shout here\n" +
+ " }\n" +
+ " }\n" +
+ "}\n"
+ },
+ "",
+ null,
+ true,
+ null,
+ options,
+ null);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// referenced in array initializer
+public void test070() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runNegativeTest(new String[] {
+ "Test070.java",
+ "import java.io.*;\n" +
+ "public class Test070 {\n" +
+ " void storeInArray(String fileName) throws IOException {\n" +
+ " FileReader fileRead = new FileReader(fileName);\n" +
+ " closeThemAll(new FileReader[] { fileRead });\n" +
+ " }\n" +
+ " void closeThemAll(FileReader[] readers) { }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in Test070.java (at line 4)\n" +
+ " FileReader fileRead = new FileReader(fileName);\n" +
+ " ^^^^^^^^\n" +
+ "Potential resource leak: \'fileRead\' may not be closed\n" +
+ "----------\n",
+ null,
+ true,
+ options);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// referenced in array initializer
+public void test071() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runNegativeTest(new String[] {
+ "Test071.java",
+ "import java.io.*;\n" +
+ "public class Test071 {\n" +
+ " class ReaderHolder {\n" +
+ " FileReader reader;\n" +
+ " }\n" +
+ " private FileReader getReader() {\n" +
+ " return null;\n" +
+ " }\n" +
+ " void invokeCompiler(ReaderHolder readerHolder, boolean flag) throws FileNotFoundException {\n" +
+ " FileReader reader = readerHolder.reader;\n" +
+ " if (reader == null)\n" +
+ " reader = getReader();\n" +
+ " try {\n" +
+ " return;\n" +
+ " } finally {\n" +
+ " try {\n" +
+ " if (flag)\n" +
+ " reader.close();\n" +
+ " } catch (IOException e) {\n" +
+ " // nop\n" +
+ " }\n" +
+ " }\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in Test071.java (at line 14)\n" +
+ " return;\n" +
+ " ^^^^^^^\n" +
+ "Potential resource leak: \'reader\' may not be closed at this location\n" +
+ "----------\n",
+ null,
+ true,
+ options);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// referenced in array initializer
+// disabled because it would require correlation analysis between the tracking variable and its original
+// need to pass to downstream: either (nonnull & open) or (null)
+public void _test071b() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runNegativeTest(new String[] {
+ "Test071b.java",
+ "import java.io.*;\n" +
+ "public class Test071b {\n" +
+ " private FileReader getReader() {\n" +
+ " return null;\n" +
+ " }\n" +
+ " void invokeCompiler(boolean flag) throws FileNotFoundException {\n" +
+ " FileReader reader = null;\n" +
+ " if (flag)\n" +
+ " reader = new FileReader(\"file\");\n" +
+ " if (reader == null)\n" +
+ " reader = getReader();\n" +
+ " try {\n" +
+ " return;\n" +
+ " } finally {\n" +
+ " try {\n" +
+ " if (flag)\n" +
+ " reader.close();\n" +
+ " } catch (IOException e) {\n" +
+ " // nop\n" +
+ " }\n" +
+ " }\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in Test071b.java (at line 13)\n" +
+ " return;\n" +
+ " ^^^^^^^\n" +
+ "Potential resource leak: \'reader\' may not be closed at this location\n" +
+ "----------\n",
+ null,
+ true,
+ options);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// throw inside loop inside try - while closed in finally
+public void test072() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runConformTest(new String[] {
+ "Test072.java",
+ "import java.io.*;\n" +
+ "public class Test072 {\n" +
+ " void readState(File file) {\n" +
+ " DataInputStream in = null;\n" +
+ " try {\n" +
+ " in= new DataInputStream(new BufferedInputStream(new FileInputStream(file)));\n" +
+ " int sizeOfFlags = in.readInt();\n" +
+ " for (int i = 0; i < sizeOfFlags; ++i) {\n" +
+ " String childPath = in.readUTF();\n" +
+ " if (childPath.length() == 0)\n" +
+ " throw new IOException();\n" +
+ " }\n" +
+ " }\n" +
+ " catch (IOException ioe) { /* nop */ }\n" +
+ " finally {\n" +
+ " if (in != null) {\n" +
+ " try {in.close();} catch (IOException ioe) {}\n" +
+ " }\n" +
+ " }\n" +
+ " }\n" +
+ "}\n"
+ },
+ "",
+ null,
+ true,
+ null,
+ options,
+ null);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// unspecific parameter is casted into a resource, yet need to mark as OWNED_BY_OUTSIDE
+public void test073() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runConformTest(new String[] {
+ "Test073.java",
+ "import java.io.*;\n" +
+ "public class Test073 {\n" +
+ " String getEncoding(Object reader) {\n" +
+ " if (reader instanceof FileReader) {\n" +
+ " final FileReader fr = (FileReader) reader;\n" +
+ " return fr.getEncoding();\n" +
+ " }\n" +
+ " return null;\n" +
+ " }\n" +
+ "}\n"
+ },
+ "",
+ null,
+ true,
+ null,
+ options,
+ null);
+}
+
+// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK
+// status after nested try-finally
+public void test074() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE);
+ this.runNegativeTest(new String[] {
+ "Test074.java",
+ "import java.io.*;\n" +
+ "public class Test074 {\n" +
+ " void foo() throws FileNotFoundException {\n" +
+ " FileOutputStream out = null;\n" +
+ " try {\n" +
+ " out = new FileOutputStream(\"outfile\");\n" +
+ " } finally {\n" +
+ " try {\n" +
+ " out.flush();\n" +
+ " out.close();\n" +
+ " } catch (IOException e) {\n" +
+ " e.printStackTrace();\n" +
+ " }\n" +
+ " out = null;\n" + // unclosed if exception occurred on flush()
+ " }\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. ERROR in Test074.java (at line 14)\n" +
+ " out = null;\n" +
+ " ^^^^^^^^^^\n" +
+ "Potential resource leak: \'out\' may not be closed at this location\n" +
+ "----------\n",
+ null,
+ true,
+ options);
+}
+// Bug 370639 - [compiler][resource] restore the default for resource leak warnings
+// check that the default is warning
+public void test075() {
+ this.runNegativeTest(
+ new String[] {
+ "X.java",
+ "import java.io.File;\n" +
+ "import java.io.FileReader;\n" +
+ "import java.io.IOException;\n" +
+ "public class X {\n" +
+ " void foo() throws IOException {\n" +
+ " File file = new File(\"somefile\");\n" +
+ " FileReader fileReader = new FileReader(file);\n" +
+ " }\n" +
+ "}\n"
+ },
+ "----------\n" +
+ "1. WARNING in X.java (at line 7)\n" +
+ " FileReader fileReader = new FileReader(file);\n" +
+ " ^^^^^^^^^^\n" +
+ "Resource leak: 'fileReader' is never closed\n" +
+ "----------\n");
+}
}
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/TestVerifier.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/TestVerifier.java
index 6f4e1fde3..cdba7bab9 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/TestVerifier.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/TestVerifier.java
@@ -23,7 +23,7 @@ public class TestVerifier {
boolean reuseVM = true;
//{ObjectTeams: support reuse even in presence of vmargs, if unchanged:
- protected String[] vmArguments = null;
+ protected String[] fVMArguments = null;
// SH}
String[] classpathCache;
LocalVirtualMachine vm;
@@ -119,7 +119,7 @@ private void compileVerifyTests(String verifierDir) {
}
String fileName = dir + File.separator + simpleName + ".java";
Util.writeToFile(getVerifyTestsCode(), fileName);
- BatchCompiler.compile("\"" + fileName + "\" -d \"" + verifierDir + "\" -classpath \"" + Util.getJavaClassLibsAsString() + "\" -warn:-resource", new PrintWriter(System.out), new PrintWriter(System.err), null/*progress*/);
+ BatchCompiler.compile("\"" + fileName + "\" -d \"" + verifierDir + "\" -warn:-resource -classpath \"" + Util.getJavaClassLibsAsString() + "\"", new PrintWriter(System.out), new PrintWriter(System.err), null/*progress*/);
}
public void execute(String className, String[] classpaths) {
this.outputBuffer = new StringBuffer();
@@ -148,236 +148,257 @@ public String getExecutionError(){
*/
private String getVerifyTestsCode() {
return
- "/*******************************************************************************" +
- " * Copyright (c) 2000, 2005 IBM Corporation and others." +
- " * All rights reserved. This program and the accompanying materials" +
- " * are made available under the terms of the Eclipse Public License v1.0" +
- " * which accompanies this distribution, and is available at" +
- " * http://www.eclipse.org/legal/epl-v10.html" +
- " *" +
- " * Contributors:" +
- " * IBM Corporation - initial API and implementation" +
- " *******************************************************************************/" +
- "package org.eclipse.jdt.core.tests.util;\n" +
- "\n" +
- "import java.lang.reflect.*;\n" +
- "import java.io.*;\n" +
- "import java.net.*;\n" +
- "import java.util.*;\n" +
- "\n" +
- "/******************************************************\n" +
- " * \n" +
- " * IMPORTANT NOTE: If modifying this class, copy the source to TestVerifier#getVerifyTestsCode()\n" +
- " * (see this method for details)\n" +
- " * \n" +
- " ******************************************************/\n" +
- "\n" +
- "public class VerifyTests {\n" +
- " int portNumber;\n" +
- " Socket socket;\n" +
- "\n" +
- "/**\n" +
- " * NOTE: Code copied from junit.util.TestCaseClassLoader.\n" +
- " *\n" +
- " * A custom class loader which enables the reloading\n" +
- " * of classes for each test run. The class loader\n" +
- " * can be configured with a list of package paths that\n" +
- " * should be excluded from loading. The loading\n" +
- " * of these packages is delegated to the system class\n" +
- " * loader. They will be shared across test runs.\n" +
- " * <p>\n" +
- " * The list of excluded package paths is specified in\n" +
- " * a properties file \"excluded.properties\" that is located in \n" +
- " * the same place as the TestCaseClassLoader class.\n" +
- " * <p>\n" +
- " * <b>Known limitation:</b> the VerifyClassLoader cannot load classes\n" +
- " * from jar files.\n" +
- " */\n" +
- "\n" +
- "\n" +
- "public class VerifyClassLoader extends ClassLoader {\n" +
- " /** scanned class path */\n" +
- " private String[] fPathItems;\n" +
- " \n" +
- " /** excluded paths */\n" +
- " private String[] fExcluded= {};\n" +
- "\n" +
- " /**\n" +
- " * Constructs a VerifyClassLoader. It scans the class path\n" +
- " * and the excluded package paths\n" +
- " */\n" +
- " public VerifyClassLoader() {\n" +
- " super();\n" +
- " String classPath= System.getProperty(\"java.class.path\");\n" +
- " String separator= System.getProperty(\"path.separator\");\n" +
- " \n" +
- " // first pass: count elements\n" +
- " StringTokenizer st= new StringTokenizer(classPath, separator);\n" +
- " int i= 0;\n" +
- " while (st.hasMoreTokens()) {\n" +
- " st.nextToken();\n" +
- " i++;\n" +
- " }\n" +
- " // second pass: split\n" +
- " fPathItems= new String[i];\n" +
- " st= new StringTokenizer(classPath, separator);\n" +
- " i= 0;\n" +
- " while (st.hasMoreTokens()) {\n" +
- " fPathItems[i++]= st.nextToken();\n" +
- " }\n" +
- "\n" +
- " }\n" +
- " public java.net.URL getResource(String name) {\n" +
- " return ClassLoader.getSystemResource(name);\n" +
- " }\n" +
- " public InputStream getResourceAsStream(String name) {\n" +
- " return ClassLoader.getSystemResourceAsStream(name);\n" +
- " }\n" +
- " protected boolean isExcluded(String name) {\n" +
- " // exclude the \"java\" packages.\n" +
- " // They always need to be excluded so that they are loaded by the system class loader\n" +
- " if (name.startsWith(\"java\"))\n" +
+ "/*******************************************************************************\n" +
+ " * Copyright (c) 2000, 2011 IBM Corporation and others.\n" +
+ " * All rights reserved. This program and the accompanying materials\n" +
+ " * are made available under the terms of the Eclipse Public License v1.0\n" +
+ " * which accompanies this distribution, and is available at\n" +
+ " * http://www.eclipse.org/legal/epl-v10.html\n" +
+ " *\n" +
+ " * Contributors:\n" +
+ " * IBM Corporation - initial API and implementation\n" +
+ " *******************************************************************************/\n" +
+ "package org.eclipse.jdt.core.tests.util;\n" +
+ "\n" +
+ "import java.io.DataInputStream;\n" +
+ "import java.io.DataOutputStream;\n" +
+ "import java.io.File;\n" +
+ "import java.io.FileInputStream;\n" +
+ "import java.io.FileNotFoundException;\n" +
+ "import java.io.IOException;\n" +
+ "import java.io.InputStream;\n" +
+ "import java.lang.reflect.InvocationTargetException;\n" +
+ "import java.lang.reflect.Method;\n" +
+ "import java.net.ServerSocket;\n" +
+ "import java.net.Socket;\n" +
+ "import java.util.StringTokenizer;\n" +
+ "\n" +
+ "/******************************************************\n" +
+ " *\n" +
+ " * IMPORTANT NOTE: If modifying this class, copy the source to TestVerifier#getVerifyTestsCode()\n" +
+ " * (see this method for details)\n" +
+ " *\n" +
+ " ******************************************************/\n" +
+ "\n" +
+ "public class VerifyTests {\n" +
+ " int portNumber;\n" +
+ " Socket socket;\n" +
+ "\n" +
+ "/**\n" +
+ " * NOTE: Code copied from junit.util.TestCaseClassLoader.\n" +
+ " *\n" +
+ " * A custom class loader which enables the reloading\n" +
+ " * of classes for each test run. The class loader\n" +
+ " * can be configured with a list of package paths that\n" +
+ " * should be excluded from loading. The loading\n" +
+ " * of these packages is delegated to the system class\n" +
+ " * loader. They will be shared across test runs.\n" +
+ " * <p>\n" +
+ " * The list of excluded package paths is specified in\n" +
+ " * a properties file \"excluded.properties\" that is located in\n" +
+ " * the same place as the TestCaseClassLoader class.\n" +
+ " * <p>\n" +
+ " * <b>Known limitation:</b> the VerifyClassLoader cannot load classes\n" +
+ " * from jar files.\n" +
+ " */\n" +
+ "\n" +
+ "\n" +
+ "public class VerifyClassLoader extends ClassLoader {\n" +
+ " /** scanned class path */\n" +
+ " private String[] pathItems;\n" +
+ "\n" +
+ " /** excluded paths */\n" +
+ " private String[] excluded= {};\n" +
+ "\n" +
+ " /**\n" +
+ " * Constructs a VerifyClassLoader. It scans the class path\n" +
+ " * and the excluded package paths\n" +
+ " */\n" +
+ " public VerifyClassLoader() {\n" +
+ " super();\n" +
+ " String classPath= System.getProperty(\"java.class.path\");\n" +
+ " String separator= System.getProperty(\"path.separator\");\n" +
+ "\n" +
+ " // first pass: count elements\n" +
+ " StringTokenizer st= new StringTokenizer(classPath, separator);\n" +
+ " int i= 0;\n" +
+ " while (st.hasMoreTokens()) {\n" +
+ " st.nextToken();\n" +
+ " i++;\n" +
+ " }\n" +
+ " // second pass: split\n" +
+ " this.pathItems= new String[i];\n" +
+ " st= new StringTokenizer(classPath, separator);\n" +
+ " i= 0;\n" +
+ " while (st.hasMoreTokens()) {\n" +
+ " this.pathItems[i++]= st.nextToken();\n" +
+ " }\n" +
+ "\n" +
+ " }\n" +
+ " public java.net.URL getResource(String name) {\n" +
+ " return ClassLoader.getSystemResource(name);\n" +
+ " }\n" +
+ " public InputStream getResourceAsStream(String name) {\n" +
+ " return ClassLoader.getSystemResourceAsStream(name);\n" +
+ " }\n" +
+ " protected boolean isExcluded(String name) {\n" +
+ " // exclude the \"java\" packages.\n" +
+ " // They always need to be excluded so that they are loaded by the system class loader\n" +
+ " if (name.startsWith(\"java\"))\n" +
+ " return true;\n" +
+ "\n" +
+//{ObjectTeams: don't process core OT-classes either (unpacked .class not available on classpath):
+ " if (name.startsWith(\"org.objectteams\"))\n" +
" return true;\n" +
" \n" +
-//{ObjectTeams: don't process core OT-classes either (unpacked .class not available on classpath):
-" if (name.startsWith(\"org.objectteams\"))\n" +
-" return true;\n" +
-" \n" +
-" if (name.startsWith(\"org.eclipse.objectteams.otre\"))\n" +
-" return true;\n" +
-" \n" +
-" if (name.startsWith(\"org.apache.bcel\"))\n" +
-" return true;\n" +
-" \n" +
-" if (name.startsWith(\"sun\"))\n" + // have a test that needs sun/reflect/SerializationConstructorAccessorImpl
-" return true;\n" +
-" \n" +
-// SH}
- " // exclude the user defined package paths\n" +
- " for (int i= 0; i < fExcluded.length; i++) {\n" +
- " if (name.startsWith(fExcluded[i])) {\n" +
- " return true;\n" +
- " }\n" +
- " }\n" +
- " return false; \n" +
- " }\n" +
- " public synchronized Class loadClass(String name, boolean resolve)\n" +
- " throws ClassNotFoundException {\n" +
+ " if (name.startsWith(\"org.eclipse.objectteams.otre\"))\n" +
+ " return true;\n" +
" \n" +
- " Class c= findLoadedClass(name);\n" +
- " if (c != null)\n" +
- " return c;\n" +
- " //\n" +
- " // Delegate the loading of excluded classes to the\n" +
- " // standard class loader.\n" +
- " //\n" +
- " if (isExcluded(name)) {\n" +
- " try {\n" +
- " c= findSystemClass(name);\n" +
- " return c;\n" +
- " } catch (ClassNotFoundException e) {\n" +
- " // keep searching\n" +
- " }\n" +
- " }\n" +
- " File file= locate(name);\n" +
- " if (file == null)\n" +
- " throw new ClassNotFoundException();\n" +
- " byte data[]= loadClassData(file);\n" +
- " c= defineClass(name, data, 0, data.length);\n" +
- " if (resolve) \n" +
- " resolveClass(c);\n" +
- " return c;\n" +
- " }\n" +
- " private byte[] loadClassData(File f) throws ClassNotFoundException {\n" +
- " try {\n" +
- " //System.out.println(\"loading: \"+f.getPath());\n" +
- " FileInputStream stream= new FileInputStream(f);\n" +
+ " if (name.startsWith(\"org.apache.bcel\"))\n" +
+ " return true;\n" +
+ " \n" +
+ " if (name.startsWith(\"sun\"))\n" + // have a test that needs sun/reflect/SerializationConstructorAccessorImpl
+ " return true;\n" +
" \n" +
- " try {\n" +
- " byte[] b= new byte[stream.available()];\n" +
- " stream.read(b);\n" +
- " stream.close();\n" +
- " return b;\n" +
- " }\n" +
- " catch (IOException e) {\n" +
- " throw new ClassNotFoundException();\n" +
- " }\n" +
- " }\n" +
- " catch (FileNotFoundException e) {\n" +
- " throw new ClassNotFoundException();\n" +
- " }\n" +
- " }\n" +
- " /**\n" +
- " * Locate the given file.\n" +
- " * @return Returns null if file couldn\'t be found.\n" +
- " */\n" +
- " private File locate(String fileName) { \n" +
- " if (fileName != null) {\n" +
- " fileName= fileName.replace(\'.\', \'/\')+\".class\";\n" +
- " File path= null;\n" +
- " for (int i= 0; i < fPathItems.length; i++) {\n" +
- " path= new File(fPathItems[i], fileName);\n" +
- " if (path.exists())\n" +
- " return path;\n" +
- " }\n" +
- " }\n" +
- " return null;\n" +
- " }\n" +
- "}\n" +
- " \n" +
- "public void loadAndRun(String className) throws Throwable {\n" +
- " //System.out.println(\"Loading \" + className + \"...\");\n" +
- " Class testClass = new VerifyClassLoader().loadClass(className);\n" +
- " //System.out.println(\"Loaded \" + className);\n" +
- " try {\n" +
- " Method main = testClass.getMethod(\"main\", new Class[] {String[].class});\n" +
- " //System.out.println(\"Running \" + className);\n" +
- " main.invoke(null, new Object[] {new String[] {}});\n" +
- " //System.out.println(\"Finished running \" + className);\n" +
- " } catch (NoSuchMethodException e) {\n" +
- " return;\n" +
- " } catch (InvocationTargetException e) {\n" +
- " throw e.getTargetException();\n" +
- " }\n" +
- "}\n" +
- "public static void main(String[] args) throws IOException {\n" +
- " VerifyTests verify = new VerifyTests();\n" +
- " verify.portNumber = Integer.parseInt(args[0]);\n" +
- " verify.run();\n" +
- "}\n" +
- "public void run() throws IOException {\n" +
- " ServerSocket server = new ServerSocket(this.portNumber);\n" +
- " this.socket = server.accept();\n" +
- " this.socket.setTcpNoDelay(true);\n" +
- " server.close();\n" +
- "\n" +
- " final DataInputStream in = new DataInputStream(this.socket.getInputStream());\n" +
- " final DataOutputStream out = new DataOutputStream(this.socket.getOutputStream());\n" +
- " while (true) {\n" +
- " final String className = in.readUTF();\n" +
- " Thread thread = new Thread() {\n" +
- " public void run() {\n" +
- " try {\n" +
- " loadAndRun(className);\n" +
- " out.writeBoolean(true);\n" +
- " System.err.println(VerifyTests.class.getName());\n" +
- " System.out.println(VerifyTests.class.getName());\n" +
- " } catch (Throwable e) {\n" +
- " e.printStackTrace();\n" +
- " try {\n" +
- " System.err.println(VerifyTests.class.getName());\n" +
- " System.out.println(VerifyTests.class.getName());\n" +
- " out.writeBoolean(false);\n" +
- " } catch (IOException e1) {\n" +
- " // ignore\n" +
- " }\n" +
- " }\n" +
- " }\n" +
- " };\n" +
- " thread.start();\n" +
- " }\n" +
- "}\n" +
- "}\n";
+// SH}
+ " // exclude the user defined package paths\n" +
+ " for (int i= 0; i < this.excluded.length; i++) {\n" +
+ " if (name.startsWith(this.excluded[i])) {\n" +
+ " return true;\n" +
+ " }\n" +
+ " }\n" +
+ " return false;\n" +
+ " }\n" +
+ " public synchronized Class loadClass(String name, boolean resolve)\n" +
+ " throws ClassNotFoundException {\n" +
+ "\n" +
+ " Class c= findLoadedClass(name);\n" +
+ " if (c != null)\n" +
+ " return c;\n" +
+ " //\n" +
+ " // Delegate the loading of excluded classes to the\n" +
+ " // standard class loader.\n" +
+ " //\n" +
+ " if (isExcluded(name)) {\n" +
+ " try {\n" +
+ " c= findSystemClass(name);\n" +
+ " return c;\n" +
+ " } catch (ClassNotFoundException e) {\n" +
+ " // keep searching\n" +
+ " }\n" +
+ " }\n" +
+ " File file= locate(name);\n" +
+ " if (file == null)\n" +
+ " throw new ClassNotFoundException();\n" +
+ " byte data[]= loadClassData(file);\n" +
+ " c= defineClass(name, data, 0, data.length);\n" +
+ " if (resolve)\n" +
+ " resolveClass(c);\n" +
+ " return c;\n" +
+ " }\n" +
+ " private byte[] loadClassData(File f) throws ClassNotFoundException {\n" +
+ " FileInputStream stream = null;\n" +
+ " try {\n" +
+ " //System.out.println(\"loading: \"+f.getPath());\n" +
+ " stream = new FileInputStream(f);\n" +
+ "\n" +
+ " try {\n" +
+ " byte[] b= new byte[stream.available()];\n" +
+ " stream.read(b);\n" +
+ " return b;\n" +
+ " }\n" +
+ " catch (IOException e) {\n" +
+ " throw new ClassNotFoundException();\n" +
+ " }\n" +
+ " }\n" +
+ " catch (FileNotFoundException e) {\n" +
+ " throw new ClassNotFoundException();\n" +
+ " } finally {\n" +
+ " if (stream != null) {\n" +
+ " try {\n" +
+ " stream.close();\n" +
+ " } catch (IOException e) {\n" +
+ " /* ignore */\n" +
+ " }\n" +
+ " }\n" +
+ " }\n" +
+ " }\n" +
+ " /**\n" +
+ " * Locate the given file.\n" +
+ " * @return Returns null if file couldn't be found.\n" +
+ " */\n" +
+ " private File locate(String fileName) {\n" +
+ " if (fileName != null) {\n" +
+ " fileName= fileName.replace('.', '/')+\".class\";\n" +
+ " File path= null;\n" +
+ " for (int i= 0; i < this.pathItems.length; i++) {\n" +
+ " path= new File(this.pathItems[i], fileName);\n" +
+ " if (path.exists())\n" +
+ " return path;\n" +
+ " }\n" +
+ " }\n" +
+ " return null;\n" +
+ " }\n" +
+ "}\n" +
+ "\n" +
+ "public void loadAndRun(String className) throws Throwable {\n" +
+ " //System.out.println(\"Loading \" + className + \"...\");\n" +
+ " Class testClass = new VerifyClassLoader().loadClass(className);\n" +
+ " //System.out.println(\"Loaded \" + className);\n" +
+ " try {\n" +
+ " Method main = testClass.getMethod(\"main\", new Class[] {String[].class});\n" +
+ " //System.out.println(\"Running \" + className);\n" +
+ " main.invoke(null, new Object[] {new String[] {}});\n" +
+ " //System.out.println(\"Finished running \" + className);\n" +
+ " } catch (NoSuchMethodException e) {\n" +
+ " return;\n" +
+ " } catch (InvocationTargetException e) {\n" +
+ " throw e.getTargetException();\n" +
+ " }\n" +
+ "}\n" +
+ "public static void main(String[] args) throws IOException {\n" +
+ " VerifyTests verify = new VerifyTests();\n" +
+ " verify.portNumber = Integer.parseInt(args[0]);\n" +
+ " verify.run();\n" +
+ "}\n" +
+ "public void run() throws IOException {\n" +
+ " ServerSocket server = new ServerSocket(this.portNumber);\n" +
+ " this.socket = server.accept();\n" +
+ " this.socket.setTcpNoDelay(true);\n" +
+ " server.close();\n" +
+ "\n" +
+ " DataInputStream in = new DataInputStream(this.socket.getInputStream());\n" +
+ " final DataOutputStream out = new DataOutputStream(this.socket.getOutputStream());\n" +
+ " while (true) {\n" +
+ " final String className = in.readUTF();\n" +
+ " Thread thread = new Thread() {\n" +
+ " public void run() {\n" +
+ " try {\n" +
+ " loadAndRun(className);\n" +
+ " out.writeBoolean(true);\n" +
+ " System.err.println(VerifyTests.class.getName());\n" +
+ " System.out.println(VerifyTests.class.getName());\n" +
+ " } catch (Throwable e) {\n" +
+ " e.printStackTrace();\n" +
+ " try {\n" +
+ " System.err.println(VerifyTests.class.getName());\n" +
+ " System.out.println(VerifyTests.class.getName());\n" +
+ " out.writeBoolean(false);\n" +
+ " } catch (IOException e1) {\n" +
+ " e1.printStackTrace();\n" +
+ " }\n" +
+ " }\n" +
+ " try {\n" +
+ " out.flush();\n" +
+ " } catch (IOException e) {\n" +
+ " e.printStackTrace();\n" +
+ " }\n" +
+ " }\n" +
+ " };\n" +
+ " thread.start();\n" +
+ " }\n" +
+ "}\n" +
+ "}";
}
private void launchAndRun(String className, String[] classpaths, String[] programArguments, String[] vmArguments) {
// we won't reuse the vm, shut the existing one if running
@@ -669,12 +690,12 @@ private void waitForFullBuffers() {
}
//{ObjectTeams: helper
public boolean vmArgsEqual(String[] newArgs) {
- if (this.vmArguments == null || newArgs == null)
- return this.vmArguments == null && newArgs == null;
- if (this.vmArguments.length != newArgs.length)
+ if (this.fVMArguments == null || newArgs == null)
+ return this.fVMArguments == null && newArgs == null;
+ if (this.fVMArguments.length != newArgs.length)
return false;
for (int i=0; i<newArgs.length; i++)
- if (!this.vmArguments[i].equals(newArgs[i]))
+ if (!this.fVMArguments[i].equals(newArgs[i]))
return false;
return true;
}
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/VerifyTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/VerifyTests.java
index 5a659373d..45404f9c9 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/VerifyTests.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/util/VerifyTests.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2011 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -10,10 +10,18 @@
*******************************************************************************/
package org.eclipse.jdt.core.tests.util;
-import java.lang.reflect.*;
-import java.io.*;
-import java.net.*;
-import java.util.*;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.util.StringTokenizer;
/******************************************************
*

Back to the top