Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Herrmann2013-02-04 10:45:40 +0000
committerJayaprakash Arthanareeswaran2013-02-04 10:45:40 +0000
commit25a0c02d6979e76d70832f78f8eb60bd371c0ad1 (patch)
treeae8aefab3f3dfaf73149fa3ea6cfedff1d1b8ce4
parentfcea29988ed692db00839aa74edd7fda8cc73655 (diff)
downloadeclipse.jdt.core-25a0c02d6979e76d70832f78f8eb60bd371c0ad1.tar.gz
eclipse.jdt.core-25a0c02d6979e76d70832f78f8eb60bd371c0ad1.tar.xz
eclipse.jdt.core-25a0c02d6979e76d70832f78f8eb60bd371c0ad1.zip
Bug 376053 - [compiler][resource] Strange potential resource
leak problems
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java44
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java28
-rw-r--r--org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java3
3 files changed, 72 insertions, 3 deletions
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 e2d8f1dd65..de2c6103d6 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 @@ private static final String GUAVA_CLOSEABLES_CONTENT = "package com.google.commo
"}\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 @@ public void testBug395977_2() {
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 687b880696..7e1f6973d2 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;
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.TypeBinding;
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 @@ import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
*/
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 @@ public class FakedTrackingVariable extends LocalDeclaration {
/* 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 @@ public class FakedTrackingVariable extends LocalDeclaration {
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 553f3907ab..dcd4505670 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
@@ -25,6 +25,7 @@
* bug 382353 - [1.8][compiler] Implementation property modifiers should be accepted on default methods.
* bug 382347 - [1.8][compiler] Compiler accepts incorrect default method inheritance
* bug 388281 - [compiler][null] inheritance of null annotations as an option
+ * bug 376053 - [compiler][resource] Strange potential resource leak problems
* bug 392862 - [1.8][compiler][null] Evaluate null annotations on array types
* bug 388739 - [1.8][compiler] consider default methods when detecting whether a class needs to be declared abstract
* Jesper S Moller <jesper@selskabet.org> - Contributions for
@@ -8377,7 +8378,7 @@ public void redundantSpecificationOfTypeArguments(ASTNode location, TypeBinding[
}
}
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,

Back to the top