View | Details | Raw Unified | Return to bug 349326 | Differences between
and this patch

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/core/compiler/IProblem.java (+11 lines)
Lines 1392-1397 Link Here
1392
	int SwitchOnStringsNotBelow17 = TypeRelated + 881;	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=348492
1392
	int SwitchOnStringsNotBelow17 = TypeRelated + 881;	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=348492
1393
	/** @since 3.7 */
1393
	/** @since 3.7 */
1394
	int UnhandledExceptionOnAutoClose =  TypeRelated + 882;
1394
	int UnhandledExceptionOnAutoClose =  TypeRelated + 882;
1395
	/** @since 3.7 */
1396
	int PotentiallyUnclosedCloseable = Internal + 883;
1397
	/** @since 3.7 */
1398
	int PotentiallyUnclosedCloseableAtExit = Internal + 884;
1399
	/** @since 3.7 */
1400
	int UnclosedCloseable = Internal + 885;
1401
	/** @since 3.7 */
1402
	int UnclosedCloseableAtExit = Internal + 886;
1403
	/** @since 3.7 */
1404
	int ExplicitlyClosedAutoCloseable = Internal + 887;
1405
1395
	/**
1406
	/**
1396
	 * External problems -- These are problems defined by other plugins
1407
	 * External problems -- These are problems defined by other plugins
1397
	 */
1408
	 */
(-)compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java (+23 lines)
Lines 48-53 Link Here
48
	if ((this.expression.implicitConversion & TypeIds.UNBOXING) != 0) {
48
	if ((this.expression.implicitConversion & TypeIds.UNBOXING) != 0) {
49
		this.expression.checkNPE(currentScope, flowContext, flowInfo);
49
		this.expression.checkNPE(currentScope, flowContext, flowInfo);
50
	}
50
	}
51
	if (local != null) {
52
		LocalVariableBinding trackerBinding = null;
53
		if (local.closeTracker != null) {
54
			// Assigning to a variable already holding an AutoCloseable, has it been closed before?
55
			trackerBinding = local.closeTracker.binding;
56
			if (flowInfo.isDefinitelyNull(trackerBinding))
57
				currentScope.problemReporter().unclosedCloseable(local.closeTracker, this);
58
			else if (flowInfo.isPotentiallyNull(trackerBinding))
59
				currentScope.problemReporter().potentiallyUnclosedCloseable(local.closeTracker, this);
60
		}
61
		if (FakedTrackingVariable.isAutoCloseable(this.expression.resolvedType)) {
62
			if (local.closeTracker != null && local.closeTracker.isInsideTWR) { 
63
				// reassigning resource of try-with-resources is a different error
64
			} else {
65
				// new value is AutoCloseable start tracking, possibly re-using existing tracker var:
66
				if (trackerBinding == null) {
67
					local.closeTracker = new FakedTrackingVariable(currentScope, this, local.name);
68
					trackerBinding = local.closeTracker.binding;
69
				}
70
				flowInfo.markAsDefinitelyNull(trackerBinding);
71
			}
72
		}
73
	}
51
	flowInfo = ((Reference) this.lhs)
74
	flowInfo = ((Reference) this.lhs)
52
		.analyseAssignment(currentScope, flowContext, flowInfo, this, false)
75
		.analyseAssignment(currentScope, flowContext, flowInfo, this, false)
53
		.unconditionalInits();
76
		.unconditionalInits();
(-)compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java (+70 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 GK Software AG and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     GK Software AG - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.ast;
12
13
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
14
import org.eclipse.jdt.internal.compiler.impl.Constant;
15
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
16
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
17
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
18
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
19
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
20
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
21
22
/**
23
 * A faked local variable declaration used for keeping track of data flows of a
24
 * special variable. Certain events will be recorded by changing the null info
25
 * for this variable.
26
 * 
27
 * @author Stephan Herrmann
28
 */
29
public class FakedTrackingVariable extends LocalDeclaration {
30
31
	public boolean isInsideTWR = false;
32
33
	public FakedTrackingVariable(BlockScope currentScope, Statement location, char[] name) {
34
		super(name, location.sourceStart, location.sourceEnd);
35
		this.type = new SingleTypeReference(
36
				TypeConstants.OBJECT,
37
				((long)this.sourceStart <<32)+this.sourceEnd);
38
		resolve(currentScope);
39
	}
40
41
	public void generateCode(BlockScope currentScope, CodeStream codeStream)
42
	{ /* NOP - this variable is completely dummy, ie. for analysis only. */ }
43
44
	public void resolve (BlockScope scope) {
45
		// only need the binding, which is used as reference in FlowInfo methods.
46
		this.binding = new LocalVariableBinding(
47
				this.name,
48
				scope.getJavaLangObject(),  // dummy, just needs to be a reference type
49
				0,
50
				false);
51
		this.binding.setConstant(Constant.NotAConstant);
52
		this.binding.useFlag = LocalVariableBinding.USED;
53
		// use a free slot without assigning it:
54
		this.binding.id = scope.outerMostMethodScope().registerTrackingVariable(this);
55
	}
56
57
	public static boolean isAutoCloseable(TypeBinding typeBinding) {
58
		return typeBinding instanceof ReferenceBinding
59
			&& ((ReferenceBinding)typeBinding).hasTypeBit(TypeIds.BitAutoCloseable);
60
	}
61
	
62
	public static FakedTrackingVariable getTrackingVariable(Expression expression) {
63
		if (expression instanceof SingleNameReference) {
64
			SingleNameReference returnName = (SingleNameReference) expression;
65
			if (returnName.binding instanceof LocalVariableBinding)
66
				return ((LocalVariableBinding)returnName.binding).closeTracker;
67
		}
68
		return null;
69
	}
70
}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java (+4 lines)
Lines 71-76 Link Here
71
	if ((this.initialization.implicitConversion & TypeIds.UNBOXING) != 0) {
71
	if ((this.initialization.implicitConversion & TypeIds.UNBOXING) != 0) {
72
		this.initialization.checkNPE(currentScope, flowContext, flowInfo);
72
		this.initialization.checkNPE(currentScope, flowContext, flowInfo);
73
	}
73
	}
74
	if (FakedTrackingVariable.isAutoCloseable(this.initialization.resolvedType)) {
75
		this.binding.closeTracker = new FakedTrackingVariable(currentScope, this, this.name);
76
		flowInfo.markAsDefinitelyNull(this.binding.closeTracker.binding);
77
	}
74
	
78
	
75
	flowInfo =
79
	flowInfo =
76
		this.initialization
80
		this.initialization
(-)compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java (+5 lines)
Lines 42-47 Link Here
42
import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
42
import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
43
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
43
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
44
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
44
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
45
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
45
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
46
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
46
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
47
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
47
48
Lines 64-69 Link Here
64
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
65
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
65
	boolean nonStatic = !this.binding.isStatic();
66
	boolean nonStatic = !this.binding.isStatic();
66
	flowInfo = this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic).unconditionalInits();
67
	flowInfo = this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic).unconditionalInits();
68
	FakedTrackingVariable trackingVariable = FakedTrackingVariable.getTrackingVariable(this.receiver);
69
	if (   trackingVariable != null 
70
		&& CharOperation.equals(TypeConstants.CLOSE, this.selector))
71
		flowInfo.markAsDefinitelyNonNull(trackingVariable.binding);
67
	if (nonStatic) {
72
	if (nonStatic) {
68
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
73
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
69
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
74
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
(-)compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java (+1 lines)
Lines 134-139 Link Here
134
				}
134
				}
135
					
135
					
136
			}
136
			}
137
			this.scope.checkUnclosedCloseables(flowInfo, null);
137
		} catch (AbortMethod e) {
138
		} catch (AbortMethod e) {
138
			this.ignoreFurtherInvestigation = true;
139
			this.ignoreFurtherInvestigation = true;
139
		}
140
		}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ReturnStatement.java (+4 lines)
Lines 40-45 Link Here
40
		if ((this.expression.implicitConversion & TypeIds.UNBOXING) != 0) {
40
		if ((this.expression.implicitConversion & TypeIds.UNBOXING) != 0) {
41
			this.expression.checkNPE(currentScope, flowContext, flowInfo);
41
			this.expression.checkNPE(currentScope, flowContext, flowInfo);
42
		}
42
		}
43
		FakedTrackingVariable trackingVariable = FakedTrackingVariable.getTrackingVariable(this.expression);
44
		if (trackingVariable != null)
45
			flowInfo.markAsDefinitelyNonNull(trackingVariable.binding);
43
	}
46
	}
44
	this.initStateIndex =
47
	this.initStateIndex =
45
		currentScope.methodScope().recordInitializationStates(flowInfo);
48
		currentScope.methodScope().recordInitializationStates(flowInfo);
Lines 104-109 Link Here
104
			this.expression.bits |= ASTNode.IsReturnedValue;
107
			this.expression.bits |= ASTNode.IsReturnedValue;
105
		}
108
		}
106
	}
109
	}
110
	currentScope.checkUnclosedCloseables(flowInfo, this);
107
	return FlowInfo.DEAD_END;
111
	return FlowInfo.DEAD_END;
108
}
112
}
109
113
(-)compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java (-4 / +14 lines)
Lines 120-127 Link Here
120
120
121
		for (int i = 0, max = this.resources.length; i < max; i++) {
121
		for (int i = 0, max = this.resources.length; i < max; i++) {
122
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
122
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
123
			this.resources[i].binding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways.
123
			LocalVariableBinding resourceBinding = this.resources[i].binding;
124
			TypeBinding type = this.resources[i].binding.type;
124
			resourceBinding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways.
125
			if (resourceBinding.closeTracker != null) {
126
				resourceBinding.closeTracker.isInsideTWR = true;
127
				flowInfo.markAsDefinitelyNonNull(resourceBinding.closeTracker.binding);
128
			} 
129
			TypeBinding type = resourceBinding.type;
125
			if (type != null && type.isValidBinding()) {
130
			if (type != null && type.isValidBinding()) {
126
				ReferenceBinding binding = (ReferenceBinding) type;
131
				ReferenceBinding binding = (ReferenceBinding) type;
127
				MethodBinding closeMethod = binding.getExactMethod(ConstantPool.Close, new TypeBinding [0], this.scope.compilationUnitScope()); // scope needs to be tighter
132
				MethodBinding closeMethod = binding.getExactMethod(ConstantPool.Close, new TypeBinding [0], this.scope.compilationUnitScope()); // scope needs to be tighter
Lines 245-252 Link Here
245
250
246
		for (int i = 0, max = this.resources.length; i < max; i++) {
251
		for (int i = 0, max = this.resources.length; i < max; i++) {
247
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
252
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
248
			this.resources[i].binding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways.
253
			LocalVariableBinding resourceBinding = this.resources[i].binding;
249
			TypeBinding type = this.resources[i].binding.type;
254
			resourceBinding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways.
255
			if (resourceBinding.closeTracker != null) {
256
				resourceBinding.closeTracker.isInsideTWR = true;
257
				flowInfo.markAsDefinitelyNonNull(resourceBinding.closeTracker.binding);
258
			} 
259
			TypeBinding type = resourceBinding.type;
250
			if (type != null && type.isValidBinding()) {
260
			if (type != null && type.isValidBinding()) {
251
				ReferenceBinding binding = (ReferenceBinding) type;
261
				ReferenceBinding binding = (ReferenceBinding) type;
252
				MethodBinding closeMethod = binding.getExactMethod(ConstantPool.Close, new TypeBinding [0], this.scope.compilationUnitScope()); // scope needs to be tighter
262
				MethodBinding closeMethod = binding.getExactMethod(ConstantPool.Close, new TypeBinding [0], this.scope.compilationUnitScope()); // scope needs to be tighter
(-)compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java (+21 lines)
Lines 136-141 Link Here
136
	public static final String OPTION_IncludeNullInfoFromAsserts = "org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts";  //$NON-NLS-1$
136
	public static final String OPTION_IncludeNullInfoFromAsserts = "org.eclipse.jdt.core.compiler.problem.includeNullInfoFromAsserts";  //$NON-NLS-1$
137
	public static final String OPTION_ReportMethodCanBeStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic";  //$NON-NLS-1$
137
	public static final String OPTION_ReportMethodCanBeStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic";  //$NON-NLS-1$
138
	public static final String OPTION_ReportMethodCanBePotentiallyStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic";  //$NON-NLS-1$
138
	public static final String OPTION_ReportMethodCanBePotentiallyStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic";  //$NON-NLS-1$
139
	public static final String OPTION_ReportUnclosedCloseable = "org.eclipse.jdt.core.compiler.problem.unclosedCloseable"; //$NON-NLS-1$
140
	public static final String OPTION_ReportPotentiallyUnclosedCloseable = "org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable"; //$NON-NLS-1$
141
	public static final String OPTION_ReportExplicitlyClosedAutoCloseable = "org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable"; //$NON-NLS-1$
139
	/**
142
	/**
140
	 * Possible values for configurable options
143
	 * Possible values for configurable options
141
	 */
144
	 */
Lines 238-243 Link Here
238
	public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4;
241
	public static final int UnusedObjectAllocation = IrritantSet.GROUP2 | ASTNode.Bit4;
239
	public static final int MethodCanBeStatic = IrritantSet.GROUP2 | ASTNode.Bit5;
242
	public static final int MethodCanBeStatic = IrritantSet.GROUP2 | ASTNode.Bit5;
240
	public static final int MethodCanBePotentiallyStatic = IrritantSet.GROUP2 | ASTNode.Bit6;
243
	public static final int MethodCanBePotentiallyStatic = IrritantSet.GROUP2 | ASTNode.Bit6;
244
	public static final int UnclosedCloseable = IrritantSet.GROUP2 | ASTNode.Bit7;
245
	public static final int PotentiallyUnclosedCloseable = IrritantSet.GROUP2 | ASTNode.Bit8;
246
	public static final int ExplicitlyClosedAutoCloseable = IrritantSet.GROUP2 | ASTNode.Bit9;
241
247
242
	// Severity level for handlers
248
	// Severity level for handlers
243
	/** 
249
	/** 
Lines 547-552 Link Here
547
				return OPTION_ReportMethodCanBeStatic;
553
				return OPTION_ReportMethodCanBeStatic;
548
			case MethodCanBePotentiallyStatic :
554
			case MethodCanBePotentiallyStatic :
549
				return OPTION_ReportMethodCanBePotentiallyStatic;
555
				return OPTION_ReportMethodCanBePotentiallyStatic;
556
			case UnclosedCloseable :
557
				return OPTION_ReportUnclosedCloseable;
558
			case PotentiallyUnclosedCloseable :
559
				return OPTION_ReportPotentiallyUnclosedCloseable;
560
			case ExplicitlyClosedAutoCloseable :
561
				return OPTION_ReportExplicitlyClosedAutoCloseable;
550
		}
562
		}
551
		return null;
563
		return null;
552
	}
564
	}
Lines 709-714 Link Here
709
			OPTION_ReportUnusedTypeArgumentsForMethodInvocation,
721
			OPTION_ReportUnusedTypeArgumentsForMethodInvocation,
710
			OPTION_ReportUnusedWarningToken,
722
			OPTION_ReportUnusedWarningToken,
711
			OPTION_ReportVarargsArgumentNeedCast,
723
			OPTION_ReportVarargsArgumentNeedCast,
724
			OPTION_ReportUnclosedCloseable,
725
			OPTION_ReportPotentiallyUnclosedCloseable,
726
			OPTION_ReportExplicitlyClosedAutoCloseable,
712
		};
727
		};
713
		return result;
728
		return result;
714
	}
729
	}
Lines 973-978 Link Here
973
		optionsMap.put(OPTION_IncludeNullInfoFromAsserts, this.includeNullInfoFromAsserts ? ENABLED : DISABLED);
988
		optionsMap.put(OPTION_IncludeNullInfoFromAsserts, this.includeNullInfoFromAsserts ? ENABLED : DISABLED);
974
		optionsMap.put(OPTION_ReportMethodCanBeStatic, getSeverityString(MethodCanBeStatic));
989
		optionsMap.put(OPTION_ReportMethodCanBeStatic, getSeverityString(MethodCanBeStatic));
975
		optionsMap.put(OPTION_ReportMethodCanBePotentiallyStatic, getSeverityString(MethodCanBePotentiallyStatic));
990
		optionsMap.put(OPTION_ReportMethodCanBePotentiallyStatic, getSeverityString(MethodCanBePotentiallyStatic));
991
		optionsMap.put(OPTION_ReportUnclosedCloseable, getSeverityString(UnclosedCloseable));
992
		optionsMap.put(OPTION_ReportPotentiallyUnclosedCloseable, getSeverityString(PotentiallyUnclosedCloseable));
993
		optionsMap.put(OPTION_ReportExplicitlyClosedAutoCloseable, getSeverityString(ExplicitlyClosedAutoCloseable));
976
		return optionsMap;
994
		return optionsMap;
977
	}
995
	}
978
996
Lines 1402-1407 Link Here
1402
		if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue);
1420
		if ((optionValue = optionsMap.get(OPTION_ReportUnusedObjectAllocation)) != null) updateSeverity(UnusedObjectAllocation, optionValue);
1403
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBeStatic)) != null) updateSeverity(MethodCanBeStatic, optionValue);
1421
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBeStatic)) != null) updateSeverity(MethodCanBeStatic, optionValue);
1404
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBePotentiallyStatic)) != null) updateSeverity(MethodCanBePotentiallyStatic, optionValue);
1422
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBePotentiallyStatic)) != null) updateSeverity(MethodCanBePotentiallyStatic, optionValue);
1423
		if ((optionValue = optionsMap.get(OPTION_ReportUnclosedCloseable)) != null) updateSeverity(UnclosedCloseable, optionValue);
1424
		if ((optionValue = optionsMap.get(OPTION_ReportPotentiallyUnclosedCloseable)) != null) updateSeverity(PotentiallyUnclosedCloseable, optionValue);
1425
		if ((optionValue = optionsMap.get(OPTION_ReportExplicitlyClosedAutoCloseable)) != null) updateSeverity(ExplicitlyClosedAutoCloseable, optionValue);
1405
1426
1406
		// Javadoc options
1427
		// Javadoc options
1407
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
1428
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
(-)compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java (-1 / +2 lines)
Lines 99-105 Link Here
99
			// group-2 warnings enabled by default
99
			// group-2 warnings enabled by default
100
			.set(
100
			.set(
101
				CompilerOptions.DeadCode
101
				CompilerOptions.DeadCode
102
				|CompilerOptions.Tasks);
102
				|CompilerOptions.Tasks
103
				|CompilerOptions.UnclosedCloseable);
103
			
104
			
104
		ALL.setAll();
105
		ALL.setAll();
105
		HIDING
106
		HIDING
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java (+19 lines)
Lines 971-976 Link Here
971
	variable.resolve();
971
	variable.resolve();
972
	return variable;
972
	return variable;
973
}
973
}
974
public boolean hasTypeBit(int bit) {
975
	// ensure hierarchy is resolved
976
	superclass();
977
	superInterfaces();
978
	return (this.typeBits & bit) != 0;
979
}
974
private void initializeTypeVariable(TypeVariableBinding variable, TypeVariableBinding[] existingVariables, SignatureWrapper wrapper, char[][][] missingTypeNames) {
980
private void initializeTypeVariable(TypeVariableBinding variable, TypeVariableBinding[] existingVariables, SignatureWrapper wrapper, char[][][] missingTypeNames) {
975
	// ParameterSignature = Identifier ':' TypeSignature
981
	// ParameterSignature = Identifier ':' TypeSignature
976
	//   or Identifier ':' TypeSignature(optional) InterfaceBound(s)
982
	//   or Identifier ':' TypeSignature(optional) InterfaceBound(s)
Lines 1141-1146 Link Here
1141
	this.tagBits &= ~TagBits.HasUnresolvedSuperclass;
1147
	this.tagBits &= ~TagBits.HasUnresolvedSuperclass;
1142
	if (this.superclass.problemId() == ProblemReasons.NotFound)
1148
	if (this.superclass.problemId() == ProblemReasons.NotFound)
1143
		this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency
1149
		this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency
1150
	else {
1151
		this.superclass.superclass();
1152
		this.superclass.superInterfaces();
1153
	}
1154
	this.typeBits |= this.superclass.typeBits;
1144
	return this.superclass;
1155
	return this.superclass;
1145
}
1156
}
1146
// NOTE: superInterfaces of binary types are resolved when needed
1157
// NOTE: superInterfaces of binary types are resolved when needed
Lines 1152-1157 Link Here
1152
		this.superInterfaces[i] = (ReferenceBinding) resolveType(this.superInterfaces[i], this.environment, true /* raw conversion */);
1163
		this.superInterfaces[i] = (ReferenceBinding) resolveType(this.superInterfaces[i], this.environment, true /* raw conversion */);
1153
		if (this.superInterfaces[i].problemId() == ProblemReasons.NotFound)
1164
		if (this.superInterfaces[i].problemId() == ProblemReasons.NotFound)
1154
			this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency
1165
			this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency
1166
		else
1167
		// TODO(SH): check if this actually speeds up anything:
1168
		if ((this.superInterfaces[i].tagBits & (TagBits.HasUnresolvedSuperclass|TagBits.HasUnresolvedSuperinterfaces)) != 0) 
1169
		{
1170
			this.superInterfaces[i].superclass();
1171
			this.superInterfaces[i].superInterfaces();
1172
		}
1173
		this.typeBits |= this.superInterfaces[i].typeBits;
1155
	}
1174
	}
1156
	this.tagBits &= ~TagBits.HasUnresolvedSuperinterfaces;
1175
	this.tagBits &= ~TagBits.HasUnresolvedSuperinterfaces;
1157
	return this.superInterfaces;
1176
	return this.superInterfaces;
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java (+27 lines)
Lines 10-19 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
11
package org.eclipse.jdt.internal.compiler.lookup;
12
12
13
import java.util.ArrayList;
14
import java.util.List;
15
13
import org.eclipse.jdt.core.compiler.CharOperation;
16
import org.eclipse.jdt.core.compiler.CharOperation;
14
import org.eclipse.jdt.internal.compiler.ast.*;
17
import org.eclipse.jdt.internal.compiler.ast.*;
15
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
18
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
16
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
19
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
20
import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
17
import org.eclipse.jdt.internal.compiler.impl.Constant;
21
import org.eclipse.jdt.internal.compiler.impl.Constant;
18
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
22
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
19
23
Lines 957-960 Link Here
957
		}
961
		}
958
	}
962
	}
959
}
963
}
964
965
// TODO: List or array?
966
// TODO: move these to FlowInfo? (beware of flowInfo copying!)
967
private List trackingVariables;
968
public int addTrackingVariable(FakedTrackingVariable fakedTrackingVariable) {
969
	if (this.trackingVariables == null)
970
		this.trackingVariables = new ArrayList();
971
	this.trackingVariables.add(fakedTrackingVariable);
972
	return this.trackingVariables.size();
973
}
974
975
public void checkUnclosedCloseables(FlowInfo flowInfo, ASTNode location) {
976
	if (this.trackingVariables == null) return;
977
	for (int i=0; i<this.trackingVariables.size(); i++) {
978
		FakedTrackingVariable trackingVar = (FakedTrackingVariable) this.trackingVariables.get(i);
979
		if (flowInfo.isDefinitelyNull(trackingVar.binding))
980
			problemReporter().unclosedCloseable(trackingVar, location);
981
		else if (flowInfo.isPotentiallyNull(trackingVar.binding))
982
			problemReporter().potentiallyUnclosedCloseable(trackingVar, location);
983
		else if (environment().globalOptions.complianceLevel >= ClassFileConstants.JDK1_7 && !trackingVar.isInsideTWR)
984
			problemReporter().explicitlyClosedAutoCloseable(trackingVar);
985
	}	
986
}
960
}
987
}
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java (+2 lines)
Lines 907-912 Link Here
907
			} else {
907
			} else {
908
				// only want to reach here when no errors are reported
908
				// only want to reach here when no errors are reported
909
				sourceType.superclass = superclass;
909
				sourceType.superclass = superclass;
910
				sourceType.typeBits |= superclass.typeBits;
910
				return true;
911
				return true;
911
			}
912
			}
912
		}
913
		}
Lines 1017-1022 Link Here
1017
				noProblems &= superInterfaceRef.resolvedType.isValidBinding();
1018
				noProblems &= superInterfaceRef.resolvedType.isValidBinding();
1018
			}
1019
			}
1019
			// only want to reach here when no errors are reported
1020
			// only want to reach here when no errors are reported
1021
			sourceType.typeBits |= superInterface.typeBits;
1020
			interfaceBindings[count++] = superInterface;
1022
			interfaceBindings[count++] = superInterface;
1021
		}
1023
		}
1022
		// hold onto all correctly resolved superinterfaces
1024
		// hold onto all correctly resolved superinterfaces
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/LocalVariableBinding.java (+3 lines)
Lines 19-24 Link Here
19
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
19
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
20
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
20
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
21
import org.eclipse.jdt.internal.compiler.ast.Annotation;
21
import org.eclipse.jdt.internal.compiler.ast.Annotation;
22
import org.eclipse.jdt.internal.compiler.ast.FakedTrackingVariable;
22
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
23
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
23
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
24
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
24
import org.eclipse.jdt.internal.compiler.impl.Constant;
25
import org.eclipse.jdt.internal.compiler.impl.Constant;
Lines 39-44 Link Here
39
	public int[] initializationPCs;
40
	public int[] initializationPCs;
40
	public int initializationCount = 0;
41
	public int initializationCount = 0;
41
42
43
	public FakedTrackingVariable closeTracker;
44
42
	// for synthetic local variables
45
	// for synthetic local variables
43
	// if declaration slot is not positionned, the variable will not be listed in attribute
46
	// if declaration slot is not positionned, the variable will not be listed in attribute
44
	// 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)
47
	// 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)
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/MethodScope.java (+6 lines)
Lines 479-482 Link Here
479
public TypeDeclaration referenceType() {
479
public TypeDeclaration referenceType() {
480
	return ((ClassScope) this.parent).referenceContext;
480
	return ((ClassScope) this.parent).referenceContext;
481
}
481
}
482
483
public int registerTrackingVariable(FakedTrackingVariable fakedTrackingVariable) {
484
	int offset = addTrackingVariable(fakedTrackingVariable);
485
	return this.analysisIndex + offset;
486
	
487
}
482
}
488
}
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java (+7 lines)
Lines 629-634 Link Here
629
	    return this.type.hasMemberTypes();
629
	    return this.type.hasMemberTypes();
630
	}
630
	}
631
631
632
	public boolean hasTypeBit(int bit) {
633
		TypeBinding erasure = erasure();
634
		if (erasure instanceof ReferenceBinding)
635
			return ((ReferenceBinding) erasure).hasTypeBit(bit);
636
		return false;
637
	}
638
632
	/**
639
	/**
633
	 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#implementsMethod(MethodBinding)
640
	 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#implementsMethod(MethodBinding)
634
	 */
641
	 */
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ProblemReferenceBinding.java (+6 lines)
Lines 40-45 Link Here
40
	return this.closestMatch;
40
	return this.closestMatch;
41
}
41
}
42
42
43
public boolean hasTypeBit(int bit) {
44
	if (this.closestMatch != null)
45
		return this.closestMatch.hasTypeBit(bit);
46
	return false;
47
}
48
43
/* API
49
/* API
44
* Answer the problem id associated with the receiver.
50
* Answer the problem id associated with the receiver.
45
* NoError if the receiver is a valid binding.
51
* NoError if the receiver is a valid binding.
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java (-1 / +12 lines)
Lines 17-22 Link Here
17
import java.util.Arrays;
17
import java.util.Arrays;
18
import java.util.Comparator;
18
import java.util.Comparator;
19
19
20
import org.eclipse.core.runtime.Assert;
20
import org.eclipse.jdt.core.compiler.CharOperation;
21
import org.eclipse.jdt.core.compiler.CharOperation;
21
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
22
import org.eclipse.jdt.internal.compiler.ast.MethodDeclaration;
22
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
23
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
Lines 45-50 Link Here
45
46
46
	private SimpleLookupTable compatibleCache;
47
	private SimpleLookupTable compatibleCache;
47
48
49
	int typeBits;
50
48
	public static final ReferenceBinding LUB_GENERIC = new ReferenceBinding() { /* used for lub computation */};
51
	public static final ReferenceBinding LUB_GENERIC = new ReferenceBinding() { /* used for lub computation */};
49
52
50
	private static final Comparator FIELD_COMPARATOR = new Comparator() {
53
	private static final Comparator FIELD_COMPARATOR = new Comparator() {
Lines 428-435 Link Here
428
				case 'A' :
431
				case 'A' :
429
					switch(typeName.length) {
432
					switch(typeName.length) {
430
						case 13 :
433
						case 13 :
431
							if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_AUTOCLOSEABLE[2]))
434
							if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_AUTOCLOSEABLE[2])) {
432
								this.id = TypeIds.T_JavaLangAutoCloseable;
435
								this.id = TypeIds.T_JavaLangAutoCloseable;
436
								this.typeBits |= TypeIds.BitAutoCloseable; 
437
							}
433
							return;
438
							return;
434
						case 14:
439
						case 14:
435
							if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ASSERTIONERROR[2]))
440
							if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ASSERTIONERROR[2]))
Lines 928-933 Link Here
928
	return (this.modifiers & ExtraCompilerModifiers.AccRestrictedAccess) != 0;
933
	return (this.modifiers & ExtraCompilerModifiers.AccRestrictedAccess) != 0;
929
}
934
}
930
935
936
public boolean hasTypeBit(int bit) {
937
	// overridden in relevant subclasses
938
	Assert.isTrue(false, "Unsupported query for type "+getClass().getName()); //$NON-NLS-1$
939
	return false; // never reached
940
}
941
931
/** Answer true if the receiver implements anInterface or is identical to anInterface.
942
/** Answer true if the receiver implements anInterface or is identical to anInterface.
932
* If searchHierarchy is true, then also search the receiver's superclasses.
943
* If searchHierarchy is true, then also search the receiver's superclasses.
933
*
944
*
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java (+8 lines)
Lines 18-23 Link Here
18
import java.util.HashMap;
18
import java.util.HashMap;
19
import java.util.Iterator;
19
import java.util.Iterator;
20
20
21
import org.eclipse.core.runtime.Assert;
21
import org.eclipse.jdt.core.compiler.CharOperation;
22
import org.eclipse.jdt.core.compiler.CharOperation;
22
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
23
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
23
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
24
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
Lines 1066-1071 Link Here
1066
	return accessors[1];
1067
	return accessors[1];
1067
}
1068
}
1068
1069
1070
public boolean hasTypeBit(int bit) {
1071
	// source types initialize type bits during connectSuperclass/interfaces()
1072
	if (!isLocalType())
1073
		Assert.isTrue((this.tagBits & TagBits.EndHierarchyCheck) != 0, "Hierarchy should be connected"); //$NON-NLS-1$
1074
	return (this.typeBits & bit) != 0;
1075
}
1076
1069
/**
1077
/**
1070
 * @see org.eclipse.jdt.internal.compiler.lookup.Binding#initializeDeprecatedAnnotationTagBits()
1078
 * @see org.eclipse.jdt.internal.compiler.lookup.Binding#initializeDeprecatedAnnotationTagBits()
1071
 */
1079
 */
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java (+1 lines)
Lines 154-159 Link Here
154
			"MethodHandle$PolymorphicSignature".toCharArray() //$NON-NLS-1$
154
			"MethodHandle$PolymorphicSignature".toCharArray() //$NON-NLS-1$
155
	};
155
	};
156
	char[][] JAVA_LANG_AUTOCLOSEABLE =  {JAVA, LANG, "AutoCloseable".toCharArray()}; //$NON-NLS-1$
156
	char[][] JAVA_LANG_AUTOCLOSEABLE =  {JAVA, LANG, "AutoCloseable".toCharArray()}; //$NON-NLS-1$
157
	char[] CLOSE = "close".toCharArray(); //$NON-NLS-1$
157
158
158
	// Constraints for generic type argument inference
159
	// Constraints for generic type argument inference
159
	int CONSTRAINT_EQUAL = 0;		// Actual = Formal
160
	int CONSTRAINT_EQUAL = 0;		// Actual = Formal
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java (+2 lines)
Lines 173-176 Link Here
173
	final int Object2Object = T_JavaLangObject + (T_JavaLangObject << 4);
173
	final int Object2Object = T_JavaLangObject + (T_JavaLangObject << 4);
174
	final int BOXING = 0x200;
174
	final int BOXING = 0x200;
175
	final int UNBOXING = 0x400;
175
	final int UNBOXING = 0x400;
176
177
	final int BitAutoCloseable = 1;
176
}
178
}
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java (+14 lines)
Lines 42-47 Link Here
42
		this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature; // treat type var as public
42
		this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature; // treat type var as public
43
		this.tagBits |= TagBits.HasTypeVariable;
43
		this.tagBits |= TagBits.HasTypeVariable;
44
		this.environment = environment;
44
		this.environment = environment;
45
		this.typeBits = -1;
45
	}
46
	}
46
47
47
	/**
48
	/**
Lines 307-312 Link Here
307
		return true;
308
		return true;
308
	}
309
	}
309
310
311
	public boolean hasTypeBit(int bit) {
312
		if (this.typeBits == -1) {
313
			// initialize from bounds
314
			this.typeBits = 0;
315
			if (this.superclass != null)
316
				this.typeBits |= this.superclass.typeBits;
317
			if (this.superInterfaces != null)
318
				for (int i = 0, l = this.superInterfaces.length; i < l; i++)
319
					this.typeBits |= this.superInterfaces[i].typeBits;
320
		}
321
		return (this.typeBits & bit) != 0;
322
	}
323
310
	/**
324
	/**
311
	 * Returns true if the type variable is directly bound to a given type
325
	 * Returns true if the type variable is directly bound to a given type
312
	 */
326
	 */
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/WildcardBinding.java (+14 lines)
Lines 54-59 Link Here
54
		if (bound instanceof UnresolvedReferenceBinding)
54
		if (bound instanceof UnresolvedReferenceBinding)
55
			((UnresolvedReferenceBinding) bound).addWrapper(this, environment);
55
			((UnresolvedReferenceBinding) bound).addWrapper(this, environment);
56
		this.tagBits |=  TagBits.HasUnresolvedTypeVariables; // cleared in resolve()
56
		this.tagBits |=  TagBits.HasUnresolvedTypeVariables; // cleared in resolve()
57
		this.typeBits = -1;
57
	}
58
	}
58
59
59
	public int kind() {
60
	public int kind() {
Lines 420-425 Link Here
420
		return this.genericType.hashCode();
421
		return this.genericType.hashCode();
421
	}
422
	}
422
423
424
	public boolean hasTypeBit(int bit) {
425
		if (this.typeBits == -1) {
426
			// initialize from upper bounds
427
			this.typeBits = 0;
428
			if (this.superclass != null)
429
				this.typeBits |= this.superclass.typeBits;
430
			if (this.superInterfaces != null)
431
				for (int i = 0, l = this.superInterfaces.length; i < l; i++)
432
					this.typeBits |= this.superInterfaces[i].typeBits;
433
		}
434
		return (this.typeBits & bit) != 0;
435
	}
436
423
	void initialize(ReferenceBinding someGenericType, TypeBinding someBound, TypeBinding[] someOtherBounds) {
437
	void initialize(ReferenceBinding someGenericType, TypeBinding someBound, TypeBinding[] someOtherBounds) {
424
		this.genericType = someGenericType;
438
		this.genericType = someGenericType;
425
		this.bound = someBound;
439
		this.bound = someBound;
(-)compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java (+58 lines)
Lines 55-60 Link Here
55
import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
55
import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
56
import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
56
import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
57
import org.eclipse.jdt.internal.compiler.ast.Expression;
57
import org.eclipse.jdt.internal.compiler.ast.Expression;
58
import org.eclipse.jdt.internal.compiler.ast.FakedTrackingVariable;
58
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
59
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
59
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
60
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
60
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
61
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
Lines 430-435 Link Here
430
			
431
			
431
		case IProblem.MethodCanBePotentiallyStatic:
432
		case IProblem.MethodCanBePotentiallyStatic:
432
			return CompilerOptions.MethodCanBePotentiallyStatic;
433
			return CompilerOptions.MethodCanBePotentiallyStatic;
434
			
435
		case IProblem.UnclosedCloseable:
436
		case IProblem.UnclosedCloseableAtExit:
437
			return CompilerOptions.UnclosedCloseable;
438
		case IProblem.PotentiallyUnclosedCloseable:
439
		case IProblem.PotentiallyUnclosedCloseableAtExit:
440
			return CompilerOptions.PotentiallyUnclosedCloseable;
441
		case IProblem.ExplicitlyClosedAutoCloseable:
442
			return CompilerOptions.ExplicitlyClosedAutoCloseable;
433
	}
443
	}
434
	return 0;
444
	return 0;
435
}
445
}
Lines 462-467 Link Here
462
			case CompilerOptions.ParameterAssignment :
472
			case CompilerOptions.ParameterAssignment :
463
			case CompilerOptions.MethodCanBeStatic :
473
			case CompilerOptions.MethodCanBeStatic :
464
			case CompilerOptions.MethodCanBePotentiallyStatic :
474
			case CompilerOptions.MethodCanBePotentiallyStatic :
475
			case CompilerOptions.ExplicitlyClosedAutoCloseable :
465
				return CategorizedProblem.CAT_CODE_STYLE;
476
				return CategorizedProblem.CAT_CODE_STYLE;
466
477
467
			case CompilerOptions.MaskedCatchBlock :
478
			case CompilerOptions.MaskedCatchBlock :
Lines 483-488 Link Here
483
			case CompilerOptions.ShouldImplementHashcode :
494
			case CompilerOptions.ShouldImplementHashcode :
484
			case CompilerOptions.DeadCode :
495
			case CompilerOptions.DeadCode :
485
			case CompilerOptions.UnusedObjectAllocation :
496
			case CompilerOptions.UnusedObjectAllocation :
497
			case CompilerOptions.UnclosedCloseable :
498
			case CompilerOptions.PotentiallyUnclosedCloseable :
486
				return CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM;
499
				return CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM;
487
			
500
			
488
			case CompilerOptions.OverriddenPackageDefaultMethod :
501
			case CompilerOptions.OverriddenPackageDefaultMethod :
Lines 7862-7865 Link Here
7862
			type.sourceStart, 
7875
			type.sourceStart, 
7863
			type.sourceEnd);
7876
			type.sourceEnd);
7864
}
7877
}
7878
public void potentiallyUnclosedCloseable(FakedTrackingVariable trackVar, ASTNode location) {
7879
	if (location == null) {
7880
		this.handle(
7881
			IProblem.PotentiallyUnclosedCloseable,
7882
			NoArgument,
7883
			NoArgument,
7884
			trackVar.sourceStart,
7885
			trackVar.sourceEnd);
7886
	} else {
7887
		String[] args = { String.valueOf(trackVar.name) };
7888
		this.handle(
7889
			IProblem.PotentiallyUnclosedCloseableAtExit,
7890
			args,
7891
			args,
7892
			location.sourceStart,
7893
			location.sourceEnd);
7894
	}
7895
}
7896
public void unclosedCloseable(FakedTrackingVariable trackVar, ASTNode location) {
7897
	if (location == null) {
7898
		this.handle(
7899
			IProblem.UnclosedCloseable,
7900
			NoArgument,
7901
			NoArgument,
7902
			trackVar.sourceStart,
7903
			trackVar.sourceEnd);
7904
	} else {
7905
		String[] args = { String.valueOf(trackVar.name) };
7906
		this.handle(
7907
			IProblem.UnclosedCloseableAtExit,
7908
			args,
7909
			args,
7910
			location.sourceStart,
7911
			location.sourceEnd);
7912
	}
7913
}
7914
public void explicitlyClosedAutoCloseable(FakedTrackingVariable trackVar) {
7915
	String[] args = { String.valueOf(trackVar.name) };
7916
	this.handle(
7917
		IProblem.ExplicitlyClosedAutoCloseable,
7918
		args,
7919
		args,
7920
		trackVar.sourceStart,
7921
		trackVar.sourceEnd);	
7922
}
7865
}
7923
}
(-)compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties (+5 lines)
Lines 645-650 Link Here
645
880 = '<>' cannot be used with anonymous classes
645
880 = '<>' cannot be used with anonymous classes
646
881 = Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum constants are permitted
646
881 = Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum constants are permitted
647
882 = Unhandled exception type {0} thrown by automatic close() invocation on {1}
647
882 = Unhandled exception type {0} thrown by automatic close() invocation on {1}
648
883 = Value of type AutoCloseable is not closed on all paths.
649
884 = Instance '{0}' of type AutoCloseable may be unclosed at this point.
650
885 = Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.
651
886 = Instance '{0}' of type AutoCloseable is not closed at this point.
652
887 = Instance '{0}' should be managed by try-with-resource.
648
653
649
### ELABORATIONS
654
### ELABORATIONS
650
## Access restrictions
655
## Access restrictions
(-)model/org/eclipse/jdt/core/JavaCore.java (+46 lines)
Lines 1357-1362 Link Here
1357
	 */
1357
	 */
1358
	public static final String COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD = PLUGIN_ID + ".compiler.problem.reportMethodCanBePotentiallyStatic"; //$NON-NLS-1$
1358
	public static final String COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD = PLUGIN_ID + ".compiler.problem.reportMethodCanBePotentiallyStatic"; //$NON-NLS-1$
1359
	/**
1359
	/**
1360
	 * Compiler option ID: Reporting a resource that is not closed properly.
1361
	 * <p>When enabled, that compiler will issue an error or a warning if a local variable 
1362
	 * 	  holds a value of type AutoCloseable (or Closeable if compliance is &lt; 1.7)
1363
	 *    and if flow analysis shows that the method <code>close()</code> is not invoked locally on that value.
1364
	 * <dl>
1365
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportUnclosedCloseable"</code></dd>
1366
	 * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
1367
	 * <dt>Default:</dt><dd><code>"warning"</code></dd>
1368
	 * </dl>
1369
	 * @since 3.7
1370
	 * @category CompilerOptionID
1371
	 */
1372
	public static final String COMPILER_PB_UNCLOSED_CLOSEABLE = PLUGIN_ID + ".compiler.problem.unclosedCloseable"; //$NON-NLS-1$
1373
	/**
1374
	 * Compiler option ID: Reporting a resource that may not be closed properly.
1375
	 * <p>When enabled, that compiler will issue an error or a warning if a local variable 
1376
	 * 	  holds a value of type AutoCloseable (or Closeable if compliance is &lt; 1.7)
1377
	 *    and if flow analysis shows that the method <code>close()</code> is not invoked locally 
1378
	 *    on that value for all execution paths.
1379
	 * <dl>
1380
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportPotentiallyUnclosedCloseable"</code></dd>
1381
	 * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
1382
	 * <dt>Default:</dt><dd><code>"ignore"</code></dd>
1383
	 * </dl>
1384
	 * @since 3.7
1385
	 * @category CompilerOptionID
1386
	 */
1387
	public static final String COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE = PLUGIN_ID + ".compiler.problem.potentiallyUnclosedCloseable"; //$NON-NLS-1$
1388
	/**
1389
	 * Compiler option ID: Reporting a resource that is not managed by try-with-resources.
1390
	 * <p>When enabled, that compiler will issue an error or a warning if a local variable 
1391
	 * 	  holds a value of type AutoCloseable, and if the method <code>close()</code> is
1392
	 * 	  explicitly invoked on that resource, but the resource is not managed by a
1393
	 * 	  try-with-resources block.
1394
	 * <p>Note that this option is not surfaced in the UI, as it is intended only for internal 
1395
	 * 	  use for computing quick assists / cleanups. 
1396
	 * <dl>
1397
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportPotentiallyUnclosedCloseable"</code></dd>
1398
	 * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
1399
	 * <dt>Default:</dt><dd><code>"ignore"</code></dd>
1400
	 * </dl>
1401
	 * @since 3.7
1402
	 * @category CompilerOptionID
1403
	 */
1404
	public static final String COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE = PLUGIN_ID + ".compiler.problem.explicitlyClosedAutoCloseable"; //$NON-NLS-1$
1405
	/**
1360
	 * Compiler option ID: Setting Source Compatibility Mode.
1406
	 * Compiler option ID: Setting Source Compatibility Mode.
1361
	 * <p>Specify whether which source level compatibility is used. From 1.4 on, <code>'assert'</code> is a keyword
1407
	 * <p>Specify whether which source level compatibility is used. From 1.4 on, <code>'assert'</code> is a keyword
1362
	 *    reserved for assertion support. Also note, than when toggling to 1.4 mode, the target VM
1408
	 *    reserved for assertion support. Also note, than when toggling to 1.4 mode, the target VM
(-)src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java (+3 lines)
Lines 1816-1821 Link Here
1816
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.discouragedReference\" value=\"warning\"/>\n" + 
1816
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.discouragedReference\" value=\"warning\"/>\n" + 
1817
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.emptyStatement\" value=\"ignore\"/>\n" + 
1817
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.emptyStatement\" value=\"ignore\"/>\n" + 
1818
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.enumIdentifier\" value=\"warning\"/>\n" + 
1818
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.enumIdentifier\" value=\"warning\"/>\n" + 
1819
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable\" value=\"ignore\"/>\n" + 
1819
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.fallthroughCase\" value=\"ignore\"/>\n" + 
1820
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.fallthroughCase\" value=\"ignore\"/>\n" + 
1820
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.fatalOptionalError\" value=\"disabled\"/>\n" + 
1821
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.fatalOptionalError\" value=\"disabled\"/>\n" + 
1821
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.fieldHiding\" value=\"ignore\"/>\n" + 
1822
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.fieldHiding\" value=\"ignore\"/>\n" + 
Lines 1857-1862 Link Here
1857
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.parameterAssignment\" value=\"ignore\"/>\n" + 
1858
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.parameterAssignment\" value=\"ignore\"/>\n" + 
1858
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment\" value=\"ignore\"/>\n" + 
1859
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment\" value=\"ignore\"/>\n" + 
1859
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.potentialNullReference\" value=\"ignore\"/>\n" + 
1860
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.potentialNullReference\" value=\"ignore\"/>\n" + 
1861
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable\" value=\"ignore\"/>\n" + 
1860
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.rawTypeReference\" value=\"warning\"/>\n" + 
1862
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.rawTypeReference\" value=\"warning\"/>\n" + 
1861
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantNullCheck\" value=\"ignore\"/>\n" + 
1863
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantNullCheck\" value=\"ignore\"/>\n" + 
1862
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantSuperinterface\" value=\"ignore\"/>\n" + 
1864
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantSuperinterface\" value=\"ignore\"/>\n" + 
Lines 1871-1876 Link Here
1871
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.typeParameterHiding\" value=\"warning\"/>\n" + 
1873
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.typeParameterHiding\" value=\"warning\"/>\n" + 
1872
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems\" value=\"enabled\"/>\n" +
1874
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems\" value=\"enabled\"/>\n" +
1873
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation\" value=\"warning\"/>\n" + 
1875
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation\" value=\"warning\"/>\n" + 
1876
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unclosedCloseable\" value=\"warning\"/>\n" + 
1874
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock\" value=\"ignore\"/>\n" + 
1877
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock\" value=\"ignore\"/>\n" + 
1875
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unhandledWarningToken\" value=\"warning\"/>\n" + 
1878
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unhandledWarningToken\" value=\"warning\"/>\n" + 
1876
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryElse\" value=\"ignore\"/>\n" + 
1879
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryElse\" value=\"ignore\"/>\n" + 
(-)src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java (+10 lines)
Lines 443-448 Link Here
443
		expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", DEPRECATED);
443
		expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", DEPRECATED);
444
		expectedProblemAttributes.put("ExceptionTypeNotFound", DEPRECATED);
444
		expectedProblemAttributes.put("ExceptionTypeNotFound", DEPRECATED);
445
		expectedProblemAttributes.put("ExceptionTypeNotVisible", DEPRECATED);
445
		expectedProblemAttributes.put("ExceptionTypeNotVisible", DEPRECATED);
446
		expectedProblemAttributes.put("ExplicitlyClosedAutoCloseable", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
446
		expectedProblemAttributes.put("ExpressionShouldBeAVariable", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX));
447
		expectedProblemAttributes.put("ExpressionShouldBeAVariable", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX));
447
		expectedProblemAttributes.put("ExternalProblemFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
448
		expectedProblemAttributes.put("ExternalProblemFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
448
		expectedProblemAttributes.put("ExternalProblemNotFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
449
		expectedProblemAttributes.put("ExternalProblemNotFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
Lines 767-772 Link Here
767
		expectedProblemAttributes.put("PolymorphicMethodNotBelow17", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
768
		expectedProblemAttributes.put("PolymorphicMethodNotBelow17", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
768
		expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
769
		expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
769
		expectedProblemAttributes.put("PotentialHeapPollutionFromVararg", new ProblemAttributes(CategorizedProblem.CAT_UNCHECKED_RAW));
770
		expectedProblemAttributes.put("PotentialHeapPollutionFromVararg", new ProblemAttributes(CategorizedProblem.CAT_UNCHECKED_RAW));
771
		expectedProblemAttributes.put("PotentiallyUnclosedCloseable", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
772
		expectedProblemAttributes.put("PotentiallyUnclosedCloseableAtExit", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
770
		expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
773
		expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
771
		expectedProblemAttributes.put("PublicClassMustMatchFileName", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
774
		expectedProblemAttributes.put("PublicClassMustMatchFileName", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
772
		expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
775
		expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
Lines 829-834 Link Here
829
		expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
832
		expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
830
		expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(CategorizedProblem.CAT_NAME_SHADOWING_CONFLICT));
833
		expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(CategorizedProblem.CAT_NAME_SHADOWING_CONFLICT));
831
		expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
834
		expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
835
		expectedProblemAttributes.put("UnclosedCloseable", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
836
		expectedProblemAttributes.put("UnclosedCloseableAtExit", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
832
		expectedProblemAttributes.put("UndefinedAnnotationMember", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
837
		expectedProblemAttributes.put("UndefinedAnnotationMember", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
833
		expectedProblemAttributes.put("UndefinedConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
838
		expectedProblemAttributes.put("UndefinedConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
834
		expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
839
		expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
Lines 1108-1113 Link Here
1108
		expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", SKIP);
1113
		expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", SKIP);
1109
		expectedProblemAttributes.put("ExceptionTypeNotFound", SKIP);
1114
		expectedProblemAttributes.put("ExceptionTypeNotFound", SKIP);
1110
		expectedProblemAttributes.put("ExceptionTypeNotVisible", SKIP);
1115
		expectedProblemAttributes.put("ExceptionTypeNotVisible", SKIP);
1116
		expectedProblemAttributes.put("ExplicitlyClosedAutoCloseable", new ProblemAttributes(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE));
1111
		expectedProblemAttributes.put("ExpressionShouldBeAVariable", SKIP);
1117
		expectedProblemAttributes.put("ExpressionShouldBeAVariable", SKIP);
1112
		expectedProblemAttributes.put("ExternalProblemFixable", SKIP);
1118
		expectedProblemAttributes.put("ExternalProblemFixable", SKIP);
1113
		expectedProblemAttributes.put("ExternalProblemNotFixable", SKIP);
1119
		expectedProblemAttributes.put("ExternalProblemNotFixable", SKIP);
Lines 1432-1437 Link Here
1432
		expectedProblemAttributes.put("PolymorphicMethodNotBelow17", SKIP);
1438
		expectedProblemAttributes.put("PolymorphicMethodNotBelow17", SKIP);
1433
		expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(JavaCore.COMPILER_PB_POSSIBLE_ACCIDENTAL_BOOLEAN_ASSIGNMENT));
1439
		expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(JavaCore.COMPILER_PB_POSSIBLE_ACCIDENTAL_BOOLEAN_ASSIGNMENT));
1434
		expectedProblemAttributes.put("PotentialHeapPollutionFromVararg", new ProblemAttributes(JavaCore.COMPILER_PB_UNCHECKED_TYPE_OPERATION));
1440
		expectedProblemAttributes.put("PotentialHeapPollutionFromVararg", new ProblemAttributes(JavaCore.COMPILER_PB_UNCHECKED_TYPE_OPERATION));
1441
		expectedProblemAttributes.put("PotentiallyUnclosedCloseable", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE));
1442
		expectedProblemAttributes.put("PotentiallyUnclosedCloseableAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE));
1435
		expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE));
1443
		expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE));
1436
		expectedProblemAttributes.put("PublicClassMustMatchFileName", SKIP);
1444
		expectedProblemAttributes.put("PublicClassMustMatchFileName", SKIP);
1437
		expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", SKIP);
1445
		expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", SKIP);
Lines 1494-1499 Link Here
1494
		expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION));
1502
		expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION));
1495
		expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(JavaCore.COMPILER_PB_TYPE_PARAMETER_HIDING));
1503
		expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(JavaCore.COMPILER_PB_TYPE_PARAMETER_HIDING));
1496
		expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(JavaCore.COMPILER_PB_AUTOBOXING));
1504
		expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(JavaCore.COMPILER_PB_AUTOBOXING));
1505
		expectedProblemAttributes.put("UnclosedCloseable", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE));
1506
		expectedProblemAttributes.put("UnclosedCloseableAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE));
1497
		expectedProblemAttributes.put("UndefinedAnnotationMember", SKIP);
1507
		expectedProblemAttributes.put("UndefinedAnnotationMember", SKIP);
1498
		expectedProblemAttributes.put("UndefinedConstructor", SKIP);
1508
		expectedProblemAttributes.put("UndefinedConstructor", SKIP);
1499
		expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", SKIP);
1509
		expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", SKIP);
(-)src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java (-5 / +286 lines)
Lines 16-28 Link Here
16
16
17
import java.util.Map;
17
import java.util.Map;
18
18
19
import org.eclipse.jdt.core.JavaCore;
19
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
20
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
20
21
21
import junit.framework.Test;
22
import junit.framework.Test;
22
public class TryWithResourcesStatementTest extends AbstractRegressionTest {
23
public class TryWithResourcesStatementTest extends AbstractRegressionTest {
23
24
24
static {
25
static {
25
//	TESTS_NAMES = new String[] { "test053" };
26
//	TESTS_NAMES = new String[] { "test014" };
26
//	TESTS_NUMBERS = new int[] { 50 };
27
//	TESTS_NUMBERS = new int[] { 50 };
27
//	TESTS_RANGE = new int[] { 11, -1 };
28
//	TESTS_RANGE = new int[] { 11, -1 };
28
}
29
}
Lines 489-500 Link Here
489
		"	               ^^\n" + 
490
		"	               ^^\n" + 
490
		"Dead code\n" + 
491
		"Dead code\n" + 
491
		"----------\n" + 
492
		"----------\n" + 
492
		"3. ERROR in X.java (at line 5)\n" + 
493
		"3. WARNING in X.java (at line 5)\n" + 
494
		"	Y why = new Y();\n" + 
495
		"	  ^^^\n" + 
496
		"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + 
497
		"----------\n" + 
498
		"4. ERROR in X.java (at line 5)\n" + 
493
		"	Y why = new Y();\n" + 
499
		"	Y why = new Y();\n" + 
494
		"	        ^^^^^^^\n" + 
500
		"	        ^^^^^^^\n" + 
495
		"Unhandled exception type WeirdException\n" + 
501
		"Unhandled exception type WeirdException\n" + 
496
		"----------\n" + 
502
		"----------\n" + 
497
		"4. WARNING in X.java (at line 22)\n" + 
503
		"5. WARNING in X.java (at line 22)\n" + 
498
		"	class WeirdException extends Throwable {}\n" + 
504
		"	class WeirdException extends Throwable {}\n" + 
499
		"	      ^^^^^^^^^^^^^^\n" + 
505
		"	      ^^^^^^^^^^^^^^\n" + 
500
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
506
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
Lines 562-573 Link Here
562
		"	               ^^\n" + 
568
		"	               ^^\n" + 
563
		"Dead code\n" + 
569
		"Dead code\n" + 
564
		"----------\n" + 
570
		"----------\n" + 
565
		"3. ERROR in X.java (at line 5)\n" + 
571
		"3. WARNING in X.java (at line 5)\n" + 
572
		"	Y why = new Y();\n" + 
573
		"	  ^^^\n" + 
574
		"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + 
575
		"----------\n" + 
576
		"4. ERROR in X.java (at line 5)\n" + 
566
		"	Y why = new Y();\n" + 
577
		"	Y why = new Y();\n" + 
567
		"	        ^^^^^^^\n" + 
578
		"	        ^^^^^^^\n" + 
568
		"Unhandled exception type WeirdException\n" + 
579
		"Unhandled exception type WeirdException\n" + 
569
		"----------\n" + 
580
		"----------\n" + 
570
		"4. WARNING in X.java (at line 20)\n" + 
581
		"5. WARNING in X.java (at line 20)\n" + 
571
		"	class WeirdException extends Throwable {}\n" + 
582
		"	class WeirdException extends Throwable {}\n" + 
572
		"	      ^^^^^^^^^^^^^^\n" + 
583
		"	      ^^^^^^^^^^^^^^\n" + 
573
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
584
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
Lines 3341-3346 Link Here
3341
		"Object.Integer cannot be resolved to a type\n" + 
3352
		"Object.Integer cannot be resolved to a type\n" + 
3342
		"----------\n");
3353
		"----------\n");
3343
}
3354
}
3355
// Bug 349326 - [1.7] new warning for missing try-with-resources
3356
// a method uses an AutoCloseable without ever closing it.
3357
public void test055() {
3358
	Map options = getCompilerOptions();
3359
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3360
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3361
	this.runNegativeTest(
3362
		new String[] {
3363
			"X.java",
3364
			"import java.io.File;\n" +
3365
			"import java.io.FileReader;\n" +
3366
			"import java.io.IOException;\n" +
3367
			"public class X {\n" +
3368
			"    void foo() throws IOException {\n" +
3369
			"        File file = new File(\"somefile\");\n" +
3370
			"        FileReader fileReader = new FileReader(file);\n" +
3371
// not invoking any methods on FileReader, try to avoid necessary call to superclass()
3372
//			"        char[] in = new char[50];\n" +
3373
//			"        fileReader.read(in);\n" +
3374
			"    }\n" +
3375
			"    public static void main(String[] args) throws IOException {\n" +
3376
			"        new X().foo();\n" +
3377
			"    }\n" +
3378
			"}\n"
3379
		},
3380
		"----------\n" + 
3381
		"1. ERROR in X.java (at line 7)\n" + 
3382
		"	FileReader fileReader = new FileReader(file);\n" + 
3383
		"	           ^^^^^^^^^^\n" + 
3384
		"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + 
3385
		"----------\n",
3386
		null,
3387
		true,
3388
		options);
3389
}
3390
// Bug 349326 - [1.7] new warning for missing try-with-resources
3391
// a method uses an AutoCloseable and closes it properly.
3392
public void test055a() {
3393
	Map options = getCompilerOptions();
3394
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3395
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3396
	this.runConformTest(
3397
		new String[] {
3398
			"X.java",
3399
			"import java.io.File;\n" +
3400
			"import java.io.FileReader;\n" +
3401
			"import java.io.IOException;\n" +
3402
			"public class X {\n" +
3403
			"    void foo() throws IOException {\n" +
3404
			"        File file = new File(\"somefile\");\n" +
3405
			"        FileReader fileReader = new FileReader(file);\n" +
3406
			"        char[] in = new char[50];\n" +
3407
			"        fileReader.read(in);\n" +
3408
			"		 fileReader.close();\n" +
3409
			"    }\n" +
3410
			"    public static void main(String[] args) {\n" +
3411
			"        try {\n" +
3412
			"            new X().foo();\n" +
3413
			"        } catch (IOException ioex) {\n" +
3414
			"            System.out.println(\"caught\");\n" +
3415
			"        }\n" +
3416
			"    }\n" +
3417
			"}\n"
3418
		},
3419
		"caught", /*output*/
3420
		null/*classLibs*/,
3421
		true/*shouldFlush*/,
3422
		null/*vmargs*/,
3423
		options,
3424
		null/*requestor*/);
3425
}
3426
// Bug 349326 - [1.7] new warning for missing try-with-resources
3427
// a method uses an AutoCloseable properly within try-with-resources.
3428
public void test055b() {
3429
	Map options = getCompilerOptions();
3430
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3431
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3432
	this.runConformTest(
3433
		new String[] {
3434
			"X.java",
3435
			"import java.io.File;\n" +
3436
			"import java.io.FileReader;\n" +
3437
			"import java.io.IOException;\n" +
3438
			"public class X {\n" +
3439
			"    void foo() throws IOException {\n" +
3440
			"        File file = new File(\"somefile\");\n" +
3441
			"        try (FileReader fileReader = new FileReader(file)) {\n" +
3442
			"            char[] in = new char[50];\n" +
3443
			"            fileReader.read(in);\n" +
3444
			"		 }\n" +
3445
			"    }\n" +
3446
			"    public static void main(String[] args) {\n" +
3447
			"        try {\n" +
3448
			"            new X().foo();\n" +
3449
			"        } catch (IOException ioex) {\n" +
3450
			"            System.out.println(\"caught\");\n" +
3451
			"        }\n" +
3452
			"    }\n" +
3453
			"}\n"
3454
		},
3455
		"caught", /*output*/
3456
		null/*classLibs*/,
3457
		true/*shouldFlush*/,
3458
		null/*vmargs*/,
3459
		options,
3460
		null/*requestor*/);
3461
}
3462
// Bug 349326 - [1.7] new warning for missing try-with-resources
3463
// a method uses two AutoCloseables (testing independent analysis)
3464
// - one closeable may be unclosed at a conditional return
3465
// - the other is only conditionally closed
3466
public void test055c() {
3467
	Map options = getCompilerOptions();
3468
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3469
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3470
	this.runNegativeTest(
3471
		new String[] {
3472
			"X.java",
3473
			"import java.io.File;\n" +
3474
			"import java.io.FileReader;\n" +
3475
			"import java.io.IOException;\n" +
3476
			"public class X {\n" +
3477
			"    void foo(boolean flag1, boolean flag2) throws IOException {\n" +
3478
			"        File file = new File(\"somefile\");\n" +
3479
			"        char[] in = new char[50];\n" +
3480
			"        FileReader fileReader1 = new FileReader(file);\n" +
3481
			"        fileReader1.read(in);\n" +
3482
			"        FileReader fileReader2 = new FileReader(file);\n" +
3483
			"        fileReader2.read(in);\n" +
3484
			"        if (flag1) {\n" +
3485
			"            fileReader2.close();\n" +
3486
			"            return;\n" +
3487
			"        } else if (flag2) {\n" +
3488
			"            fileReader2.close();\n" +
3489
			"        }\n" +
3490
			"        fileReader1.close();\n" +
3491
			"    }\n" +
3492
			"    public static void main(String[] args) throws IOException {\n" +
3493
			"        new X().foo(false, true);\n" +
3494
			"    }\n" +
3495
			"}\n"
3496
		},
3497
		"----------\n" + 
3498
		"1. WARNING in X.java (at line 10)\n" + 
3499
		"	FileReader fileReader2 = new FileReader(file);\n" + 
3500
		"	           ^^^^^^^^^^^\n" + 
3501
		"Value of type AutoCloseable is not closed on all paths.\n" + 
3502
		"----------\n" + 
3503
		"2. ERROR in X.java (at line 14)\n" + 
3504
		"	return;\n" + 
3505
		"	^^^^^^^\n" + 
3506
		"Instance \'fileReader1\' of type AutoCloseable is not closed at this point.\n" + 
3507
		"----------\n",
3508
		null,
3509
		true,
3510
		options);
3511
}
3512
// Bug 349326 - [1.7] new warning for missing try-with-resources
3513
// one method returns an AutoCleasble, a second method uses this object without ever closing it.
3514
public void test055d() {
3515
	Map options = getCompilerOptions();
3516
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3517
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3518
	this.runNegativeTest(
3519
		new String[] {
3520
			"X.java",
3521
			"import java.io.File;\n" +
3522
			"import java.io.FileReader;\n" +
3523
			"import java.io.IOException;\n" +
3524
			"public class X {\n" +
3525
			"    FileReader getReader(String filename) throws IOException {\n" +
3526
			"        File file = new File(\"somefile\");\n" +
3527
			"        FileReader fileReader = new FileReader(file);\n" +
3528
			"        return fileReader;\n" + 		// don't complain here, pass responsibility to caller
3529
			"    }\n" +
3530
			"    void foo() throws IOException {\n" +
3531
			"        FileReader reader = getReader(\"somefile\");\n" +
3532
			"        char[] in = new char[50];\n" +
3533
			"        reader.read(in);\n" +
3534
			"    }\n" +
3535
			"    public static void main(String[] args) throws IOException {\n" +
3536
			"        new X().foo();\n" +
3537
			"    }\n" +
3538
			"}\n"
3539
		},
3540
		"----------\n" + 
3541
		"1. ERROR in X.java (at line 11)\n" + 
3542
		"	FileReader reader = getReader(\"somefile\");\n" + 
3543
		"	           ^^^^^^\n" + 
3544
		"Value of type AutoCloseable is not closed neither explicitly nor using a try-with-resources.\n" + 
3545
		"----------\n",
3546
		null,
3547
		true,
3548
		options);
3549
}
3550
// Bug 349326 - [1.7] new warning for missing try-with-resources
3551
// a method explicitly closes its AutoCloseable rather than using t-w-r
3552
public void test055e() {
3553
	Map options = getCompilerOptions();
3554
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3555
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3556
	options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR);
3557
	this.runNegativeTest(
3558
		new String[] {
3559
			"X.java",
3560
			"import java.io.File;\n" +
3561
			"import java.io.FileReader;\n" +
3562
			"import java.io.IOException;\n" +
3563
			"public class X {\n" +
3564
			"    void foo() throws IOException {\n" +
3565
			"        File file = new File(\"somefile\");\n" +
3566
			"        FileReader fileReader;\n" +
3567
			"        fileReader = new FileReader(file);\n" +
3568
			"        char[] in = new char[50];\n" +
3569
			"        fileReader.read(in);\n" +
3570
			"        fileReader.close();\n" +
3571
			"    }\n" +
3572
			"    public static void main(String[] args) throws IOException {\n" +
3573
			"        new X().foo();\n" +
3574
			"    }\n" +
3575
			"}\n"
3576
		},
3577
		"----------\n" + 
3578
		"1. ERROR in X.java (at line 8)\n" + 
3579
		"	fileReader = new FileReader(file);\n" + 
3580
		"	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
3581
		"Instance \'fileReader\' should be managed by try-with-resource.\n" + 
3582
		"----------\n",
3583
		null,
3584
		true,
3585
		options);
3586
}
3587
// Bug 349326 - [1.7] new warning for missing try-with-resources
3588
// an AutoCloseable local is re-assigned
3589
public void test055f() {
3590
	Map options = getCompilerOptions();
3591
	options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR);
3592
	options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING);
3593
	options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE);
3594
	this.runNegativeTest(
3595
		new String[] {
3596
			"X.java",
3597
			"import java.io.File;\n" +
3598
			"import java.io.FileReader;\n" +
3599
			"import java.io.IOException;\n" +
3600
			"public class X {\n" +
3601
			"    void foo() throws IOException {\n" +
3602
			"        File file = new File(\"somefile\");\n" +
3603
			"        FileReader fileReader = new FileReader(file);\n" +
3604
			"        char[] in = new char[50];\n" +
3605
			"        fileReader.read(in);\n" +
3606
			"        fileReader = new FileReader(file);\n" +
3607
			"        fileReader.read(in);\n" +
3608
			"        fileReader.close();\n" +
3609
			"    }\n" +
3610
			"    public static void main(String[] args) throws IOException {\n" +
3611
			"        new X().foo();\n" +
3612
			"    }\n" +
3613
			"}\n"
3614
		},
3615
		"----------\n" + 
3616
		"1. ERROR in X.java (at line 10)\n" + 
3617
		"	fileReader = new FileReader(file);\n" + 
3618
		"	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
3619
		"Instance \'fileReader\' of type AutoCloseable is not closed at this point.\n" + 
3620
		"----------\n",
3621
		null,
3622
		true,
3623
		options);
3624
}
3344
public static Class testClass() {
3625
public static Class testClass() {
3345
	return TryWithResourcesStatementTest.class;
3626
	return TryWithResourcesStatementTest.class;
3346
}
3627
}

Return to bug 349326