Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 338402 | Differences between
and this patch

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ASTVisitor.java (-6 lines)
Lines 437-445 Link Here
437
	public void endVisit(TryStatement tryStatement, BlockScope scope) {
437
	public void endVisit(TryStatement tryStatement, BlockScope scope) {
438
		// do nothing by default
438
		// do nothing by default
439
	}
439
	}
440
	public void endVisit(TryStatementWithResources tryStatementWithResources, BlockScope scope) {
441
		// do nothing by default
442
	}
443
	public void endVisit(
440
	public void endVisit(
444
		TypeDeclaration localTypeDeclaration,
441
		TypeDeclaration localTypeDeclaration,
445
		BlockScope scope) {
442
		BlockScope scope) {
Lines 885-893 Link Here
885
	public boolean visit(TryStatement tryStatement, BlockScope scope) {
882
	public boolean visit(TryStatement tryStatement, BlockScope scope) {
886
		return true; // do nothing by default, keep traversing
883
		return true; // do nothing by default, keep traversing
887
	}
884
	}
888
	public boolean visit(TryStatementWithResources tryStatementWithResources, BlockScope scope) {
889
		return true; // do nothing by default, keep traversing
890
	}
891
	public boolean visit(
885
	public boolean visit(
892
		TypeDeclaration localTypeDeclaration,
886
		TypeDeclaration localTypeDeclaration,
893
		BlockScope scope) {
887
		BlockScope scope) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java (-4 / +56 lines)
Lines 28-33 Link Here
28
	static final char[] SECRET_ANY_HANDLER_NAME = " anyExceptionHandler".toCharArray(); //$NON-NLS-1$
28
	static final char[] SECRET_ANY_HANDLER_NAME = " anyExceptionHandler".toCharArray(); //$NON-NLS-1$
29
	static final char[] SECRET_RETURN_VALUE_NAME = " returnValue".toCharArray(); //$NON-NLS-1$
29
	static final char[] SECRET_RETURN_VALUE_NAME = " returnValue".toCharArray(); //$NON-NLS-1$
30
30
31
	private static LocalDeclaration [] NO_RESOURCES = new LocalDeclaration[0];
32
	public LocalDeclaration[] resources = NO_RESOURCES;
33
31
	public Block tryBlock;
34
	public Block tryBlock;
32
	public Block[] catchBlocks;
35
	public Block[] catchBlocks;
33
36
Lines 100-105 Link Here
100
		// only try blocks initialize that member - may consider creating a
103
		// only try blocks initialize that member - may consider creating a
101
		// separate class if needed
104
		// separate class if needed
102
105
106
		for (int i = 0, max = this.resources.length; i < max; i++) {
107
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
108
		}
103
		FlowInfo tryInfo;
109
		FlowInfo tryInfo;
104
		if (this.tryBlock.isEmptyBlock()) {
110
		if (this.tryBlock.isEmptyBlock()) {
105
			tryInfo = flowInfo;
111
			tryInfo = flowInfo;
Lines 209-214 Link Here
209
		// only try blocks initialize that member - may consider creating a
215
		// only try blocks initialize that member - may consider creating a
210
		// separate class if needed
216
		// separate class if needed
211
217
218
		for (int i = 0, max = this.resources.length; i < max; i++) {
219
			flowInfo = this.resources[i].analyseCode(currentScope, handlingContext, flowInfo.copy());
220
		}
212
		FlowInfo tryInfo;
221
		FlowInfo tryInfo;
213
		if (this.tryBlock.isEmptyBlock()) {
222
		if (this.tryBlock.isEmptyBlock()) {
214
			tryInfo = flowInfo;
223
			tryInfo = flowInfo;
Lines 738-744 Link Here
738
}
747
}
739
748
740
public StringBuffer printStatement(int indent, StringBuffer output) {
749
public StringBuffer printStatement(int indent, StringBuffer output) {
741
	printIndent(indent, output).append("try\n"); //$NON-NLS-1$
750
	int length = this.resources.length;
751
	printIndent(indent, output).append("try" + (length == 0 ? "\n" : " (")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
752
	for (int i = 0; i < length; i++) {
753
		this.resources[i].printAsExpression(0, output);
754
		if (i != length - 1) {
755
			output.append(";\n"); //$NON-NLS-1$
756
			printIndent(indent + 2, output);
757
		}
758
	}
759
	if (length > 0) {
760
		output.append(")\n"); //$NON-NLS-1$
761
	}
742
	this.tryBlock.printStatement(indent + 1, output);
762
	this.tryBlock.printStatement(indent + 1, output);
743
763
744
	//catches
764
	//catches
Lines 762-770 Link Here
762
	// special scope for secret locals optimization.
782
	// special scope for secret locals optimization.
763
	this.scope = new BlockScope(upperScope);
783
	this.scope = new BlockScope(upperScope);
764
784
765
	BlockScope tryScope = new BlockScope(this.scope);
766
	BlockScope finallyScope = null;
785
	BlockScope finallyScope = null;
767
786
787
	// resolve all resources and inject them into separate scopes
788
	BlockScope localScope = new BlockScope(this.scope);
789
	for (int i = 0, max = this.resources.length; i < max; i++) {
790
		this.resources[i].resolve(localScope);
791
		LocalVariableBinding localVariableBinding = this.resources[i].binding;
792
		if (localVariableBinding != null && localVariableBinding.isValidBinding()) {
793
			localVariableBinding.modifiers |= ClassFileConstants.AccFinal;
794
			localVariableBinding.tagBits |= TagBits.IsResource;
795
			TypeBinding resourceType = localVariableBinding.type;
796
			if (resourceType.isClass() || resourceType.isInterface()) {
797
				if (resourceType.findSuperTypeOriginatingFrom(TypeIds.T_JavaLangAutoCloseable, false /*AutoCloseable is not a class*/) == null && resourceType.isValidBinding()) {
798
					upperScope.problemReporter().resourceHasToBeAutoCloseable(resourceType, this.resources[i].type);
799
				}
800
			} else { 
801
				upperScope.problemReporter().resourceHasToBeAutoCloseable(resourceType, this.resources[i].type);
802
			}
803
		}
804
		localScope = new BlockScope(localScope, 1);
805
	}
806
	BlockScope tryScope = localScope;
807
768
	if (this.finallyBlock != null) {
808
	if (this.finallyBlock != null) {
769
		if (this.finallyBlock.isEmptyBlock()) {
809
		if (this.finallyBlock.isEmptyBlock()) {
770
			if ((this.finallyBlock.bits & ASTNode.UndocumentedEmptyBlock) != 0) {
810
			if ((this.finallyBlock.bits & ASTNode.UndocumentedEmptyBlock) != 0) {
Lines 809-816 Link Here
809
			}
849
			}
810
			this.finallyBlock.resolveUsing(finallyScope);
850
			this.finallyBlock.resolveUsing(finallyScope);
811
			// force the finally scope to have variable positions shifted after its try scope and catch ones
851
			// force the finally scope to have variable positions shifted after its try scope and catch ones
812
			finallyScope.shiftScopes = new BlockScope[this.catchArguments == null ? 1 : this.catchArguments.length+1];
852
			int shiftScopesLength = this.catchArguments == null ? 1 : this.catchArguments.length + 1;
813
			finallyScope.shiftScopes[0] = tryScope;
853
			shiftScopesLength += this.resources.length;
854
			finallyScope.shiftScopes = new BlockScope[shiftScopesLength];
855
			for (int i = 0, max = this.resources.length; i < max; i++) {
856
				LocalVariableBinding localVariableBinding = this.resources[i].binding;
857
				if (localVariableBinding != null && localVariableBinding.isValidBinding()) {
858
					finallyScope.shiftScopes[i] = localVariableBinding.declaringScope;
859
				}
860
			}
861
			finallyScope.shiftScopes[this.resources.length] = tryScope;
814
		}
862
		}
815
	}
863
	}
816
	this.tryBlock.resolveUsing(tryScope);
864
	this.tryBlock.resolveUsing(tryScope);
Lines 854-859 Link Here
854
}
902
}
855
public void traverse(ASTVisitor visitor, BlockScope blockScope) {
903
public void traverse(ASTVisitor visitor, BlockScope blockScope) {
856
	if (visitor.visit(this, blockScope)) {
904
	if (visitor.visit(this, blockScope)) {
905
		LocalDeclaration[] localDeclarations = this.resources;
906
		for (int i = 0, max = localDeclarations.length; i < max; i++) {
907
			localDeclarations[i].traverse(visitor, this.scope);
908
		}
857
		this.tryBlock.traverse(visitor, this.scope);
909
		this.tryBlock.traverse(visitor, this.scope);
858
		if (this.catchArguments != null) {
910
		if (this.catchArguments != null) {
859
			for (int i = 0, max = this.catchBlocks.length; i < max; i++) {
911
			for (int i = 0, max = this.catchBlocks.length; i < max; i++) {
(-)compiler/org/eclipse/jdt/internal/compiler/ast/TryStatementWithResources.java (-201 lines)
Removed Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2011 IBM Corporation 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
 * This is an implementation of an early-draft specification developed under the Java
9
 * Community Process (JCP) and is made available for testing and evaluation purposes
10
 * only. The code is not compatible with any specification of the JCP.
11
 * 
12
 * Contributors:
13
 *     IBM Corporation - initial API and implementation
14
 *******************************************************************************/
15
package org.eclipse.jdt.internal.compiler.ast;
16
17
import org.eclipse.jdt.internal.compiler.ASTVisitor;
18
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
19
import org.eclipse.jdt.internal.compiler.codegen.BranchLabel;
20
import org.eclipse.jdt.internal.compiler.impl.Constant;
21
import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
22
import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding;
23
import org.eclipse.jdt.internal.compiler.lookup.MethodBinding;
24
import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
25
import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
26
import org.eclipse.jdt.internal.compiler.lookup.TagBits;
27
import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
28
import org.eclipse.jdt.internal.compiler.lookup.TypeIds;
29
30
public class TryStatementWithResources extends TryStatement {
31
32
	private static LocalDeclaration [] NO_RESOURCES = new LocalDeclaration[0];
33
	public LocalDeclaration[] resources = NO_RESOURCES;
34
35
	public StringBuffer printStatement(int indent, StringBuffer output) {
36
		printIndent(indent, output).append("try ("); //$NON-NLS-1$
37
		int length = this.resources.length;
38
		for (int i = 0; i < length; i++) {
39
			this.resources[i].printAsExpression(0, output);
40
			if (i != length - 1) {
41
				output.append(";\n"); //$NON-NLS-1$
42
				printIndent(indent + 2, output);
43
			}
44
		}
45
		output.append(")\n"); //$NON-NLS-1$
46
		this.tryBlock.printStatement(indent + 1, output);
47
48
		// catches
49
		if (this.catchBlocks != null)
50
			for (int i = 0; i < this.catchBlocks.length; i++) {
51
				output.append('\n');
52
				printIndent(indent, output).append("catch ("); //$NON-NLS-1$
53
				this.catchArguments[i].print(0, output).append(")\n"); //$NON-NLS-1$
54
				this.catchBlocks[i].printStatement(indent + 1, output);
55
			}
56
		// finally
57
		if (this.finallyBlock != null) {
58
			output.append('\n');
59
			printIndent(indent, output).append("finally\n"); //$NON-NLS-1$
60
			this.finallyBlock.printStatement(indent + 1, output);
61
		}
62
		return output;
63
	}
64
65
	public void resolve(BlockScope upperScope) {
66
		// special scope for secret locals optimization.
67
		this.scope = new BlockScope(upperScope);
68
69
		BlockScope finallyScope = null;
70
71
		// resolve all resources and inject them into separate scopes
72
		BlockScope localScope = new BlockScope(this.scope);
73
		for (int i = 0, max = this.resources.length; i < max; i++) {
74
			this.resources[i].resolve(localScope);
75
			LocalVariableBinding localVariableBinding = this.resources[i].binding;
76
			if (localVariableBinding != null && localVariableBinding.isValidBinding()) {
77
				localVariableBinding.modifiers |= ClassFileConstants.AccFinal;
78
				localVariableBinding.tagBits |= TagBits.IsResource;
79
				TypeBinding resourceType = localVariableBinding.type;
80
				if (resourceType.isClass() || resourceType.isInterface()) {
81
					if (resourceType.findSuperTypeOriginatingFrom(TypeIds.T_JavaLangAutoCloseable, false /*AutoCloseable is not a class*/) == null && resourceType.isValidBinding()) {
82
						upperScope.problemReporter().resourceHasToBeAutoCloseable(resourceType, this.resources[i].type);
83
					}
84
				} else { 
85
					upperScope.problemReporter().resourceHasToBeAutoCloseable(resourceType, this.resources[i].type);
86
				}
87
			}
88
			localScope = new BlockScope(localScope, 1);
89
		}
90
91
		BlockScope tryScope = new BlockScope(localScope);
92
93
		if (this.finallyBlock != null) {
94
			if (this.finallyBlock.isEmptyBlock()) {
95
				if ((this.finallyBlock.bits & ASTNode.UndocumentedEmptyBlock) != 0) {
96
					this.scope.problemReporter().undocumentedEmptyBlock(this.finallyBlock.sourceStart,
97
							this.finallyBlock.sourceEnd);
98
				}
99
			} else {
100
				finallyScope = new BlockScope(this.scope, false); // don't add it yet to parent scope
101
102
				// provision for returning and forcing the finally block to run
103
				MethodScope methodScope = this.scope.methodScope();
104
105
				// the type does not matter as long as it is not a base type
106
				if (!upperScope.compilerOptions().inlineJsrBytecode) {
107
					this.returnAddressVariable = new LocalVariableBinding(TryStatement.SECRET_RETURN_ADDRESS_NAME,
108
							upperScope.getJavaLangObject(), ClassFileConstants.AccDefault, false);
109
					finallyScope.addLocalVariable(this.returnAddressVariable);
110
					this.returnAddressVariable.setConstant(Constant.NotAConstant); // not inlinable
111
				}
112
				this.subRoutineStartLabel = new BranchLabel();
113
114
				this.anyExceptionVariable = new LocalVariableBinding(TryStatement.SECRET_ANY_HANDLER_NAME,
115
						this.scope.getJavaLangThrowable(), ClassFileConstants.AccDefault, false);
116
				finallyScope.addLocalVariable(this.anyExceptionVariable);
117
				this.anyExceptionVariable.setConstant(Constant.NotAConstant); // not inlinable
118
119
				if (!methodScope.isInsideInitializer()) {
120
					MethodBinding methodBinding = ((AbstractMethodDeclaration) methodScope.referenceContext).binding;
121
					if (methodBinding != null) {
122
						TypeBinding methodReturnType = methodBinding.returnType;
123
						if (methodReturnType.id != TypeIds.T_void) {
124
							this.secretReturnValue = new LocalVariableBinding(TryStatement.SECRET_RETURN_VALUE_NAME,
125
									methodReturnType, ClassFileConstants.AccDefault, false);
126
							finallyScope.addLocalVariable(this.secretReturnValue);
127
							this.secretReturnValue.setConstant(Constant.NotAConstant); // not inlinable
128
						}
129
					}
130
				}
131
				this.finallyBlock.resolveUsing(finallyScope);
132
				// force the finally scope to have variable positions shifted after its try scope and catch ones
133
				int shiftScopesLength = this.catchArguments == null ? 1 : this.catchArguments.length + 1;
134
				shiftScopesLength += this.resources.length;
135
				finallyScope.shiftScopes = new BlockScope[shiftScopesLength];
136
				for (int i = 0, max = this.resources.length; i < max; i++) {
137
					LocalVariableBinding localVariableBinding = this.resources[i].binding;
138
					if (localVariableBinding != null && localVariableBinding.isValidBinding()) {
139
						finallyScope.shiftScopes[i] = localVariableBinding.declaringScope;
140
					}
141
				}
142
				finallyScope.shiftScopes[this.resources.length] = tryScope;
143
			}
144
		}
145
146
		this.tryBlock.resolveUsing(tryScope);
147
148
		// arguments type are checked against JavaLangThrowable in resolveForCatch(..)
149
		if (this.catchBlocks != null) {
150
			int length = this.catchArguments.length;
151
			TypeBinding[] argumentTypes = new TypeBinding[length];
152
			boolean containsDisjunctiveTypes = false;
153
			boolean catchHasError = false;
154
			for (int i = 0; i < length; i++) {
155
				BlockScope catchScope = new BlockScope(this.scope);
156
				if (finallyScope != null) {
157
					finallyScope.shiftScopes[i + 1] = catchScope;
158
				}
159
				// side effect on catchScope in resolveForCatch(..)
160
				Argument catchArgument = this.catchArguments[i];
161
				containsDisjunctiveTypes |= (catchArgument.type.bits & ASTNode.IsDisjuntive) != 0;
162
				if ((argumentTypes[i] = catchArgument.resolveForCatch(catchScope)) == null) {
163
					catchHasError = true;
164
				}
165
				this.catchBlocks[i].resolveUsing(catchScope);
166
			}
167
			if (catchHasError) {
168
				return;
169
			}
170
			this.caughtExceptionTypes = new ReferenceBinding[length];
171
			verifyDuplicationAndOrder(length, argumentTypes, containsDisjunctiveTypes);
172
		} else {
173
			this.caughtExceptionTypes = new ReferenceBinding[0];
174
		}
175
176
		if (finallyScope != null) {
177
			// add finallyScope as last subscope, so it can be shifted behind try/catch subscopes.
178
			// the shifting is necessary to achieve no overlay in between the finally scope and its
179
			// sibling in term of local variable positions.
180
			this.scope.addSubscope(finallyScope);
181
		}
182
	}
183
	public void traverse(ASTVisitor visitor, BlockScope blockScope) {
184
		if (visitor.visit(this, blockScope)) {
185
			LocalDeclaration[] localDeclarations = this.resources;
186
			for (int i = 0, max = localDeclarations.length; i < max; i++) {
187
				localDeclarations[i].traverse(visitor, this.scope);
188
			}
189
			this.tryBlock.traverse(visitor, this.scope);
190
			if (this.catchArguments != null) {
191
				for (int i = 0, max = this.catchBlocks.length; i < max; i++) {
192
					this.catchArguments[i].traverse(visitor, this.scope);
193
					this.catchBlocks[i].traverse(visitor, this.scope);
194
				}
195
			}
196
			if (this.finallyBlock != null)
197
				this.finallyBlock.traverse(visitor, this.scope);
198
		}
199
		visitor.endVisit(this, blockScope);
200
	}
201
}
(-)compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java (-1 / +1 lines)
Lines 7420-7426 Link Here
7420
	// TryStatementWithResources ::= 'try' ResourceSpecification TryBlock Catchesopt
7420
	// TryStatementWithResources ::= 'try' ResourceSpecification TryBlock Catchesopt
7421
	// TryStatementWithResources ::= 'try' ResourceSpecification TryBlock Catchesopt Finally
7421
	// TryStatementWithResources ::= 'try' ResourceSpecification TryBlock Catchesopt Finally
7422
	int length;
7422
	int length;
7423
	TryStatementWithResources tryStmt = new TryStatementWithResources();
7423
	TryStatement tryStmt = new TryStatement();
7424
	//finally
7424
	//finally
7425
	if (withFinally) {
7425
	if (withFinally) {
7426
		this.astLengthPtr--;
7426
		this.astLengthPtr--;
(-)dom/org/eclipse/jdt/core/dom/ASTConverter.java (-5 / +7 lines)
Lines 2479-2489 Link Here
2479
		if (statement instanceof org.eclipse.jdt.internal.compiler.ast.ThrowStatement) {
2479
		if (statement instanceof org.eclipse.jdt.internal.compiler.ast.ThrowStatement) {
2480
			return convert((org.eclipse.jdt.internal.compiler.ast.ThrowStatement) statement);
2480
			return convert((org.eclipse.jdt.internal.compiler.ast.ThrowStatement) statement);
2481
		}
2481
		}
2482
		if (statement instanceof org.eclipse.jdt.internal.compiler.ast.TryStatementWithResources) {
2483
			return convert((org.eclipse.jdt.internal.compiler.ast.TryStatementWithResources) statement);
2484
		}
2485
		if (statement instanceof org.eclipse.jdt.internal.compiler.ast.TryStatement) {
2482
		if (statement instanceof org.eclipse.jdt.internal.compiler.ast.TryStatement) {
2486
			return convert((org.eclipse.jdt.internal.compiler.ast.TryStatement) statement);
2483
			org.eclipse.jdt.internal.compiler.ast.TryStatement stmt = (org.eclipse.jdt.internal.compiler.ast.TryStatement) statement;
2484
			if (stmt.resources.length > 0) {
2485
				return convert(stmt, true);
2486
			} else {
2487
				return convert(stmt);
2488
			}
2487
		}
2489
		}
2488
		if (statement instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) {
2490
		if (statement instanceof org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) {
2489
			ASTNode result = convert((org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) statement);
2491
			ASTNode result = convert((org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) statement);
Lines 2654-2660 Link Here
2654
		return tryStatement;
2656
		return tryStatement;
2655
	}
2657
	}
2656
2658
2657
	public TryStatementWithResources convert(org.eclipse.jdt.internal.compiler.ast.TryStatementWithResources statement) {
2659
	public TryStatementWithResources convert(org.eclipse.jdt.internal.compiler.ast.TryStatement statement, boolean hasResources /* unused */) {
2658
		final TryStatementWithResources tryStatementWithResources = new TryStatementWithResources(this.ast);
2660
		final TryStatementWithResources tryStatementWithResources = new TryStatementWithResources(this.ast);
2659
		tryStatementWithResources.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
2661
		tryStatementWithResources.setSourceRange(statement.sourceStart, statement.sourceEnd - statement.sourceStart + 1);
2660
		LocalDeclaration[] localDeclarations = statement.resources;
2662
		LocalDeclaration[] localDeclarations = statement.resources;
(-)formatter/org/eclipse/jdt/internal/formatter/CodeFormatterVisitor.java (-49 / +14 lines)
Lines 105-111 Link Here
105
import org.eclipse.jdt.internal.compiler.ast.ThrowStatement;
105
import org.eclipse.jdt.internal.compiler.ast.ThrowStatement;
106
import org.eclipse.jdt.internal.compiler.ast.TrueLiteral;
106
import org.eclipse.jdt.internal.compiler.ast.TrueLiteral;
107
import org.eclipse.jdt.internal.compiler.ast.TryStatement;
107
import org.eclipse.jdt.internal.compiler.ast.TryStatement;
108
import org.eclipse.jdt.internal.compiler.ast.TryStatementWithResources;
109
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
108
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
110
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
109
import org.eclipse.jdt.internal.compiler.ast.TypeParameter;
111
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
110
import org.eclipse.jdt.internal.compiler.ast.TypeReference;
Lines 5424-5470 Link Here
5424
	public boolean visit(TryStatement tryStatement, BlockScope scope) {
5423
	public boolean visit(TryStatement tryStatement, BlockScope scope) {
5425
5424
5426
		this.scribe.printNextToken(TerminalTokens.TokenNametry);
5425
		this.scribe.printNextToken(TerminalTokens.TokenNametry);
5427
		tryStatement.tryBlock.traverse(this, scope);
5426
		LocalDeclaration[] resources = tryStatement.resources;
5428
		if (tryStatement.catchArguments != null) {
5429
			for (int i = 0, max = tryStatement.catchBlocks.length; i < max; i++) {
5430
				if (this.preferences.insert_new_line_before_catch_in_try_statement) {
5431
					this.scribe.printNewLine();
5432
				}
5433
				this.scribe.printNextToken(TerminalTokens.TokenNamecatch, this.preferences.insert_space_after_closing_brace_in_block);
5434
				final int line = this.scribe.line;
5435
				this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, this.preferences.insert_space_before_opening_paren_in_catch);
5436
5437
				if (this.preferences.insert_space_after_opening_paren_in_catch) {
5438
					this.scribe.space();
5439
				}
5440
5441
				tryStatement.catchArguments[i].traverse(this, scope);
5442
5443
				this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_catch);
5444
5445
				formatLeftCurlyBrace(line, this.preferences.brace_position_for_block);
5446
				tryStatement.catchBlocks[i].traverse(this, scope);
5447
			}
5448
		}
5449
		if (tryStatement.finallyBlock != null) {
5450
			if (this.preferences.insert_new_line_before_finally_in_try_statement) {
5451
				this.scribe.printNewLine();
5452
			}
5453
			this.scribe.printNextToken(TerminalTokens.TokenNamefinally, this.preferences.insert_space_after_closing_brace_in_block);
5454
			tryStatement.finallyBlock.traverse(this, scope);
5455
		}
5456
		return false;
5457
	}
5458
5459
	/**
5460
	 * @see org.eclipse.jdt.internal.compiler.ASTVisitor#visit(org.eclipse.jdt.internal.compiler.ast.TryStatement, org.eclipse.jdt.internal.compiler.lookup.BlockScope)
5461
	 */
5462
	public boolean visit(TryStatementWithResources tryStatementWithResources, BlockScope scope) {
5463
5464
		this.scribe.printNextToken(TerminalTokens.TokenNametry);
5465
		this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, true);
5466
		LocalDeclaration[] resources = tryStatementWithResources.resources;
5467
		int length = resources.length;
5427
		int length = resources.length;
5428
		if (length > 0) {
5429
			this.scribe.printNextToken(TerminalTokens.TokenNameLPAREN, true);
5430
		}
5468
		for (int i = 0; i < length; i++) {
5431
		for (int i = 0; i < length; i++) {
5469
			if (i != 0) {
5432
			if (i != 0) {
5470
				this.scribe.printNewLine();
5433
				this.scribe.printNewLine();
Lines 5480-5489 Link Here
5480
			this.scribe.unIndent();
5443
			this.scribe.unIndent();
5481
			this.scribe.unIndent();
5444
			this.scribe.unIndent();
5482
		}
5445
		}
5483
		this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN);
5446
		if (length > 0) {
5484
		tryStatementWithResources.tryBlock.traverse(this, scope);
5447
			this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN);
5485
		if (tryStatementWithResources.catchArguments != null) {
5448
		}
5486
			for (int i = 0, max = tryStatementWithResources.catchBlocks.length; i < max; i++) {
5449
		tryStatement.tryBlock.traverse(this, scope);
5450
		if (tryStatement.catchArguments != null) {
5451
			for (int i = 0, max = tryStatement.catchBlocks.length; i < max; i++) {
5487
				if (this.preferences.insert_new_line_before_catch_in_try_statement) {
5452
				if (this.preferences.insert_new_line_before_catch_in_try_statement) {
5488
					this.scribe.printNewLine();
5453
					this.scribe.printNewLine();
5489
				}
5454
				}
Lines 5495-5514 Link Here
5495
					this.scribe.space();
5460
					this.scribe.space();
5496
				}
5461
				}
5497
5462
5498
				tryStatementWithResources.catchArguments[i].traverse(this, scope);
5463
				tryStatement.catchArguments[i].traverse(this, scope);
5499
5464
5500
				this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_catch);
5465
				this.scribe.printNextToken(TerminalTokens.TokenNameRPAREN, this.preferences.insert_space_before_closing_paren_in_catch);
5501
5466
5502
				formatLeftCurlyBrace(line, this.preferences.brace_position_for_block);
5467
				formatLeftCurlyBrace(line, this.preferences.brace_position_for_block);
5503
				tryStatementWithResources.catchBlocks[i].traverse(this, scope);
5468
				tryStatement.catchBlocks[i].traverse(this, scope);
5504
			}
5469
			}
5505
		}
5470
		}
5506
		if (tryStatementWithResources.finallyBlock != null) {
5471
		if (tryStatement.finallyBlock != null) {
5507
			if (this.preferences.insert_new_line_before_finally_in_try_statement) {
5472
			if (this.preferences.insert_new_line_before_finally_in_try_statement) {
5508
				this.scribe.printNewLine();
5473
				this.scribe.printNewLine();
5509
			}
5474
			}
5510
			this.scribe.printNextToken(TerminalTokens.TokenNamefinally, this.preferences.insert_space_after_closing_brace_in_block);
5475
			this.scribe.printNextToken(TerminalTokens.TokenNamefinally, this.preferences.insert_space_after_closing_brace_in_block);
5511
			tryStatementWithResources.finallyBlock.traverse(this, scope);
5476
			tryStatement.finallyBlock.traverse(this, scope);
5512
		}
5477
		}
5513
		return false;
5478
		return false;
5514
	}
5479
	}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java (-9 / +168 lines)
Lines 376-388 Link Here
376
		"y cannot be resolved to a variable\n" + 
376
		"y cannot be resolved to a variable\n" + 
377
		"----------\n");
377
		"----------\n");
378
}
378
}
379
public void _test014() {
379
public void test014() {
380
	this.runNegativeTest(
380
	this.runNegativeTest(
381
		new String[] {
381
		new String[] {
382
			"X.java",
382
			"X.java",
383
			"public class X {\n" +
383
			"public class X {\n" +
384
			"	public static void main(String [] args) {    \n" +
384
			"	public static void main(String [] args) {    \n" +
385
			"		try (Y y = new Y();) {\n" +
385
			"		try (Y y = new Y();) {\n" +
386
			"           if (y == null) {}\n" +
387
			"           Y why = new Y();\n" +
386
			"		    System.out.println(\"Try block\");\n" +
388
			"		    System.out.println(\"Try block\");\n" +
387
			"		} finally {\n" +
389
			"		} finally {\n" +
388
			"		    System.out.println(\"Finally block\");\n" +
390
			"		    System.out.println(\"Finally block\");\n" +
Lines 402-419 Link Here
402
			"class WeirdException extends Throwable {}\n",
404
			"class WeirdException extends Throwable {}\n",
403
		},
405
		},
404
		"----------\n" + 
406
		"----------\n" + 
407
		"1. ERROR in X.java (at line 3)\n" + 
408
		"	try (Y y = new Y();) {\n" + 
409
		"	           ^^^^^^^\n" + 
410
		"Unhandled exception type WeirdException\n" + 
411
		"----------\n" + 
412
		"2. WARNING in X.java (at line 4)\n" + 
413
		"	if (y == null) {}\n" + 
414
		"	               ^^\n" + 
415
		"Dead code\n" + 
416
		"----------\n" + 
417
		"3. ERROR in X.java (at line 5)\n" + 
418
		"	Y why = new Y();\n" + 
419
		"	        ^^^^^^^\n" + 
420
		"Unhandled exception type WeirdException\n" + 
421
		"----------\n" + 
422
		"4. WARNING in X.java (at line 22)\n" + 
423
		"	class WeirdException extends Throwable {}\n" + 
424
		"	      ^^^^^^^^^^^^^^\n" + 
425
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
426
		"----------\n");
427
}
428
public void test015() {
429
	this.runNegativeTest(
430
		new String[] {
431
			"X.java",
432
			"public class X {\n" +
433
			"	public static void main(String [] args) {    \n" +
434
			"		try (Y y = new Y();) {\n" +
435
			"           if (y == null)\n {}\n" +
436
			"		}\n" +
437
			"	}\n" +
438
			"} \n" +
439
			"\n" +
440
			"class Y implements AutoCloseable {\n" +
441
			"	public void close() {\n" +
442
			"	}\n" +
443
			"}\n"
444
		},
445
		"----------\n" + 
405
		"1. WARNING in X.java (at line 5)\n" + 
446
		"1. WARNING in X.java (at line 5)\n" + 
406
		"	public void foo(int p) {\n" + 
447
		"	{}\n" + 
407
		"	                    ^\n" + 
448
		"	^^\n" + 
408
		"The parameter p is hiding another local variable defined in an enclosing type scope\n" + 
449
		"Dead code\n" + 
450
		"----------\n");
451
}
452
public void test016() {
453
	this.runNegativeTest(
454
		new String[] {
455
			"X.java",
456
			"public class X {\n" +
457
			"	public static void main(String [] args) {    \n" +
458
			"		try (Y y = new Y();) {\n" +
459
			"           if (y == null) {}\n" +
460
			"           Y why = new Y();\n" +
461
			"		    System.out.println(\"Try block\");\n" +
462
			"		}\n" +
463
			"	}\n" +
464
			"} \n" +
465
			"\n" +
466
			"class Y implements AutoCloseable {\n" +
467
			"	public Y() throws WeirdException {\n" +
468
			"		throw new WeirdException();\n" +
469
			"	}\n" +
470
			"	public void close() {\n" +
471
			"		    System.out.println(\"Closing resource\");\n" +
472
			"	}\n" +
473
			"}\n" +
474
			"\n" +
475
			"class WeirdException extends Throwable {}\n",
476
		},
477
		"----------\n" + 
478
		"1. ERROR in X.java (at line 3)\n" + 
479
		"	try (Y y = new Y();) {\n" + 
480
		"	           ^^^^^^^\n" + 
481
		"Unhandled exception type WeirdException\n" + 
482
		"----------\n" + 
483
		"2. WARNING in X.java (at line 4)\n" + 
484
		"	if (y == null) {}\n" + 
485
		"	               ^^\n" + 
486
		"Dead code\n" + 
487
		"----------\n" + 
488
		"3. ERROR in X.java (at line 5)\n" + 
489
		"	Y why = new Y();\n" + 
490
		"	        ^^^^^^^\n" + 
491
		"Unhandled exception type WeirdException\n" + 
409
		"----------\n" + 
492
		"----------\n" + 
410
		"2. WARNING in X.java (at line 8)\n" + 
493
		"4. WARNING in X.java (at line 20)\n" + 
411
		"	} catch (Exception y) {\n" + 
494
		"	class WeirdException extends Throwable {}\n" + 
495
		"	      ^^^^^^^^^^^^^^\n" + 
496
		"The serializable class WeirdException does not declare a static final serialVersionUID field of type long\n" + 
497
		"----------\n");
498
}
499
public void test017() {
500
	this.runNegativeTest(
501
		new String[] {
502
			"X.java",
503
			"public class X {\n" +
504
			"	public static void main(String [] args) {    \n" +
505
			"		try (Y y = new Y();) {\n" +
506
			"           if (y == null)\n {}\n" +
507
			"		} finally {\n" +
508
			"       }\n" +
509
			"	}\n" +
510
			"} \n" +
511
			"\n" +
512
			"class Y implements AutoCloseable {\n" +
513
			"	public void close() {\n" +
514
			"	}\n" +
515
			"}\n"
516
		},
517
		"----------\n" + 
518
		"1. WARNING in X.java (at line 5)\n" + 
519
		"	{}\n" + 
520
		"	^^\n" + 
521
		"Dead code\n" + 
522
		"----------\n");
523
}
524
public void test018() {
525
	this.runNegativeTest(
526
		new String[] {
527
			"X.java",
528
			"public class X {\n" +
529
			"	public static void main(String [] args) {    \n" +
530
			"		try () {\n" +
531
			"		} finally {\n" +
532
			"       }\n" +
533
			"	}\n" +
534
			"} \n"
535
		},
536
		"----------\n" + 
537
		"1. ERROR in X.java (at line 3)\n" + 
538
		"	try () {\n" + 
539
		"	    ^\n" + 
540
		"Syntax error on token \"(\", Resources expected after this token\n" + 
541
		"----------\n");
542
}
543
public void test019() {
544
	this.runNegativeTest(
545
		new String[] {
546
			"X.java",
547
			"public class X {\n" +
548
			"	public static void main(String [] args) {\n" +
549
			"	    try (Y y = null) {\n" +
550
			"	    } catch (Exception e) {\n" +
551
			"		System.out.println(y); \n" +
552
			"	    } finally {\n" +
553
			"		System.out.println(y); \n" +
554
			"	    }\n" +
555
			"	    System.out.println(y); \n" +
556
			"	}\n" +
557
			"}\n" +
558
			"class Y implements AutoCloseable {\n" +
559
			"	public void close() {\n" +
560
			"	}\n" +
561
			"}\n"
562
		},
563
		"----------\n" + 
564
		"1. ERROR in X.java (at line 5)\n" + 
565
		"	System.out.println(y); \n" + 
412
		"	                   ^\n" + 
566
		"	                   ^\n" + 
413
		"The parameter y is hiding another local variable defined in an enclosing type scope\n" + 
567
		"y cannot be resolved to a variable\n" + 
568
		"----------\n" + 
569
		"2. ERROR in X.java (at line 7)\n" + 
570
		"	System.out.println(y); \n" + 
571
		"	                   ^\n" + 
572
		"y cannot be resolved to a variable\n" + 
414
		"----------\n" + 
573
		"----------\n" + 
415
		"3. ERROR in X.java (at line 13)\n" + 
574
		"3. ERROR in X.java (at line 9)\n" + 
416
		"	System.out.println(y);\n" + 
575
		"	System.out.println(y); \n" + 
417
		"	                   ^\n" + 
576
		"	                   ^\n" + 
418
		"y cannot be resolved to a variable\n" + 
577
		"y cannot be resolved to a variable\n" + 
419
		"----------\n");
578
		"----------\n");
(-)src/org/eclipse/jdt/core/tests/formatter/FormatterRegressionTests.java (-1 / +1 lines)
Lines 66-72 Link Here
66
	Map formatterOptions;
66
	Map formatterOptions;
67
67
68
	static {
68
	static {
69
		TESTS_NUMBERS = new int[] { 746, 747 };
69
//		TESTS_NUMBERS = new int[] { 746, 747 };
70
	}
70
	}
71
	public static Test suite() {
71
	public static Test suite() {
72
		return buildModelTestSuite(FormatterRegressionTests.class);
72
		return buildModelTestSuite(FormatterRegressionTests.class);

Return to bug 338402