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/core/compiler/IProblem.java (+2 lines)
Lines 1371-1376 Link Here
1371
	int AssignmentToResource = Internal + 872;
1371
	int AssignmentToResource = Internal + 872;
1372
	/** @since 3.7 */
1372
	/** @since 3.7 */
1373
	int InvalidDisjunctiveTypeReferenceSequence = Internal + TypeRelated + 873; 
1373
	int InvalidDisjunctiveTypeReferenceSequence = Internal + TypeRelated + 873; 
1374
	/** @since 3.7 */
1375
	int AutoManagedResourceNotBelow17 = Syntax + Internal + 874;
1374
	/**
1376
	/**
1375
	 * External problems -- These are problems defined by other plugins
1377
	 * External problems -- These are problems defined by other plugins
1376
	 */
1378
	 */
(-)compiler/org/eclipse/jdt/internal/compiler/ast/TryStatementWithResources.java (-2 / +9 lines)
Lines 29-35 Link Here
29
29
30
public class TryStatementWithResources extends TryStatement {
30
public class TryStatementWithResources extends TryStatement {
31
31
32
	public LocalDeclaration[] resources;
32
	private static LocalDeclaration [] NO_RESOURCES = new LocalDeclaration[0];
33
	public LocalDeclaration[] resources = NO_RESOURCES;
33
34
34
	public StringBuffer printStatement(int indent, StringBuffer output) {
35
	public StringBuffer printStatement(int indent, StringBuffer output) {
35
		printIndent(indent, output).append("try ("); //$NON-NLS-1$
36
		printIndent(indent, output).append("try ("); //$NON-NLS-1$
Lines 132-138 Link Here
132
				int shiftScopesLength = this.catchArguments == null ? 1 : this.catchArguments.length + 1;
133
				int shiftScopesLength = this.catchArguments == null ? 1 : this.catchArguments.length + 1;
133
				shiftScopesLength += this.resources.length;
134
				shiftScopesLength += this.resources.length;
134
				finallyScope.shiftScopes = new BlockScope[shiftScopesLength];
135
				finallyScope.shiftScopes = new BlockScope[shiftScopesLength];
135
				finallyScope.shiftScopes[0] = tryScope;
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;
136
			}
143
			}
137
		}
144
		}
138
145
(-)compiler/org/eclipse/jdt/internal/compiler/lookup/BlockScope.java (-2 / +4 lines)
Lines 841-848 Link Here
841
	int max = -1;
841
	int max = -1;
842
	if (this.shiftScopes != null){
842
	if (this.shiftScopes != null){
843
		for (int i = 0, length = this.shiftScopes.length; i < length; i++){
843
		for (int i = 0, length = this.shiftScopes.length; i < length; i++){
844
			int subMaxOffset = this.shiftScopes[i].maxOffset;
844
			if (this.shiftScopes[i] != null) {
845
			if (subMaxOffset > max) max = subMaxOffset;
845
				int subMaxOffset = this.shiftScopes[i].maxOffset;
846
				if (subMaxOffset > max) max = subMaxOffset;
847
			}
846
		}
848
		}
847
	}
849
	}
848
	return max;
850
	return max;
(-)compiler/org/eclipse/jdt/internal/compiler/parser/Parser.java (-1 / +5 lines)
Lines 7453-7459 Link Here
7453
			resources,
7453
			resources,
7454
			0,
7454
			0,
7455
			length);
7455
			length);
7456
	tryStmt.resources = resources;
7456
	if (this.options.sourceLevel < ClassFileConstants.JDK1_7) {
7457
		problemReporter().autoManagedResourcesNotBelow17(resources);
7458
	} else {
7459
		tryStmt.resources = resources;
7460
	}
7457
	//positions
7461
	//positions
7458
	tryStmt.sourceEnd = this.endStatementPosition;
7462
	tryStmt.sourceEnd = this.endStatementPosition;
7459
	tryStmt.sourceStart = this.intStack[this.intPtr--];
7463
	tryStmt.sourceStart = this.intStack[this.intPtr--];
(-)compiler/org/eclipse/jdt/internal/compiler/problem/ProblemReporter.java (+9 lines)
Lines 7688-7691 Link Here
7688
		typeRef.sourceStart,
7688
		typeRef.sourceStart,
7689
		typeRef.sourceEnd);
7689
		typeRef.sourceEnd);
7690
}
7690
}
7691
7692
public void autoManagedResourcesNotBelow17(LocalDeclaration[] resources) {
7693
	this.handle(
7694
			IProblem.AutoManagedResourceNotBelow17,
7695
			NoArgument,
7696
			NoArgument,
7697
			resources[0].declarationSourceStart,
7698
			resources[resources.length - 1].declarationSourceEnd);
7699
}
7691
}
7700
}
(-)compiler/org/eclipse/jdt/internal/compiler/problem/messages.properties (+1 lines)
Lines 635-640 Link Here
635
871 = The resource type {0} has to be a subclass of java.lang.AutoCloseable 
635
871 = The resource type {0} has to be a subclass of java.lang.AutoCloseable 
636
872 = The resource {0} of a try-with-resources statement cannot be assigned
636
872 = The resource {0} of a try-with-resources statement cannot be assigned
637
873 = The exception {0} is already caught by the exception {1}
637
873 = The exception {0} is already caught by the exception {1}
638
874 = Resource specification not allowed here for source level below 1.7
638
639
639
### ELABORATIONS
640
### ELABORATIONS
640
## Access restrictions
641
## Access restrictions
(-)src/org/eclipse/jdt/core/tests/compiler/parser/ComplianceDiagnoseTest.java (+38 lines)
Lines 2460-2463 Link Here
2460
		expected15ProblemLog
2460
		expected15ProblemLog
2461
	);
2461
	);
2462
}
2462
}
2463
public void test0054() {
2464
	String[] testFiles = new String[] {
2465
		"X.java",
2466
		"public class X {\n" +
2467
		"	public static void main(String[] args) {\n" +
2468
		"		try (int i = 0) {};\n" +
2469
		"	}\n" +
2470
		"}\n"
2471
	};
2472
2473
	String expected13ProblemLog =
2474
		"----------\n" + 
2475
		"1. ERROR in X.java (at line 3)\n" + 
2476
		"	try (int i = 0) {};\n" + 
2477
		"	     ^^^^^^^^^\n" + 
2478
		"Resource specification not allowed here for source level below 1.7\n" + 
2479
		"----------\n";
2480
	String expected14ProblemLog =
2481
		expected13ProblemLog;
2482
2483
	String expected15ProblemLog =
2484
		expected14ProblemLog;
2485
2486
	String expected17ProblemLog = 
2487
		"----------\n" + 
2488
		"1. ERROR in X.java (at line 3)\n" + 
2489
		"	try (int i = 0) {};\n" + 
2490
		"	     ^^^\n" + 
2491
		"The resource type int has to be a subclass of java.lang.AutoCloseable \n" + 
2492
		"----------\n";
2493
	runComplianceParserTest(
2494
		testFiles,
2495
		expected13ProblemLog,
2496
		expected14ProblemLog,
2497
		expected15ProblemLog,
2498
		expected17ProblemLog
2499
	);
2500
}
2463
}
2501
}
(-)src/org/eclipse/jdt/core/tests/compiler/parser/ParserTest1_7.java (-3 / +3 lines)
Lines 235-243 Link Here
235
235
236
protected Map getCompilerOptions() {
236
protected Map getCompilerOptions() {
237
	Map options = super.getCompilerOptions();
237
	Map options = super.getCompilerOptions();
238
	options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_5);
238
	options.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_7);
239
	options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_5);
239
	options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_7);
240
	options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_5);
240
	options.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_7);
241
	return options;
241
	return options;
242
}
242
}
243
public void test0001() {
243
public void test0001() {
(-)src/org/eclipse/jdt/core/tests/compiler/regression/CompilerInvocationTests.java (+2 lines)
Lines 362-367 Link Here
362
		expectedProblemAttributes.put("AssignmentHasNoEffect", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
362
		expectedProblemAttributes.put("AssignmentHasNoEffect", new ProblemAttributes(CategorizedProblem.CAT_POTENTIAL_PROGRAMMING_PROBLEM));
363
		expectedProblemAttributes.put("AssignmentToMultiCatchParameter", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
363
		expectedProblemAttributes.put("AssignmentToMultiCatchParameter", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
364
		expectedProblemAttributes.put("AssignmentToResource", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
364
		expectedProblemAttributes.put("AssignmentToResource", new ProblemAttributes(CategorizedProblem.CAT_INTERNAL));
365
		expectedProblemAttributes.put("AutoManagedResourceNotBelow17", new ProblemAttributes(CategorizedProblem.CAT_SYNTAX));
365
		expectedProblemAttributes.put("BodyForAbstractMethod", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
366
		expectedProblemAttributes.put("BodyForAbstractMethod", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
366
		expectedProblemAttributes.put("BodyForNativeMethod", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
367
		expectedProblemAttributes.put("BodyForNativeMethod", new ProblemAttributes(CategorizedProblem.CAT_MEMBER));
367
		expectedProblemAttributes.put("BoundCannotBeArray", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
368
		expectedProblemAttributes.put("BoundCannotBeArray", new ProblemAttributes(CategorizedProblem.CAT_TYPE));
Lines 1018-1023 Link Here
1018
		expectedProblemAttributes.put("AssignmentHasNoEffect", new ProblemAttributes(JavaCore.COMPILER_PB_NO_EFFECT_ASSIGNMENT));
1019
		expectedProblemAttributes.put("AssignmentHasNoEffect", new ProblemAttributes(JavaCore.COMPILER_PB_NO_EFFECT_ASSIGNMENT));
1019
		expectedProblemAttributes.put("AssignmentToMultiCatchParameter", SKIP);
1020
		expectedProblemAttributes.put("AssignmentToMultiCatchParameter", SKIP);
1020
		expectedProblemAttributes.put("AssignmentToResource", SKIP);
1021
		expectedProblemAttributes.put("AssignmentToResource", SKIP);
1022
		expectedProblemAttributes.put("AutoManagedResourceNotBelow17", SKIP);
1021
		expectedProblemAttributes.put("BodyForAbstractMethod", SKIP);
1023
		expectedProblemAttributes.put("BodyForAbstractMethod", SKIP);
1022
		expectedProblemAttributes.put("BodyForNativeMethod", SKIP);
1024
		expectedProblemAttributes.put("BodyForNativeMethod", SKIP);
1023
		expectedProblemAttributes.put("BoundCannotBeArray", SKIP);
1025
		expectedProblemAttributes.put("BoundCannotBeArray", SKIP);
(-)src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java (+42 lines)
Lines 376-381 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() {
380
	this.runNegativeTest(
381
		new String[] {
382
			"X.java",
383
			"public class X {\n" +
384
			"	public static void main(String [] args) {    \n" +
385
			"		try (Y y = new Y();) {\n" +
386
			"		    System.out.println(\"Try block\");\n" +
387
			"		} finally {\n" +
388
			"		    System.out.println(\"Finally block\");\n" +
389
			"		}\n" +
390
			"	}\n" +
391
			"} \n" +
392
			"\n" +
393
			"class Y implements AutoCloseable {\n" +
394
			"	public Y() throws WeirdException {\n" +
395
			"		throw new WeirdException();\n" +
396
			"	}\n" +
397
			"	public void close() {\n" +
398
			"		    System.out.println(\"Closing resource\");\n" +
399
			"	}\n" +
400
			"}\n" +
401
			"\n" +
402
			"class WeirdException extends Throwable {}\n",
403
		},
404
		"----------\n" + 
405
		"1. WARNING in X.java (at line 5)\n" + 
406
		"	public void foo(int p) {\n" + 
407
		"	                    ^\n" + 
408
		"The parameter p is hiding another local variable defined in an enclosing type scope\n" + 
409
		"----------\n" + 
410
		"2. WARNING in X.java (at line 8)\n" + 
411
		"	} catch (Exception y) {\n" + 
412
		"	                   ^\n" + 
413
		"The parameter y is hiding another local variable defined in an enclosing type scope\n" + 
414
		"----------\n" + 
415
		"3. ERROR in X.java (at line 13)\n" + 
416
		"	System.out.println(y);\n" + 
417
		"	                   ^\n" + 
418
		"y cannot be resolved to a variable\n" + 
419
		"----------\n");
420
}
379
public static Class testClass() {
421
public static Class testClass() {
380
	return TryWithResourcesStatementTest.class;
422
	return TryWithResourcesStatementTest.class;
381
}
423
}

Return to bug 338402