Fixed Bug 533187: JDT generates incorrect code for
try-with-resources and synchronized construct

Change-Id: Icd13499f9bd98b4fb8b94f538902d54bdfa7283d
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StackMapAttributeTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StackMapAttributeTest.java
index b2c6034..2e7081a 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StackMapAttributeTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/StackMapAttributeTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2006, 2014 IBM Corporation and others.
+ * Copyright (c) 2006, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -7878,8 +7878,8 @@
 					"    127  return\n" + 
 					"      Exception Table:\n" + 
 					"        [pc: 17, pc: 22] -> 51 when : any\n" + 
-					"        [pc: 32, pc: 38] -> 51 when : any\n" + 
-					"        [pc: 6, pc: 64] -> 64 when : any\n" + 
+					"        [pc: 6, pc: 32] -> 64 when : any\n" +
+					"        [pc: 38, pc: 64] -> 64 when : any\n" +
 					"        [pc: 2, pc: 32] -> 98 when : java.io.IOException\n" + 
 					"        [pc: 38, pc: 92] -> 98 when : java.io.IOException\n" + 
 					"        [pc: 2, pc: 32] -> 112 when : any\n" + 
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java
index 802f002..5b3e711 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/TryWithResourcesStatementTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2014 IBM Corporation and others.
+ * Copyright (c) 2011, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -4230,6 +4230,82 @@
 			}, 
 			"computeclose");
 }
+// https://bugs.eclipse.org/bugs/show_bug.cgi?id=533187
+public void testBug533187() {
+	this.runConformTest(
+			true,
+			new String[] {
+				"Stuck.java", 
+				"public class Stuck {\n" + 
+				"    public static void main(String[] args) {\n" + 
+				"        System.out.println(snippet1());\n" + 
+				"    }\n" + 
+				"    public static String snippet1() {\n" + 
+				"        try {\n" + 
+				"            synchronized (String.class) {\n" + 
+				"                try (AutoCloseable scope = null) { \n" + 
+				"                    return \"RETURN\";\n" + 
+				"                } catch (Throwable t) {\n" + 
+				"                    return t.toString();\n" + 
+				"                }\n" + 
+				"            }\n" + 
+				"        } finally {\n" + 
+				"            raise();\n" + 
+				"        }\n" + 
+				"    }\n" + 
+				"    public static void raise() {\n" + 
+				"        throw new RuntimeException();\n" + 
+				"    }\n" + 
+				"}"
+			},
+			null,
+			null,
+			null,
+			null,
+			"java.lang.RuntimeException\n" + 
+			"	at Stuck.raise(Stuck.java:19)\n" + 
+			"	at Stuck.snippet1(Stuck.java:15)\n" + 
+			"	at Stuck.main(Stuck.java:3)\n",
+			null);
+}
+// https://bugs.eclipse.org/bugs/show_bug.cgi?id=467230
+public void testBug467230() {
+	this.runConformTest(
+			true,
+			new String[] {
+				"Test.java", 
+				"public class Test {\n" + 
+				"	static class C implements AutoCloseable {\n" + 
+				"		@Override\n" + 
+				"		public void close() {\n" + 
+				"			System.out.println(\"close\");\n" + 
+				"		}\n" + 
+				"	}\n" + 
+				"	public static void main(String[] args) {\n" + 
+				"		try (C c = new C()) {\n" + 
+				"			return;\n" + 
+				"		} catch (Exception e) {\n" + 
+				"			System.out.println(\"catch\");\n" + 
+				"		} finally {\n" + 
+				"			f();\n" + 
+				"		}\n" + 
+				"	}\n" + 
+				"	private static void f() {\n" + 
+				"		System.out.println(\"finally\");\n" + 
+				"		throw new RuntimeException();\n" + 
+				"	}\n" + 
+				"}"
+			},
+			null,
+			null,
+			null,
+			"close\n" +
+			"finally",
+			"java.lang.RuntimeException\n" + 
+			"	at Test.f(Test.java:19)\n" + 
+			"	at Test.main(Test.java:14)\n",
+			null);
+}
 public static Class testClass() {
 	return TryWithResourcesStatementTest.class;
 }
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java
index e10440a..8514e86 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/TryStatement.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2017 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -452,6 +452,13 @@
 	for (int i = 0, length = this.declaredExceptionLabels == null ? 0 : this.declaredExceptionLabels.length; i < length; i++) {
 		this.declaredExceptionLabels[i].placeStart();
 	}
+	int resourceCount = this.resources.length;
+	if (resourceCount > 0 && this.resourceExceptionLabels != null) { // https://bugs.eclipse.org/bugs/show_bug.cgi?id=375248
+		// Reinstall handlers
+		for (int i = resourceCount; i >= 0; --i) {
+			this.resourceExceptionLabels[i].placeStart();
+		}
+	}
 }
 
 @Override
@@ -950,10 +957,7 @@
 			codeStream.recordPositionsFrom(invokeCloseStartPc, this.tryBlock.sourceEnd);
 			exitLabel.place();
 		}
-		// Reinstall handlers
-		for (int i = resourceCount; i > 0; --i) {
-			this.resourceExceptionLabels[i].placeStart();
-		}
+		this.resourceExceptionLabels[0].placeEnd(); // outermost should end here as well, will start again on enter
 	}
 
 	boolean isStackMapFrameCodeStream = codeStream instanceof StackMapFrameCodeStream;