Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 198503 Details for
Bug 349326
[1.7] new warning for missing try-with-resources
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
sketch of impl and some tests
Bug_349326_v0.1.diff (text/plain), 29.18 KB, created by
Stephan Herrmann
on 2011-06-23 17:47:10 EDT
(
hide
)
Description:
sketch of impl and some tests
Filename:
MIME Type:
Creator:
Stephan Herrmann
Created:
2011-06-23 17:47:10 EDT
Size:
29.18 KB
patch
obsolete
>### Eclipse Workspace Patch 1.0 >#P org.eclipse.jdt.core >Index: compiler/org/eclipse/jdt/core/compiler/IProblem.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/core/compiler/IProblem.java,v >retrieving revision 1.225.2.22 >diff -u -r1.225.2.22 IProblem.java >--- compiler/org/eclipse/jdt/core/compiler/IProblem.java 21 Jun 2011 15:48:53 -0000 1.225.2.22 >+++ compiler/org/eclipse/jdt/core/compiler/IProblem.java 23 Jun 2011 21:24:35 -0000 >@@ -1392,6 +1392,15 @@ > int SwitchOnStringsNotBelow17 = TypeRelated + 881; // https://bugs.eclipse.org/bugs/show_bug.cgi?id=348492 > /** @since 3.7 */ > int UnhandledExceptionOnAutoClose = TypeRelated + 882; >+ /** @since 3.7 */ >+ int PotentiallyUnclosedCloseable = Internal + 883; >+ /** @since 3.7 */ >+ int PotentiallyUnclosedCloseableAtExit = Internal + 884; >+ /** @since 3.7 */ >+ int UnclosedCloseable = Internal + 885; >+ /** @since 3.7 */ >+ int UnclosedCloseableAtExit = Internal + 886; >+ > /** > * External problems -- These are problems defined by other plugins > */ >Index: compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java >=================================================================== >RCS file: compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java >diff -N compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java >--- /dev/null 1 Jan 1970 00:00:00 -0000 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java 1 Jan 1970 00:00:00 -0000 >@@ -0,0 +1,69 @@ >+/******************************************************************************* >+ * Copyright (c) 2011 GK Software AG 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: >+ * GK Software AG - initial API and implementation >+ *******************************************************************************/ >+package org.eclipse.jdt.internal.compiler.ast; >+ >+import org.eclipse.jdt.internal.compiler.codegen.CodeStream; >+import org.eclipse.jdt.internal.compiler.impl.Constant; >+import org.eclipse.jdt.internal.compiler.lookup.BlockScope; >+import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding; >+import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding; >+import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; >+import org.eclipse.jdt.internal.compiler.lookup.TypeConstants; >+import org.eclipse.jdt.internal.compiler.lookup.TypeIds; >+ >+/** >+ * A faked local variable declaration used for keeping track of data flows of a >+ * special variable. Certain events will be recorded by changing the null info >+ * for this variable. >+ * >+ * @author Stephan Herrmann >+ */ >+public class FakedTrackingVariable extends LocalDeclaration { >+ >+ public FakedTrackingVariable(BlockScope currentScope, LocalDeclaration localDeclaration) { >+ super(localDeclaration.name, localDeclaration.sourceStart, localDeclaration.sourceEnd); >+ this.type = new SingleTypeReference( >+ TypeConstants.OBJECT, >+ ((long)this.sourceStart <<32)+this.sourceEnd); >+ currentScope.addTrackingVariable(this); >+ resolve(currentScope); >+ } >+ >+ public void generateCode(BlockScope currentScope, CodeStream codeStream) >+ { /* NOP - this variable is completely dummy, ie. for analysis only. */ } >+ >+ public void resolve (BlockScope scope) { >+ // only need the binding, which is used as reference in FlowInfo methods. >+ this.binding = new LocalVariableBinding( >+ this.name, >+ scope.getJavaLangObject(), // dummy, just needs to be a reference type >+ 0, >+ false); >+ this.binding.setConstant(Constant.NotAConstant); >+ this.binding.useFlag = LocalVariableBinding.USED; >+ // use a free slot without assigning it: >+ this.binding.id = scope.outerMostMethodScope().analysisIndex+1; // FIXME(SH): avoid conflicting index for several tracking vars within 1 method >+ } >+ >+ public static boolean isAutoCloseable(TypeBinding typeBinding) { >+ return typeBinding instanceof ReferenceBinding >+ && (((ReferenceBinding)typeBinding).typeBits & TypeIds.BitAutoCloseable) != 0; >+ } >+ >+ public static FakedTrackingVariable getTrackingVariable(Expression expression) { >+ if (expression instanceof SingleNameReference) { >+ SingleNameReference returnName = (SingleNameReference) expression; >+ if (returnName.binding instanceof LocalVariableBinding) >+ return ((LocalVariableBinding)returnName.binding).closeTracker; >+ } >+ return null; >+ } >+} >Index: compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java,v >retrieving revision 1.77.2.3 >diff -u -r1.77.2.3 LocalDeclaration.java >--- compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java 8 Mar 2011 17:20:09 -0000 1.77.2.3 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java 23 Jun 2011 21:24:37 -0000 >@@ -71,6 +71,10 @@ > if ((this.initialization.implicitConversion & TypeIds.UNBOXING) != 0) { > this.initialization.checkNPE(currentScope, flowContext, flowInfo); > } >+ if (FakedTrackingVariable.isAutoCloseable(this.initialization.resolvedType)) { >+ this.binding.closeTracker = new FakedTrackingVariable(currentScope, this); >+ flowInfo.markAsDefinitelyNull(this.binding.closeTracker.binding); >+ } > > flowInfo = > this.initialization >Index: compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java,v >retrieving revision 1.151.2.7 >diff -u -r1.151.2.7 MessageSend.java >--- compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java 7 Jun 2011 16:14:10 -0000 1.151.2.7 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java 23 Jun 2011 21:24:39 -0000 >@@ -42,6 +42,7 @@ > import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding; > import org.eclipse.jdt.internal.compiler.lookup.TagBits; > 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.ProblemSeverities; > >@@ -64,6 +65,10 @@ > public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) { > boolean nonStatic = !this.binding.isStatic(); > flowInfo = this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic).unconditionalInits(); >+ FakedTrackingVariable trackingVariable = FakedTrackingVariable.getTrackingVariable(this.receiver); >+ if ( trackingVariable != null >+ && CharOperation.equals(TypeConstants.CLOSE, this.selector)) >+ flowInfo.markAsDefinitelyNonNull(trackingVariable.binding); > if (nonStatic) { > this.receiver.checkNPE(currentScope, flowContext, flowInfo); > // https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682 >Index: compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java,v >retrieving revision 1.79.2.1 >diff -u -r1.79.2.1 MethodDeclaration.java >--- compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java 5 Mar 2011 18:12:56 -0000 1.79.2.1 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java 23 Jun 2011 21:24:39 -0000 >@@ -134,6 +134,7 @@ > } > > } >+ this.scope.checkUnclosedCloseables(flowInfo, null); > } catch (AbortMethod e) { > this.ignoreFurtherInvestigation = true; > } >Index: compiler/org/eclipse/jdt/internal/compiler/ast/ReturnStatement.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ReturnStatement.java,v >retrieving revision 1.70.2.1 >diff -u -r1.70.2.1 ReturnStatement.java >--- compiler/org/eclipse/jdt/internal/compiler/ast/ReturnStatement.java 27 Apr 2011 16:33:51 -0000 1.70.2.1 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/ReturnStatement.java 23 Jun 2011 21:24:39 -0000 >@@ -40,6 +40,9 @@ > if ((this.expression.implicitConversion & TypeIds.UNBOXING) != 0) { > this.expression.checkNPE(currentScope, flowContext, flowInfo); > } >+ FakedTrackingVariable trackingVariable = FakedTrackingVariable.getTrackingVariable(this.expression); >+ if (trackingVariable != null) >+ flowInfo.markAsDefinitelyNonNull(trackingVariable.binding); > } > this.initStateIndex = > currentScope.methodScope().recordInitializationStates(flowInfo); >@@ -104,6 +107,7 @@ > this.expression.bits |= ASTNode.IsReturnedValue; > } > } >+ currentScope.checkUnclosedCloseables(flowInfo, this); > return FlowInfo.DEAD_END; > } > >Index: compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java,v >retrieving revision 1.116.2.25 >diff -u -r1.116.2.25 TryStatement.java >--- compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java 21 Jun 2011 15:48:53 -0000 1.116.2.25 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java 23 Jun 2011 21:24:42 -0000 >@@ -120,8 +120,11 @@ > > for (int i = 0, max = this.resources.length; i < max; i++) { > flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy()); >- this.resources[i].binding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways. >- TypeBinding type = this.resources[i].binding.type; >+ LocalVariableBinding resourceBinding = this.resources[i].binding; >+ resourceBinding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways. >+ if (resourceBinding.closeTracker != null) >+ flowInfo.markAsDefinitelyNonNull(resourceBinding.closeTracker.binding); >+ TypeBinding type = resourceBinding.type; > if (type != null && type.isValidBinding()) { > ReferenceBinding binding = (ReferenceBinding) type; > MethodBinding closeMethod = binding.getExactMethod(ConstantPool.Close, new TypeBinding [0], this.scope.compilationUnitScope()); // scope needs to be tighter >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java,v >retrieving revision 1.136 >diff -u -r1.136 BinaryTypeBinding.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java 17 Jan 2011 13:00:56 -0000 1.136 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java 23 Jun 2011 21:24:44 -0000 >@@ -1141,6 +1141,7 @@ > this.tagBits &= ~TagBits.HasUnresolvedSuperclass; > if (this.superclass.problemId() == ProblemReasons.NotFound) > this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency >+ this.typeBits |= (this.superclass.id & TypeIds.BitAutoCloseable); > return this.superclass; > } > // NOTE: superInterfaces of binary types are resolved when needed >@@ -1152,6 +1153,9 @@ > this.superInterfaces[i] = (ReferenceBinding) resolveType(this.superInterfaces[i], this.environment, true /* raw conversion */); > if (this.superInterfaces[i].problemId() == ProblemReasons.NotFound) > this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency >+ if ( this.superInterfaces[i].id == TypeIds.T_JavaLangAutoCloseable >+ || (this.superInterfaces[i].typeBits & TypeIds.BitAutoCloseable) != 0) >+ this.typeBits |= TypeIds.BitAutoCloseable; > } > this.tagBits &= ~TagBits.HasUnresolvedSuperinterfaces; > return this.superInterfaces; >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java,v >retrieving revision 1.123.2.3 >diff -u -r1.123.2.3 BlockScope.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java 5 May 2011 14:31:10 -0000 1.123.2.3 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java 23 Jun 2011 21:24:46 -0000 >@@ -10,10 +10,14 @@ > *******************************************************************************/ > package org.eclipse.jdt.internal.compiler.lookup; > >+import java.util.ArrayList; >+import java.util.List; >+ > import org.eclipse.jdt.core.compiler.CharOperation; > import org.eclipse.jdt.internal.compiler.ast.*; > import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; > import org.eclipse.jdt.internal.compiler.codegen.CodeStream; >+import org.eclipse.jdt.internal.compiler.flow.FlowInfo; > import org.eclipse.jdt.internal.compiler.impl.Constant; > import org.eclipse.jdt.internal.compiler.problem.ProblemReporter; > >@@ -957,4 +961,24 @@ > } > } > } >+ >+// TODO: List or array? >+// TODO: move these to FlowInfo? (beware of flowInfo copying!) >+private List trackingVariables; >+public void addTrackingVariable(FakedTrackingVariable fakedTrackingVariable) { >+ if (this.trackingVariables == null) >+ this.trackingVariables = new ArrayList(); >+ this.trackingVariables.add(fakedTrackingVariable); >+} >+ >+public void checkUnclosedCloseables(FlowInfo flowInfo, ASTNode location) { >+ if (this.trackingVariables == null) return; >+ for (int i=0; i<this.trackingVariables.size(); i++) { >+ FakedTrackingVariable trackingVar = (FakedTrackingVariable) this.trackingVariables.get(0); >+ if (flowInfo.isDefinitelyNull(trackingVar.binding)) >+ problemReporter().unclosedCloseable(trackingVar, location); >+ else if (flowInfo.isPotentiallyNull(trackingVar.binding)) >+ problemReporter().potentiallyUnclosedCloseable(trackingVar, location); >+ } >+} > } >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java,v >retrieving revision 1.183.2.1 >diff -u -r1.183.2.1 ClassScope.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java 20 Apr 2011 20:40:00 -0000 1.183.2.1 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java 23 Jun 2011 21:24:48 -0000 >@@ -907,6 +907,7 @@ > } else { > // only want to reach here when no errors are reported > sourceType.superclass = superclass; >+ sourceType.typeBits |= (superclass.typeBits & TypeIds.BitAutoCloseable); > return true; > } > } >@@ -1017,6 +1018,9 @@ > noProblems &= superInterfaceRef.resolvedType.isValidBinding(); > } > // only want to reach here when no errors are reported >+ if ( superInterface.id == TypeIds.T_JavaLangAutoCloseable >+ || (superInterface.typeBits & TypeIds.BitAutoCloseable) != 0) >+ superInterface.typeBits |= TypeIds.BitAutoCloseable; > interfaceBindings[count++] = superInterface; > } > // hold onto all correctly resolved superinterfaces >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/LocalVariableBinding.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/LocalVariableBinding.java,v >retrieving revision 1.49.2.2 >diff -u -r1.49.2.2 LocalVariableBinding.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/LocalVariableBinding.java 27 Apr 2011 16:33:51 -0000 1.49.2.2 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/LocalVariableBinding.java 23 Jun 2011 21:24:48 -0000 >@@ -19,6 +19,7 @@ > import org.eclipse.jdt.internal.compiler.ast.ASTNode; > import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; > import org.eclipse.jdt.internal.compiler.ast.Annotation; >+import org.eclipse.jdt.internal.compiler.ast.FakedTrackingVariable; > import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration; > import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration; > import org.eclipse.jdt.internal.compiler.impl.Constant; >@@ -39,6 +40,8 @@ > public int[] initializationPCs; > public int initializationCount = 0; > >+ public FakedTrackingVariable closeTracker; >+ > // for synthetic local variables > // if declaration slot is not positionned, the variable will not be listed in attribute > // note that the name of a variable should be chosen so as not to conflict with user ones (usually starting with a space char is all needed) >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java,v >retrieving revision 1.140.2.5 >diff -u -r1.140.2.5 ReferenceBinding.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java 19 Apr 2011 13:09:09 -0000 1.140.2.5 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java 23 Jun 2011 21:24:50 -0000 >@@ -45,6 +45,8 @@ > > private SimpleLookupTable compatibleCache; > >+ public int typeBits; >+ > public static final ReferenceBinding LUB_GENERIC = new ReferenceBinding() { /* used for lub computation */}; > > private static final Comparator FIELD_COMPARATOR = new Comparator() { >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java,v >retrieving revision 1.51.2.5 >diff -u -r1.51.2.5 TypeConstants.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java 24 Apr 2011 11:01:15 -0000 1.51.2.5 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java 23 Jun 2011 21:24:51 -0000 >@@ -148,6 +148,7 @@ > "PolymorphicSignature".toCharArray() //$NON-NLS-1$ > }; > char[][] JAVA_LANG_AUTOCLOSEABLE = {JAVA, LANG, "AutoCloseable".toCharArray()}; //$NON-NLS-1$ >+ char[] CLOSE = "close".toCharArray(); //$NON-NLS-1$ > > // Constraints for generic type argument inference > int CONSTRAINT_EQUAL = 0; // Actual = Formal >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java,v >retrieving revision 1.36.2.4 >diff -u -r1.36.2.4 TypeIds.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java 19 Apr 2011 13:09:09 -0000 1.36.2.4 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java 23 Jun 2011 21:24:51 -0000 >@@ -173,4 +173,6 @@ > final int Object2Object = T_JavaLangObject + (T_JavaLangObject << 4); > final int BOXING = 0x200; > final int UNBOXING = 0x400; >+ >+ final int BitAutoCloseable = 1; > } >Index: compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java,v >retrieving revision 1.430.2.32 >diff -u -r1.430.2.32 ProblemReporter.java >--- compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java 21 Jun 2011 15:48:53 -0000 1.430.2.32 >+++ compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java 23 Jun 2011 21:25:04 -0000 >@@ -55,6 +55,7 @@ > import org.eclipse.jdt.internal.compiler.ast.EqualExpression; > import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall; > import org.eclipse.jdt.internal.compiler.ast.Expression; >+import org.eclipse.jdt.internal.compiler.ast.FakedTrackingVariable; > import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration; > import org.eclipse.jdt.internal.compiler.ast.FieldReference; > import org.eclipse.jdt.internal.compiler.ast.ImportReference; >@@ -7863,4 +7864,40 @@ > type.sourceStart, > type.sourceEnd); > } >+public void potentiallyUnclosedCloseable(FakedTrackingVariable trackVar, ASTNode location) { >+ if (location == null) { >+ this.handle( >+ IProblem.PotentiallyUnclosedCloseable, >+ NoArgument, >+ NoArgument, >+ trackVar.sourceStart, >+ trackVar.sourceEnd); >+ } else { >+ String[] args = { String.valueOf(trackVar.name) }; >+ this.handle( >+ IProblem.PotentiallyUnclosedCloseableAtExit, >+ args, >+ args, >+ location.sourceStart, >+ location.sourceEnd); >+ } >+} >+public void unclosedCloseable(FakedTrackingVariable trackVar, ASTNode location) { >+ if (location == null) { >+ this.handle( >+ IProblem.UnclosedCloseable, >+ NoArgument, >+ NoArgument, >+ trackVar.sourceStart, >+ trackVar.sourceEnd); >+ } else { >+ String[] args = { String.valueOf(trackVar.name) }; >+ this.handle( >+ IProblem.UnclosedCloseableAtExit, >+ args, >+ args, >+ location.sourceStart, >+ location.sourceEnd); >+ } >+} > } >\ No newline at end of file >Index: compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties,v >retrieving revision 1.262.2.28 >diff -u -r1.262.2.28 messages.properties >--- compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties 21 Jun 2011 04:41:29 -0000 1.262.2.28 >+++ compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties 23 Jun 2011 21:25:05 -0000 >@@ -645,6 +645,10 @@ > 880 = '<>' cannot be used with anonymous classes > 881 = Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum constants are permitted > 882 = Unhandled exception type {0} thrown by automatic close() invocation on {1} >+883 = Value of type AutoCloseable is not closed on all paths. >+884 = Instance '{0}' of type AutoCloseable may be unclosed at this point. >+885 = Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources. >+886 = Instance '{0}' of type AutoCloseable is not closed at this point. > > ### ELABORATIONS > ## Access restrictions >#P org.eclipse.jdt.core.tests.compiler >Index: src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/Attic/TryWithResourcesStatementTest.java,v >retrieving revision 1.1.2.27 >diff -u -r1.1.2.27 TryWithResourcesStatementTest.java >--- src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java 21 Jun 2011 06:09:22 -0000 1.1.2.27 >+++ src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java 23 Jun 2011 21:25:11 -0000 >@@ -22,7 +22,7 @@ > public class TryWithResourcesStatementTest extends AbstractRegressionTest { > > static { >-// TESTS_NAMES = new String[] { "test053" }; >+ TESTS_NAMES = new String[] { "test055" }; > // TESTS_NUMBERS = new int[] { 50 }; > // TESTS_RANGE = new int[] { 11, -1 }; > } >@@ -3341,6 +3341,184 @@ > "Object.Integer cannot be resolved to a type\n" + > "----------\n"); > } >+// Bug 349326 - [1.7] new warning for missing try-with-resources >+// a method uses an AutoCloseable without ever closing it. >+public void test055() { >+ Map options = getCompilerOptions(); >+// FIXME(SH): make configurable >+// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); >+ 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" + >+ " char[] in = new char[50];\n" + >+ " fileReader.read(in);\n" + >+ " }\n" + >+ " public static void main(String[] args) throws IOException {\n" + >+ " new X().foo();\n" + >+ " }\n" + >+ "}\n" >+ }, >+ "----------\n" + >+ "1. ERROR in X.java (at line 7)\n" + >+ " FileReader fileReader = new FileReader(file);\n" + >+ " ^^^^^^^^^^\n" + >+ "Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + >+ "----------\n", >+ null, >+ true, >+ options); >+} >+// Bug 349326 - [1.7] new warning for missing try-with-resources >+// a method uses an AutoCloseable and closes it properly. >+public void test055a() { >+ Map options = getCompilerOptions(); >+// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); >+ this.runConformTest( >+ 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" + >+ " char[] in = new char[50];\n" + >+ " fileReader.read(in);\n" + >+ " fileReader.close();\n" + >+ " }\n" + >+ " public static void main(String[] args) {\n" + >+ " try {\n" + >+ " new X().foo();\n" + >+ " } catch (IOException ioex) {\n" + >+ " System.out.println(\"caught\");\n" + >+ " }\n" + >+ " }\n" + >+ "}\n" >+ }, >+ "caught", /*output*/ >+ null/*classLibs*/, >+ true/*shouldFlush*/, >+ null/*vmargs*/, >+ options, >+ null/*requestor*/); >+} >+// Bug 349326 - [1.7] new warning for missing try-with-resources >+// a method uses an AutoCloseable properly within try-with-resources. >+public void test055b() { >+ Map options = getCompilerOptions(); >+// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); >+ this.runConformTest( >+ 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" + >+ " try (FileReader fileReader = new FileReader(file)) {\n" + >+ " char[] in = new char[50];\n" + >+ " fileReader.read(in);\n" + >+ " }\n" + >+ " }\n" + >+ " public static void main(String[] args) {\n" + >+ " try {\n" + >+ " new X().foo();\n" + >+ " } catch (IOException ioex) {\n" + >+ " System.out.println(\"caught\");\n" + >+ " }\n" + >+ " }\n" + >+ "}\n" >+ }, >+ "caught", /*output*/ >+ null/*classLibs*/, >+ true/*shouldFlush*/, >+ null/*vmargs*/, >+ options, >+ null/*requestor*/); >+} >+// Bug 349326 - [1.7] new warning for missing try-with-resources >+// a method uses an AutoCloseable but may return without closing it. >+public void test055c() { >+ Map options = getCompilerOptions(); >+// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); >+ 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(boolean flag) throws IOException {\n" + >+ " File file = new File(\"somefile\");\n" + >+ " FileReader fileReader = new FileReader(file);\n" + >+ " char[] in = new char[50];\n" + >+ " fileReader.read(in);\n" + >+ " if (flag)\n" + >+ " return;\n" + >+ " fileReader.close();\n" + >+ " }\n" + >+ " public static void main(String[] args) throws IOException {\n" + >+ " new X().foo(false);\n" + >+ " }\n" + >+ "}\n" >+ }, >+ "----------\n" + >+ "1. ERROR in X.java (at line 11)\n" + >+ " return;\n" + >+ " ^^^^^^^\n" + >+ "Instance \'fileReader\' of type AutoCloseable is not closed at this point.\n" + >+ "----------\n", >+ null, >+ true, >+ options); >+} >+// Bug 349326 - [1.7] new warning for missing try-with-resources >+// one method returns an AutoCleasble, a second method uses this object without ever closing it. >+public void test055d() { >+ Map options = getCompilerOptions(); >+//FIXME(SH): make configurable >+// options.put(CompilerOptions.OPTION_ReportUnclosedAutocloseable, CompilerOptions.ERROR); >+ 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" + >+ " FileReader getReader(String filename) throws IOException {\n" + >+ " File file = new File(\"somefile\");\n" + >+ " FileReader fileReader = new FileReader(file);\n" + >+ " return fileReader;\n" + // don't complain here, pass responsibility to caller >+ " }\n" + >+ " void foo() throws IOException {\n" + >+ " FileReader reader = getReader(\"somefile\");\n" + >+ " char[] in = new char[50];\n" + >+ " reader.read(in);\n" + >+ " }\n" + >+ " public static void main(String[] args) throws IOException {\n" + >+ " new X().foo();\n" + >+ " }\n" + >+ "}\n" >+ }, >+ "----------\n" + >+ "1. ERROR in X.java (at line 11)\n" + >+ " FileReader reader = getReader(\"somefile\");\n" + >+ " ^^^^^^\n" + >+ "Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + >+ "----------\n", >+ null, >+ true, >+ options); >+} > public static Class testClass() { > return TryWithResourcesStatementTest.class; > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 349326
:
198503
|
199192
|
199216
|
199235
|
199358
|
199378
|
199385
|
199402
|
199435
|
199443
|
199812
|
201450
|
202016
|
202189
|
202721
|
203866
|
203961
|
203975
|
204041
|
204066
|
204108