diff options
author | Stephan Herrmann | 2019-10-29 20:35:10 +0000 |
---|---|---|
committer | Stephan Herrmann | 2019-10-29 20:35:10 +0000 |
commit | f1626034b1d0ea97452695d8c6e785636709a2be (patch) | |
tree | 521a89e2791429c1bc8b1e561906a92a4937b109 | |
parent | ea2cf5819a5be4907255ff9da714a96100bb270f (diff) | |
download | eclipse.jdt.core-f1626034b1d0ea97452695d8c6e785636709a2be.tar.gz eclipse.jdt.core-f1626034b1d0ea97452695d8c6e785636709a2be.tar.xz eclipse.jdt.core-f1626034b1d0ea97452695d8c6e785636709a2be.zip |
Bug 486506 - [1.8][resource] leak analysis should consider rc::closeI20191030-0010I20191029-1800
references
Change-Id: I8f1ffe8bd06587fe17c5edab2ac85a432259b505
2 files changed, 44 insertions, 1 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 d79f1ee407..5b741c918c 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011, 2019 GK Software AG and others. + * Copyright (c) 2011, 2019 GK Software SE and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -5668,4 +5668,39 @@ public void testBug542707_003() { "----------\n", options); } +public void testBug486506() { + if (this.complianceLevel < ClassFileConstants.JDK1_8) return; // uses switch expression + Map options = getCompilerOptions(); + options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); + options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); + runLeakTest( + new String[] { + "LogMessage.java", + "import java.util.stream.*;\n" + + "import java.io.*;\n" + + "import java.nio.file.*;\n" + + "import java.nio.charset.*;\n" + + "class LogMessage {\n" + + " LogMessage(Path path, String message) {}\n" + + " public static Stream<LogMessage> streamSingleLineLogMessages(Path path) {\n" + + " try {\n" + + " Stream<String> lineStream = Files.lines(path, StandardCharsets.ISO_8859_1);\n" + + " Stream<LogMessage> logMessageStream =\n" + + " lineStream.map(message -> new LogMessage(path, message));\n" + + " logMessageStream.onClose(lineStream::close);\n" + + " return logMessageStream;\n" + + " } catch (IOException e) {\n" + + " throw new RuntimeException(e);\n" + + " }\n" + + " }\n" + + "}\n" + }, + "----------\n" + + "1. ERROR in LogMessage.java (at line 13)\n" + + " return logMessageStream;\n" + + " ^^^^^^^^^^^^^^^^^^^^^^^^\n" + + "Potential resource leak: \'lineStream\' may not be closed at this location\n" + + "----------\n", + options); +} } diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java index d6d044c7a5..cfa39258eb 100644 --- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java +++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReferenceExpression.java @@ -533,6 +533,14 @@ public class ReferenceExpression extends FunctionalExpression implements IPolyEx } } + if (currentScope.compilerOptions().analyseResourceLeaks) { + if (this.haveReceiver && CharOperation.equals(this.selector, TypeConstants.CLOSE)) { + FakedTrackingVariable trackingVariable = FakedTrackingVariable.getCloseTrackingVariable(this.lhs, flowInfo, flowContext); + if (trackingVariable != null) { // null happens if target is not a local variable or not an AutoCloseable + trackingVariable.markClosedInNestedMethod(); // there is a close()-call, but we don't know if it will be invoked + } + } + } manageSyntheticAccessIfNecessary(currentScope, flowInfo); return flowInfo; } |