Bug 376053 - [compiler][resource] Strange potential resource
leak problems
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 e2d8f1d..de2c610 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
@@ -34,7 +34,7 @@
"}\n";
static {
-// TESTS_NAMES = new String[] { "testBug395977" };
+// TESTS_NAMES = new String[] { "testBug376053" };
// TESTS_NUMBERS = new int[] { 50 };
// TESTS_RANGE = new int[] { 11, -1 };
}
@@ -4368,4 +4368,46 @@
true,
options);
}
+
+// Bug 376053 - [compiler][resource] Strange potential resource leak problems
+// include line number when reporting against <unassigned Closeable value>
+public void testBug376053() {
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ runNegativeTest(
+ new String[] {
+ "Try.java",
+ "package xy;\n" +
+ "\n" +
+ "import java.io.FileNotFoundException;\n" +
+ "import java.io.PrintStream;\n" +
+ "\n" +
+ "public class Try {\n" +
+ " public static void main(String[] args) throws FileNotFoundException {\n" +
+ " System.setOut(new PrintStream(\"log.txt\"));\n" +
+ " \n" +
+ " if (Math.random() > .5) {\n" +
+ " return;\n" +
+ " }\n" +
+ " System.out.println(\"Hello World\");\n" +
+ " return;\n" +
+ " }\n" +
+ "}"
+ },
+ "----------\n" +
+ "1. ERROR in Try.java (at line 11)\n" +
+ " return;\n" +
+ " ^^^^^^^\n" +
+ "Potential resource leak: \'<unassigned Closeable value from line 8>\' may not be closed at this location\n" +
+ "----------\n" +
+ "2. ERROR in Try.java (at line 14)\n" +
+ " return;\n" +
+ " ^^^^^^^\n" +
+ "Potential resource leak: \'<unassigned Closeable value from line 8>\' may not be closed at this location\n" +
+ "----------\n",
+ null,
+ true,
+ options);
+}
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java
index 687b880..7e1f697 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java
@@ -16,11 +16,14 @@
import java.util.Map.Entry;
import java.util.Set;
+import org.eclipse.jdt.core.compiler.CharOperation;
+import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
import org.eclipse.jdt.internal.compiler.flow.FinallyFlowContext;
import org.eclipse.jdt.internal.compiler.flow.FlowContext;
import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
import org.eclipse.jdt.internal.compiler.impl.Constant;
+import org.eclipse.jdt.internal.compiler.impl.ReferenceContext;
import org.eclipse.jdt.internal.compiler.lookup.Binding;
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
@@ -31,6 +34,7 @@
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
+import org.eclipse.jdt.internal.compiler.util.Util;
/**
* A faked local variable declaration used for keeping track of data flows of a
@@ -41,6 +45,10 @@
*/
public class FakedTrackingVariable extends LocalDeclaration {
+ private static final char[] UNASSIGNED_CLOSEABLE_NAME = "<unassigned Closeable value>".toCharArray(); //$NON-NLS-1$
+ private static final char[] UNASSIGNED_CLOSEABLE_NAME_TEMPLATE = "<unassigned Closeable value from line {0}>".toCharArray(); //$NON-NLS-1$
+ private static final char[] TEMPLATE_ARGUMENT = "{0}".toCharArray(); //$NON-NLS-1$
+
// a call to close() was seen at least on one path:
private static final int CLOSE_SEEN = 1;
// the resource is shared with outside code either by
@@ -107,7 +115,7 @@
/* Create an unassigned tracking variable while analyzing an allocation expression: */
private FakedTrackingVariable(BlockScope scope, ASTNode location, FlowInfo flowInfo, int nullStatus) {
- super("<unassigned Closeable value>".toCharArray(), location.sourceStart, location.sourceEnd); //$NON-NLS-1$
+ super(UNASSIGNED_CLOSEABLE_NAME, location.sourceStart, location.sourceEnd);
this.type = new SingleTypeReference(
TypeConstants.OBJECT,
((long)this.sourceStart <<32)+this.sourceEnd);
@@ -828,4 +836,22 @@
current = current.innerTracker;
} while (current != null);
}
+
+ public String nameForReporting(ASTNode location, ReferenceContext referenceContext) {
+ if (this.name == UNASSIGNED_CLOSEABLE_NAME) {
+ if (location != null && referenceContext != null) {
+ CompilationResult compResult = referenceContext.compilationResult();
+ if (compResult != null) {
+ int[] lineEnds = compResult.getLineSeparatorPositions();
+ int resourceLine = Util.getLineNumber(this.sourceStart, lineEnds , 0, lineEnds.length-1);
+ int reportLine = Util.getLineNumber(location.sourceStart, lineEnds , 0, lineEnds.length-1);
+ if (resourceLine != reportLine) {
+ char[] replacement = Integer.toString(resourceLine).toCharArray();
+ return String.valueOf(CharOperation.replace(UNASSIGNED_CLOSEABLE_NAME_TEMPLATE, TEMPLATE_ARGUMENT, replacement));
+ }
+ }
+ }
+ }
+ return String.valueOf(this.name);
+ }
}
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java
index fbade11..3b69110 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java
@@ -19,6 +19,7 @@
* bug 365859 - [compiler][null] distinguish warnings based on flow analysis vs. null annotations
* bug 374605 - Unreasonable warning for enum-based switch statements
* bug 388281 - [compiler][null] inheritance of null annotations as an option
+ * bug 376053 - [compiler][resource] Strange potential resource leak problems
*******************************************************************************/
package org.eclipse.jdt.internal.compiler.problem;
@@ -8163,7 +8164,7 @@
}
}
public void potentiallyUnclosedCloseable(FakedTrackingVariable trackVar, ASTNode location) {
- String[] args = { String.valueOf(trackVar.name) };
+ String[] args = { trackVar.nameForReporting(location, this.referenceContext) };
if (location == null) {
this.handle(
IProblem.PotentiallyUnclosedCloseable,