Bug 388060 - [compiler] closeable role triggers resource warning from
lift method
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java
index 2a8b863..181d580 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/Assignment.java
@@ -85,6 +85,9 @@
 	boolean shouldAnalyseResource = local != null
 			&& flowInfo.reachMode() == FlowInfo.REACHABLE
 			&& currentScope.compilerOptions().analyseResourceLeaks
+//{ObjectTeams: notably lift methods of Closeable roles would trigger warnings against synthetic code
+			&& !currentScope.isGeneratedScope()
+// SH}
 			&& (FakedTrackingVariable.isAnyCloseable(this.expression.resolvedType)
 					|| this.expression.resolvedType == TypeBinding.NULL);
 	if (shouldAnalyseResource) {
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java
index 7aa2afc..b1211b9 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/LocalDeclaration.java
@@ -137,6 +137,9 @@
 	boolean shouldAnalyseResource = this.binding != null 
 			&& flowInfo.reachMode() == FlowInfo.REACHABLE
 			&& currentScope.compilerOptions().analyseResourceLeaks
+//{ObjectTeams: notably lift methods of Closeable roles would trigger warnings against synthetic code
+			&& !currentScope.isGeneratedScope()
+// SH}
 			&& FakedTrackingVariable.isAnyCloseable(this.initialization.resolvedType);
 	if (shouldAnalyseResource) {
 		preInitInfo = flowInfo.unconditionalCopy();
diff --git a/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/other/Java7.java b/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/other/Java7.java
index f3575cb..8e5cf98 100644
--- a/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/other/Java7.java
+++ b/testplugins/org.eclipse.objectteams.otdt.tests/otjld/org/eclipse/objectteams/otdt/tests/otjld/other/Java7.java
@@ -15,8 +15,13 @@
  **********************************************************************/
 package org.eclipse.objectteams.otdt.tests.otjld.other;
 
+import java.io.File;
+import java.util.Map;
+
 import junit.framework.Test;
 
+import org.eclipse.jdt.core.JavaCore;
+import org.eclipse.jdt.core.tests.util.Util;
 import org.eclipse.objectteams.otdt.tests.otjld.AbstractOTJLDTest;
 
 public class Java7 extends AbstractOTJLDTest {
@@ -46,7 +51,11 @@
 	
 	// Bug 355007 - Using role as resource in try-with-resources creates bogus byte code
 	public void testA01_tryWithResources01() {
+		String fileName = OUTPUT_DIR+"/p1/data.txt";
+		new File(OUTPUT_DIR+"/p1").mkdirs();
+		Util.writeToFile("Hello Reader", fileName);
 		runConformTest(
+			false, // should not flush
 			new String[] {
 		"p1/TeamA01twr01.java",
 			"package p1;\n" +
@@ -54,8 +63,8 @@
 			"public team class TeamA01twr01 {\n" + 
 			"    protected class ReaderRole implements AutoCloseable playedBy TA01twr01 {\n" + 
 			"        public char[] content;\n" + 
-			"        protected void read16() throws IOException {\n" + 
-			"            content = new char[16];\n" + 
+			"        protected void read12() throws IOException {\n" + 
+			"            content = new char[12];\n" + 
 			"            read(content);\n" + 
 			"        }\n" + 
 			"        void read(char[] chars) -> int read(char[] chars);\n" + 
@@ -63,12 +72,12 @@
 			"    }\n" + 
 			"    void test(TA01twr01 bfr) throws Exception {\n" + 
 			"        try (ReaderRole r = new ReaderRole(bfr)) {\n" + 
-			"            r.read16();\n" + 
+			"            r.read12();\n" + 
 			"            System.out.println(String.valueOf(r.content));\n" + 
 			"        }\n" + 
 			"    } \n" + 
 			"    public static void main(String[] args) throws Exception {\n" + 
-			"        new TeamA01twr01().test(new TA01twr01(\"META-INF/MANIFEST.MF\"));\n" + 
+			"        new TeamA01twr01().test(new TA01twr01(\""+fileName+"\"));\n" + 
 			"    }\n" + 
 			"}",
 	"p1/TA01twr01.java",
@@ -89,7 +98,80 @@
 			"    }\n" + 
 			"}",
 			},
-			"Manifest-Version\n" +
-			"closed");
+			"----------\n" +
+			"1. WARNING in p1\\TeamA01twr01.java (at line 14)\n" +
+			"	try (ReaderRole r = new ReaderRole(bfr)) {\n" +
+			"	                    ^^^^^^^^^^^^^^^^^^^\n" +
+			"Argument to lifting constructor ReaderRole(TA01twr01) is not a freshly created base object (of type p1.TA01twr01); may cause a DuplicateRoleException at runtime (OTJLD 2.4.1(c)).\n" +
+			"----------\n",
+			"Hello Reader\n" +
+			"closed",
+			"",
+			null);
+	}
+	
+	// Bug 388060 - [compiler] closeable role triggers resource warning from lift method
+	@SuppressWarnings("unchecked")
+	public void testA01_tryWithResources02() {
+		String fileName = OUTPUT_DIR+"/p1/data.txt";
+		new File(OUTPUT_DIR+"/p1").mkdirs();
+		Util.writeToFile("Hello Reader", fileName);
+		Map options = getCompilerOptions();
+		options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, JavaCore.ERROR);
+		runTest(
+			new String[] {
+		"p1/TeamA01twr01.java",
+			"package p1;\n" +
+			"import java.io.*;\n" +
+			"public team class TeamA01twr01 {\n" + 
+			"    protected class ReaderRole implements AutoCloseable playedBy TA01twr01 {\n" + 
+			"        public char[] content;\n" + 
+			"        protected void read12() throws IOException {\n" + 
+			"            content = new char[12];\n" + 
+			"            read(content);\n" + 
+			"        }\n" + 
+			"        void read(char[] chars) -> int read(char[] chars);\n" + 
+			"        close -> close;\n" + 
+			"    }\n" + 
+			"    void test(String filename) throws Exception {\n" + 
+			"        try (ReaderRole r = new ReaderRole(new TA01twr01(filename))) {\n" + 
+			"            r.read12();\n" + 
+			"            System.out.println(String.valueOf(r.content));\n" + 
+			"        }\n" + 
+			"    } \n" + 
+			"    public static void main(String[] args) throws Exception {\n" + 
+			"        new TeamA01twr01().test(\""+fileName+"\");\n" + 
+			"    }\n" + 
+			"}",
+	"p1/TA01twr01.java",
+			"package p1;\n" +
+			"import java.io.*;\n" +
+			"public class TA01twr01 extends FileReader {\n" + 
+			"    public TA01twr01(String fileName) throws IOException {\n" + 
+			"        super(fileName);\n" + 
+			"    }\n" + 
+			"    @Override\n" + 
+			"    public void close() {\n" + 
+			"        try {\n" + 
+			"            super.close();\n" + 
+			"            System.out.println(\"closed\");\n" + 
+			"        } catch (IOException ioe) {\n" + 
+			"            ioe.printStackTrace();\n" + 
+			"        }\n" + 
+			"    }\n" + 
+			"}",
+			},
+			false, // no comp err
+			"",
+			"Hello Reader\n" +
+			"closed",
+			"",
+			true, // force exec
+			null, // libs
+			false, // should not flush
+			null, // vm args
+			options,
+			null, // requestor
+			true);// skip javac
 	}
 }