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 199235 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]
patch v0.5
Bug_349326_v0.5.patch (text/plain), 70.33 KB, created by
Stephan Herrmann
on 2011-07-07 05:10:46 EDT
(
hide
)
Description:
patch v0.5
Filename:
MIME Type:
Creator:
Stephan Herrmann
Created:
2011-07-07 05:10:46 EDT
Size:
70.33 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.23 >diff -u -r1.225.2.23 IProblem.java >--- compiler/org/eclipse/jdt/core/compiler/IProblem.java 6 Jul 2011 18:46:34 -0000 1.225.2.23 >+++ compiler/org/eclipse/jdt/core/compiler/IProblem.java 7 Jul 2011 09:04:42 -0000 >@@ -1394,6 +1394,17 @@ > int UnhandledExceptionOnAutoClose = TypeRelated + 882; > /** @since 3.7 */ > int DiamondNotBelow17 = TypeRelated + 883; >+ /** @since 3.7 */ >+ int PotentiallyUnclosedCloseable = Internal + 884; >+ /** @since 3.7 */ >+ int PotentiallyUnclosedCloseableAtExit = Internal + 885; >+ /** @since 3.7 */ >+ int UnclosedCloseable = Internal + 886; >+ /** @since 3.7 */ >+ int UnclosedCloseableAtExit = Internal + 887; >+ /** @since 3.7 */ >+ int ExplicitlyClosedAutoCloseable = Internal + 888; >+ > /** > * External problems -- These are problems defined by other plugins > */ >Index: compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java,v >retrieving revision 1.94.2.3 >diff -u -r1.94.2.3 Assignment.java >--- compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java 16 Mar 2011 10:04:29 -0000 1.94.2.3 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java 7 Jul 2011 09:04:42 -0000 >@@ -48,6 +48,29 @@ > if ((this.expression.implicitConversion & TypeIds.UNBOXING) != 0) { > this.expression.checkNPE(currentScope, flowContext, flowInfo); > } >+ if (local != null) { >+ LocalVariableBinding trackerBinding = null; >+ if (local.closeTracker != null) { >+ // Assigning to a variable already holding an AutoCloseable, has it been closed before? >+ trackerBinding = local.closeTracker.binding; >+ if (flowInfo.isDefinitelyNull(trackerBinding)) >+ currentScope.problemReporter().unclosedCloseable(local.closeTracker, this); >+ else if (flowInfo.isPotentiallyNull(trackerBinding)) >+ currentScope.problemReporter().potentiallyUnclosedCloseable(local.closeTracker, this); >+ } >+ if (FakedTrackingVariable.isAutoCloseable(this.expression.resolvedType)) { >+ if (local.closeTracker != null && local.closeTracker.isInsideTWR) { >+ // reassigning resource of try-with-resources is a different error >+ } else { >+ // new value is AutoCloseable start tracking, possibly re-using existing tracker var: >+ if (trackerBinding == null) { >+ local.closeTracker = new FakedTrackingVariable(currentScope, this, local.name); >+ trackerBinding = local.closeTracker.binding; >+ } >+ flowInfo.markAsDefinitelyNull(trackerBinding); >+ } >+ } >+ } > flowInfo = ((Reference) this.lhs) > .analyseAssignment(currentScope, flowContext, flowInfo, this, false) > .unconditionalInits(); >Index: compiler/org/eclipse/jdt/internal/compiler/ast/Block.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Block.java,v >retrieving revision 1.43 >diff -u -r1.43 Block.java >--- compiler/org/eclipse/jdt/internal/compiler/ast/Block.java 14 Oct 2009 18:08:37 -0000 1.43 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/Block.java 7 Jul 2011 09:04:42 -0000 >@@ -36,6 +36,8 @@ > flowInfo = stat.analyseCode(this.scope, flowContext, flowInfo); > } > } >+ if (this.explicitDeclarations > 0) // if block has its own scope analyze tracking vars now: >+ this.scope.checkUnclosedCloseables(flowInfo, null); > return flowInfo; > } > /** >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,74 @@ >+/******************************************************************************* >+ * 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.MethodScope; >+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 boolean isInsideTWR = false; >+ MethodScope methodScope; // designates the method declaring this variable >+ public boolean closedInNestedMethod; // if close() is invoked from a nested method (inside a local type) >+ >+ public FakedTrackingVariable(BlockScope currentScope, Statement location, char[] name) { >+ super(name, location.sourceStart, location.sourceEnd); >+ this.type = new SingleTypeReference( >+ TypeConstants.OBJECT, >+ ((long)this.sourceStart <<32)+this.sourceEnd); >+ this.methodScope = currentScope.methodScope(); >+ 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.registerTrackingVariable(this); >+ } >+ >+ public static boolean isAutoCloseable(TypeBinding typeBinding) { >+ return typeBinding instanceof ReferenceBinding >+ && ((ReferenceBinding)typeBinding).hasTypeBit(TypeIds.BitAutoCloseable); >+ } >+ >+ 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 7 Jul 2011 09:04:43 -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, this.name); >+ 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.8 >diff -u -r1.151.2.8 MessageSend.java >--- compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java 28 Jun 2011 14:49:04 -0000 1.151.2.8 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java 7 Jul 2011 09:04:44 -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,14 @@ > 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)) { >+ if (trackingVariable.methodScope == currentScope.methodScope()) >+ flowInfo.markAsDefinitelyNonNull(trackingVariable.binding); >+ else >+ trackingVariable.closedInNestedMethod = true; >+ } > 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 7 Jul 2011 09:04:44 -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 7 Jul 2011 09:04:44 -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.26 >diff -u -r1.116.2.26 TryStatement.java >--- compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java 4 Jul 2011 08:57:17 -0000 1.116.2.26 >+++ compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java 7 Jul 2011 09:04:45 -0000 >@@ -120,8 +120,13 @@ > > 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) { >+ resourceBinding.closeTracker.isInsideTWR = true; >+ 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 >@@ -245,8 +250,13 @@ > > 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) { >+ resourceBinding.closeTracker.isInsideTWR = true; >+ 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/impl/CompilerOptions.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java,v >retrieving revision 1.241.2.3 >diff -u -r1.241.2.3 CompilerOptions.java >--- compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java 14 Feb 2011 14:08:01 -0000 1.241.2.3 >+++ compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java 7 Jul 2011 09:04:46 -0000 >@@ -136,6 +136,9 @@ > public static final String OPTION_IncludeNullInfoFromAsserts = "org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts"; //$NON-NLS-1$ > public static final String OPTION_ReportMethodCanBeStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic"; //$NON-NLS-1$ > public static final String OPTION_ReportMethodCanBePotentiallyStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic"; //$NON-NLS-1$ >+ public static final String OPTION_ReportUnclosedCloseable = "org.eclipse.jdt.core.compiler.problem.unclosedCloseable"; //$NON-NLS-1$ >+ public static final String OPTION_ReportPotentiallyUnclosedCloseable = "org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable"; //$NON-NLS-1$ >+ public static final String OPTION_ReportExplicitlyClosedAutoCloseable = "org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable"; //$NON-NLS-1$ > /** > * Possible values for configurable options > */ >@@ -238,6 +241,9 @@ > public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4; > public static final int MethodCanBeStatic = IrritantSet.GROUP2 | ASTNode.Bit5; > public static final int MethodCanBePotentiallyStatic = IrritantSet.GROUP2 | ASTNode.Bit6; >+ public static final int UnclosedCloseable = IrritantSet.GROUP2 | ASTNode.Bit7; >+ public static final int PotentiallyUnclosedCloseable = IrritantSet.GROUP2 | ASTNode.Bit8; >+ public static final int ExplicitlyClosedAutoCloseable = IrritantSet.GROUP2 | ASTNode.Bit9; > > // Severity level for handlers > /** >@@ -547,6 +553,12 @@ > return OPTION_ReportMethodCanBeStatic; > case MethodCanBePotentiallyStatic : > return OPTION_ReportMethodCanBePotentiallyStatic; >+ case UnclosedCloseable : >+ return OPTION_ReportUnclosedCloseable; >+ case PotentiallyUnclosedCloseable : >+ return OPTION_ReportPotentiallyUnclosedCloseable; >+ case ExplicitlyClosedAutoCloseable : >+ return OPTION_ReportExplicitlyClosedAutoCloseable; > } > return null; > } >@@ -709,6 +721,9 @@ > OPTION_ReportUnusedTypeArgumentsForMethodInvocation, > OPTION_ReportUnusedWarningToken, > OPTION_ReportVarargsArgumentNeedCast, >+ OPTION_ReportUnclosedCloseable, >+ OPTION_ReportPotentiallyUnclosedCloseable, >+ OPTION_ReportExplicitlyClosedAutoCloseable, > }; > return result; > } >@@ -973,6 +988,9 @@ > optionsMap.put(OPTION_IncludeNullInfoFromAsserts, this.includeNullInfoFromAsserts ? ENABLED : DISABLED); > optionsMap.put(OPTION_ReportMethodCanBeStatic, getSeverityString(MethodCanBeStatic)); > optionsMap.put(OPTION_ReportMethodCanBePotentiallyStatic, getSeverityString(MethodCanBePotentiallyStatic)); >+ optionsMap.put(OPTION_ReportUnclosedCloseable, getSeverityString(UnclosedCloseable)); >+ optionsMap.put(OPTION_ReportPotentiallyUnclosedCloseable, getSeverityString(PotentiallyUnclosedCloseable)); >+ optionsMap.put(OPTION_ReportExplicitlyClosedAutoCloseable, getSeverityString(ExplicitlyClosedAutoCloseable)); > return optionsMap; > } > >@@ -1402,6 +1420,9 @@ > if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue); > if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBeStatic)) != null) updateSeverity(MethodCanBeStatic, optionValue); > if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBePotentiallyStatic)) != null) updateSeverity(MethodCanBePotentiallyStatic, optionValue); >+ if ((optionValue = optionsMap.get(OPTION_ReportUnclosedCloseable)) != null) updateSeverity(UnclosedCloseable, optionValue); >+ if ((optionValue = optionsMap.get(OPTION_ReportPotentiallyUnclosedCloseable)) != null) updateSeverity(PotentiallyUnclosedCloseable, optionValue); >+ if ((optionValue = optionsMap.get(OPTION_ReportExplicitlyClosedAutoCloseable)) != null) updateSeverity(ExplicitlyClosedAutoCloseable, optionValue); > > // Javadoc options > if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) { >Index: compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java,v >retrieving revision 1.13.2.1 >diff -u -r1.13.2.1 IrritantSet.java >--- compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java 11 Feb 2011 15:17:16 -0000 1.13.2.1 >+++ compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java 7 Jul 2011 09:04:46 -0000 >@@ -99,7 +99,8 @@ > // group-2 warnings enabled by default > .set( > CompilerOptions.DeadCode >- |CompilerOptions.Tasks); >+ |CompilerOptions.Tasks >+ |CompilerOptions.UnclosedCloseable); > > ALL.setAll(); > HIDING >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 7 Jul 2011 09:04:47 -0000 >@@ -971,6 +971,12 @@ > variable.resolve(); > return variable; > } >+public boolean hasTypeBit(int bit) { >+ // ensure hierarchy is resolved >+ superclass(); >+ superInterfaces(); >+ return (this.typeBits & bit) != 0; >+} > private void initializeTypeVariable(TypeVariableBinding variable, TypeVariableBinding[] existingVariables, SignatureWrapper wrapper, char[][][] missingTypeNames) { > // ParameterSignature = Identifier ':' TypeSignature > // or Identifier ':' TypeSignature(optional) InterfaceBound(s) >@@ -1141,6 +1147,11 @@ > this.tagBits &= ~TagBits.HasUnresolvedSuperclass; > if (this.superclass.problemId() == ProblemReasons.NotFound) > this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency >+ else { >+ this.superclass.superclass(); >+ this.superclass.superInterfaces(); >+ } >+ this.typeBits |= this.superclass.typeBits; > return this.superclass; > } > // NOTE: superInterfaces of binary types are resolved when needed >@@ -1152,6 +1163,14 @@ > 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 >+ else >+ // TODO(SH): check if this actually speeds up anything: >+ if ((this.superInterfaces[i].tagBits & (TagBits.HasUnresolvedSuperclass|TagBits.HasUnresolvedSuperinterfaces)) != 0) >+ { >+ this.superInterfaces[i].superclass(); >+ this.superInterfaces[i].superInterfaces(); >+ } >+ this.typeBits |= this.superInterfaces[i].typeBits; > } > 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 7 Jul 2011 09:04:48 -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,31 @@ > } > } > } >+ >+// TODO: List or array? >+private List trackingVariables; >+public int registerTrackingVariable(FakedTrackingVariable fakedTrackingVariable) { >+ if (this.trackingVariables == null) >+ this.trackingVariables = new ArrayList(); >+ this.trackingVariables.add(fakedTrackingVariable); >+ MethodScope outerMethodScope = outerMostMethodScope(); >+ return outerMethodScope.analysisIndex + (++outerMethodScope.trackVarCount); >+ >+} >+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(i); >+ if (flowInfo.isDefinitelyNull(trackingVar.binding)) { >+ if (trackingVar.closedInNestedMethod) >+ problemReporter().potentiallyUnclosedCloseable(trackingVar, location); >+ else >+ problemReporter().unclosedCloseable(trackingVar, location); >+ } else if (flowInfo.isPotentiallyNull(trackingVar.binding)) { >+ problemReporter().potentiallyUnclosedCloseable(trackingVar, location); >+ } else if (environment().globalOptions.complianceLevel >= ClassFileConstants.JDK1_7 && !trackingVar.isInsideTWR) { >+ problemReporter().explicitlyClosedAutoCloseable(trackingVar); >+ } >+ } >+} > } >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 7 Jul 2011 09:04:49 -0000 >@@ -907,6 +907,7 @@ > } else { > // only want to reach here when no errors are reported > sourceType.superclass = superclass; >+ sourceType.typeBits |= superclass.typeBits; > return true; > } > } >@@ -1017,6 +1018,7 @@ > noProblems &= superInterfaceRef.resolvedType.isValidBinding(); > } > // only want to reach here when no errors are reported >+ sourceType.typeBits |= superInterface.typeBits; > 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 7 Jul 2011 09:04:49 -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/MethodScope.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/MethodScope.java,v >retrieving revision 1.78.2.1 >diff -u -r1.78.2.1 MethodScope.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/MethodScope.java 8 Mar 2011 17:20:09 -0000 1.78.2.1 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/MethodScope.java 7 Jul 2011 09:04:49 -0000 >@@ -48,6 +48,9 @@ > > // inner-emulation > public SyntheticArgumentBinding[] extraSyntheticArguments; >+ >+ // count number of tracking variables, see FakedTrackingVariable >+ int trackVarCount; > > public MethodScope(ClassScope parent, ReferenceContext context, boolean isStatic) { > super(METHOD_SCOPE, parent); >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java,v >retrieving revision 1.120.2.2 >diff -u -r1.120.2.2 ParameterizedTypeBinding.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java 9 May 2011 14:30:21 -0000 1.120.2.2 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java 7 Jul 2011 09:04:50 -0000 >@@ -629,6 +629,13 @@ > return this.type.hasMemberTypes(); > } > >+ public boolean hasTypeBit(int bit) { >+ TypeBinding erasure = erasure(); >+ if (erasure instanceof ReferenceBinding) >+ return ((ReferenceBinding) erasure).hasTypeBit(bit); >+ return false; >+ } >+ > /** > * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#implementsMethod(MethodBinding) > */ >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/ProblemReferenceBinding.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ProblemReferenceBinding.java,v >retrieving revision 1.22 >diff -u -r1.22 ProblemReferenceBinding.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/ProblemReferenceBinding.java 27 Jun 2008 16:04:02 -0000 1.22 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/ProblemReferenceBinding.java 7 Jul 2011 09:04:50 -0000 >@@ -40,6 +40,12 @@ > return this.closestMatch; > } > >+public boolean hasTypeBit(int bit) { >+ if (this.closestMatch != null) >+ return this.closestMatch.hasTypeBit(bit); >+ return false; >+} >+ > /* API > * Answer the problem id associated with the receiver. > * NoError if the receiver is a valid binding. >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.6 >diff -u -r1.140.2.6 ReferenceBinding.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java 28 Jun 2011 15:35:07 -0000 1.140.2.6 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java 7 Jul 2011 09:04:51 -0000 >@@ -17,6 +17,7 @@ > import java.util.Arrays; > import java.util.Comparator; > >+import org.eclipse.core.runtime.Assert; > import org.eclipse.jdt.core.compiler.CharOperation; > import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration; > import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; >@@ -45,6 +46,8 @@ > > private SimpleLookupTable compatibleCache; > >+ int typeBits; >+ > public static final ReferenceBinding LUB_GENERIC = new ReferenceBinding() { /* used for lub computation */}; > > private static final Comparator FIELD_COMPARATOR = new Comparator() { >@@ -428,8 +431,10 @@ > case 'A' : > switch(typeName.length) { > case 13 : >- if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_AUTOCLOSEABLE[2])) >+ if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_AUTOCLOSEABLE[2])) { > this.id = TypeIds.T_JavaLangAutoCloseable; >+ this.typeBits |= TypeIds.BitAutoCloseable; >+ } > return; > case 14: > if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ASSERTIONERROR[2])) >@@ -928,6 +933,12 @@ > return (this.modifiers & ExtraCompilerModifiers.AccRestrictedAccess) != 0; > } > >+public boolean hasTypeBit(int bit) { >+ // overridden in relevant subclasses >+ Assert.isTrue(false, "Unsupported query for type "+getClass().getName()); //$NON-NLS-1$ >+ return false; // never reached >+} >+ > /** Answer true if the receiver implements anInterface or is identical to anInterface. > * If searchHierarchy is true, then also search the receiver's superclasses. > * >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java,v >retrieving revision 1.186.2.5 >diff -u -r1.186.2.5 SourceTypeBinding.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java 20 Apr 2011 20:40:00 -0000 1.186.2.5 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java 7 Jul 2011 09:04:52 -0000 >@@ -18,6 +18,7 @@ > import java.util.HashMap; > import java.util.Iterator; > >+import org.eclipse.core.runtime.Assert; > import org.eclipse.jdt.core.compiler.CharOperation; > import org.eclipse.jdt.internal.compiler.ast.ASTNode; > import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration; >@@ -1066,6 +1067,13 @@ > return accessors[1]; > } > >+public boolean hasTypeBit(int bit) { >+ // source types initialize type bits during connectSuperclass/interfaces() >+ if (!isLocalType()) >+ Assert.isTrue((this.tagBits & TagBits.EndHierarchyCheck) != 0, "Hierarchy should be connected"); //$NON-NLS-1$ >+ return (this.typeBits & bit) != 0; >+} >+ > /** > * @see org.eclipse.jdt.internal.compiler.lookup.Binding#initializeDeprecatedAnnotationTagBits() > */ >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.6 >diff -u -r1.51.2.6 TypeConstants.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java 28 Jun 2011 15:35:07 -0000 1.51.2.6 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java 7 Jul 2011 09:04:52 -0000 >@@ -154,6 +154,7 @@ > "MethodHandle$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.5 >diff -u -r1.36.2.5 TypeIds.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java 28 Jun 2011 15:35:07 -0000 1.36.2.5 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java 7 Jul 2011 09:04:52 -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/lookup/TypeVariableBinding.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java,v >retrieving revision 1.75 >diff -u -r1.75 TypeVariableBinding.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java 25 Oct 2010 08:50:02 -0000 1.75 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java 7 Jul 2011 09:04:52 -0000 >@@ -42,6 +42,7 @@ > this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature; // treat type var as public > this.tagBits |= TagBits.HasTypeVariable; > this.environment = environment; >+ this.typeBits = -1; > } > > /** >@@ -307,6 +308,19 @@ > return true; > } > >+ public boolean hasTypeBit(int bit) { >+ if (this.typeBits == -1) { >+ // initialize from bounds >+ this.typeBits = 0; >+ if (this.superclass != null) >+ this.typeBits |= this.superclass.typeBits; >+ if (this.superInterfaces != null) >+ for (int i = 0, l = this.superInterfaces.length; i < l; i++) >+ this.typeBits |= this.superInterfaces[i].typeBits; >+ } >+ return (this.typeBits & bit) != 0; >+ } >+ > /** > * Returns true if the type variable is directly bound to a given type > */ >Index: compiler/org/eclipse/jdt/internal/compiler/lookup/WildcardBinding.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/WildcardBinding.java,v >retrieving revision 1.76 >diff -u -r1.76 WildcardBinding.java >--- compiler/org/eclipse/jdt/internal/compiler/lookup/WildcardBinding.java 24 Jun 2009 18:21:59 -0000 1.76 >+++ compiler/org/eclipse/jdt/internal/compiler/lookup/WildcardBinding.java 7 Jul 2011 09:04:53 -0000 >@@ -54,6 +54,7 @@ > if (bound instanceof UnresolvedReferenceBinding) > ((UnresolvedReferenceBinding) bound).addWrapper(this, environment); > this.tagBits |= TagBits.HasUnresolvedTypeVariables; // cleared in resolve() >+ this.typeBits = -1; > } > > public int kind() { >@@ -420,6 +421,19 @@ > return this.genericType.hashCode(); > } > >+ public boolean hasTypeBit(int bit) { >+ if (this.typeBits == -1) { >+ // initialize from upper bounds >+ this.typeBits = 0; >+ if (this.superclass != null) >+ this.typeBits |= this.superclass.typeBits; >+ if (this.superInterfaces != null) >+ for (int i = 0, l = this.superInterfaces.length; i < l; i++) >+ this.typeBits |= this.superInterfaces[i].typeBits; >+ } >+ return (this.typeBits & bit) != 0; >+ } >+ > void initialize(ReferenceBinding someGenericType, TypeBinding someBound, TypeBinding[] someOtherBounds) { > this.genericType = someGenericType; > this.bound = someBound; >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.34 >diff -u -r1.430.2.34 ProblemReporter.java >--- compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java 6 Jul 2011 18:46:35 -0000 1.430.2.34 >+++ compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java 7 Jul 2011 09:04:58 -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; >@@ -430,6 +431,15 @@ > > case IProblem.MethodCanBePotentiallyStatic: > return CompilerOptions.MethodCanBePotentiallyStatic; >+ >+ case IProblem.UnclosedCloseable: >+ case IProblem.UnclosedCloseableAtExit: >+ return CompilerOptions.UnclosedCloseable; >+ case IProblem.PotentiallyUnclosedCloseable: >+ case IProblem.PotentiallyUnclosedCloseableAtExit: >+ return CompilerOptions.PotentiallyUnclosedCloseable; >+ case IProblem.ExplicitlyClosedAutoCloseable: >+ return CompilerOptions.ExplicitlyClosedAutoCloseable; > } > return 0; > } >@@ -462,6 +472,7 @@ > case CompilerOptions.ParameterAssignment : > case CompilerOptions.MethodCanBeStatic : > case CompilerOptions.MethodCanBePotentiallyStatic : >+ case CompilerOptions.ExplicitlyClosedAutoCloseable : > return CategorizedProblem.CAT_CODE_STYLE; > > case CompilerOptions.MaskedCatchBlock : >@@ -483,6 +494,8 @@ > case CompilerOptions.ShouldImplementHashcode : > case CompilerOptions.DeadCode : > case CompilerOptions.UnusedObjectAllocation : >+ case CompilerOptions.UnclosedCloseable : >+ case CompilerOptions.PotentiallyUnclosedCloseable : > return CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM; > > case CompilerOptions.OverriddenPackageDefaultMethod : >@@ -7884,4 +7897,49 @@ > 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); >+ } >+} >+public void explicitlyClosedAutoCloseable(FakedTrackingVariable trackVar) { >+ String[] args = { String.valueOf(trackVar.name) }; >+ this.handle( >+ IProblem.ExplicitlyClosedAutoCloseable, >+ args, >+ args, >+ trackVar.sourceStart, >+ trackVar.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.31 >diff -u -r1.262.2.31 messages.properties >--- compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties 6 Jul 2011 18:46:35 -0000 1.262.2.31 >+++ compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties 7 Jul 2011 09:04:59 -0000 >@@ -646,6 +646,11 @@ > 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 = '<>' operator is not allowed for source level below 1.7 >+884 = Value of type AutoCloseable is not closed on all paths. >+885 = Instance '{0}' of type AutoCloseable may be unclosed at this point. >+886 = Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources. >+887 = Instance '{0}' of type AutoCloseable is not closed at this point. >+888 = Instance '{0}' should be managed by try-with-resource. > > ### ELABORATIONS > ## Access restrictions >Index: model/org/eclipse/jdt/core/JavaCore.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/model/org/eclipse/jdt/core/JavaCore.java,v >retrieving revision 1.659.2.2 >diff -u -r1.659.2.2 JavaCore.java >--- model/org/eclipse/jdt/core/JavaCore.java 19 Apr 2011 16:41:17 -0000 1.659.2.2 >+++ model/org/eclipse/jdt/core/JavaCore.java 7 Jul 2011 09:05:02 -0000 >@@ -1357,6 +1357,52 @@ > */ > public static final String COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD = PLUGIN_ID + ".compiler.problem.reportMethodCanBePotentiallyStatic"; //$NON-NLS-1$ > /** >+ * Compiler option ID: Reporting a resource that is not closed properly. >+ * <p>When enabled, that compiler will issue an error or a warning if a local variable >+ * holds a value of type AutoCloseable (or Closeable if compliance is < 1.7) >+ * and if flow analysis shows that the method <code>close()</code> is not invoked locally on that value. >+ * <dl> >+ * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportUnclosedCloseable"</code></dd> >+ * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd> >+ * <dt>Default:</dt><dd><code>"warning"</code></dd> >+ * </dl> >+ * @since 3.7 >+ * @category CompilerOptionID >+ */ >+ public static final String COMPILER_PB_UNCLOSED_CLOSEABLE = PLUGIN_ID + ".compiler.problem.unclosedCloseable"; //$NON-NLS-1$ >+ /** >+ * Compiler option ID: Reporting a resource that may not be closed properly. >+ * <p>When enabled, that compiler will issue an error or a warning if a local variable >+ * holds a value of type AutoCloseable (or Closeable if compliance is < 1.7) >+ * and if flow analysis shows that the method <code>close()</code> is not invoked locally >+ * on that value for all execution paths. >+ * <dl> >+ * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportPotentiallyUnclosedCloseable"</code></dd> >+ * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd> >+ * <dt>Default:</dt><dd><code>"ignore"</code></dd> >+ * </dl> >+ * @since 3.7 >+ * @category CompilerOptionID >+ */ >+ public static final String COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE = PLUGIN_ID + ".compiler.problem.potentiallyUnclosedCloseable"; //$NON-NLS-1$ >+ /** >+ * Compiler option ID: Reporting a resource that is not managed by try-with-resources. >+ * <p>When enabled, that compiler will issue an error or a warning if a local variable >+ * holds a value of type AutoCloseable, and if the method <code>close()</code> is >+ * explicitly invoked on that resource, but the resource is not managed by a >+ * try-with-resources block. >+ * <p>Note that this option is not surfaced in the UI, as it is intended only for internal >+ * use for computing quick assists / cleanups. >+ * <dl> >+ * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportPotentiallyUnclosedCloseable"</code></dd> >+ * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd> >+ * <dt>Default:</dt><dd><code>"ignore"</code></dd> >+ * </dl> >+ * @since 3.7 >+ * @category CompilerOptionID >+ */ >+ public static final String COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE = PLUGIN_ID + ".compiler.problem.explicitlyClosedAutoCloseable"; //$NON-NLS-1$ >+ /** > * Compiler option ID: Setting Source Compatibility Mode. > * <p>Specify whether which source level compatibility is used. From 1.4 on, <code>'assert'</code> is a keyword > * reserved for assertion support. Also note, than when toggling to 1.4 mode, the target VM >#P org.eclipse.jdt.core.tests.compiler >Index: src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java,v >retrieving revision 1.224.2.2 >diff -u -r1.224.2.2 BatchCompilerTest.java >--- src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java 28 Jun 2011 19:34:33 -0000 1.224.2.2 >+++ src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java 7 Jul 2011 09:05:10 -0000 >@@ -1816,6 +1816,7 @@ > " <option key=\"org.eclipse.jdt.core.compiler.problem.discouragedReference\" value=\"warning\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.emptyStatement\" value=\"ignore\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.enumIdentifier\" value=\"warning\"/>\n" + >+ " <option key=\"org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable\" value=\"ignore\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.fallthroughCase\" value=\"ignore\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.fatalOptionalError\" value=\"disabled\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.fieldHiding\" value=\"ignore\"/>\n" + >@@ -1857,6 +1858,7 @@ > " <option key=\"org.eclipse.jdt.core.compiler.problem.parameterAssignment\" value=\"ignore\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment\" value=\"ignore\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.potentialNullReference\" value=\"ignore\"/>\n" + >+ " <option key=\"org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable\" value=\"ignore\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.rawTypeReference\" value=\"warning\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.redundantNullCheck\" value=\"ignore\"/>\n" + > " <option key=\"org.eclipse.jdt.core.compiler.problem.redundantSuperinterface\" value=\"ignore\"/>\n" + >@@ -1871,6 +1873,7 @@ > " <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=\"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" + >Index: src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java >=================================================================== >RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java,v >retrieving revision 1.40.2.19 >diff -u -r1.40.2.19 CompilerInvocationTests.java >--- src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java 6 Jul 2011 18:45:55 -0000 1.40.2.19 >+++ src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java 7 Jul 2011 09:05:12 -0000 >@@ -444,6 +444,7 @@ > expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", DEPRECATED); > expectedProblemAttributes.put("ExceptionTypeNotFound", DEPRECATED); > expectedProblemAttributes.put("ExceptionTypeNotVisible", DEPRECATED); >+ expectedProblemAttributes.put("ExplicitlyClosedAutoCloseable", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE)); > expectedProblemAttributes.put("ExpressionShouldBeAVariable", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX)); > expectedProblemAttributes.put("ExternalProblemFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); > expectedProblemAttributes.put("ExternalProblemNotFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL)); >@@ -768,6 +769,8 @@ > expectedProblemAttributes.put("PolymorphicMethodNotBelow17", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); > expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM)); > 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("PotentialNullLocalVariableReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM)); > expectedProblemAttributes.put("PublicClassMustMatchFileName", new ProblemAttributes(CategorizedProblem.CAT_TYPE)); > expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", new ProblemAttributes(CategorizedProblem.CAT_TYPE)); >@@ -830,6 +833,8 @@ > expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE)); > expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(CategorizedProblem.CAT_NAME_SHADOWING_CONFLICT)); > expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE)); >+ expectedProblemAttributes.put("UnclosedCloseable", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM)); >+ expectedProblemAttributes.put("UnclosedCloseableAtExit", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM)); > expectedProblemAttributes.put("UndefinedAnnotationMember", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); > expectedProblemAttributes.put("UndefinedConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); > expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER)); >@@ -1110,6 +1115,7 @@ > expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", SKIP); > expectedProblemAttributes.put("ExceptionTypeNotFound", SKIP); > expectedProblemAttributes.put("ExceptionTypeNotVisible", SKIP); >+ expectedProblemAttributes.put("ExplicitlyClosedAutoCloseable", new ProblemAttributes(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE)); > expectedProblemAttributes.put("ExpressionShouldBeAVariable", SKIP); > expectedProblemAttributes.put("ExternalProblemFixable", SKIP); > expectedProblemAttributes.put("ExternalProblemNotFixable", SKIP); >@@ -1434,6 +1440,8 @@ > expectedProblemAttributes.put("PolymorphicMethodNotBelow17", SKIP); > expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(JavaCore.COMPILER_PB_POSSIBLE_ACCIDENTAL_BOOLEAN_ASSIGNMENT)); > 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("PotentialNullLocalVariableReference", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE)); > expectedProblemAttributes.put("PublicClassMustMatchFileName", SKIP); > expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", SKIP); >@@ -1496,6 +1504,8 @@ > expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION)); > expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(JavaCore.COMPILER_PB_TYPE_PARAMETER_HIDING)); > expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(JavaCore.COMPILER_PB_AUTOBOXING)); >+ expectedProblemAttributes.put("UnclosedCloseable", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE)); >+ expectedProblemAttributes.put("UnclosedCloseableAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE)); > expectedProblemAttributes.put("UndefinedAnnotationMember", SKIP); > expectedProblemAttributes.put("UndefinedConstructor", SKIP); > expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", SKIP); >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 7 Jul 2011 09:05:14 -0000 >@@ -16,13 +16,14 @@ > > import java.util.Map; > >+import org.eclipse.jdt.core.JavaCore; > import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; > > import junit.framework.Test; > 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 }; > } >@@ -489,12 +490,17 @@ > " ^^\n" + > "Dead code\n" + > "----------\n" + >- "3. ERROR in X.java (at line 5)\n" + >+ "3. WARNING in X.java (at line 5)\n" + >+ " Y why = new Y();\n" + >+ " ^^^\n" + >+ "Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + >+ "----------\n" + >+ "4. ERROR in X.java (at line 5)\n" + > " Y why = new Y();\n" + > " ^^^^^^^\n" + > "Unhandled exception type WeirdException\n" + > "----------\n" + >- "4. WARNING in X.java (at line 22)\n" + >+ "5. WARNING in X.java (at line 22)\n" + > " class WeirdException extends Throwable {}\n" + > " ^^^^^^^^^^^^^^\n" + > "The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + >@@ -562,12 +568,17 @@ > " ^^\n" + > "Dead code\n" + > "----------\n" + >- "3. ERROR in X.java (at line 5)\n" + >+ "3. WARNING in X.java (at line 5)\n" + >+ " Y why = new Y();\n" + >+ " ^^^\n" + >+ "Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + >+ "----------\n" + >+ "4. ERROR in X.java (at line 5)\n" + > " Y why = new Y();\n" + > " ^^^^^^^\n" + > "Unhandled exception type WeirdException\n" + > "----------\n" + >- "4. WARNING in X.java (at line 20)\n" + >+ "5. WARNING in X.java (at line 20)\n" + > " class WeirdException extends Throwable {}\n" + > " ^^^^^^^^^^^^^^\n" + > "The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + >@@ -3341,6 +3352,373 @@ > "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(); >+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); >+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); >+ 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" + >+// not invoking any methods on FileReader, try to avoid necessary call to superclass() >+// " 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_ReportUnclosedCloseable, CompilerOptions.ERROR); >+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); >+ 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_ReportUnclosedCloseable, CompilerOptions.ERROR); >+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); >+ 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 two AutoCloseables (testing independent analysis) >+// - one closeable may be unclosed at a conditional return >+// - the other is only conditionally closed >+public void test055c() { >+ Map options = getCompilerOptions(); >+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); >+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); >+ 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 flag1, boolean flag2) throws IOException {\n" + >+ " File file = new File(\"somefile\");\n" + >+ " char[] in = new char[50];\n" + >+ " FileReader fileReader1 = new FileReader(file);\n" + >+ " fileReader1.read(in);\n" + >+ " FileReader fileReader2 = new FileReader(file);\n" + >+ " fileReader2.read(in);\n" + >+ " if (flag1) {\n" + >+ " fileReader2.close();\n" + >+ " return;\n" + >+ " } else if (flag2) {\n" + >+ " fileReader2.close();\n" + >+ " }\n" + >+ " fileReader1.close();\n" + >+ " }\n" + >+ " public static void main(String[] args) throws IOException {\n" + >+ " new X().foo(false, true);\n" + >+ " }\n" + >+ "}\n" >+ }, >+ "----------\n" + >+ "1. WARNING in X.java (at line 10)\n" + >+ " FileReader fileReader2 = new FileReader(file);\n" + >+ " ^^^^^^^^^^^\n" + >+ "Value of type AutoCloseable is not closed on all paths.\n" + >+ "----------\n" + >+ "2. ERROR in X.java (at line 14)\n" + >+ " return;\n" + >+ " ^^^^^^^\n" + >+ "Instance \'fileReader1\' 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(); >+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); >+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); >+ 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); >+} >+// Bug 349326 - [1.7] new warning for missing try-with-resources >+// a method explicitly closes its AutoCloseable rather than using t-w-r >+public void test055e() { >+ Map options = getCompilerOptions(); >+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); >+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); >+ options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, 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;\n" + >+ " 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) throws IOException {\n" + >+ " new X().foo();\n" + >+ " }\n" + >+ "}\n" >+ }, >+ "----------\n" + >+ "1. ERROR in X.java (at line 8)\n" + >+ " fileReader = new FileReader(file);\n" + >+ " ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + >+ "Instance \'fileReader\' should be managed by try-with-resource.\n" + >+ "----------\n", >+ null, >+ true, >+ options); >+} >+// Bug 349326 - [1.7] new warning for missing try-with-resources >+// an AutoCloseable local is re-assigned >+public void test055f() { >+ Map options = getCompilerOptions(); >+ options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); >+ options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); >+ options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); >+ 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" + >+ " fileReader = new FileReader(file);\n" + >+ " fileReader.read(in);\n" + >+ " fileReader.close();\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 10)\n" + >+ " fileReader = new FileReader(file);\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 >+// two AutoCloseables at different nesting levels (anonymous local type) >+public void test055g() { >+ Map options = getCompilerOptions(); >+ options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); >+ options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); >+ options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); >+ 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" + >+ " final File file = new File(\"somefile\");\n" + >+ " final FileReader fileReader = new FileReader(file);\n" + >+ " char[] in = new char[50];\n" + >+ " fileReader.read(in);\n" + >+ " new Runnable() {\n public void run() {\n" + >+ " try {\n" + >+ " fileReader.close();\n" + >+ " FileReader localReader = new FileReader(file);\n" + >+ " } catch (IOException ex) { /* nop */ }\n" + >+ " }}.run();\n" + >+ " }\n" + >+ " public static void main(String[] args) throws IOException {\n" + >+ " new X().foo();\n" + >+ " }\n" + >+ "}\n" >+ }, >+ "----------\n" + >+ "1. WARNING in X.java (at line 7)\n" + >+ " final FileReader fileReader = new FileReader(file);\n" + >+ " ^^^^^^^^^^\n" + >+ "Value of type AutoCloseable is not closed on all paths.\n" + >+ "----------\n" + >+ "2. ERROR in X.java (at line 14)\n" + >+ " FileReader localReader = 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 >+// three AutoCloseables in different blocks of the same method >+public void test055h() { >+ Map options = getCompilerOptions(); >+ options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); >+ options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); >+ options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); >+ 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 f1, boolean f2) throws IOException {\n" + >+ " File file = new File(\"somefile\");\n" + >+ " if (f1) {\n" + >+ " FileReader fileReader = new FileReader(file); // err: not closed\n" + >+ " char[] in = new char[50];\n" + >+ " fileReader.read(in);\n" + >+ " while (true) {\n" + >+ " FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + >+ " loopReader.close();" + >+ " break;\n" + >+ " }\n" + >+ " } else {\n" + >+ " FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + >+ " if (f2)\n" + >+ " fileReader.close();\n" + >+ " }\n" + >+ " }\n" + >+ " public static void main(String[] args) throws IOException {\n" + >+ " new X().foo(true, true);\n" + >+ " }\n" + >+ "}\n" >+ }, >+ "----------\n" + >+ "1. ERROR in X.java (at line 8)\n" + >+ " FileReader fileReader = new FileReader(file); // err: not closed\n" + >+ " ^^^^^^^^^^\n" + >+ "Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + >+ "----------\n" + >+ "2. WARNING in X.java (at line 16)\n" + >+ " FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + >+ " ^^^^^^^^^^\n" + >+ "Value of type AutoCloseable is not closed on all paths.\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