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 (-1 / +16 lines)
Lines 116-122 Link Here
116
 *		Benjamin Muskalla - added the following constants
116
 *		Benjamin Muskalla - added the following constants
117
 *									MissingSynchronizedModifierInInheritedMethod
117
 *									MissingSynchronizedModifierInInheritedMethod
118
 *		Stephan Herrmann  - added the following constants
118
 *		Stephan Herrmann  - added the following constants
119
 *									UnusedObjectAllocation									
119
 *									UnusedObjectAllocation
120
 *									PotentiallyUnclosedCloseable
121
 *									PotentiallyUnclosedCloseableAtExit
122
 *									UnclosedCloseable
123
 *									UnclosedCloseableAtExit
124
 *									ExplicitlyClosedAutoCloseable
120
 *******************************************************************************/
125
 *******************************************************************************/
121
package org.eclipse.jdt.core.compiler;
126
package org.eclipse.jdt.core.compiler;
122
127
Lines 1394-1399 Link Here
1394
	int DiamondNotBelow17 =  TypeRelated + 883;
1399
	int DiamondNotBelow17 =  TypeRelated + 883;
1395
	/** @since 3.7.1 */
1400
	/** @since 3.7.1 */
1396
	int RedundantSpecificationOfTypeArguments = TypeRelated + 884;
1401
	int RedundantSpecificationOfTypeArguments = TypeRelated + 884;
1402
	/** @since 3.8 */
1403
	int PotentiallyUnclosedCloseable = Internal + 885;
1404
	/** @since 3.8 */
1405
	int PotentiallyUnclosedCloseableAtExit = Internal + 886;
1406
	/** @since 3.8 */
1407
	int UnclosedCloseable = Internal + 887;
1408
	/** @since 3.8 */
1409
	int UnclosedCloseableAtExit = Internal + 888;
1410
	/** @since 3.8 */
1411
	int ExplicitlyClosedAutoCloseable = Internal + 889;
1397
	/**
1412
	/**
1398
	 * External problems -- These are problems defined by other plugins
1413
	 * External problems -- These are problems defined by other plugins
1399
	 */
1414
	 */
(-)compiler/org/eclipse/jdt/internal/compiler/ast/AllocationExpression.java (+3 lines)
Lines 10-15 Link Here
10
 *     Stephan Herrmann - Contributions for 
10
 *     Stephan Herrmann - Contributions for 
11
 *     						bug 236385 - [compiler] Warn for potential programming problem if an object is created but not used
11
 *     						bug 236385 - [compiler] Warn for potential programming problem if an object is created but not used
12
 *     						bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
12
 *     						bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
13
 *     						bug 349326 - [1.7] new warning for missing try-with-resources
13
 *******************************************************************************/
14
 *******************************************************************************/
14
package org.eclipse.jdt.internal.compiler.ast;
15
package org.eclipse.jdt.internal.compiler.ast;
15
16
Lines 42-47 Link Here
42
	// process arguments
43
	// process arguments
43
	if (this.arguments != null) {
44
	if (this.arguments != null) {
44
		for (int i = 0, count = this.arguments.length; i < count; i++) {
45
		for (int i = 0, count = this.arguments.length; i < count; i++) {
46
			// if argument is an AutoCloseable insert info that it *may* be closed (by the target method, i.e.)
47
			flowInfo = FakedTrackingVariable.markPotentialClosing(this.arguments[i], flowInfo);
45
			flowInfo =
48
			flowInfo =
46
				this.arguments[i]
49
				this.arguments[i]
47
					.analyseCode(currentScope, flowContext, flowInfo)
50
					.analyseCode(currentScope, flowContext, flowInfo)
(-)compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java (+29 lines)
Lines 12-17 Link Here
12
 * 							bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
12
 * 							bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
13
 * 							bug 292478 - Report potentially null across variable assignment
13
 * 							bug 292478 - Report potentially null across variable assignment
14
 *     						bug 335093 - [compiler][null] minimal hook for future null annotation support
14
 *     						bug 335093 - [compiler][null] minimal hook for future null annotation support
15
 *     						bug 349326 - [1.7] new warning for missing try-with-resources
15
 *******************************************************************************/
16
 *******************************************************************************/
16
package org.eclipse.jdt.internal.compiler.ast;
17
package org.eclipse.jdt.internal.compiler.ast;
17
18
Lines 47-52 Link Here
47
	flowInfo = ((Reference) this.lhs)
48
	flowInfo = ((Reference) this.lhs)
48
		.analyseAssignment(currentScope, flowContext, flowInfo, this, false)
49
		.analyseAssignment(currentScope, flowContext, flowInfo, this, false)
49
		.unconditionalInits();
50
		.unconditionalInits();
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(local)) { // only if previous value may be non-null
57
				if (flowInfo.isDefinitelyNull(trackerBinding)) 
58
					currentScope.problemReporter().unclosedCloseable(local.closeTracker, this);
59
				else if (flowInfo.isPotentiallyNull(trackerBinding)) 
60
					currentScope.problemReporter().potentiallyUnclosedCloseable(local.closeTracker, this);
61
			}
62
		}
63
		if (FakedTrackingVariable.isAutoCloseable(this.expression.resolvedType)) {
64
			if (local.closeTracker != null && local.closeTracker.isInsideTryWithResources) { 
65
				// re-assigning resource of try-with-resources is a different error
66
			} else {
67
				// new value is AutoCloseable, start tracking, possibly re-using existing tracker var:
68
				if (trackerBinding == null) {
69
					local.closeTracker = new FakedTrackingVariable(local, this);
70
					trackerBinding = local.closeTracker.binding;
71
				}
72
				flowInfo.markAsDefinitelyNull(trackerBinding);
73
// TODO(stephan): this might be useful, but I could not find a test case for it: 
74
//				if (flowContext.initsOnFinally != null)
75
//					flowContext.initsOnFinally.markAsDefinitelyNonNull(trackerBinding);
76
			}
77
		}
78
	}
50
	int nullStatus = this.expression.nullStatus(flowInfo);
79
	int nullStatus = this.expression.nullStatus(flowInfo);
51
	if (local != null && (local.type.tagBits & TagBits.IsBaseType) == 0) {
80
	if (local != null && (local.type.tagBits & TagBits.IsBaseType) == 0) {
52
		if (nullStatus == FlowInfo.NULL) {
81
		if (nullStatus == FlowInfo.NULL) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/Block.java (-1 / +4 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.ast;
12
package org.eclipse.jdt.internal.compiler.ast;
12
13
Lines 36-41 Link Here
36
			flowInfo = stat.analyseCode(this.scope, flowContext, flowInfo);
37
			flowInfo = stat.analyseCode(this.scope, flowContext, flowInfo);
37
		}
38
		}
38
	}
39
	}
40
	if (this.explicitDeclarations > 0) // if block has its own scope analyze tracking vars now:
41
		this.scope.checkUnclosedCloseables(flowInfo, flowContext, null);
39
	return flowInfo;
42
	return flowInfo;
40
}
43
}
41
/**
44
/**
(-)compiler/org/eclipse/jdt/internal/compiler/ast/FakedTrackingVariable.java (+133 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
 *     Stephan Herrmann - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.ast;
12
13
import org.eclipse.jdt.core.compiler.CharOperation;
14
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
15
import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
16
import org.eclipse.jdt.internal.compiler.impl.Constant;
17
import org.eclipse.jdt.internal.compiler.lookup.Binding;
18
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
19
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
20
import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
21
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
22
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
23
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
24
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
25
26
/**
27
 * A faked local variable declaration used for keeping track of data flows of a
28
 * special variable. Certain events will be recorded by changing the null info
29
 * for this variable.
30
 * 
31
 * See bug 349326 - [1.7] new warning for missing try-with-resources
32
 */
33
public class FakedTrackingVariable extends LocalDeclaration {
34
35
	/** If resource is already managed by try-with-resources don't complain. */
36
	public boolean isInsideTryWithResources;
37
38
	/** 
39
	 * If close() is invoked from a nested method (inside a local type) 
40
	 * report remaining problems only as potential.
41
	 */
42
	public boolean closedInNestedMethod; 
43
44
	/** If a problem has already been reported don't complain again. */
45
	public boolean hasReportedProblem;
46
47
	MethodScope methodScope; // designates the method declaring this variable
48
	
49
	public LocalVariableBinding originalBinding; // the real local being tracked
50
51
	public FakedTrackingVariable(LocalVariableBinding original, Statement location) {
52
		super(original.name, location.sourceStart, location.sourceEnd);
53
		this.type = new SingleTypeReference(
54
				TypeConstants.OBJECT,
55
				((long)this.sourceStart <<32)+this.sourceEnd);
56
		this.methodScope = original.declaringScope.methodScope();
57
		this.originalBinding = original;
58
		resolve(original.declaringScope);
59
	}
60
	
61
	public void generateCode(BlockScope currentScope, CodeStream codeStream)
62
	{ /* NOP - this variable is completely dummy, ie. for analysis only. */ }
63
64
	public void resolve (BlockScope scope) {
65
		// only need the binding, which is used as reference in FlowInfo methods.
66
		this.binding = new LocalVariableBinding(
67
				this.name,
68
				scope.getJavaLangObject(),  // dummy, just needs to be a reference type
69
				0,
70
				false);
71
		this.binding.setConstant(Constant.NotAConstant);
72
		this.binding.useFlag = LocalVariableBinding.USED;
73
		// use a free slot without assigning it:
74
		this.binding.id = scope.registerTrackingVariable(this);
75
	}
76
77
	/**
78
	 * If expression resolves to a local variable binding of type AutoCloseable,
79
	 * answer the variable that tracks closing of that local, creating it if needed.
80
	 * @param expression
81
	 * @return a new {@link FakedTrackingVariable} or null.
82
	 */
83
	public static FakedTrackingVariable getCloseTrackingVariable(Expression expression) {
84
		if (expression instanceof SingleNameReference) {
85
			SingleNameReference name = (SingleNameReference) expression;
86
			if (name.binding instanceof LocalVariableBinding) {
87
				LocalVariableBinding local = (LocalVariableBinding)name.binding;
88
				if (local.closeTracker != null)
89
					return local.closeTracker;
90
				if (local.isParameter() || !isAutoCloseable(expression.resolvedType))
91
					return null;
92
				// tracking var doesn't yet exist. This happens in finally block
93
				// which is analyzed before the corresponding try block
94
				Statement location = local.declaration;
95
				return local.closeTracker = new FakedTrackingVariable(local, location);
96
			}
97
		}
98
		return null;
99
	}
100
101
	/** if 'invocationSite' is a call to close() that has a registered tracking variable, answer that variable's binding. */
102
	public static LocalVariableBinding getTrackerForCloseCall(ASTNode invocationSite) {
103
		if (invocationSite instanceof MessageSend) {
104
			MessageSend send = (MessageSend) invocationSite;
105
			if (CharOperation.equals(TypeConstants.CLOSE, send.selector) && send.receiver instanceof SingleNameReference) {
106
				Binding receiverBinding = ((SingleNameReference)send.receiver).binding;
107
				if (receiverBinding instanceof LocalVariableBinding) {
108
					FakedTrackingVariable trackingVariable = ((LocalVariableBinding)receiverBinding).closeTracker;
109
					if (trackingVariable != null)
110
						return trackingVariable.binding;
111
				}
112
			}
113
		}
114
		return null;
115
	}
116
117
	public static FlowInfo markPotentialClosing(Expression expression, FlowInfo flowInfo) {
118
		FakedTrackingVariable trackVar = getCloseTrackingVariable(expression);
119
		if (trackVar != null) {
120
			// insert info that the tracked resource *may* be closed (by the target method, i.e.)
121
			FlowInfo infoResourceIsClosed = flowInfo.copy();
122
			infoResourceIsClosed.markAsDefinitelyNonNull(trackVar.binding);
123
			return FlowInfo.conditional(flowInfo, infoResourceIsClosed);
124
		}
125
		return flowInfo;
126
	}
127
128
	/** Answer wither the given type binding is a subtype of java.lang.AutoCloseable. */
129
	public static boolean isAutoCloseable(TypeBinding typeBinding) {
130
		return typeBinding instanceof ReferenceBinding
131
			&& ((ReferenceBinding)typeBinding).hasTypeBit(TypeIds.BitAutoCloseable);
132
	}
133
}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/IfStatement.java (-1 / +5 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
10
 *     Stephan Herrmann - Contributions for 
11
 *     							bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
12
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
11
 *******************************************************************************/
13
 *******************************************************************************/
12
package org.eclipse.jdt.internal.compiler.ast;
14
package org.eclipse.jdt.internal.compiler.ast;
13
15
Lines 124-129 Link Here
124
		}
126
		}
125
		elseFlowInfo = this.elseStatement.analyseCode(currentScope, flowContext, elseFlowInfo);
127
		elseFlowInfo = this.elseStatement.analyseCode(currentScope, flowContext, elseFlowInfo);
126
	}
128
	}
129
	// process AutoCloseable resources closed in only one branch:
130
	currentScope.correlateTrackingVarsIfElse(thenFlowInfo, elseFlowInfo);
127
	// merge THEN & ELSE initializations
131
	// merge THEN & ELSE initializations
128
	FlowInfo mergedInfo = FlowInfo.mergedOptimizedBranchesIfElse(
132
	FlowInfo mergedInfo = FlowInfo.mergedOptimizedBranchesIfElse(
129
		thenFlowInfo,
133
		thenFlowInfo,
(-)compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java (+8 lines)
Lines 11-16 Link Here
11
 *     						bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
11
 *     						bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
12
 *     						bug 292478 - Report potentially null across variable assignment
12
 *     						bug 292478 - Report potentially null across variable assignment
13
 *     						bug 335093 - [compiler][null] minimal hook for future null annotation support
13
 *     						bug 335093 - [compiler][null] minimal hook for future null annotation support
14
 *     						bug 349326 - [1.7] new warning for missing try-with-resources
14
 *******************************************************************************/
15
 *******************************************************************************/
15
package org.eclipse.jdt.internal.compiler.ast;
16
package org.eclipse.jdt.internal.compiler.ast;
16
17
Lines 76-81 Link Here
76
		this.initialization
77
		this.initialization
77
			.analyseCode(currentScope, flowContext, flowInfo)
78
			.analyseCode(currentScope, flowContext, flowInfo)
78
			.unconditionalInits();
79
			.unconditionalInits();
80
	if (FakedTrackingVariable.isAutoCloseable(this.initialization.resolvedType)) {
81
		this.binding.closeTracker = new FakedTrackingVariable(this.binding, this);
82
		flowInfo.markAsDefinitelyNull(this.binding.closeTracker.binding);
83
// TODO(stephan): this might be useful, but I could not find a test case for it: 
84
//		if (flowContext.initsOnFinally != null)
85
//			flowContext.initsOnFinally.markAsDefinitelyNonNull(this.binding.closeTracker.binding);
86
	}
79
	int nullStatus = this.initialization.nullStatus(flowInfo);
87
	int nullStatus = this.initialization.nullStatus(flowInfo);
80
	if (!flowInfo.isDefinitelyAssigned(this.binding)){// for local variable debug attributes
88
	if (!flowInfo.isDefinitelyAssigned(this.binding)){// for local variable debug attributes
81
		this.bits |= FirstAssignmentToLocal;
89
		this.bits |= FirstAssignmentToLocal;
(-)compiler/org/eclipse/jdt/internal/compiler/ast/MessageSend.java (-1 / +21 lines)
Lines 8-14 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Nick Teryaev - fix for bug (https://bugs.eclipse.org/bugs/show_bug.cgi?id=40752)
10
 *     Nick Teryaev - fix for bug (https://bugs.eclipse.org/bugs/show_bug.cgi?id=40752)
11
 *     Stephan Herrmann - Contribution for bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
11
 *     Stephan Herrmann - Contributions for
12
 *     	 						bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
13
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
12
 *******************************************************************************/
14
 *******************************************************************************/
13
package org.eclipse.jdt.internal.compiler.ast;
15
package org.eclipse.jdt.internal.compiler.ast;
14
16
Lines 38-43 Link Here
38
import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
40
import org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding;
39
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
41
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
40
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
42
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
43
import org.eclipse.jdt.internal.compiler.lookup.TypeConstants;
41
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
44
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
42
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
45
import org.eclipse.jdt.internal.compiler.problem.ProblemSeverities;
43
46
Lines 60-65 Link Here
60
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
63
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
61
	boolean nonStatic = !this.binding.isStatic();
64
	boolean nonStatic = !this.binding.isStatic();
62
	flowInfo = this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic).unconditionalInits();
65
	flowInfo = this.receiver.analyseCode(currentScope, flowContext, flowInfo, nonStatic).unconditionalInits();
66
	// recording the closing of AutoCloseable resources:
67
	if (CharOperation.equals(TypeConstants.CLOSE, this.selector)) 
68
	{
69
		FakedTrackingVariable trackingVariable = FakedTrackingVariable.getCloseTrackingVariable(this.receiver);
70
		if (trackingVariable != null) { // null happens if receiver is not a local variable or not an AutoCloseable
71
			if (trackingVariable.methodScope == currentScope.methodScope()) {
72
				flowInfo.markAsDefinitelyNonNull(trackingVariable.binding);
73
// TODO(stephan): this might be useful, but I could not find a test case for it: 
74
//				if (flowContext.initsOnFinally != null)
75
//					flowContext.initsOnFinally.markAsDefinitelyNonNull(trackingVariable.binding);
76
			} else {
77
				trackingVariable.closedInNestedMethod = true;
78
			}
79
		}
80
	}
63
	if (nonStatic) {
81
	if (nonStatic) {
64
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
82
		this.receiver.checkNPE(currentScope, flowContext, flowInfo);
65
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
83
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=318682
Lines 80-85 Link Here
80
			if ((this.arguments[i].implicitConversion & TypeIds.UNBOXING) != 0) {
98
			if ((this.arguments[i].implicitConversion & TypeIds.UNBOXING) != 0) {
81
				this.arguments[i].checkNPE(currentScope, flowContext, flowInfo);
99
				this.arguments[i].checkNPE(currentScope, flowContext, flowInfo);
82
			}
100
			}
101
			// if argument is an AutoCloseable insert info that it *may* be closed (by the target method, i.e.)
102
			flowInfo = FakedTrackingVariable.markPotentialClosing(this.arguments[i], flowInfo);
83
			flowInfo = this.arguments[i].analyseCode(currentScope, flowContext, flowInfo).unconditionalInits();
103
			flowInfo = this.arguments[i].analyseCode(currentScope, flowContext, flowInfo).unconditionalInits();
84
		}
104
		}
85
	}
105
	}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/MethodDeclaration.java (+2 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.ast;
12
package org.eclipse.jdt.internal.compiler.ast;
12
13
Lines 134-139 Link Here
134
				}
135
				}
135
					
136
					
136
			}
137
			}
138
			this.scope.checkUnclosedCloseables(flowInfo, methodContext, null/*don't report against a specific location*/);
137
		} catch (AbortMethod e) {
139
		} catch (AbortMethod e) {
138
			this.ignoreFurtherInvestigation = true;
140
			this.ignoreFurtherInvestigation = true;
139
		}
141
		}
(-)compiler/org/eclipse/jdt/internal/compiler/ast/QualifiedAllocationExpression.java (-1 / +5 lines)
Lines 7-13 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
10
 *     Stephan Herrmann - Contributions for
11
 *     							bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
12
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
11
 *******************************************************************************/
13
 *******************************************************************************/
12
package org.eclipse.jdt.internal.compiler.ast;
14
package org.eclipse.jdt.internal.compiler.ast;
13
15
Lines 72-77 Link Here
72
		// process arguments
74
		// process arguments
73
		if (this.arguments != null) {
75
		if (this.arguments != null) {
74
			for (int i = 0, count = this.arguments.length; i < count; i++) {
76
			for (int i = 0, count = this.arguments.length; i < count; i++) {
77
				// if argument is an AutoCloseable insert info that it *may* be closed (by the target method, i.e.)
78
				flowInfo = FakedTrackingVariable.markPotentialClosing(this.arguments[i], flowInfo);
75
				flowInfo = this.arguments[i].analyseCode(currentScope, flowContext, flowInfo);
79
				flowInfo = this.arguments[i].analyseCode(currentScope, flowContext, flowInfo);
76
				if ((this.arguments[i].implicitConversion & TypeIds.UNBOXING) != 0) {
80
				if ((this.arguments[i].implicitConversion & TypeIds.UNBOXING) != 0) {
77
					this.arguments[i].checkNPE(currentScope, flowContext, flowInfo);
81
					this.arguments[i].checkNPE(currentScope, flowContext, flowInfo);
(-)compiler/org/eclipse/jdt/internal/compiler/ast/ReturnStatement.java (-1 / +11 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
10
 *     Stephan Herrmann - Contributions for 
11
 *     							bug 319201 - [null] no warning when unboxing SingleNameReference causes NPE
12
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
11
 *******************************************************************************/
13
 *******************************************************************************/
12
package org.eclipse.jdt.internal.compiler.ast;
14
package org.eclipse.jdt.internal.compiler.ast;
13
15
Lines 40-45 Link Here
40
		if ((this.expression.implicitConversion & TypeIds.UNBOXING) != 0) {
42
		if ((this.expression.implicitConversion & TypeIds.UNBOXING) != 0) {
41
			this.expression.checkNPE(currentScope, flowContext, flowInfo);
43
			this.expression.checkNPE(currentScope, flowContext, flowInfo);
42
		}
44
		}
45
		FakedTrackingVariable trackingVariable = FakedTrackingVariable.getCloseTrackingVariable(this.expression);
46
		if (trackingVariable != null) {
47
			// don't report issues concerning this local, since by returning
48
			// the method passes the responsibility to the caller:
49
			flowInfo.markAsDefinitelyNonNull(trackingVariable.binding);
50
			trackingVariable.hasReportedProblem = true;
51
		}
43
	}
52
	}
44
	this.initStateIndex =
53
	this.initStateIndex =
45
		currentScope.methodScope().recordInitializationStates(flowInfo);
54
		currentScope.methodScope().recordInitializationStates(flowInfo);
Lines 104-109 Link Here
104
			this.expression.bits |= ASTNode.IsReturnedValue;
113
			this.expression.bits |= ASTNode.IsReturnedValue;
105
		}
114
		}
106
	}
115
	}
116
	currentScope.checkUnclosedCloseables(flowInfo, null/*ignore exception exits from flowContext*/, this);
107
	return FlowInfo.DEAD_END;
117
	return FlowInfo.DEAD_END;
108
}
118
}
109
119
(-)compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java (-5 / +17 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 332637 - Dead Code detection removing code that isn't dead
10
 *     Stephan Herrmann - Contributions for
11
 *     							bug 332637 - Dead Code detection removing code that isn't dead
12
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
11
 *******************************************************************************/
13
 *******************************************************************************/
12
package org.eclipse.jdt.internal.compiler.ast;
14
package org.eclipse.jdt.internal.compiler.ast;
13
15
Lines 124-131 Link Here
124
		for (int i = 0; i < resourcesLength; i++) {
126
		for (int i = 0; i < resourcesLength; i++) {
125
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
127
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
126
			this.postResourcesInitStateIndexes[i] = currentScope.methodScope().recordInitializationStates(flowInfo);
128
			this.postResourcesInitStateIndexes[i] = currentScope.methodScope().recordInitializationStates(flowInfo);
127
			this.resources[i].binding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways.
129
			LocalVariableBinding resourceBinding = this.resources[i].binding;
128
			TypeBinding type = this.resources[i].binding.type;
130
			resourceBinding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways.
131
			if (resourceBinding.closeTracker != null) {
132
				resourceBinding.closeTracker.isInsideTryWithResources = true;
133
				flowInfo.markAsDefinitelyNonNull(resourceBinding.closeTracker.binding); // all is well
134
			} 
135
			TypeBinding type = resourceBinding.type;
129
			if (type != null && type.isValidBinding()) {
136
			if (type != null && type.isValidBinding()) {
130
				ReferenceBinding binding = (ReferenceBinding) type;
137
				ReferenceBinding binding = (ReferenceBinding) type;
131
				MethodBinding closeMethod = binding.getExactMethod(ConstantPool.Close, new TypeBinding [0], this.scope.compilationUnitScope()); // scope needs to be tighter
138
				MethodBinding closeMethod = binding.getExactMethod(ConstantPool.Close, new TypeBinding [0], this.scope.compilationUnitScope()); // scope needs to be tighter
Lines 250-257 Link Here
250
		for (int i = 0; i < resourcesLength; i++) {
257
		for (int i = 0; i < resourcesLength; i++) {
251
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
258
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
252
			this.postResourcesInitStateIndexes[i] = currentScope.methodScope().recordInitializationStates(flowInfo);
259
			this.postResourcesInitStateIndexes[i] = currentScope.methodScope().recordInitializationStates(flowInfo);
253
			this.resources[i].binding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways.
260
			LocalVariableBinding resourceBinding = this.resources[i].binding;
254
			TypeBinding type = this.resources[i].binding.type;
261
			resourceBinding.useFlag = LocalVariableBinding.USED; // Is implicitly used anyways.
262
			if (resourceBinding.closeTracker != null) {
263
				resourceBinding.closeTracker.isInsideTryWithResources = true;
264
				flowInfo.markAsDefinitelyNonNull(resourceBinding.closeTracker.binding); // all is well
265
			} 
266
			TypeBinding type = resourceBinding.type;
255
			if (type != null && type.isValidBinding()) {
267
			if (type != null && type.isValidBinding()) {
256
				ReferenceBinding binding = (ReferenceBinding) type;
268
				ReferenceBinding binding = (ReferenceBinding) type;
257
				MethodBinding closeMethod = binding.getExactMethod(ConstantPool.Close, new TypeBinding [0], this.scope.compilationUnitScope()); // scope needs to be tighter
269
				MethodBinding closeMethod = binding.getExactMethod(ConstantPool.Close, new TypeBinding [0], this.scope.compilationUnitScope()); // scope needs to be tighter
(-)compiler/org/eclipse/jdt/internal/compiler/flow/ExceptionHandlingFlowContext.java (-8 / +26 lines)
Lines 7-28 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.flow;
12
package org.eclipse.jdt.internal.compiler.flow;
12
13
13
import java.util.ArrayList;
14
import java.util.ArrayList;
14
15
15
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
16
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
16
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
17
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
17
import org.eclipse.jdt.internal.compiler.ast.Argument;
18
import org.eclipse.jdt.internal.compiler.ast.Argument;
18
import org.eclipse.jdt.internal.compiler.ast.UnionTypeReference;
19
import org.eclipse.jdt.internal.compiler.ast.FakedTrackingVariable;
19
import org.eclipse.jdt.internal.compiler.ast.SubRoutineStatement;
20
import org.eclipse.jdt.internal.compiler.ast.SubRoutineStatement;
20
import org.eclipse.jdt.internal.compiler.ast.TryStatement;
21
import org.eclipse.jdt.internal.compiler.ast.TryStatement;
21
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
22
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
23
import org.eclipse.jdt.internal.compiler.ast.UnionTypeReference;
22
import org.eclipse.jdt.internal.compiler.codegen.ObjectCache;
24
import org.eclipse.jdt.internal.compiler.codegen.ObjectCache;
23
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
25
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
24
import org.eclipse.jdt.internal.compiler.lookup.CatchParameterBinding;
26
import org.eclipse.jdt.internal.compiler.lookup.CatchParameterBinding;
25
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
27
import org.eclipse.jdt.internal.compiler.lookup.ExtraCompilerModifiers;
28
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
26
import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
29
import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
27
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
30
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
28
import org.eclipse.jdt.internal.compiler.lookup.Scope;
31
import org.eclipse.jdt.internal.compiler.lookup.Scope;
Lines 180-186 Link Here
180
	return node;
183
	return node;
181
}
184
}
182
185
183
186
public int getNullStatusAfter(LocalVariableBinding local, FlowInfo flowInfo) {
187
	// collect info from normal flow and exceptional flows:
188
	int status = flowInfo.nullStatus(local);
189
	if (this.initsOnExceptions != null)
190
		for (int i = 0; i < this.initsOnExceptions.length; i++)
191
			status |= (this.initsOnExceptions[i].nullStatus(local) & (FlowInfo.NULL|FlowInfo.POTENTIALLY_NULL));
192
	return status;
193
}
184
194
185
public String individualToString() {
195
public String individualToString() {
186
	StringBuffer buffer = new StringBuffer("Exception flow context"); //$NON-NLS-1$
196
	StringBuffer buffer = new StringBuffer("Exception flow context"); //$NON-NLS-1$
Lines 251-257 Link Here
251
		TypeBinding raisedException,
261
		TypeBinding raisedException,
252
		TypeBinding caughtException,
262
		TypeBinding caughtException,
253
		ASTNode invocationSite,
263
		ASTNode invocationSite,
254
		boolean wasAlreadyDefinitelyCaught) {
264
		boolean wasAlreadyDefinitelyCaught,
265
		boolean isFirstException) {
255
266
256
	int index = this.indexes.get(exceptionType);
267
	int index = this.indexes.get(exceptionType);
257
	int cacheIndex = index / ExceptionHandlingFlowContext.BitCacheSize;
268
	int cacheIndex = index / ExceptionHandlingFlowContext.BitCacheSize;
Lines 265-274 Link Here
265
		CatchParameterBinding catchParameter = (CatchParameterBinding) this.catchArguments[catchBlock].binding;
276
		CatchParameterBinding catchParameter = (CatchParameterBinding) this.catchArguments[catchBlock].binding;
266
		catchParameter.setPreciseType(caughtException);
277
		catchParameter.setPreciseType(caughtException);
267
	}
278
	}
268
	this.initsOnExceptions[catchBlock] =
279
	UnconditionalFlowInfo mergedInfo =
269
		(this.initsOnExceptions[catchBlock].tagBits & FlowInfo.UNREACHABLE) == 0 ?
280
			(this.initsOnExceptions[catchBlock].tagBits & FlowInfo.UNREACHABLE) == 0 ?
270
			this.initsOnExceptions[catchBlock].mergedWith(flowInfo):
281
					this.initsOnExceptions[catchBlock].mergedWith(flowInfo):
271
			flowInfo.unconditionalCopy();
282
						flowInfo.unconditionalCopy();
283
	if (isFirstException) {
284
		LocalVariableBinding trackBinding = FakedTrackingVariable.getTrackerForCloseCall(invocationSite);
285
		if (trackBinding != null)
286
			// if the first statement throwing an exception is a close() call, consider the corresponding resource as closed despite the exception
287
			mergedInfo.markAsDefinitelyNonNull(trackBinding);
288
	}
289
	this.initsOnExceptions[catchBlock] = mergedInfo;
272
}
290
}
273
291
274
public void recordReturnFrom(UnconditionalFlowInfo flowInfo) {
292
public void recordReturnFrom(UnconditionalFlowInfo flowInfo) {
(-)compiler/org/eclipse/jdt/internal/compiler/flow/FinallyFlowContext.java (+8 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.flow;
12
package org.eclipse.jdt.internal.compiler.flow;
12
13
Lines 17-22 Link Here
17
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
18
import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
18
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
19
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
19
import org.eclipse.jdt.internal.compiler.lookup.Scope;
20
import org.eclipse.jdt.internal.compiler.lookup.Scope;
21
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
20
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
22
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
21
import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
23
import org.eclipse.jdt.internal.compiler.lookup.VariableBinding;
22
24
Lines 34-44 Link Here
34
	Expression[] nullReferences;
36
	Expression[] nullReferences;
35
	int[] nullCheckTypes;
37
	int[] nullCheckTypes;
36
	int nullCount;
38
	int nullCount;
39
	boolean hasSeenExceptions;
37
40
38
	public FinallyFlowContext(FlowContext parent, ASTNode associatedNode) {
41
	public FinallyFlowContext(FlowContext parent, ASTNode associatedNode) {
39
		super(parent, associatedNode);
42
		super(parent, associatedNode);
40
	}
43
	}
41
44
45
	public void checkExceptionHandlers(TypeBinding[] raisedExceptions, ASTNode location, FlowInfo flowInfo, BlockScope scope) {
46
		checkExceptionHandlers(raisedExceptions, location, flowInfo, scope, !this.hasSeenExceptions);
47
		this.hasSeenExceptions = true;
48
	}
49
42
/**
50
/**
43
 * Given some contextual initialization info (derived from a try block or a catch block), this
51
 * Given some contextual initialization info (derived from a try block or a catch block), this
44
 * code will check that the subroutine context does not also initialize a final variable potentially set
52
 * code will check that the subroutine context does not also initialize a final variable potentially set
(-)compiler/org/eclipse/jdt/internal/compiler/flow/FlowContext.java (-6 / +53 lines)
Lines 7-19 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.flow;
12
package org.eclipse.jdt.internal.compiler.flow;
12
13
13
import java.util.ArrayList;
14
import java.util.ArrayList;
15
14
import org.eclipse.jdt.core.compiler.CharOperation;
16
import org.eclipse.jdt.core.compiler.CharOperation;
15
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
16
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
17
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
18
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
17
import org.eclipse.jdt.internal.compiler.ast.Expression;
19
import org.eclipse.jdt.internal.compiler.ast.Expression;
18
import org.eclipse.jdt.internal.compiler.ast.LabeledStatement;
20
import org.eclipse.jdt.internal.compiler.ast.LabeledStatement;
19
import org.eclipse.jdt.internal.compiler.ast.Reference;
21
import org.eclipse.jdt.internal.compiler.ast.Reference;
Lines 153-159 Link Here
153
								raisedException,
155
								raisedException,
154
								raisedException, // precise exception that will be caught
156
								raisedException, // precise exception that will be caught
155
								location,
157
								location,
156
								definitelyCaught);
158
								definitelyCaught,
159
								false/* not first exception in finally block*/);
157
							// was it already definitely caught ?
160
							// was it already definitely caught ?
158
							definitelyCaught = true;
161
							definitelyCaught = true;
159
							break;
162
							break;
Lines 164-170 Link Here
164
								raisedException,
167
								raisedException,
165
								caughtException,
168
								caughtException,
166
								location,
169
								location,
167
								false);
170
								false,
171
								false/* not first exception in finally block*/);
168
							// was not caught already per construction
172
							// was not caught already per construction
169
					}
173
					}
170
				}
174
				}
Lines 213-220 Link Here
213
		scope.problemReporter().unhandledException(raisedException, location);
217
		scope.problemReporter().unhandledException(raisedException, location);
214
	}
218
	}
215
}
219
}
216
217
public void checkExceptionHandlers(TypeBinding[] raisedExceptions, ASTNode location, FlowInfo flowInfo, BlockScope scope) {
220
public void checkExceptionHandlers(TypeBinding[] raisedExceptions, ASTNode location, FlowInfo flowInfo, BlockScope scope) {
221
	checkExceptionHandlers(raisedExceptions, location, flowInfo, scope, false/*not known to be first in finally*/);
222
}
223
224
public void checkExceptionHandlers(TypeBinding[] raisedExceptions, ASTNode location, FlowInfo flowInfo, BlockScope scope, boolean isFirstExceptionInFinally) {
218
	// check that all the argument exception types are handled
225
	// check that all the argument exception types are handled
219
	// JDK Compatible implementation - when an exception type is thrown,
226
	// JDK Compatible implementation - when an exception type is thrown,
220
	// all related catch blocks are marked as reachable... instead of those only
227
	// all related catch blocks are marked as reachable... instead of those only
Lines 276-282 Link Here
276
										raisedException,
283
										raisedException,
277
										raisedException, // precise exception that will be caught
284
										raisedException, // precise exception that will be caught
278
										location,
285
										location,
279
										locallyCaught[raisedIndex]);
286
										locallyCaught[raisedIndex],
287
										isFirstExceptionInFinally);
280
									// was already definitely caught ?
288
									// was already definitely caught ?
281
									if (!locallyCaught[raisedIndex]) {
289
									if (!locallyCaught[raisedIndex]) {
282
										locallyCaught[raisedIndex] = true;
290
										locallyCaught[raisedIndex] = true;
Lines 291-297 Link Here
291
										raisedException,
299
										raisedException,
292
										caughtException, 
300
										caughtException, 
293
										location,
301
										location,
294
										false);
302
										false,
303
										isFirstExceptionInFinally);
295
									// was not caught already per construction
304
									// was not caught already per construction
296
							}
305
							}
297
						}
306
						}
Lines 391-396 Link Here
391
	return null;
400
	return null;
392
}
401
}
393
402
403
/**
404
 * Answer the combined null status that local will have after this flow context.
405
 * Subclasses will respect break and exception exits.
406
 */
407
public int getNullStatusAfter(LocalVariableBinding local, FlowInfo flowInfo) {
408
	return flowInfo.nullStatus(local);
409
}
410
394
/*
411
/*
395
 * lookup through break labels
412
 * lookup through break labels
396
 */
413
 */
Lines 732-735 Link Here
732
	buffer.append(individualToString()).append('\n');
749
	buffer.append(individualToString()).append('\n');
733
	return buffer.toString();
750
	return buffer.toString();
734
}
751
}
752
753
/**
754
 * Get the null status that local will have after the current point as indicated by flowInfo and flowContext
755
 * @param local
756
 * @param flowInfo
757
 * @param flowContext may be null
758
 * @return a bitset from the constants FlowInfo.{NULL,POTENTIALLY_NULL,POTENTIALLY_NON_NULL,NON_NULL}.
759
 */
760
public static int getNullStatusAfter(LocalVariableBinding local, FlowInfo flowInfo, FlowContext flowContext) {
761
	int status = (flowContext != null) 
762
					? flowContext.getNullStatusAfter(local, flowInfo)
763
					: flowInfo.nullStatus(local);
764
	// at this point some combinations are not useful so flatten to a single bit:
765
	return mergeNullStatus(status);
766
}
767
768
/* Merge the bits NULL, NON_NULL, POTENTIALLY_NULL, POTENTIALLY_NON_NULL to a one-bit answer. */
769
private static int mergeNullStatus(int status) {
770
	if ((status & FlowInfo.NULL) != 0) {
771
		if ((status & (FlowInfo.NON_NULL | FlowInfo.POTENTIALLY_NON_NULL)) != 0)
772
			return FlowInfo.POTENTIALLY_NULL; 	// null + doubt = pot null
773
		return FlowInfo.NULL;
774
	} else if ((status & FlowInfo.NON_NULL) != 0) {
775
		if ((status & FlowInfo.POTENTIALLY_NULL) != 0)
776
			return FlowInfo.POTENTIALLY_NULL;	// non-null + doubt = pot null
777
		return FlowInfo.NON_NULL;
778
	} else if ((status & FlowInfo.POTENTIALLY_NULL) != 0)
779
		return FlowInfo.POTENTIALLY_NULL;
780
	return status;
781
}
735
}
782
}
(-)compiler/org/eclipse/jdt/internal/compiler/flow/InitializationFlowContext.java (-1 / +3 lines)
Lines 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.flow;
12
package org.eclipse.jdt.internal.compiler.flow;
12
13
Lines 67-73 Link Here
67
		TypeBinding raisedException,
68
		TypeBinding raisedException,
68
		TypeBinding caughtException,
69
		TypeBinding caughtException,
69
		ASTNode invocationSite,
70
		ASTNode invocationSite,
70
		boolean wasMasked) {
71
		boolean wasMasked,
72
		boolean isFirstExceptionInFinally) {
71
73
72
		// even if unreachable code, need to perform unhandled exception diagnosis
74
		// even if unreachable code, need to perform unhandled exception diagnosis
73
		int size = this.thrownExceptions.length;
75
		int size = this.thrownExceptions.length;
(-)compiler/org/eclipse/jdt/internal/compiler/flow/LoopingFlowContext.java (-2 / +7 lines)
Lines 7-13 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - contribution for Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
10
 *     Stephan Herrmann - contributions for
11
 *     							Bug 336428 - [compiler][null] bogus warning "redundant null check" in condition of do {} while() loop
12
 *     							Bug 349326 - [1.7] new warning for missing try-with-resources
11
 *******************************************************************************/
13
 *******************************************************************************/
12
package org.eclipse.jdt.internal.compiler.flow;
14
package org.eclipse.jdt.internal.compiler.flow;
13
15
Lines 59-65 Link Here
59
		void simulateThrowAfterLoopBack(FlowInfo flowInfo) {
61
		void simulateThrowAfterLoopBack(FlowInfo flowInfo) {
60
			this.catchingContext.recordHandlingException(this.caughtException,
62
			this.catchingContext.recordHandlingException(this.caughtException,
61
					flowInfo.unconditionalInits(), null, // raised exception, irrelevant here,
63
					flowInfo.unconditionalInits(), null, // raised exception, irrelevant here,
62
					null, null, /* invocation site, irrelevant here */ true // we have no business altering the needed status.
64
					null,
65
					null, // invocation site, irrelevant here
66
					true, // we have no business altering the needed status.
67
					false // not known to be the first exception in a finally block
63
					);
68
					);
64
		}
69
		}
65
	}
70
	}
(-)compiler/org/eclipse/jdt/internal/compiler/flow/SwitchFlowContext.java (+10 lines)
Lines 7-17 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.flow;
12
package org.eclipse.jdt.internal.compiler.flow;
12
13
13
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
14
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
14
import org.eclipse.jdt.internal.compiler.codegen.BranchLabel;
15
import org.eclipse.jdt.internal.compiler.codegen.BranchLabel;
16
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
15
17
16
/**
18
/**
17
 * Reflects the context of code analysis, keeping track of enclosing
19
 * Reflects the context of code analysis, keeping track of enclosing
Lines 31-36 Link Here
31
	return this.breakLabel;
33
	return this.breakLabel;
32
}
34
}
33
35
36
public int getNullStatusAfter(LocalVariableBinding local, FlowInfo flowInfo) {
37
	// collect info from normal flow and breaks flows:
38
	int status = flowInfo.nullStatus(local);
39
	if (this.initsOnBreak != null)
40
		status |= this.initsOnBreak.nullStatus(local);
41
	return status;
42
}
43
34
public String individualToString() {
44
public String individualToString() {
35
	StringBuffer buffer = new StringBuffer("Switch flow context"); //$NON-NLS-1$
45
	StringBuffer buffer = new StringBuffer("Switch flow context"); //$NON-NLS-1$
36
	buffer.append("[initsOnBreak -").append(this.initsOnBreak.toString()).append(']'); //$NON-NLS-1$
46
	buffer.append("[initsOnBreak -").append(this.initsOnBreak.toString()).append(']'); //$NON-NLS-1$
(-)compiler/org/eclipse/jdt/internal/compiler/flow/UnconditionalFlowInfo.java (-6 / +7 lines)
Lines 13-18 Link Here
13
 *     						bug 292478 - Report potentially null across variable assignment
13
 *     						bug 292478 - Report potentially null across variable assignment
14
 *     						bug 332637 - Dead Code detection removing code that isn't dead
14
 *     						bug 332637 - Dead Code detection removing code that isn't dead
15
 *     						bug 341499 - [compiler][null] allocate extra bits in all methods of UnconditionalFlowInfo
15
 *     						bug 341499 - [compiler][null] allocate extra bits in all methods of UnconditionalFlowInfo
16
 *     						bug 349326 - [1.7] new warning for missing try-with-resources
16
 *******************************************************************************/
17
 *******************************************************************************/
17
package org.eclipse.jdt.internal.compiler.flow;
18
package org.eclipse.jdt.internal.compiler.flow;
18
19
Lines 770-776 Link Here
770
	}
771
	}
771
	int vectorIndex;
772
	int vectorIndex;
772
	if ((vectorIndex = (position / BitCacheSize) - 1)
773
	if ((vectorIndex = (position / BitCacheSize) - 1)
773
			>= this.extra[0].length) {
774
			>= this.extra[2].length) {
774
		return false; // if not enough room in vector, then not initialized
775
		return false; // if not enough room in vector, then not initialized
775
	}
776
	}
776
	return ((this.extra[2][vectorIndex] & this.extra[4][vectorIndex]
777
	return ((this.extra[2][vectorIndex] & this.extra[4][vectorIndex]
Lines 797-803 Link Here
797
	}
798
	}
798
	int vectorIndex;
799
	int vectorIndex;
799
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
800
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
800
			this.extra[0].length) {
801
			this.extra[2].length) {
801
		return false; // if not enough room in vector, then not initialized
802
		return false; // if not enough room in vector, then not initialized
802
	}
803
	}
803
	return ((this.extra[2][vectorIndex] & this.extra[3][vectorIndex]
804
	return ((this.extra[2][vectorIndex] & this.extra[3][vectorIndex]
Lines 822-828 Link Here
822
	}
823
	}
823
	int vectorIndex;
824
	int vectorIndex;
824
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
825
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
825
			this.extra[0].length) {
826
			this.extra[2].length) {
826
		return false; // if not enough room in vector, then not initialized
827
		return false; // if not enough room in vector, then not initialized
827
	}
828
	}
828
	return ((this.extra[2][vectorIndex] & this.extra[5][vectorIndex]
829
	return ((this.extra[2][vectorIndex] & this.extra[5][vectorIndex]
Lines 882-888 Link Here
882
	}
883
	}
883
	int vectorIndex;
884
	int vectorIndex;
884
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
885
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
885
			this.extra[0].length) {
886
			this.extra[2].length) {
886
		return false; // if not enough room in vector, then not initialized
887
		return false; // if not enough room in vector, then not initialized
887
	}
888
	}
888
	return ((this.extra[4][vectorIndex]
889
	return ((this.extra[4][vectorIndex]
Lines 908-914 Link Here
908
	}
909
	}
909
	int vectorIndex;
910
	int vectorIndex;
910
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
911
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
911
			this.extra[0].length) {
912
			this.extra[2].length) {
912
		return false; // if not enough room in vector, then not initialized
913
		return false; // if not enough room in vector, then not initialized
913
	}
914
	}
914
	return ((this.extra[3][vectorIndex]
915
	return ((this.extra[3][vectorIndex]
Lines 934-940 Link Here
934
	}
935
	}
935
	int vectorIndex;
936
	int vectorIndex;
936
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
937
	if ((vectorIndex = (position / BitCacheSize) - 1) >=
937
			this.extra[0].length) {
938
			this.extra[2].length) {
938
		return false; // if not enough room in vector, then not initialized
939
		return false; // if not enough room in vector, then not initialized
939
	}
940
	}
940
	return (this.extra[5][vectorIndex]
941
	return (this.extra[5][vectorIndex]
(-)compiler/org/eclipse/jdt/internal/compiler/impl/CompilerOptions.java (-4 / +38 lines)
Lines 8-15 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Benjamin Muskalla - Contribution for bug 239066
10
 *     Benjamin Muskalla - Contribution for bug 239066
11
 *     Stephan Herrmann  - Contribution for bug 236385
11
 *     Stephan Herrmann  - Contributions for
12
 *     Stephan Herrmann  - Contribution for bug 295551
12
 *     							bug 236385 - [compiler] Warn for potential programming problem if an object is created but not used
13
 *     							bug 295551 - Add option to automatically promote all warnings to errors
14
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
13
 *******************************************************************************/
15
 *******************************************************************************/
14
package org.eclipse.jdt.internal.compiler.impl;
16
package org.eclipse.jdt.internal.compiler.impl;
15
17
Lines 137-142 Link Here
137
	public static final String OPTION_ReportMethodCanBeStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBeStatic";  //$NON-NLS-1$
139
	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$
140
	public static final String OPTION_ReportMethodCanBePotentiallyStatic = "org.eclipse.jdt.core.compiler.problem.reportMethodCanBePotentiallyStatic";  //$NON-NLS-1$
139
	public static final String OPTION_ReportRedundantSpecificationOfTypeArguments =  "org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments"; //$NON-NLS-1$
141
	public static final String OPTION_ReportRedundantSpecificationOfTypeArguments =  "org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments"; //$NON-NLS-1$
142
	public static final String OPTION_ReportUnclosedCloseable = "org.eclipse.jdt.core.compiler.problem.unclosedCloseable"; //$NON-NLS-1$
143
	public static final String OPTION_ReportPotentiallyUnclosedCloseable = "org.eclipse.jdt.core.compiler.problem.potentiallyUnclosedCloseable"; //$NON-NLS-1$
144
	public static final String OPTION_ReportExplicitlyClosedAutoCloseable = "org.eclipse.jdt.core.compiler.problem.explicitlyClosedAutoCloseable"; //$NON-NLS-1$
140
	/**
145
	/**
141
	 * Possible values for configurable options
146
	 * Possible values for configurable options
142
	 */
147
	 */
Lines 240-245 Link Here
240
	public static final int MethodCanBeStatic = IrritantSet.GROUP2 | ASTNode.Bit5;
245
	public static final int MethodCanBeStatic = IrritantSet.GROUP2 | ASTNode.Bit5;
241
	public static final int MethodCanBePotentiallyStatic = IrritantSet.GROUP2 | ASTNode.Bit6;
246
	public static final int MethodCanBePotentiallyStatic = IrritantSet.GROUP2 | ASTNode.Bit6;
242
	public static final int RedundantSpecificationOfTypeArguments = IrritantSet.GROUP2 | ASTNode.Bit7;
247
	public static final int RedundantSpecificationOfTypeArguments = IrritantSet.GROUP2 | ASTNode.Bit7;
248
	// bits 8-10 reserved for https://bugs.eclipse.org/bugs/show_bug.cgi?id=186342
249
	public static final int UnclosedCloseable = IrritantSet.GROUP2 | ASTNode.Bit11;
250
	public static final int PotentiallyUnclosedCloseable = IrritantSet.GROUP2 | ASTNode.Bit12;
251
	public static final int ExplicitlyClosedAutoCloseable = IrritantSet.GROUP2 | ASTNode.Bit13;
243
252
244
	// Severity level for handlers
253
	// Severity level for handlers
245
	/** 
254
	/** 
Lines 376-383 Link Here
376
		"javadoc", //$NON-NLS-1$
385
		"javadoc", //$NON-NLS-1$
377
		"nls", //$NON-NLS-1$
386
		"nls", //$NON-NLS-1$
378
		"null", //$NON-NLS-1$
387
		"null", //$NON-NLS-1$
379
		"restriction", //$NON-NLS-1$
380
		"rawtypes", //$NON-NLS-1$
388
		"rawtypes", //$NON-NLS-1$
389
		"resource", //$NON-NLS-1$
390
		"restriction", //$NON-NLS-1$		
381
		"serial", //$NON-NLS-1$
391
		"serial", //$NON-NLS-1$
382
		"static-access", //$NON-NLS-1$
392
		"static-access", //$NON-NLS-1$
383
		"static-method", //$NON-NLS-1$
393
		"static-method", //$NON-NLS-1$
Lines 551-556 Link Here
551
				return OPTION_ReportMethodCanBePotentiallyStatic;
561
				return OPTION_ReportMethodCanBePotentiallyStatic;
552
			case RedundantSpecificationOfTypeArguments :
562
			case RedundantSpecificationOfTypeArguments :
553
				return OPTION_ReportRedundantSpecificationOfTypeArguments;
563
				return OPTION_ReportRedundantSpecificationOfTypeArguments;
564
			case UnclosedCloseable :
565
				return OPTION_ReportUnclosedCloseable;
566
			case PotentiallyUnclosedCloseable :
567
				return OPTION_ReportPotentiallyUnclosedCloseable;
568
			case ExplicitlyClosedAutoCloseable :
569
				return OPTION_ReportExplicitlyClosedAutoCloseable;
554
		}
570
		}
555
		return null;
571
		return null;
556
	}
572
	}
Lines 714-719 Link Here
714
			OPTION_ReportUnusedTypeArgumentsForMethodInvocation,
730
			OPTION_ReportUnusedTypeArgumentsForMethodInvocation,
715
			OPTION_ReportUnusedWarningToken,
731
			OPTION_ReportUnusedWarningToken,
716
			OPTION_ReportVarargsArgumentNeedCast,
732
			OPTION_ReportVarargsArgumentNeedCast,
733
			OPTION_ReportUnclosedCloseable,
734
			OPTION_ReportPotentiallyUnclosedCloseable,
735
			OPTION_ReportExplicitlyClosedAutoCloseable,
717
		};
736
		};
718
		return result;
737
		return result;
719
	}
738
	}
Lines 784-793 Link Here
784
			case MethodCanBeStatic :
803
			case MethodCanBeStatic :
785
			case MethodCanBePotentiallyStatic :
804
			case MethodCanBePotentiallyStatic :
786
				return "static-method"; //$NON-NLS-1$
805
				return "static-method"; //$NON-NLS-1$
806
			case PotentiallyUnclosedCloseable:
807
			case UnclosedCloseable:
808
			case ExplicitlyClosedAutoCloseable:
809
				return "resource"; //$NON-NLS-1$
787
			case InvalidJavadoc :
810
			case InvalidJavadoc :
788
			case MissingJavadocComments :
811
			case MissingJavadocComments :
789
			case MissingJavadocTags:
812
			case MissingJavadocTags:
790
				return "javadoc"; //$NON-NLS-1$				
813
				return "javadoc"; //$NON-NLS-1$
791
		}
814
		}
792
		return null;
815
		return null;
793
	}
816
	}
Lines 841-846 Link Here
841
			case 'r' :
864
			case 'r' :
842
				if ("rawtypes".equals(warningToken)) //$NON-NLS-1$
865
				if ("rawtypes".equals(warningToken)) //$NON-NLS-1$
843
					return IrritantSet.RAW;
866
					return IrritantSet.RAW;
867
				if ("resource".equals(warningToken)) //$NON-NLS-1$
868
					return IrritantSet.RESOURCE;
844
				if ("restriction".equals(warningToken)) //$NON-NLS-1$
869
				if ("restriction".equals(warningToken)) //$NON-NLS-1$
845
					return IrritantSet.RESTRICTION;
870
					return IrritantSet.RESTRICTION;
846
				break;
871
				break;
Lines 980-985 Link Here
980
		optionsMap.put(OPTION_ReportMethodCanBeStatic, getSeverityString(MethodCanBeStatic));
1005
		optionsMap.put(OPTION_ReportMethodCanBeStatic, getSeverityString(MethodCanBeStatic));
981
		optionsMap.put(OPTION_ReportMethodCanBePotentiallyStatic, getSeverityString(MethodCanBePotentiallyStatic));
1006
		optionsMap.put(OPTION_ReportMethodCanBePotentiallyStatic, getSeverityString(MethodCanBePotentiallyStatic));
982
		optionsMap.put(OPTION_ReportRedundantSpecificationOfTypeArguments, getSeverityString(RedundantSpecificationOfTypeArguments));
1007
		optionsMap.put(OPTION_ReportRedundantSpecificationOfTypeArguments, getSeverityString(RedundantSpecificationOfTypeArguments));
1008
		optionsMap.put(OPTION_ReportUnclosedCloseable, getSeverityString(UnclosedCloseable));
1009
		optionsMap.put(OPTION_ReportPotentiallyUnclosedCloseable, getSeverityString(PotentiallyUnclosedCloseable));
1010
		optionsMap.put(OPTION_ReportExplicitlyClosedAutoCloseable, getSeverityString(ExplicitlyClosedAutoCloseable));
983
		return optionsMap;
1011
		return optionsMap;
984
	}
1012
	}
985
1013
Lines 1410-1415 Link Here
1410
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBeStatic)) != null) updateSeverity(MethodCanBeStatic, optionValue);
1438
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBeStatic)) != null) updateSeverity(MethodCanBeStatic, optionValue);
1411
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBePotentiallyStatic)) != null) updateSeverity(MethodCanBePotentiallyStatic, optionValue);
1439
		if ((optionValue = optionsMap.get(OPTION_ReportMethodCanBePotentiallyStatic)) != null) updateSeverity(MethodCanBePotentiallyStatic, optionValue);
1412
		if ((optionValue = optionsMap.get(OPTION_ReportRedundantSpecificationOfTypeArguments)) != null) updateSeverity(RedundantSpecificationOfTypeArguments, optionValue);
1440
		if ((optionValue = optionsMap.get(OPTION_ReportRedundantSpecificationOfTypeArguments)) != null) updateSeverity(RedundantSpecificationOfTypeArguments, optionValue);
1441
		if ((optionValue = optionsMap.get(OPTION_ReportUnclosedCloseable)) != null) updateSeverity(UnclosedCloseable, optionValue);
1442
		if ((optionValue = optionsMap.get(OPTION_ReportPotentiallyUnclosedCloseable)) != null) updateSeverity(PotentiallyUnclosedCloseable, optionValue);
1443
		if ((optionValue = optionsMap.get(OPTION_ReportExplicitlyClosedAutoCloseable)) != null) updateSeverity(ExplicitlyClosedAutoCloseable, optionValue);
1413
1444
1414
		// Javadoc options
1445
		// Javadoc options
1415
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
1446
		if ((optionValue = optionsMap.get(OPTION_DocCommentSupport)) != null) {
Lines 1625-1630 Link Here
1625
		buf.append("\n\t- method can be static: ").append(getSeverityString(MethodCanBeStatic)); //$NON-NLS-1$
1656
		buf.append("\n\t- method can be static: ").append(getSeverityString(MethodCanBeStatic)); //$NON-NLS-1$
1626
		buf.append("\n\t- method can be potentially static: ").append(getSeverityString(MethodCanBePotentiallyStatic)); //$NON-NLS-1$
1657
		buf.append("\n\t- method can be potentially static: ").append(getSeverityString(MethodCanBePotentiallyStatic)); //$NON-NLS-1$
1627
		buf.append("\n\t- redundant specification of type arguments: ").append(getSeverityString(RedundantSpecificationOfTypeArguments)); //$NON-NLS-1$
1658
		buf.append("\n\t- redundant specification of type arguments: ").append(getSeverityString(RedundantSpecificationOfTypeArguments)); //$NON-NLS-1$
1659
		buf.append("\n\t- resource is not closed: ").append(getSeverityString(UnclosedCloseable)); //$NON-NLS-1$
1660
		buf.append("\n\t- resource may not be closed: ").append(getSeverityString(PotentiallyUnclosedCloseable)); //$NON-NLS-1$
1661
		buf.append("\n\t- resource should be handled by try-with-resources: ").append(getSeverityString(ExplicitlyClosedAutoCloseable)); //$NON-NLS-1$
1628
		return buf.toString();
1662
		return buf.toString();
1629
	}
1663
	}
1630
	
1664
	
(-)compiler/org/eclipse/jdt/internal/compiler/impl/IrritantSet.java (-1 / +7 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
12
12
package org.eclipse.jdt.internal.compiler.impl;
13
package org.eclipse.jdt.internal.compiler.impl;
Lines 57-62 Link Here
57
	public static final IrritantSet UNUSED = new IrritantSet(CompilerOptions.UnusedLocalVariable);
58
	public static final IrritantSet UNUSED = new IrritantSet(CompilerOptions.UnusedLocalVariable);
58
	public static final IrritantSet UNCHECKED = new IrritantSet(CompilerOptions.UncheckedTypeOperation);
59
	public static final IrritantSet UNCHECKED = new IrritantSet(CompilerOptions.UncheckedTypeOperation);
59
	public static final IrritantSet UNQUALIFIED_FIELD_ACCESS = new IrritantSet(CompilerOptions.UnqualifiedFieldAccess);
60
	public static final IrritantSet UNQUALIFIED_FIELD_ACCESS = new IrritantSet(CompilerOptions.UnqualifiedFieldAccess);
61
	public static final IrritantSet RESOURCE = new IrritantSet(CompilerOptions.UnclosedCloseable);
60
62
61
	public static final IrritantSet JAVADOC = new IrritantSet(CompilerOptions.InvalidJavadoc);
63
	public static final IrritantSet JAVADOC = new IrritantSet(CompilerOptions.InvalidJavadoc);
62
	public static final IrritantSet COMPILER_DEFAULT_ERRORS = new IrritantSet(0); // no optional error by default	
64
	public static final IrritantSet COMPILER_DEFAULT_ERRORS = new IrritantSet(0); // no optional error by default	
Lines 99-105 Link Here
99
			// group-2 warnings enabled by default
101
			// group-2 warnings enabled by default
100
			.set(
102
			.set(
101
				CompilerOptions.DeadCode
103
				CompilerOptions.DeadCode
102
				|CompilerOptions.Tasks);
104
				|CompilerOptions.Tasks
105
				|CompilerOptions.UnclosedCloseable);
103
			
106
			
104
		ALL.setAll();
107
		ALL.setAll();
105
		HIDING
108
		HIDING
Lines 124-129 Link Here
124
			.set(CompilerOptions.RedundantSpecificationOfTypeArguments);
127
			.set(CompilerOptions.RedundantSpecificationOfTypeArguments);
125
		STATIC_METHOD
128
		STATIC_METHOD
126
		    .set(CompilerOptions.MethodCanBePotentiallyStatic);
129
		    .set(CompilerOptions.MethodCanBePotentiallyStatic);
130
		RESOURCE
131
			.set(CompilerOptions.PotentiallyUnclosedCloseable)
132
			.set(CompilerOptions.ExplicitlyClosedAutoCloseable);
127
		String suppressRawWhenUnchecked = System.getProperty("suppressRawWhenUnchecked"); //$NON-NLS-1$
133
		String suppressRawWhenUnchecked = System.getProperty("suppressRawWhenUnchecked"); //$NON-NLS-1$
128
		if (suppressRawWhenUnchecked != null && "true".equalsIgnoreCase(suppressRawWhenUnchecked)) { //$NON-NLS-1$
134
		if (suppressRawWhenUnchecked != null && "true".equalsIgnoreCase(suppressRawWhenUnchecked)) { //$NON-NLS-1$
129
			UNCHECKED.set(CompilerOptions.RawTypeReference);
135
			UNCHECKED.set(CompilerOptions.RawTypeReference);
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/BinaryTypeBinding.java (-2 / +21 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 971-976 Link Here
971
	variable.resolve();
972
	variable.resolve();
972
	return variable;
973
	return variable;
973
}
974
}
975
public boolean hasTypeBit(int bit) {
976
	// ensure hierarchy is resolved, which will propagate bits down to us
977
	superclass();
978
	superInterfaces();
979
	return (this.typeBits & bit) != 0;
980
}
974
private void initializeTypeVariable(TypeVariableBinding variable, TypeVariableBinding[] existingVariables, SignatureWrapper wrapper, char[][][] missingTypeNames) {
981
private void initializeTypeVariable(TypeVariableBinding variable, TypeVariableBinding[] existingVariables, SignatureWrapper wrapper, char[][][] missingTypeNames) {
975
	// ParameterSignature = Identifier ':' TypeSignature
982
	// ParameterSignature = Identifier ':' TypeSignature
976
	//   or Identifier ':' TypeSignature(optional) InterfaceBound(s)
983
	//   or Identifier ':' TypeSignature(optional) InterfaceBound(s)
Lines 1139-1146 Link Here
1139
	// finish resolving the type
1146
	// finish resolving the type
1140
	this.superclass = (ReferenceBinding) resolveType(this.superclass, this.environment, true /* raw conversion */);
1147
	this.superclass = (ReferenceBinding) resolveType(this.superclass, this.environment, true /* raw conversion */);
1141
	this.tagBits &= ~TagBits.HasUnresolvedSuperclass;
1148
	this.tagBits &= ~TagBits.HasUnresolvedSuperclass;
1142
	if (this.superclass.problemId() == ProblemReasons.NotFound)
1149
	if (this.superclass.problemId() == ProblemReasons.NotFound) {
1143
		this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency
1150
		this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency
1151
	} else {
1152
		// make super-type resolving recursive for propagating typeBits downwards
1153
		this.superclass.superclass();
1154
		this.superclass.superInterfaces();
1155
	}
1156
	this.typeBits |= this.superclass.typeBits;
1144
	return this.superclass;
1157
	return this.superclass;
1145
}
1158
}
1146
// NOTE: superInterfaces of binary types are resolved when needed
1159
// NOTE: superInterfaces of binary types are resolved when needed
Lines 1150-1157 Link Here
1150
1163
1151
	for (int i = this.superInterfaces.length; --i >= 0;) {
1164
	for (int i = this.superInterfaces.length; --i >= 0;) {
1152
		this.superInterfaces[i] = (ReferenceBinding) resolveType(this.superInterfaces[i], this.environment, true /* raw conversion */);
1165
		this.superInterfaces[i] = (ReferenceBinding) resolveType(this.superInterfaces[i], this.environment, true /* raw conversion */);
1153
		if (this.superInterfaces[i].problemId() == ProblemReasons.NotFound)
1166
		if (this.superInterfaces[i].problemId() == ProblemReasons.NotFound) {
1154
			this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency
1167
			this.tagBits |= TagBits.HierarchyHasProblems; // propagate type inconsistency
1168
		} else {
1169
			// make super-type resolving recursive for propagating typeBits downwards
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 (+84 lines)
Lines 7-19 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
14
import java.util.ArrayList;
15
import java.util.List;
16
13
import org.eclipse.jdt.core.compiler.CharOperation;
17
import org.eclipse.jdt.core.compiler.CharOperation;
14
import org.eclipse.jdt.internal.compiler.ast.*;
18
import org.eclipse.jdt.internal.compiler.ast.*;
15
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
19
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
16
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
20
import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
21
import org.eclipse.jdt.internal.compiler.flow.FlowContext;
22
import org.eclipse.jdt.internal.compiler.flow.FlowInfo;
17
import org.eclipse.jdt.internal.compiler.impl.Constant;
23
import org.eclipse.jdt.internal.compiler.impl.Constant;
18
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
24
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
19
25
Lines 957-960 Link Here
957
		}
963
		}
958
	}
964
	}
959
}
965
}
966
967
private List trackingVariables; // can be null if no resources are tracked
968
/**
969
 * Register a tracking variable and compute its id.
970
 */
971
public int registerTrackingVariable(FakedTrackingVariable fakedTrackingVariable) {
972
	if (this.trackingVariables == null)
973
		this.trackingVariables = new ArrayList();
974
	this.trackingVariables.add(fakedTrackingVariable);
975
	MethodScope outerMethodScope = outerMostMethodScope();
976
	return outerMethodScope.analysisIndex + (++outerMethodScope.trackVarCount);
977
	
978
}
979
/**
980
 * At the end of a block check the closing-status of all tracked closeables that are declared in this block. 
981
 */
982
public void checkUnclosedCloseables(FlowInfo flowInfo, FlowContext flowContext, ASTNode location) {
983
	if (this.trackingVariables == null) return;
984
	for (int i=0; i<this.trackingVariables.size(); i++) {
985
		FakedTrackingVariable trackingVar = (FakedTrackingVariable) this.trackingVariables.get(i);
986
		if (trackingVar.hasReportedProblem)
987
			continue;
988
		if (trackingVar.originalBinding != null && flowInfo.isDefinitelyNull(trackingVar.originalBinding))
989
			continue; // resource is null at this flow, don't complain
990
		int status = FlowContext.getNullStatusAfter(trackingVar.binding, flowInfo, flowContext);
991
		if (status == FlowInfo.NULL) {
992
			if (trackingVar.closedInNestedMethod)
993
				problemReporter().potentiallyUnclosedCloseable(trackingVar, location);
994
			else
995
				problemReporter().unclosedCloseable(trackingVar, location);
996
		} else if (status == FlowInfo.POTENTIALLY_NULL) {
997
			problemReporter().potentiallyUnclosedCloseable(trackingVar, location);
998
		} else if (status == FlowInfo.NON_NULL) {
999
			if (environment().globalOptions.complianceLevel >= ClassFileConstants.JDK1_7 
1000
					&& !trackingVar.isInsideTryWithResources)
1001
				problemReporter().explicitlyClosedAutoCloseable(trackingVar);
1002
		}
1003
	}
1004
}
1005
1006
/** 
1007
 * If one branch of an if-else closes any AutoCloseable resource, and if the same
1008
 * resource is known to be null on the other branch mark it as closed, too,
1009
 * so that merging both branches indicates that the resource is always closed.
1010
 * Example:
1011
 *	FileReader fr1 = null;
1012
 *	try {\n" +
1013
 *      fr1 = new FileReader(someFile);" + 
1014
 *		fr1.read(buf);\n" + 
1015
 *	} finally {\n" + 
1016
 *		if (fr1 != null)\n" +
1017
 *           try {\n" +
1018
 *               fr1.close();\n" +
1019
 *           } catch (IOException e) {
1020
 *              // do nothing 
1021
 *           }
1022
 *      // after this if statement fr1 is definitely not leaked 
1023
 *	}
1024
 */
1025
public void correlateTrackingVarsIfElse(FlowInfo thenFlowInfo, FlowInfo elseFlowInfo) {
1026
	if (this.trackingVariables != null) {
1027
		for (int i=0; i<this.trackingVariables.size(); i++) {
1028
			FakedTrackingVariable trackingVar = (FakedTrackingVariable) this.trackingVariables.get(i);
1029
			if (   thenFlowInfo.isDefinitelyNonNull(trackingVar.binding)			// closed in then branch
1030
				&& elseFlowInfo.isDefinitelyNull(trackingVar.originalBinding))		// null in else branch
1031
			{
1032
				elseFlowInfo.markAsDefinitelyNonNull(trackingVar.binding);			// -> always closed
1033
			}
1034
			else if (   elseFlowInfo.isDefinitelyNonNull(trackingVar.binding)		// closed in else branch
1035
					 && thenFlowInfo.isDefinitelyNull(trackingVar.originalBinding))	// null in then branch
1036
			{
1037
				thenFlowInfo.markAsDefinitelyNonNull(trackingVar.binding);			// -> always closed
1038
			}
1039
		}
1040
	}
1041
	if (this.parent instanceof BlockScope)
1042
		((BlockScope) this.parent).correlateTrackingVarsIfElse(thenFlowInfo, elseFlowInfo);
1043
}
960
}
1044
}
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ClassScope.java (+3 lines)
Lines 11-16 Link Here
11
 *     						Bug 328281 - visibility leaks not detected when analyzing unused field in private class
11
 *     						Bug 328281 - visibility leaks not detected when analyzing unused field in private class
12
 *     						Bug 300576 - NPE Computing type hierarchy when compliance doesn't match libraries
12
 *     						Bug 300576 - NPE Computing type hierarchy when compliance doesn't match libraries
13
 *     						Bug 354536 - compiling package-info.java still depends on the order of compilation units
13
 *     						Bug 354536 - compiling package-info.java still depends on the order of compilation units
14
 *     						Bug 349326 - [1.7] new warning for missing try-with-resources
14
 *******************************************************************************/
15
 *******************************************************************************/
15
package org.eclipse.jdt.internal.compiler.lookup;
16
package org.eclipse.jdt.internal.compiler.lookup;
16
17
Lines 908-913 Link Here
908
			} else {
909
			} else {
909
				// only want to reach here when no errors are reported
910
				// only want to reach here when no errors are reported
910
				sourceType.superclass = superclass;
911
				sourceType.superclass = superclass;
912
				sourceType.typeBits |= superclass.typeBits;
911
				return true;
913
				return true;
912
			}
914
			}
913
		}
915
		}
Lines 1023-1028 Link Here
1023
				noProblems &= superInterfaceRef.resolvedType.isValidBinding();
1025
				noProblems &= superInterfaceRef.resolvedType.isValidBinding();
1024
			}
1026
			}
1025
			// only want to reach here when no errors are reported
1027
			// only want to reach here when no errors are reported
1028
			sourceType.typeBits |= superInterface.typeBits;
1026
			interfaceBindings[count++] = superInterface;
1029
			interfaceBindings[count++] = superInterface;
1027
		}
1030
		}
1028
		// hold onto all correctly resolved superinterfaces
1031
		// hold onto all correctly resolved superinterfaces
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/LocalVariableBinding.java (-1 / +6 lines)
Lines 7-13 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contribution for bug 185682 - Increment/decrement operators mark local variables as read
10
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contributions for
11
 *     							bug 185682 - Increment/decrement operators mark local variables as read
12
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
11
 *******************************************************************************/
13
 *******************************************************************************/
12
package org.eclipse.jdt.internal.compiler.lookup;
14
package org.eclipse.jdt.internal.compiler.lookup;
13
15
Lines 15-20 Link Here
15
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
17
import org.eclipse.jdt.internal.compiler.ast.ASTNode;
16
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
18
import org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration;
17
import org.eclipse.jdt.internal.compiler.ast.Annotation;
19
import org.eclipse.jdt.internal.compiler.ast.Annotation;
20
import org.eclipse.jdt.internal.compiler.ast.FakedTrackingVariable;
18
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
21
import org.eclipse.jdt.internal.compiler.ast.LocalDeclaration;
19
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
22
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
20
import org.eclipse.jdt.internal.compiler.impl.Constant;
23
import org.eclipse.jdt.internal.compiler.impl.Constant;
Lines 35-40 Link Here
35
	public int[] initializationPCs;
38
	public int[] initializationPCs;
36
	public int initializationCount = 0;
39
	public int initializationCount = 0;
37
40
41
	public FakedTrackingVariable closeTracker; // track closing of instances of type AutoCloseable, maybe null
42
38
	// for synthetic local variables
43
	// for synthetic local variables
39
	// if declaration slot is not positionned, the variable will not be listed in attribute
44
	// if declaration slot is not positionned, the variable will not be listed in attribute
40
	// 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)
45
	// 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 (+4 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 49-54 Link Here
49
	// inner-emulation
50
	// inner-emulation
50
	public SyntheticArgumentBinding[] extraSyntheticArguments;
51
	public SyntheticArgumentBinding[] extraSyntheticArguments;
51
52
53
	// count number of tracking variables, see FakedTrackingVariable
54
	int trackVarCount;
55
52
public MethodScope(ClassScope parent, ReferenceContext context, boolean isStatic) {
56
public MethodScope(ClassScope parent, ReferenceContext context, boolean isStatic) {
53
	super(METHOD_SCOPE, parent);
57
	super(METHOD_SCOPE, parent);
54
	this.locals = new LocalVariableBinding[5];
58
	this.locals = new LocalVariableBinding[5];
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ParameterizedTypeBinding.java (+8 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 629-634 Link Here
629
	    return this.type.hasMemberTypes();
630
	    return this.type.hasMemberTypes();
630
	}
631
	}
631
632
633
	public boolean hasTypeBit(int bit) {
634
		TypeBinding erasure = erasure();
635
		if (erasure instanceof ReferenceBinding)
636
			return ((ReferenceBinding) erasure).hasTypeBit(bit);
637
		return false;
638
	}
639
632
	/**
640
	/**
633
	 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#implementsMethod(MethodBinding)
641
	 * @see org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding#implementsMethod(MethodBinding)
634
	 */
642
	 */
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ProblemReferenceBinding.java (+7 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 40-45 Link Here
40
	return this.closestMatch;
41
	return this.closestMatch;
41
}
42
}
42
43
44
public boolean hasTypeBit(int bit) {
45
	if (this.closestMatch != null)
46
		return this.closestMatch.hasTypeBit(bit);
47
	return false;
48
}
49
43
/* API
50
/* API
44
* Answer the problem id associated with the receiver.
51
* Answer the problem id associated with the receiver.
45
* NoError if the receiver is a valid binding.
52
* NoError if the receiver is a valid binding.
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/ReferenceBinding.java (-2 / +11 lines)
Lines 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 41-47 Link Here
41
42
42
	private SimpleLookupTable compatibleCache;
43
	private SimpleLookupTable compatibleCache;
43
44
44
	public static final ReferenceBinding LUB_GENERIC = new ReferenceBinding() { /* used for lub computation */};
45
	int typeBits; // additional bits characterizing this type
46
47
	public static final ReferenceBinding LUB_GENERIC = new ReferenceBinding() { /* used for lub computation */
48
		public boolean hasTypeBit(int bit) { return false; }
49
	};
45
50
46
	private static final Comparator FIELD_COMPARATOR = new Comparator() {
51
	private static final Comparator FIELD_COMPARATOR = new Comparator() {
47
		public int compare(Object o1, Object o2) {
52
		public int compare(Object o1, Object o2) {
Lines 424-431 Link Here
424
				case 'A' :
429
				case 'A' :
425
					switch(typeName.length) {
430
					switch(typeName.length) {
426
						case 13 :
431
						case 13 :
427
							if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_AUTOCLOSEABLE[2]))
432
							if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_AUTOCLOSEABLE[2])) {
428
								this.id = TypeIds.T_JavaLangAutoCloseable;
433
								this.id = TypeIds.T_JavaLangAutoCloseable;
434
								this.typeBits |= TypeIds.BitAutoCloseable; 
435
							}
429
							return;
436
							return;
430
						case 14:
437
						case 14:
431
							if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ASSERTIONERROR[2]))
438
							if (CharOperation.equals(typeName, TypeConstants.JAVA_LANG_ASSERTIONERROR[2]))
Lines 923-928 Link Here
923
public final boolean hasRestrictedAccess() {
930
public final boolean hasRestrictedAccess() {
924
	return (this.modifiers & ExtraCompilerModifiers.AccRestrictedAccess) != 0;
931
	return (this.modifiers & ExtraCompilerModifiers.AccRestrictedAccess) != 0;
925
}
932
}
933
/** Answer an additional bit characterizing this type, like {@link TypeIds#BitAutoCloseable}. */
934
abstract public boolean hasTypeBit(int bit);
926
935
927
/** Answer true if the receiver implements anInterface or is identical to anInterface.
936
/** Answer true if the receiver implements anInterface or is identical to anInterface.
928
* If searchHierarchy is true, then also search the receiver's superclasses.
937
* If searchHierarchy is true, then also search the receiver's superclasses.
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/SourceTypeBinding.java (-1 / +8 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contribution for bug 328281 - visibility leaks not detected when analyzing unused field in private class
10
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contributions for
11
 *     							bug 328281 - visibility leaks not detected when analyzing unused field in private class
12
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
11
 *******************************************************************************/
13
 *******************************************************************************/
12
package org.eclipse.jdt.internal.compiler.lookup;
14
package org.eclipse.jdt.internal.compiler.lookup;
13
15
Lines 1063-1068 Link Here
1063
	return accessors[1];
1065
	return accessors[1];
1064
}
1066
}
1065
1067
1068
public boolean hasTypeBit(int bit) {
1069
	// source types initialize type bits during connectSuperclass/interfaces()
1070
	return (this.typeBits & bit) != 0;
1071
}
1072
1066
/**
1073
/**
1067
 * @see org.eclipse.jdt.internal.compiler.lookup.Binding#initializeDeprecatedAnnotationTagBits()
1074
 * @see org.eclipse.jdt.internal.compiler.lookup.Binding#initializeDeprecatedAnnotationTagBits()
1068
 */
1075
 */
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/TypeConstants.java (+2 lines)
Lines 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 150-155 Link Here
150
			"MethodHandle$PolymorphicSignature".toCharArray() //$NON-NLS-1$
151
			"MethodHandle$PolymorphicSignature".toCharArray() //$NON-NLS-1$
151
	};
152
	};
152
	char[][] JAVA_LANG_AUTOCLOSEABLE =  {JAVA, LANG, "AutoCloseable".toCharArray()}; //$NON-NLS-1$
153
	char[][] JAVA_LANG_AUTOCLOSEABLE =  {JAVA, LANG, "AutoCloseable".toCharArray()}; //$NON-NLS-1$
154
	char[] CLOSE = "close".toCharArray(); //$NON-NLS-1$
153
155
154
	// Constraints for generic type argument inference
156
	// Constraints for generic type argument inference
155
	int CONSTRAINT_EQUAL = 0;		// Actual = Formal
157
	int CONSTRAINT_EQUAL = 0;		// Actual = Formal
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/TypeIds.java (+7 lines)
Lines 7-12 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 177-180 Link Here
177
	final int Object2boolean = T_JavaLangObject + (T_boolean << 4);
178
	final int Object2boolean = T_JavaLangObject + (T_boolean << 4);
178
	final int BOXING = 0x200;
179
	final int BOXING = 0x200;
179
	final int UNBOXING = 0x400;
180
	final int UNBOXING = 0x400;
181
182
	/** 
183
	 * Marks all sub-types of java.lang.AutoCloseable.
184
	 * @see ReferenceBinding#hasTypeBit(int)
185
	 */
186
	final int BitAutoCloseable = 1;
180
}
187
}
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/TypeVariableBinding.java (-2 / +18 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contribution for bug 282152 - [1.5][compiler] Generics code rejected by Eclipse but accepted by javac
10
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contributions for
11
 *     							bug 282152 - [1.5][compiler] Generics code rejected by Eclipse but accepted by javac
12
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
11
 *******************************************************************************/
13
 *******************************************************************************/
12
package org.eclipse.jdt.internal.compiler.lookup;
14
package org.eclipse.jdt.internal.compiler.lookup;
13
15
Lines 42-47 Link Here
42
		this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature; // treat type var as public
44
		this.modifiers = ClassFileConstants.AccPublic | ExtraCompilerModifiers.AccGenericSignature; // treat type var as public
43
		this.tagBits |= TagBits.HasTypeVariable;
45
		this.tagBits |= TagBits.HasTypeVariable;
44
		this.environment = environment;
46
		this.environment = environment;
47
		this.typeBits = -1;
45
	}
48
	}
46
49
47
	/**
50
	/**
Lines 307-312 Link Here
307
		return true;
310
		return true;
308
	}
311
	}
309
312
313
	public boolean hasTypeBit(int bit) {
314
		if (this.typeBits == -1) {
315
			// initialize from bounds
316
			this.typeBits = 0;
317
			if (this.superclass != null)
318
				this.typeBits |= this.superclass.typeBits;
319
			if (this.superInterfaces != null)
320
				for (int i = 0, l = this.superInterfaces.length; i < l; i++)
321
					this.typeBits |= this.superInterfaces[i].typeBits;
322
		}
323
		return (this.typeBits & bit) != 0;
324
	}
325
310
	/**
326
	/**
311
	 * Returns true if the type variable is directly bound to a given type
327
	 * Returns true if the type variable is directly bound to a given type
312
	 */
328
	 */
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/UnresolvedReferenceBinding.java (-1 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2008 IBM Corporation and others.
2
 * Copyright (c) 2000, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 41-46 Link Here
41
public String debugName() {
42
public String debugName() {
42
	return toString();
43
	return toString();
43
}
44
}
45
public boolean hasTypeBit(int bit) {
46
	// shouldn't happen since we are not called before analyseCode(), but play safe:
47
	return false;
48
}
44
ReferenceBinding resolve(LookupEnvironment environment, boolean convertGenericToRawType) {
49
ReferenceBinding resolve(LookupEnvironment environment, boolean convertGenericToRawType) {
45
    ReferenceBinding targetType = this.resolvedType;
50
    ReferenceBinding targetType = this.resolvedType;
46
	if (targetType == null) {
51
	if (targetType == null) {
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/WildcardBinding.java (-1 / +16 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2009 IBM Corporation and others.
2
 * Copyright (c) 2005, 2011 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.lookup;
12
package org.eclipse.jdt.internal.compiler.lookup;
12
13
Lines 54-59 Link Here
54
		if (bound instanceof UnresolvedReferenceBinding)
55
		if (bound instanceof UnresolvedReferenceBinding)
55
			((UnresolvedReferenceBinding) bound).addWrapper(this, environment);
56
			((UnresolvedReferenceBinding) bound).addWrapper(this, environment);
56
		this.tagBits |=  TagBits.HasUnresolvedTypeVariables; // cleared in resolve()
57
		this.tagBits |=  TagBits.HasUnresolvedTypeVariables; // cleared in resolve()
58
		this.typeBits = -1;
57
	}
59
	}
58
60
59
	public int kind() {
61
	public int kind() {
Lines 420-425 Link Here
420
		return this.genericType.hashCode();
422
		return this.genericType.hashCode();
421
	}
423
	}
422
424
425
	public boolean hasTypeBit(int bit) {
426
		if (this.typeBits == -1) {
427
			// initialize from upper bounds
428
			this.typeBits = 0;
429
			if (this.superclass != null)
430
				this.typeBits |= this.superclass.typeBits;
431
			if (this.superInterfaces != null)
432
				for (int i = 0, l = this.superInterfaces.length; i < l; i++)
433
					this.typeBits |= this.superInterfaces[i].typeBits;
434
		}
435
		return (this.typeBits & bit) != 0;
436
	}
437
423
	void initialize(ReferenceBinding someGenericType, TypeBinding someBound, TypeBinding[] someOtherBounds) {
438
	void initialize(ReferenceBinding someGenericType, TypeBinding someBound, TypeBinding[] someOtherBounds) {
424
		this.genericType = someGenericType;
439
		this.genericType = someGenericType;
425
		this.bound = someBound;
440
		this.bound = someBound;
(-)compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java (+61 lines)
Lines 11-16 Link Here
11
 *     Stephan Herrmann  - Contributions for 
11
 *     Stephan Herrmann  - Contributions for 
12
 *     						bug 236385 - 
12
 *     						bug 236385 - 
13
 *     						bug 338303 - Warning about Redundant assignment conflicts with definite assignment
13
 *     						bug 338303 - Warning about Redundant assignment conflicts with definite assignment
14
 *     						bug 349326 - [1.7] new warning for missing try-with-resources
14
 *******************************************************************************/
15
 *******************************************************************************/
15
package org.eclipse.jdt.internal.compiler.problem;
16
package org.eclipse.jdt.internal.compiler.problem;
16
17
Lines 51-56 Link Here
51
import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
52
import org.eclipse.jdt.internal.compiler.ast.EqualExpression;
52
import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
53
import org.eclipse.jdt.internal.compiler.ast.ExplicitConstructorCall;
53
import org.eclipse.jdt.internal.compiler.ast.Expression;
54
import org.eclipse.jdt.internal.compiler.ast.Expression;
55
import org.eclipse.jdt.internal.compiler.ast.FakedTrackingVariable;
54
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
56
import org.eclipse.jdt.internal.compiler.ast.FieldDeclaration;
55
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
57
import org.eclipse.jdt.internal.compiler.ast.FieldReference;
56
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
58
import org.eclipse.jdt.internal.compiler.ast.ImportReference;
Lines 426-431 Link Here
426
			
428
			
427
		case IProblem.MethodCanBePotentiallyStatic:
429
		case IProblem.MethodCanBePotentiallyStatic:
428
			return CompilerOptions.MethodCanBePotentiallyStatic;
430
			return CompilerOptions.MethodCanBePotentiallyStatic;
431
432
		case IProblem.UnclosedCloseable:
433
		case IProblem.UnclosedCloseableAtExit:
434
			return CompilerOptions.UnclosedCloseable;
435
		case IProblem.PotentiallyUnclosedCloseable:
436
		case IProblem.PotentiallyUnclosedCloseableAtExit:
437
			return CompilerOptions.PotentiallyUnclosedCloseable;
438
		case IProblem.ExplicitlyClosedAutoCloseable:
439
			return CompilerOptions.ExplicitlyClosedAutoCloseable;
429
				
440
				
430
		case IProblem.RedundantSpecificationOfTypeArguments:
441
		case IProblem.RedundantSpecificationOfTypeArguments:
431
			return CompilerOptions.RedundantSpecificationOfTypeArguments;
442
			return CompilerOptions.RedundantSpecificationOfTypeArguments;
Lines 461-466 Link Here
461
			case CompilerOptions.ParameterAssignment :
472
			case CompilerOptions.ParameterAssignment :
462
			case CompilerOptions.MethodCanBeStatic :
473
			case CompilerOptions.MethodCanBeStatic :
463
			case CompilerOptions.MethodCanBePotentiallyStatic :
474
			case CompilerOptions.MethodCanBePotentiallyStatic :
475
			case CompilerOptions.ExplicitlyClosedAutoCloseable :
464
				return CategorizedProblem.CAT_CODE_STYLE;
476
				return CategorizedProblem.CAT_CODE_STYLE;
465
477
466
			case CompilerOptions.MaskedCatchBlock :
478
			case CompilerOptions.MaskedCatchBlock :
Lines 482-487 Link Here
482
			case CompilerOptions.ShouldImplementHashcode :
494
			case CompilerOptions.ShouldImplementHashcode :
483
			case CompilerOptions.DeadCode :
495
			case CompilerOptions.DeadCode :
484
			case CompilerOptions.UnusedObjectAllocation :
496
			case CompilerOptions.UnusedObjectAllocation :
497
			case CompilerOptions.UnclosedCloseable :
498
			case CompilerOptions.PotentiallyUnclosedCloseable :
485
				return CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM;
499
				return CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM;
486
			
500
			
487
			case CompilerOptions.OverriddenPackageDefaultMethod :
501
			case CompilerOptions.OverriddenPackageDefaultMethod :
Lines 7927-7930 Link Here
7927
			location.sourceEnd);
7941
			location.sourceEnd);
7928
    }
7942
    }
7929
}
7943
}
7944
public void potentiallyUnclosedCloseable(FakedTrackingVariable trackVar, ASTNode location) {
7945
	String[] args = { String.valueOf(trackVar.name) };
7946
	if (location == null) {
7947
		this.handle(
7948
			IProblem.PotentiallyUnclosedCloseable,
7949
			args,
7950
			args,
7951
			trackVar.sourceStart,
7952
			trackVar.sourceEnd);
7953
	} else {
7954
		this.handle(
7955
			IProblem.PotentiallyUnclosedCloseableAtExit,
7956
			args,
7957
			args,
7958
			location.sourceStart,
7959
			location.sourceEnd);
7960
	}
7961
	trackVar.hasReportedProblem = true;
7962
}
7963
public void unclosedCloseable(FakedTrackingVariable trackVar, ASTNode location) {
7964
	String[] args = { String.valueOf(trackVar.name) };
7965
	if (location == null) {
7966
		this.handle(
7967
			IProblem.UnclosedCloseable,
7968
			args,
7969
			args,
7970
			trackVar.sourceStart,
7971
			trackVar.sourceEnd);
7972
	} else {
7973
		this.handle(
7974
			IProblem.UnclosedCloseableAtExit,
7975
			args,
7976
			args,
7977
			location.sourceStart,
7978
			location.sourceEnd);
7979
	}
7980
	trackVar.hasReportedProblem = true;
7981
}
7982
public void explicitlyClosedAutoCloseable(FakedTrackingVariable trackVar) {
7983
	String[] args = { String.valueOf(trackVar.name) };
7984
	this.handle(
7985
		IProblem.ExplicitlyClosedAutoCloseable,
7986
		args,
7987
		args,
7988
		trackVar.sourceStart,
7989
		trackVar.sourceEnd);	
7990
}
7930
}
7991
}
(-)compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties (-1 / +8 lines)
Lines 8-14 Link Here
8
# Contributors:
8
# Contributors:
9
#     IBM Corporation - initial API and implementation
9
#     IBM Corporation - initial API and implementation
10
#		Benjamin Muskalla - Contribution for bug 239066
10
#		Benjamin Muskalla - Contribution for bug 239066
11
#		Stephan Herrmann <stephan@cs.tu-berlin.de> - Contribution for bug 185682 - Increment/decrement operators mark local variables as read
11
#		Stephan Herrmann <stephan@cs.tu-berlin.de> - Contributions for 
12
#							bug 185682 - Increment/decrement operators mark local variables as read
13
#							bug 349326 - [1.7] new warning for missing try-with-resources
12
###############################################################################
14
###############################################################################
13
0 = {0}
15
0 = {0}
14
1 = super cannot be used in java.lang.Object
16
1 = super cannot be used in java.lang.Object
Lines 644-649 Link Here
644
882 = Unhandled exception type {0} thrown by automatic close() invocation on {1}
646
882 = Unhandled exception type {0} thrown by automatic close() invocation on {1}
645
883 = '<>' operator is not allowed for source level below 1.7
647
883 = '<>' operator is not allowed for source level below 1.7
646
884 = Redundant specification of type arguments <{0}>
648
884 = Redundant specification of type arguments <{0}>
649
885 = Potentially leaking resource '{0}': is not closed on all paths
650
886 = Potentially leaking resource '{0}': may not be closed at this location
651
887 = Leaking resource '{0}': is never closed
652
888 = Leaking resource '{0}': is not closed at this location
653
889 = Resource '{0}' should be managed by try-with-resource
647
654
648
### ELABORATIONS
655
### ELABORATIONS
649
## Access restrictions
656
## Access restrictions
(-)model/org/eclipse/jdt/core/JavaCore.java (+50 lines)
Lines 83-88 Link Here
83
 *     Benjamin Muskalla - added COMPILER_PB_MISSING_SYNCHRONIZED_ON_INHERITED_METHOD
83
 *     Benjamin Muskalla - added COMPILER_PB_MISSING_SYNCHRONIZED_ON_INHERITED_METHOD
84
 *     Stephan Herrmann  - added COMPILER_PB_UNUSED_OBJECT_ALLOCATION
84
 *     Stephan Herrmann  - added COMPILER_PB_UNUSED_OBJECT_ALLOCATION
85
 *     Stephan Herrmann  - added COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS
85
 *     Stephan Herrmann  - added COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS
86
 *     Stephan Herrmann  - added the following constants:
87
 *     								COMPILER_PB_UNCLOSED_CLOSEABLE,
88
 *     								COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE
89
 *     								COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE
86
 *******************************************************************************/
90
 *******************************************************************************/
87
91
88
package org.eclipse.jdt.core;
92
package org.eclipse.jdt.core;
Lines 1357-1362 Link Here
1357
	 */
1361
	 */
1358
	public static final String COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD = PLUGIN_ID + ".compiler.problem.reportMethodCanBePotentiallyStatic"; //$NON-NLS-1$
1362
	public static final String COMPILER_PB_POTENTIALLY_MISSING_STATIC_ON_METHOD = PLUGIN_ID + ".compiler.problem.reportMethodCanBePotentiallyStatic"; //$NON-NLS-1$
1359
	/**
1363
	/**
1364
	 * Compiler option ID: Reporting a resource that is not closed properly.
1365
	 * <p>When enabled, that compiler will issue an error or a warning if
1366
	 *    a local variable holds a value of type AutoCloseable and if
1367
	 *    flow analysis shows that the method <code>close()</code> is not invoked locally on that value.
1368
	 * <dl>
1369
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportUnclosedCloseable"</code></dd>
1370
	 * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
1371
	 * <dt>Default:</dt><dd><code>"warning"</code></dd>
1372
	 * </dl>
1373
	 * @since 3.8
1374
	 * @category CompilerOptionID
1375
	 */
1376
	public static final String COMPILER_PB_UNCLOSED_CLOSEABLE = PLUGIN_ID + ".compiler.problem.unclosedCloseable"; //$NON-NLS-1$
1377
	/**
1378
	 * Compiler option ID: Reporting a resource that may not be closed properly.
1379
	 * <p>When enabled, that compiler will issue an error or a warning if
1380
	 *    a local variable holds a value of type AutoCloseable and if
1381
	 *    flow analysis shows that the method <code>close()</code> is 
1382
	 *    not invoked locally on that value for all execution paths.
1383
	 * <dl>
1384
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportPotentiallyUnclosedCloseable"</code></dd>
1385
	 * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
1386
	 * <dt>Default:</dt><dd><code>"ignore"</code></dd>
1387
	 * </dl>
1388
	 * @since 3.8
1389
	 * @category CompilerOptionID
1390
	 */
1391
	public static final String COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE = PLUGIN_ID + ".compiler.problem.potentiallyUnclosedCloseable"; //$NON-NLS-1$
1392
	/**
1393
	 * Compiler option ID: Reporting a resource that is not managed by try-with-resources.
1394
	 * <p>When enabled, that compiler will issue an error or a warning if a local variable 
1395
	 * 	  holds a value of type AutoCloseable, and if the method <code>close()</code> is
1396
	 * 	  explicitly invoked on that resource, but the resource is not managed by a
1397
	 * 	  try-with-resources block.
1398
	 * <p>Note that this option is not intended to be surfaced in the UI, as it is intended
1399
	 *    only for internal use for computing quick assists / cleanups. 
1400
	 * <dl>
1401
	 * <dt>Option id:</dt><dd><code>"org.eclipse.jdt.core.compiler.problem.reportPotentiallyUnclosedCloseable"</code></dd>
1402
	 * <dt>Possible values:</dt><dd><code>{ "error", "warning", "ignore" }</code></dd>
1403
	 * <dt>Default:</dt><dd><code>"ignore"</code></dd>
1404
	 * </dl>
1405
	 * @since 3.8
1406
	 * @category CompilerOptionID
1407
	 */
1408
	public static final String COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE = PLUGIN_ID + ".compiler.problem.explicitlyClosedAutoCloseable"; //$NON-NLS-1$
1409
	/**
1360
	 * Compiler option ID: Setting Source Compatibility Mode.
1410
	 * 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
1411
	 * <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
1412
	 *    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 / +8 lines)
Lines 8-16 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Benjamin Muskalla - Contribution for bug 239066
10
 *     Benjamin Muskalla - Contribution for bug 239066
11
 *     Stephan Herrmann  - Contribution for bug 236385
11
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contributions for
12
 *     Stephan Herrmann  - Contribution for bug 295551
12
 *     							bug 236385 - [compiler] Warn for potential programming problem if an object is created but not used
13
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contribution for bug 185682 - Increment/decrement operators mark local variables as read
13
 *     							bug 295551 - Add option to automatically promote all warnings to errors
14
 *     							bug 185682 - Increment/decrement operators mark local variables as read
15
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
14
 *******************************************************************************/
16
 *******************************************************************************/
15
package org.eclipse.jdt.core.tests.compiler.regression;
17
package org.eclipse.jdt.core.tests.compiler.regression;
16
18
Lines 1816-1821 Link Here
1816
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.discouragedReference\" value=\"warning\"/>\n" + 
1818
			"		<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" + 
1819
			"		<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" + 
1820
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.enumIdentifier\" value=\"warning\"/>\n" + 
1821
			"		<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" + 
1822
			"		<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" + 
1823
			"		<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" + 
1824
			"		<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" + 
1860
			"		<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" + 
1861
			"		<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" + 
1862
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.potentialNullReference\" value=\"ignore\"/>\n" + 
1863
			"		<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" +
1864
			"		<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" + 
1865
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantNullCheck\" value=\"ignore\"/>\n" + 
1862
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments\" value=\"ignore\"/>\n" + 
1866
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.redundantSpecificationOfTypeArguments\" value=\"ignore\"/>\n" + 
Lines 1872-1877 Link Here
1872
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.typeParameterHiding\" value=\"warning\"/>\n" + 
1876
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.typeParameterHiding\" value=\"warning\"/>\n" + 
1873
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems\" value=\"enabled\"/>\n" +
1877
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unavoidableGenericTypeProblems\" value=\"enabled\"/>\n" +
1874
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation\" value=\"warning\"/>\n" + 
1878
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation\" value=\"warning\"/>\n" + 
1879
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unclosedCloseable\" value=\"warning\"/>\n" + 
1875
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock\" value=\"ignore\"/>\n" + 
1880
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock\" value=\"ignore\"/>\n" + 
1876
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unhandledWarningToken\" value=\"warning\"/>\n" + 
1881
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unhandledWarningToken\" value=\"warning\"/>\n" + 
1877
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryElse\" value=\"ignore\"/>\n" + 
1882
			"		<option key=\"org.eclipse.jdt.core.compiler.problem.unnecessaryElse\" value=\"ignore\"/>\n" + 
(-)src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java (-1 / +13 lines)
Lines 8-14 Link Here
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Benjamin Muskalla - Contribution for bug 239066
10
 *     Benjamin Muskalla - Contribution for bug 239066
11
 *     Stephan Herrmann  - Contribution for bug 236385
11
 *     Stephan Herrmann  - Contributions for 
12
 *     							bug 236385: [compiler] Warn for potential programming problem if an object is created but not used
13
 *     							bug 349326 - [1.7] new warning for missing try-with-resources
12
 *******************************************************************************/
14
 *******************************************************************************/
13
package org.eclipse.jdt.core.tests.compiler.regression;
15
package org.eclipse.jdt.core.tests.compiler.regression;
14
16
Lines 440-445 Link Here
440
		expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", DEPRECATED);
442
		expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", DEPRECATED);
441
		expectedProblemAttributes.put("ExceptionTypeNotFound", DEPRECATED);
443
		expectedProblemAttributes.put("ExceptionTypeNotFound", DEPRECATED);
442
		expectedProblemAttributes.put("ExceptionTypeNotVisible", DEPRECATED);
444
		expectedProblemAttributes.put("ExceptionTypeNotVisible", DEPRECATED);
445
		expectedProblemAttributes.put("ExplicitlyClosedAutoCloseable", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
443
		expectedProblemAttributes.put("ExpressionShouldBeAVariable", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX));
446
		expectedProblemAttributes.put("ExpressionShouldBeAVariable", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX));
444
		expectedProblemAttributes.put("ExternalProblemFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
447
		expectedProblemAttributes.put("ExternalProblemFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
445
		expectedProblemAttributes.put("ExternalProblemNotFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
448
		expectedProblemAttributes.put("ExternalProblemNotFixable", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
Lines 765-770 Link Here
765
		expectedProblemAttributes.put("PolymorphicMethodNotBelow17", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
768
		expectedProblemAttributes.put("PolymorphicMethodNotBelow17", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
766
		expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
769
		expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
767
		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));
768
		expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
773
		expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
769
		expectedProblemAttributes.put("PublicClassMustMatchFileName", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
774
		expectedProblemAttributes.put("PublicClassMustMatchFileName", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
770
		expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
775
		expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
Lines 828-833 Link Here
828
		expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
833
		expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
829
		expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(CategorizedProblem.CAT_NAME_SHADOWING_CONFLICT));
834
		expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(CategorizedProblem.CAT_NAME_SHADOWING_CONFLICT));
830
		expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
835
		expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(CategorizedProblem.CAT_CODE_STYLE));
836
		expectedProblemAttributes.put("UnclosedCloseable", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
837
		expectedProblemAttributes.put("UnclosedCloseableAtExit", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
831
		expectedProblemAttributes.put("UndefinedAnnotationMember", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
838
		expectedProblemAttributes.put("UndefinedAnnotationMember", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
832
		expectedProblemAttributes.put("UndefinedConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
839
		expectedProblemAttributes.put("UndefinedConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
833
		expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
840
		expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
Lines 1108-1113 Link Here
1108
		expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", SKIP);
1115
		expectedProblemAttributes.put("ExceptionTypeInternalNameProvided", SKIP);
1109
		expectedProblemAttributes.put("ExceptionTypeNotFound", SKIP);
1116
		expectedProblemAttributes.put("ExceptionTypeNotFound", SKIP);
1110
		expectedProblemAttributes.put("ExceptionTypeNotVisible", SKIP);
1117
		expectedProblemAttributes.put("ExceptionTypeNotVisible", SKIP);
1118
		expectedProblemAttributes.put("ExplicitlyClosedAutoCloseable", new ProblemAttributes(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE));
1111
		expectedProblemAttributes.put("ExpressionShouldBeAVariable", SKIP);
1119
		expectedProblemAttributes.put("ExpressionShouldBeAVariable", SKIP);
1112
		expectedProblemAttributes.put("ExternalProblemFixable", SKIP);
1120
		expectedProblemAttributes.put("ExternalProblemFixable", SKIP);
1113
		expectedProblemAttributes.put("ExternalProblemNotFixable", SKIP);
1121
		expectedProblemAttributes.put("ExternalProblemNotFixable", SKIP);
Lines 1433-1438 Link Here
1433
		expectedProblemAttributes.put("PolymorphicMethodNotBelow17", SKIP);
1441
		expectedProblemAttributes.put("PolymorphicMethodNotBelow17", SKIP);
1434
		expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(JavaCore.COMPILER_PB_POSSIBLE_ACCIDENTAL_BOOLEAN_ASSIGNMENT));
1442
		expectedProblemAttributes.put("PossibleAccidentalBooleanAssignment", new ProblemAttributes(JavaCore.COMPILER_PB_POSSIBLE_ACCIDENTAL_BOOLEAN_ASSIGNMENT));
1435
		expectedProblemAttributes.put("PotentialHeapPollutionFromVararg", new ProblemAttributes(JavaCore.COMPILER_PB_UNCHECKED_TYPE_OPERATION));
1443
		expectedProblemAttributes.put("PotentialHeapPollutionFromVararg", new ProblemAttributes(JavaCore.COMPILER_PB_UNCHECKED_TYPE_OPERATION));
1444
		expectedProblemAttributes.put("PotentiallyUnclosedCloseable", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE));
1445
		expectedProblemAttributes.put("PotentiallyUnclosedCloseableAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE));
1436
		expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE));
1446
		expectedProblemAttributes.put("PotentialNullLocalVariableReference", new ProblemAttributes(JavaCore.COMPILER_PB_POTENTIAL_NULL_REFERENCE));
1437
		expectedProblemAttributes.put("PublicClassMustMatchFileName", SKIP);
1447
		expectedProblemAttributes.put("PublicClassMustMatchFileName", SKIP);
1438
		expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", SKIP);
1448
		expectedProblemAttributes.put("RawMemberTypeCannotBeParameterized", SKIP);
Lines 1496-1501 Link Here
1496
		expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION));
1506
		expectedProblemAttributes.put("TypeMissingDeprecatedAnnotation", new ProblemAttributes(JavaCore.COMPILER_PB_MISSING_DEPRECATED_ANNOTATION));
1497
		expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(JavaCore.COMPILER_PB_TYPE_PARAMETER_HIDING));
1507
		expectedProblemAttributes.put("TypeParameterHidingType", new ProblemAttributes(JavaCore.COMPILER_PB_TYPE_PARAMETER_HIDING));
1498
		expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(JavaCore.COMPILER_PB_AUTOBOXING));
1508
		expectedProblemAttributes.put("UnboxingConversion", new ProblemAttributes(JavaCore.COMPILER_PB_AUTOBOXING));
1509
		expectedProblemAttributes.put("UnclosedCloseable", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE));
1510
		expectedProblemAttributes.put("UnclosedCloseableAtExit", new ProblemAttributes(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE));
1499
		expectedProblemAttributes.put("UndefinedAnnotationMember", SKIP);
1511
		expectedProblemAttributes.put("UndefinedAnnotationMember", SKIP);
1500
		expectedProblemAttributes.put("UndefinedConstructor", SKIP);
1512
		expectedProblemAttributes.put("UndefinedConstructor", SKIP);
1501
		expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", SKIP);
1513
		expectedProblemAttributes.put("UndefinedConstructorInDefaultConstructor", SKIP);
(-)src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java (-4 / +833 lines)
Lines 7-17 Link Here
7
 * 
7
 * 
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann - Contribution for bug 349326 - [1.7] new warning for missing try-with-resources
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.core.tests.compiler.regression;
12
package org.eclipse.jdt.core.tests.compiler.regression;
12
13
13
import java.util.Map;
14
import java.util.Map;
14
15
16
import org.eclipse.jdt.core.JavaCore;
15
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
17
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
16
18
17
import junit.framework.Test;
19
import junit.framework.Test;
Lines 485-496 Link Here
485
		"	               ^^\n" + 
487
		"	               ^^\n" + 
486
		"Dead code\n" + 
488
		"Dead code\n" + 
487
		"----------\n" + 
489
		"----------\n" + 
488
		"3. ERROR in X.java (at line 5)\n" + 
490
		"3. WARNING in X.java (at line 5)\n" + 
491
		"	Y why = new Y();\n" + 
492
		"	  ^^^\n" + 
493
		"Leaking resource 'why': is never closed\n" + 
494
		"----------\n" + 
495
		"4. ERROR in X.java (at line 5)\n" + 
489
		"	Y why = new Y();\n" + 
496
		"	Y why = new Y();\n" + 
490
		"	        ^^^^^^^\n" + 
497
		"	        ^^^^^^^\n" + 
491
		"Unhandled exception type WeirdException\n" + 
498
		"Unhandled exception type WeirdException\n" + 
492
		"----------\n" + 
499
		"----------\n" + 
493
		"4. WARNING in X.java (at line 22)\n" + 
500
		"5. WARNING in X.java (at line 22)\n" + 
494
		"	class WeirdException extends Throwable {}\n" + 
501
		"	class WeirdException extends Throwable {}\n" + 
495
		"	      ^^^^^^^^^^^^^^\n" + 
502
		"	      ^^^^^^^^^^^^^^\n" + 
496
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
503
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
Lines 558-569 Link Here
558
		"	               ^^\n" + 
565
		"	               ^^\n" + 
559
		"Dead code\n" + 
566
		"Dead code\n" + 
560
		"----------\n" + 
567
		"----------\n" + 
561
		"3. ERROR in X.java (at line 5)\n" + 
568
		"3. WARNING in X.java (at line 5)\n" + 
569
		"	Y why = new Y();\n" + 
570
		"	  ^^^\n" + 
571
		"Leaking resource 'why': is never closed\n" + 
572
		"----------\n" + 
573
		"4. ERROR in X.java (at line 5)\n" + 
562
		"	Y why = new Y();\n" + 
574
		"	Y why = new Y();\n" + 
563
		"	        ^^^^^^^\n" + 
575
		"	        ^^^^^^^\n" + 
564
		"Unhandled exception type WeirdException\n" + 
576
		"Unhandled exception type WeirdException\n" + 
565
		"----------\n" + 
577
		"----------\n" + 
566
		"4. WARNING in X.java (at line 20)\n" + 
578
		"5. WARNING in X.java (at line 20)\n" + 
567
		"	class WeirdException extends Throwable {}\n" + 
579
		"	class WeirdException extends Throwable {}\n" + 
568
		"	      ^^^^^^^^^^^^^^\n" + 
580
		"	      ^^^^^^^^^^^^^^\n" + 
569
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
581
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
Lines 3380-3385 Link Here
3380
		},
3392
		},
3381
		"Done");
3393
		"Done");
3382
}
3394
}
3395
// Bug 349326 - [1.7] new warning for missing try-with-resources
3396
// a method uses an AutoCloseable without ever closing it.
3397
public void test056() {
3398
	Map options = getCompilerOptions();
3399
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3400
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3401
	this.runNegativeTest(
3402
		new String[] {
3403
			"X.java",
3404
			"import java.io.File;\n" +
3405
			"import java.io.FileReader;\n" +
3406
			"import java.io.IOException;\n" +
3407
			"public class X {\n" +
3408
			"    void foo() throws IOException {\n" +
3409
			"        File file = new File(\"somefile\");\n" +
3410
			"        FileReader fileReader = new FileReader(file);\n" +
3411
// not invoking any methods on FileReader, try to avoid necessary call to superclass() in the compiler
3412
//			"        char[] in = new char[50];\n" +
3413
//			"        fileReader.read(in);\n" +
3414
			"    }\n" +
3415
			"    public static void main(String[] args) throws IOException {\n" +
3416
			"        new X().foo();\n" +
3417
			"    }\n" +
3418
			"}\n"
3419
		},
3420
		"----------\n" + 
3421
		"1. ERROR in X.java (at line 7)\n" + 
3422
		"	FileReader fileReader = new FileReader(file);\n" + 
3423
		"	           ^^^^^^^^^^\n" + 
3424
		"Leaking resource 'fileReader': is never closed\n" +
3425
		"----------\n",
3426
		null,
3427
		true,
3428
		options);
3429
}
3430
// Bug 349326 - [1.7] new warning for missing try-with-resources
3431
// a method uses an AutoCloseable and closes it but exception may occur earlier.
3432
public void test056a() {
3433
	Map options = getCompilerOptions();
3434
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3435
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
3436
	this.runNegativeTest(
3437
		new String[] {
3438
			"X.java",
3439
			"import java.io.File;\n" +
3440
			"import java.io.FileReader;\n" +
3441
			"import java.io.IOException;\n" +
3442
			"public class X {\n" +
3443
			"    void foo() throws IOException {\n" +
3444
			"        File file = new File(\"somefile\");\n" +
3445
			"        FileReader fileReader = new FileReader(file);\n" +
3446
			"        char[] in = new char[50];\n" +
3447
			"        fileReader.read(in);\n" +
3448
			"		 fileReader.close();\n" +
3449
			"    }\n" +
3450
			"    public static void main(String[] args) {\n" +
3451
			"        try {\n" +
3452
			"            new X().foo();\n" +
3453
			"        } catch (IOException ioex) {\n" +
3454
			"            System.out.println(\"caught\");\n" +
3455
			"        }\n" +
3456
			"    }\n" +
3457
			"}\n"
3458
		},
3459
		"----------\n" + 
3460
		"1. ERROR in X.java (at line 7)\n" + 
3461
		"	FileReader fileReader = new FileReader(file);\n" + 
3462
		"	           ^^^^^^^^^^\n" + 
3463
		"Potentially leaking resource 'fileReader': is not closed on all paths\n" + 
3464
		"----------\n",
3465
		null,
3466
		true,
3467
		options);
3468
}
3469
// Bug 349326 - [1.7] new warning for missing try-with-resources
3470
// a method uses an AutoCloseable and closes it properly in a finally block
3471
public void test056b() {
3472
	Map options = getCompilerOptions();
3473
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3474
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
3475
	this.runConformTest(
3476
		new String[] {
3477
			"X.java",
3478
			"import java.io.File;\n" +
3479
			"import java.io.FileReader;\n" +
3480
			"import java.io.IOException;\n" +
3481
			"public class X {\n" +
3482
			"    void foo() throws IOException {\n" +
3483
			"        File file = new File(\"somefile\");\n" +
3484
			"        FileReader fileReader = new FileReader(file);\n" +
3485
			"        try {\n" +
3486
			"            char[] in = new char[50];\n" +
3487
			"            fileReader.read(in);\n" +
3488
			"        } finally {\n" +
3489
			"		     fileReader.close();\n" +
3490
			"        }\n" +
3491
			"    }\n" +
3492
			"    public static void main(String[] args) {\n" +
3493
			"        try {\n" +
3494
			"            new X().foo();\n" +
3495
			"        } catch (IOException ioex) {\n" +
3496
			"            System.out.println(\"caught\");\n" +
3497
			"        }\n" +
3498
			"    }\n" +
3499
			"}\n"
3500
		},
3501
		"caught", /*output*/
3502
		null/*classLibs*/,
3503
		true/*shouldFlush*/,
3504
		null/*vmargs*/,
3505
		options,
3506
		null/*requestor*/);
3507
}
3508
// Bug 349326 - [1.7] new warning for missing try-with-resources
3509
// a method uses an AutoCloseable properly within try-with-resources.
3510
public void test056c() {
3511
	Map options = getCompilerOptions();
3512
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3513
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3514
	this.runConformTest(
3515
		new String[] {
3516
			"X.java",
3517
			"import java.io.File;\n" +
3518
			"import java.io.FileReader;\n" +
3519
			"import java.io.IOException;\n" +
3520
			"public class X {\n" +
3521
			"    void foo() throws IOException {\n" +
3522
			"        File file = new File(\"somefile\");\n" +
3523
			"        try (FileReader fileReader = new FileReader(file)) {\n" +
3524
			"            char[] in = new char[50];\n" +
3525
			"            fileReader.read(in);\n" +
3526
			"		 }\n" +
3527
			"    }\n" +
3528
			"    public static void main(String[] args) {\n" +
3529
			"        try {\n" +
3530
			"            new X().foo();\n" +
3531
			"        } catch (IOException ioex) {\n" +
3532
			"            System.out.println(\"caught\");\n" +
3533
			"        }\n" +
3534
			"    }\n" +
3535
			"}\n"
3536
		},
3537
		"caught", /*output*/
3538
		null/*classLibs*/,
3539
		true/*shouldFlush*/,
3540
		null/*vmargs*/,
3541
		options,
3542
		null/*requestor*/);
3543
}
3544
// Bug 349326 - [1.7] new warning for missing try-with-resources
3545
// a method uses two AutoCloseables (testing independent analysis)
3546
// - one closeable may be unclosed at a conditional return
3547
// - the other is only conditionally closed
3548
public void test056d() {
3549
	Map options = getCompilerOptions();
3550
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3551
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3552
	this.runNegativeTest(
3553
		new String[] {
3554
			"X.java",
3555
			"import java.io.File;\n" +
3556
			"import java.io.FileReader;\n" +
3557
			"import java.io.IOException;\n" +
3558
			"public class X {\n" +
3559
			"    void foo(boolean flag1, boolean flag2) throws IOException {\n" +
3560
			"        File file = new File(\"somefile\");\n" +
3561
			"        char[] in = new char[50];\n" +
3562
			"        FileReader fileReader1 = new FileReader(file);\n" +
3563
			"        fileReader1.read(in);\n" +
3564
			"        FileReader fileReader2 = new FileReader(file);\n" +
3565
			"        fileReader2.read(in);\n" +
3566
			"        if (flag1) {\n" +
3567
			"            fileReader2.close();\n" +
3568
			"            return;\n" +
3569
			"        } else if (flag2) {\n" +
3570
			"            fileReader2.close();\n" +
3571
			"        }\n" +
3572
			"        fileReader1.close();\n" +
3573
			"    }\n" +
3574
			"    public static void main(String[] args) throws IOException {\n" +
3575
			"        new X().foo(false, true);\n" +
3576
			"    }\n" +
3577
			"}\n"
3578
		},
3579
		"----------\n" + 
3580
		"1. WARNING in X.java (at line 10)\n" + 
3581
		"	FileReader fileReader2 = new FileReader(file);\n" + 
3582
		"	           ^^^^^^^^^^^\n" + 
3583
		"Potentially leaking resource 'fileReader2': is not closed on all paths\n" +
3584
		"----------\n" + 
3585
		"2. ERROR in X.java (at line 14)\n" + 
3586
		"	return;\n" + 
3587
		"	^^^^^^^\n" + 
3588
		"Leaking resource \'fileReader1\': is not closed at this location\n" + 
3589
		"----------\n",
3590
		null,
3591
		true,
3592
		options);
3593
}
3594
//Bug 349326 - [1.7] new warning for missing try-with-resources
3595
//a method uses two AutoCloseables (testing independent analysis)
3596
//- one closeable may be unclosed at a conditional return
3597
//- the other is only conditionally closed
3598
public void test056d_suppress() {
3599
	Map options = getCompilerOptions();
3600
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3601
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3602
	options.put(CompilerOptions.OPTION_SuppressOptionalErrors, CompilerOptions.ENABLED);
3603
	this.runNegativeTest(
3604
		new String[] {
3605
			"X.java",
3606
			"import java.io.File;\n" +
3607
			"import java.io.FileReader;\n" +
3608
			"import java.io.IOException;\n" +
3609
			"public class X {\n" +
3610
			"    void foo(boolean flag1, boolean flag2) throws IOException {\n" +
3611
			"        @SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" +
3612
			"        char[] in = new char[50];\n" +
3613
			"        FileReader fileReader1 = new FileReader(file);\n" +
3614
			"        fileReader1.read(in);\n" +
3615
			"        @SuppressWarnings(\"resource\") FileReader fileReader2 = new FileReader(file); // useful suppress\n" +
3616
			"        fileReader2.read(in);\n" +
3617
			"        if (flag1) {\n" +
3618
			"            fileReader2.close();\n" +
3619
			"            return; // not suppressed\n" +
3620
			"        } else if (flag2) {\n" +
3621
			"            fileReader2.close();\n" +
3622
			"        }\n" +
3623
			"        fileReader1.close();\n" +
3624
			"    }\n" +
3625
			"    @SuppressWarnings(\"resource\") // useful suppress\n" +
3626
			"    void bar() throws IOException {\n" +
3627
			"        File file = new File(\"somefile\");\n" +
3628
			"        FileReader fileReader = new FileReader(file);\n" +
3629
			"        char[] in = new char[50];\n" +
3630
			"        fileReader.read(in);\n" +
3631
			"    }\n" +
3632
			"    public static void main(String[] args) throws IOException {\n" +
3633
			"        new X().foo(false, true);\n" +
3634
			"    }\n" +
3635
			"}\n"
3636
		},
3637
		"----------\n" + 
3638
		"1. WARNING in X.java (at line 6)\n" + 
3639
		"	@SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" + 
3640
		"	                  ^^^^^^^^^^\n" + 
3641
		"Unnecessary @SuppressWarnings(\"resource\")\n" + 
3642
		"----------\n" + 
3643
		"2. ERROR in X.java (at line 14)\n" + 
3644
		"	return; // not suppressed\n" + 
3645
		"	^^^^^^^\n" + 
3646
		"Leaking resource \'fileReader1\': is not closed at this location\n" + 
3647
		"----------\n",
3648
		null,
3649
		true,
3650
		options);
3651
}
3652
// Bug 349326 - [1.7] new warning for missing try-with-resources
3653
// one method returns an AutoCleasble, a second method uses this object without ever closing it.
3654
public void test056e() {
3655
	Map options = getCompilerOptions();
3656
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3657
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3658
	this.runNegativeTest(
3659
		new String[] {
3660
			"X.java",
3661
			"import java.io.File;\n" +
3662
			"import java.io.FileReader;\n" +
3663
			"import java.io.IOException;\n" +
3664
			"public class X {\n" +
3665
			"    FileReader getReader(String filename) throws IOException {\n" +
3666
			"        File file = new File(\"somefile\");\n" +
3667
			"        FileReader fileReader = new FileReader(file);\n" +
3668
			"        return fileReader;\n" + 		// don't complain here, pass responsibility to caller
3669
			"    }\n" +
3670
			"    void foo() throws IOException {\n" +
3671
			"        FileReader reader = getReader(\"somefile\");\n" +
3672
			"        char[] in = new char[50];\n" +
3673
			"        reader.read(in);\n" +
3674
			"    }\n" +
3675
			"    public static void main(String[] args) throws IOException {\n" +
3676
			"        new X().foo();\n" +
3677
			"    }\n" +
3678
			"}\n"
3679
		},
3680
		"----------\n" + 
3681
		"1. ERROR in X.java (at line 11)\n" + 
3682
		"	FileReader reader = getReader(\"somefile\");\n" + 
3683
		"	           ^^^^^^\n" + 
3684
		"Leaking resource 'reader': is never closed\n" + 
3685
		"----------\n",
3686
		null,
3687
		true,
3688
		options);
3689
}
3690
// Bug 349326 - [1.7] new warning for missing try-with-resources
3691
// a method explicitly closes its AutoCloseable rather than using t-w-r
3692
public void test056f() {
3693
	Map options = getCompilerOptions();
3694
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3695
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3696
	options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR);
3697
	this.runNegativeTest(
3698
		new String[] {
3699
			"X.java",
3700
			"import java.io.File;\n" +
3701
			"import java.io.FileReader;\n" +
3702
			"import java.io.IOException;\n" +
3703
			"public class X {\n" +
3704
			"    void foo() throws IOException {\n" +
3705
			"        File file = new File(\"somefile\");\n" +
3706
			"        FileReader fileReader = null;\n" +
3707
			"        try {\n" +
3708
			"            fileReader = new FileReader(file);\n" +
3709
			"            char[] in = new char[50];\n" +
3710
			"            fileReader.read(in);\n" +
3711
			"        } finally {\n" +
3712
			"            fileReader.close();\n" +
3713
			"        }\n" +
3714
			"    }\n" +
3715
			"    public static void main(String[] args) throws IOException {\n" +
3716
			"        new X().foo();\n" +
3717
			"    }\n" +
3718
			"}\n"
3719
		},
3720
		"----------\n" + 
3721
		"1. ERROR in X.java (at line 7)\n" + 
3722
		"	FileReader fileReader = null;\n" + 
3723
		"	           ^^^^^^^^^^\n" + 
3724
		"Resource \'fileReader\' should be managed by try-with-resource\n" + 
3725
		"----------\n",
3726
		null,
3727
		true,
3728
		options);
3729
}
3730
// Bug 349326 - [1.7] new warning for missing try-with-resources
3731
// an AutoCloseable local is re-assigned
3732
public void test056g() {
3733
	Map options = getCompilerOptions();
3734
	options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR);
3735
	options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING);
3736
	options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE);
3737
	this.runNegativeTest(
3738
		new String[] {
3739
			"X.java",
3740
			"import java.io.File;\n" +
3741
			"import java.io.FileReader;\n" +
3742
			"import java.io.IOException;\n" +
3743
			"public class X {\n" +
3744
			"    void foo() throws IOException {\n" +
3745
			"        File file = new File(\"somefile\");\n" +
3746
			"        FileReader fileReader = new FileReader(file);\n" +
3747
			"        char[] in = new char[50];\n" +
3748
			"        fileReader.read(in);\n" +
3749
			"        fileReader = new FileReader(file);\n" +
3750
			"        fileReader.read(in);\n" +
3751
			"        fileReader.close();\n" +
3752
			"    }\n" +
3753
			"    public static void main(String[] args) throws IOException {\n" +
3754
			"        new X().foo();\n" +
3755
			"    }\n" +
3756
			"}\n"
3757
		},
3758
		"----------\n" + 
3759
		"1. ERROR in X.java (at line 10)\n" + 
3760
		"	fileReader = new FileReader(file);\n" + 
3761
		"	^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
3762
		"Leaking resource \'fileReader\': is not closed at this location\n" + 
3763
		"----------\n",
3764
		null,
3765
		true,
3766
		options);
3767
}
3768
// Bug 349326 - [1.7] new warning for missing try-with-resources
3769
// two AutoCloseables at different nesting levels (anonymous local type)
3770
public void test056h() {
3771
	Map options = getCompilerOptions();
3772
	options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR);
3773
	options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING);
3774
	options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE);
3775
	this.runNegativeTest(
3776
		new String[] {
3777
			"X.java",
3778
			"import java.io.File;\n" +
3779
			"import java.io.FileReader;\n" +
3780
			"import java.io.IOException;\n" +
3781
			"public class X {\n" +
3782
			"    void foo() throws IOException {\n" +
3783
			"        final File file = new File(\"somefile\");\n" +
3784
			"        final FileReader fileReader = new FileReader(file);\n" +
3785
			"        char[] in = new char[50];\n" +
3786
			"        fileReader.read(in);\n" +
3787
			"        new Runnable() {\n public void run() {\n" +
3788
			"            try {\n" +
3789
			"                fileReader.close();\n" +
3790
			"                FileReader localReader = new FileReader(file);\n" +
3791
			"            } catch (IOException ex) { /* nop */ }\n" +
3792
			"        }}.run();\n" +
3793
			"    }\n" +
3794
			"    public static void main(String[] args) throws IOException {\n" +
3795
			"        new X().foo();\n" +
3796
			"    }\n" +
3797
			"}\n"
3798
		},
3799
		"----------\n" + 
3800
		"1. WARNING in X.java (at line 7)\n" + 
3801
		"	final FileReader fileReader = new FileReader(file);\n" + 
3802
		"	                 ^^^^^^^^^^\n" + 
3803
		"Potentially leaking resource 'fileReader': is not closed on all paths\n" + 
3804
		"----------\n" + 
3805
		"2. ERROR in X.java (at line 14)\n" + 
3806
		"	FileReader localReader = new FileReader(file);\n" + 
3807
		"	           ^^^^^^^^^^^\n" + 
3808
		"Leaking resource 'localReader': is never closed\n" + 
3809
		"----------\n",
3810
		null,
3811
		true,
3812
		options);
3813
}
3814
// Bug 349326 - [1.7] new warning for missing try-with-resources
3815
// three AutoCloseables in different blocks of the same method
3816
public void test056i() {
3817
	Map options = getCompilerOptions();
3818
	options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR);
3819
	options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING);
3820
	options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE);
3821
	this.runNegativeTest(
3822
		new String[] {
3823
			"X.java",
3824
			"import java.io.File;\n" +
3825
			"import java.io.FileReader;\n" +
3826
			"import java.io.IOException;\n" +
3827
			"public class X {\n" +
3828
			"    void foo(boolean f1, boolean f2) throws IOException {\n" +
3829
			"        File file = new File(\"somefile\");\n" +
3830
			"        if (f1) {\n" +
3831
			"            FileReader fileReader = new FileReader(file); // err: not closed\n" +
3832
			"            char[] in = new char[50];\n" +
3833
			"            fileReader.read(in);\n" +
3834
			"            while (true) {\n" +
3835
			"                 FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" +
3836
			"                 loopReader.close();" +
3837
			"                 break;\n" +
3838
			"            }\n" +
3839
			"        } else {\n" +
3840
			"            FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" +
3841
			"            if (f2)\n" +
3842
			"                fileReader.close();\n" +
3843
			"        }\n" +
3844
			"    }\n" +
3845
			"    public static void main(String[] args) throws IOException {\n" +
3846
			"        new X().foo(true, true);\n" +
3847
			"    }\n" +
3848
			"}\n"
3849
		},
3850
		"----------\n" + 
3851
		"1. ERROR in X.java (at line 8)\n" + 
3852
		"	FileReader fileReader = new FileReader(file); // err: not closed\n" + 
3853
		"	           ^^^^^^^^^^\n" + 
3854
		"Leaking resource 'fileReader': is never closed\n" + 
3855
		"----------\n" + 
3856
		"2. WARNING in X.java (at line 16)\n" + 
3857
		"	FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + 
3858
		"	           ^^^^^^^^^^\n" + 
3859
		"Potentially leaking resource 'fileReader': is not closed on all paths\n" + 
3860
		"----------\n",
3861
		null,
3862
		true,
3863
		options);
3864
}
3865
// Bug 349326 - [1.7] new warning for missing try-with-resources
3866
// a method uses an AutoCloseable without closing it locally but passing as arg to another method
3867
public void test056j() {
3868
	Map options = getCompilerOptions();
3869
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3870
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
3871
	this.runNegativeTest(
3872
		new String[] {
3873
			"X.java",
3874
			"import java.io.File;\n" +
3875
			"import java.io.FileReader;\n" +
3876
			"import java.io.IOException;\n" +
3877
			"public class X {\n" +
3878
			"    void foo() throws IOException {\n" +
3879
			"        File file = new File(\"somefile\");\n" +
3880
			"        FileReader fileReader = new FileReader(file);\n" +
3881
			"        read(fileReader);\n" +
3882
			"    }\n" +
3883
			"    void read(FileReader reader) { }\n" +
3884
			"    public static void main(String[] args) throws IOException {\n" +
3885
			"        new X().foo();\n" +
3886
			"    }\n" +
3887
			"}\n"
3888
		},
3889
		"----------\n" + 
3890
		"1. ERROR in X.java (at line 7)\n" + 
3891
		"	FileReader fileReader = new FileReader(file);\n" + 
3892
		"	           ^^^^^^^^^^\n" + 
3893
		"Potentially leaking resource 'fileReader': is not closed on all paths\n" + 
3894
		"----------\n",
3895
		null,
3896
		true,
3897
		options);
3898
}
3899
// Bug 349326 - [1.7] new warning for missing try-with-resources
3900
// many locals, some are AutoCloseable.
3901
// Unfortunately analysis cannot respect how exception exits may affect ra3 and rb3,
3902
// doing so would create false positives.
3903
public void test056k() {
3904
	Map options = getCompilerOptions();
3905
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3906
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
3907
	options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR);
3908
	this.runNegativeTest(
3909
		new String[] {
3910
			"X.java",
3911
			"import java.io.File;\n" +
3912
			"import java.io.FileReader;\n" +
3913
			"import java.io.IOException;\n" +
3914
			"public class X {\n" +
3915
			"    void foo() throws IOException {\n" +
3916
			"        int i01, i02, i03, i04, i05, i06, i07, i08, i09,\n" +
3917
			"            i11, i12, i13, i14, i15, i16, i17, i18, i19,\n" +
3918
			"            i21, i22, i23, i24, i25, i26, i27, i28, i29,\n" +
3919
			"            i31, i32, i33, i34, i35, i36, i37, i38, i39,\n" +
3920
			"            i41, i42, i43, i44, i45, i46, i47, i48, i49;\n" +
3921
			"        File file = new File(\"somefile\");\n" +
3922
			"        FileReader ra1 = null, ra2 = null;\n" +
3923
			"        try {\n" +
3924
			"            ra1 = new FileReader(file);\n" +
3925
			"            ra2 = new FileReader(file);\n" +
3926
			"            FileReader ra3 = new FileReader(file);\n" +
3927
			"            char[] in = new char[50];\n" +
3928
			"            ra1.read(in);\n" +
3929
			"            ra2.read(in);\n" +
3930
			"            ra3.close();\n" +
3931
			"        } finally {\n" +
3932
			"            ra1.close();\n" +
3933
			"        }\n" +
3934
			"        int i51, i52, i53, i54, i55, i56, i57, i58, i59, i60;\n" + // beyond this point locals are analyzed using extraBits
3935
			"        FileReader rb1 = null, rb2 = null;\n" +
3936
			"        try {\n" +
3937
			"            rb1 = new FileReader(file);\n" +
3938
			"            rb2 = new FileReader(file);\n" +
3939
			"            FileReader rb3 = new FileReader(file);\n" +
3940
			"            char[] in = new char[50];\n" +
3941
			"            rb1.read(in);\n" +
3942
			"            rb2.read(in);\n" +
3943
			"            rb3.close();\n" +
3944
			"        } finally {\n" +
3945
			"            rb1.close();\n" +
3946
			"        }\n" +
3947
			"    }\n" +
3948
			"    public static void main(String[] args) throws IOException {\n" +
3949
			"        new X().foo();\n" +
3950
			"    }\n" +
3951
			"}\n"
3952
		},
3953
		"----------\n" + 
3954
		"1. ERROR in X.java (at line 12)\n" + 
3955
		"	FileReader ra1 = null, ra2 = null;\n" + 
3956
		"	           ^^^\n" + 
3957
		"Resource \'ra1\' should be managed by try-with-resource\n" + 
3958
		"----------\n" + 
3959
		"2. ERROR in X.java (at line 15)\n" + 
3960
		"	ra2 = new FileReader(file);\n" + 
3961
		"	^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
3962
		"Leaking resource 'ra2': is never closed\n" + 
3963
		"----------\n" + 
3964
		"3. ERROR in X.java (at line 16)\n" + 
3965
		"	FileReader ra3 = new FileReader(file);\n" + 
3966
		"	           ^^^\n" + 
3967
		"Resource \'ra3\' should be managed by try-with-resource\n" +
3968
		"----------\n" + 
3969
		"4. ERROR in X.java (at line 25)\n" + 
3970
		"	FileReader rb1 = null, rb2 = null;\n" + 
3971
		"	           ^^^\n" + 
3972
		"Resource \'rb1\' should be managed by try-with-resource\n" + 
3973
		"----------\n" + 
3974
		"5. ERROR in X.java (at line 28)\n" + 
3975
		"	rb2 = new FileReader(file);\n" + 
3976
		"	^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + 
3977
		"Leaking resource 'rb2': is never closed\n" + 
3978
		"----------\n" + 
3979
		"6. ERROR in X.java (at line 29)\n" + 
3980
		"	FileReader rb3 = new FileReader(file);\n" + 
3981
		"	           ^^^\n" + 
3982
		"Resource \'rb3\' should be managed by try-with-resource\n" + 
3983
		"----------\n",
3984
		null,
3985
		true,
3986
		options);
3987
}
3988
// Bug 349326 - [1.7] new warning for missing try-with-resources
3989
// various non-problems
3990
public void test056l() {
3991
	Map options = getCompilerOptions();
3992
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
3993
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
3994
	options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR);
3995
	this.runNegativeTest(
3996
		new String[] {
3997
			"X.java",
3998
			"import java.io.File;\n" +
3999
			"import java.io.FileReader;\n" +
4000
			"import java.io.IOException;\n" +
4001
			"public class X {\n" +
4002
			"    X(FileReader r0) {}\n" + // don't complain against argument
4003
			"    FileReader getReader() { return null; }\n" +
4004
			"    void foo(FileReader r1) throws IOException {\n" +
4005
			"        FileReader fileReader = getReader();\n" +
4006
			"        if (fileReader == null)\n" +
4007
			"            return;\n" + // don't complain, resource is actually null
4008
			"        FileReader r3 = getReader();\n" +
4009
			"        if (r3 == null)\n" +
4010
			"            r3 = new FileReader(new File(\"absent\"));\n" + // don't complain, previous resource is actually null
4011
			"        try {\n" +
4012
			"            char[] in = new char[50];\n" +
4013
			"            fileReader.read(in);\n" +
4014
			"            r1.read(in);\n" +
4015
			"        } finally {\n" +
4016
			"            fileReader.close();\n" +
4017
			"            r3.close();\n" +  // the effect of this close() call might be spoiled by exception in fileReader.close() above
4018
			"        }\n" +
4019
			"    }\n" +
4020
			"    public static void main(String[] args) throws IOException {\n" +
4021
			"        FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" +
4022
			"        new X(r2).foo(new FileReader(new File(\"notthere\")));\n" +
4023
			"    }\n" +
4024
			"}\n"
4025
		},
4026
		"----------\n" + 
4027
		"1. ERROR in X.java (at line 8)\n" + 
4028
		"	FileReader fileReader = getReader();\n" + 
4029
		"	           ^^^^^^^^^^\n" + 
4030
		"Resource \'fileReader\' should be managed by try-with-resource\n" + 
4031
		"----------\n" + 
4032
		"2. ERROR in X.java (at line 11)\n" + 
4033
		"	FileReader r3 = getReader();\n" + 
4034
		"	           ^^\n" + 
4035
		"Potentially leaking resource 'r3': is not closed on all paths\n" +
4036
		"----------\n" + 
4037
		"3. ERROR in X.java (at line 24)\n" + 
4038
		"	FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + 
4039
		"	           ^^\n" + 
4040
		"Potentially leaking resource 'r2': is not closed on all paths\n" + 
4041
		"----------\n",
4042
		null,
4043
		true,
4044
		options);
4045
}
4046
// Bug 349326 - [1.7] new warning for missing try-with-resources
4047
// nested try with early exit
4048
public void test056m() {
4049
	Map options = getCompilerOptions();
4050
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
4051
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
4052
	this.runConformTest(
4053
		new String[] {
4054
			"X.java",
4055
			"import java.io.File;\n" +
4056
			"import java.io.FileReader;\n" +
4057
			"import java.io.IOException;\n" +
4058
			"public class X {\n" +
4059
			"    void foo() {\n" +
4060
			"        File file = new File(\"somefile\");" +
4061
			"        try {\n" +
4062
			"            FileReader fileReader = new FileReader(file);\n" +
4063
			"            try {\n" +
4064
			"                char[] in = new char[50];\n" +
4065
			"                if (fileReader.read(in)==0)\n" +
4066
			"                    return;\n" +
4067
			"            } finally {\n" +
4068
			"		         fileReader.close();\n" +
4069
			"            }\n" +
4070
			"        } catch (IOException e) {\n" +
4071
			"            System.out.println(\"caught\");\n" +
4072
			"        }\n" +
4073
			"    }\n" +
4074
			"    public static void main(String[] args) {\n" +
4075
			"        new X().foo();\n" +
4076
			"    }\n" +
4077
			"}\n"
4078
		},
4079
		"caught", /*output*/
4080
		null/*classLibs*/,
4081
		true/*shouldFlush*/,
4082
		null/*vmargs*/,
4083
		options,
4084
		null/*requestor*/);
4085
}
4086
// Bug 349326 - [1.7] new warning for missing try-with-resources
4087
// nested try should not interfere with earlier analysis.
4088
public void test056n() {
4089
	Map options = getCompilerOptions();
4090
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
4091
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
4092
	this.runConformTest(
4093
		new String[] {
4094
			"X.java",
4095
			"import java.io.File;\n" +
4096
			"import java.io.FileReader;\n" +
4097
			"import java.io.IOException;\n" +
4098
			"import java.io.FileNotFoundException;\n" +
4099
			"public class X {\n" +
4100
			"    void foo(File someFile, char[] buf) throws IOException {\n" + 
4101
			"		FileReader fr1 = new FileReader(someFile);\n" + 
4102
			"		try {\n" + 
4103
			"			fr1.read(buf);\n" + 
4104
			"		} finally {\n" + 
4105
			"			fr1.close();\n" + 
4106
			"		}\n" + 
4107
			"		try {\n" + 
4108
			"			FileReader fr3 = new FileReader(someFile);\n" + 
4109
			"			try {\n" + 
4110
			"			} finally {\n" + 
4111
			"				fr3.close();\n" + 
4112
			"			}\n" + 
4113
			"		} catch (IOException e) {\n" + 
4114
			"		}\n" + 
4115
			"	 }\n" +
4116
			"    public static void main(String[] args) throws IOException {\n" +
4117
			"        try {\n" +
4118
			"            new X().foo(new File(\"missing\"), new char[100]);\n" +
4119
			"        } catch (FileNotFoundException e) {\n" +
4120
			"            System.out.println(\"caught\");\n" +
4121
			"        }\n" +
4122
			"    }\n" +
4123
			"}\n"
4124
		},
4125
		"caught", /*output*/
4126
		null/*classLibs*/,
4127
		true/*shouldFlush*/,
4128
		null/*vmargs*/,
4129
		options,
4130
		null/*requestor*/);
4131
}
4132
// Bug 349326 - [1.7] new warning for missing try-with-resources
4133
// if close is guarded by null check this should still be recognized as definitely closed
4134
public void test056o() {
4135
	Map options = getCompilerOptions();
4136
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
4137
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
4138
	this.runConformTest(
4139
		new String[] {
4140
			"X.java",
4141
			"import java.io.File;\n" +
4142
			"import java.io.FileReader;\n" +
4143
			"import java.io.IOException;\n" +
4144
			"import java.io.FileNotFoundException;\n" +
4145
			"public class X {\n" +
4146
			"    void foo(File someFile, char[] buf) throws IOException {\n" + 
4147
			"		FileReader fr1 = null;\n" + 
4148
			"		try {\n" +
4149
			"           fr1 = new FileReader(someFile);" + 
4150
			"			fr1.read(buf);\n" + 
4151
			"		} finally {\n" + 
4152
			"			if (fr1 != null)\n" +
4153
			"               try {\n" +
4154
			"                   fr1.close();\n" +
4155
			"               } catch (IOException e) { /*do nothing*/ }\n" + 
4156
			"		}\n" + 
4157
			"	 }\n" +
4158
			"    public static void main(String[] args) throws IOException {\n" +
4159
			"        try {\n" +
4160
			"            new X().foo(new File(\"missing\"), new char[100]);\n" +
4161
			"        } catch (FileNotFoundException e) {\n" +
4162
			"            System.out.println(\"caught\");\n" +
4163
			"        }\n" +
4164
			"    }\n" +
4165
			"}\n"
4166
		},
4167
		"caught", /*output*/
4168
		null/*classLibs*/,
4169
		true/*shouldFlush*/,
4170
		null/*vmargs*/,
4171
		options,
4172
		null/*requestor*/);
4173
}
4174
// Bug 349326 - [1.7] new warning for missing try-with-resources
4175
// a method uses an AutoCloseable without ever closing it, type from a type variable
4176
public void test056p() {
4177
	Map options = getCompilerOptions();
4178
	options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
4179
	options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING);
4180
	this.runNegativeTest(
4181
		new String[] {
4182
			"X.java",
4183
			"import java.io.File;\n" +
4184
			"import java.io.FileReader;\n" +
4185
			"import java.io.Reader;\n" +
4186
			"import java.io.IOException;\n" +
4187
			"public abstract class X <T extends Reader> {\n" +
4188
			"    void foo() throws IOException {\n" +
4189
			"        File file = new File(\"somefile\");\n" +
4190
			"        T fileReader = newReader(file);\n" +
4191
			"        char[] in = new char[50];\n" +
4192
			"        fileReader.read(in);\n" +
4193
			"    }\n" +
4194
			"    abstract T newReader(File file) throws IOException;\n" +
4195
			"    public static void main(String[] args) throws IOException {\n" +
4196
			"        new X<FileReader>() {\n" +
4197
			"            FileReader newReader(File f) throws IOException { return new FileReader(f); }\n" +
4198
			"        }.foo();\n" +
4199
			"    }\n" +
4200
			"}\n"
4201
		},
4202
		"----------\n" + 
4203
		"1. ERROR in X.java (at line 8)\n" + 
4204
		"	T fileReader = newReader(file);\n" + 
4205
		"	  ^^^^^^^^^^\n" + 
4206
		"Leaking resource 'fileReader': is never closed\n" +
4207
		"----------\n",
4208
		null,
4209
		true,
4210
		options);
4211
}
3383
public static Class testClass() {
4212
public static Class testClass() {
3384
	return TryWithResourcesStatementTest.class;
4213
	return TryWithResourcesStatementTest.class;
3385
}
4214
}

Return to bug 349326