Bug 572212 - Avoid reflecting on JRE boot classes in tests

Java 16 tightens the reflective rules to obey the Java module opens
rules by default.  This changes instead reflects on methods of the
framework implementation class for the bundle class loader.  This change
will fail if somehow the tests run with the framework implementation
loaded in a ModuleLayer.

Change-Id: I9de041666313d6829f1009c7b3c8284013b4c558
Signed-off-by: Thomas Watson <tjwatson@us.ibm.com>
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.a1/nativetest/a1/Activator.java b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.a1/nativetest/a1/Activator.java
index 11b08b9..0a106ad 100644
--- a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.a1/nativetest/a1/Activator.java
+++ b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.a1/nativetest/a1/Activator.java
@@ -19,9 +19,20 @@
 import org.osgi.framework.BundleContext;
 
 public class Activator implements BundleActivator {
+	public Method findDeclaredMethod(Class<?> clazz, String method, Class... args) throws NoSuchMethodException {
+		do {
+			try {
+				return clazz.getDeclaredMethod(method, args);
+			} catch (NoSuchMethodException e) {
+				clazz = clazz.getSuperclass();
+			}
+		} while (clazz != null);
+		throw new NoSuchMethodException(method);
+	}
 
 	public void start(BundleContext context) throws Exception {
-		Method findLibrary = ClassLoader.class.getDeclaredMethod("findLibrary", new Class[] {String.class});
+		Method findLibrary = findDeclaredMethod(this.getClass().getClassLoader().getClass(), "findLibrary",
+				String.class);
 		findLibrary.setAccessible(true);
 		AbstractBundleTests.simpleResults.addEvent(findLibrary.invoke(this.getClass().getClassLoader(), new Object[] {"nativefile1.txt"}));
 	}
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.a2/nativetest/a2/Activator.java b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.a2/nativetest/a2/Activator.java
index 999fb53..1d4b967 100644
--- a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.a2/nativetest/a2/Activator.java
+++ b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.a2/nativetest/a2/Activator.java
@@ -20,8 +20,20 @@
 
 public class Activator implements BundleActivator {
 
+	public Method findDeclaredMethod(Class<?> clazz, String method, Class... args) throws NoSuchMethodException {
+		do {
+			try {
+				return clazz.getDeclaredMethod(method, args);
+			} catch (NoSuchMethodException e) {
+				clazz = clazz.getSuperclass();
+			}
+		} while (clazz != null);
+		throw new NoSuchMethodException(method);
+	}
+
 	public void start(BundleContext context) throws Exception {
-		Method findLibrary = ClassLoader.class.getDeclaredMethod("findLibrary", new Class[] {String.class});
+		Method findLibrary = findDeclaredMethod(this.getClass().getClassLoader().getClass(), "findLibrary",
+				String.class);
 		findLibrary.setAccessible(true);
 		AbstractBundleTests.simpleResults.addEvent(findLibrary.invoke(this.getClass().getClassLoader(), new Object[] {"nativefile2.txt"}));
 	}
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.b1/nativetest/b1/Activator.java b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.b1/nativetest/b1/Activator.java
index a1bdf02..38e7c52 100644
--- a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.b1/nativetest/b1/Activator.java
+++ b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.b1/nativetest/b1/Activator.java
@@ -20,8 +20,20 @@
 
 public class Activator implements BundleActivator {
 
+	public Method findDeclaredMethod(Class<?> clazz, String method, Class... args) throws NoSuchMethodException {
+		do {
+			try {
+				return clazz.getDeclaredMethod(method, args);
+			} catch (NoSuchMethodException e) {
+				clazz = clazz.getSuperclass();
+			}
+		} while (clazz != null);
+		throw new NoSuchMethodException(method);
+	}
+
 	public void start(BundleContext context) throws Exception {
-		Method findLibrary = ClassLoader.class.getDeclaredMethod("findLibrary", new Class[] {String.class});
+		Method findLibrary = findDeclaredMethod(this.getClass().getClassLoader().getClass(), "findLibrary",
+				String.class);
 		findLibrary.setAccessible(true);
 		AbstractBundleTests.simpleResults.addEvent(findLibrary.invoke(this.getClass().getClassLoader(), new Object[] {"nativefile.txt"}));
 	}
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.b2/nativetest/b2/Activator.java b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.b2/nativetest/b2/Activator.java
index 97c922e..e5dd530 100644
--- a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.b2/nativetest/b2/Activator.java
+++ b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.b2/nativetest/b2/Activator.java
@@ -20,8 +20,20 @@
 
 public class Activator implements BundleActivator {
 
+	public Method findDeclaredMethod(Class<?> clazz, String method, Class... args) throws NoSuchMethodException {
+		do {
+			try {
+				return clazz.getDeclaredMethod(method, args);
+			} catch (NoSuchMethodException e) {
+				clazz = clazz.getSuperclass();
+			}
+		} while (clazz != null);
+		throw new NoSuchMethodException(method);
+	}
+
 	public void start(BundleContext context) throws Exception {
-		Method findLibrary = ClassLoader.class.getDeclaredMethod("findLibrary", new Class[] {String.class});
+		Method findLibrary = findDeclaredMethod(this.getClass().getClassLoader().getClass(), "findLibrary",
+				String.class);
 		findLibrary.setAccessible(true);
 		AbstractBundleTests.simpleResults.addEvent(findLibrary.invoke(this.getClass().getClassLoader(), new Object[] {"nativefile.txt"}));
 	}
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.c/nativetest/c/Activator.java b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.c/nativetest/c/Activator.java
index 9dd1554..37219d1 100644
--- a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.c/nativetest/c/Activator.java
+++ b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.c/nativetest/c/Activator.java
@@ -20,8 +20,20 @@
 
 public class Activator implements BundleActivator {
 
+	public Method findDeclaredMethod(Class<?> clazz, String method, Class... args) throws NoSuchMethodException {
+		do {
+			try {
+				return clazz.getDeclaredMethod(method, args);
+			} catch (NoSuchMethodException e) {
+				clazz = clazz.getSuperclass();
+			}
+		} while (clazz != null);
+		throw new NoSuchMethodException(method);
+	}
+
 	public void start(BundleContext context) throws Exception {
-		Method findLibrary = ClassLoader.class.getDeclaredMethod("findLibrary", new Class[] {String.class});
+		Method findLibrary = findDeclaredMethod(this.getClass().getClassLoader().getClass(), "findLibrary",
+				String.class);
 		findLibrary.setAccessible(true);
 		AbstractBundleTests.simpleResults.addEvent(findLibrary.invoke(this.getClass().getClassLoader(), new Object[] {"nativecode.txt"}));
 	}
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.d/nativetest/d/Activator.java b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.d/nativetest/d/Activator.java
index 9573d24..855c0c1 100644
--- a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.d/nativetest/d/Activator.java
+++ b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.d/nativetest/d/Activator.java
@@ -20,8 +20,20 @@
 
 public class Activator implements BundleActivator {
 
+	public Method findDeclaredMethod(Class<?> clazz, String method, Class... args) throws NoSuchMethodException {
+		do {
+			try {
+				return clazz.getDeclaredMethod(method, args);
+			} catch (NoSuchMethodException e) {
+				clazz = clazz.getSuperclass();
+			}
+		} while (clazz != null);
+		throw new NoSuchMethodException(method);
+	}
+
 	public void start(BundleContext context) throws Exception {
-		Method findLibrary = ClassLoader.class.getDeclaredMethod("findLibrary", new Class[] {String.class});
+		Method findLibrary = findDeclaredMethod(this.getClass().getClassLoader().getClass(), "findLibrary",
+				String.class);
 		findLibrary.setAccessible(true);
 		AbstractBundleTests.simpleResults.addEvent(findLibrary.invoke(this.getClass().getClassLoader(), new Object[] {"nativecode.txt"}));
 	}
diff --git a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.e/nativetest/e/Activator.java b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.e/nativetest/e/Activator.java
index b4a3c1f..2c1e5cb 100644
--- a/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.e/nativetest/e/Activator.java
+++ b/bundles/org.eclipse.osgi.tests/bundles_src/nativetest.e/nativetest/e/Activator.java
@@ -20,8 +20,20 @@
 
 public class Activator implements BundleActivator {
 
+	public Method findDeclaredMethod(Class<?> clazz, String method, Class... args) throws NoSuchMethodException {
+		do {
+			try {
+				return clazz.getDeclaredMethod(method, args);
+			} catch (NoSuchMethodException e) {
+				clazz = clazz.getSuperclass();
+			}
+		} while (clazz != null);
+		throw new NoSuchMethodException(method);
+	}
+
 	public void start(BundleContext context) throws Exception {
-		Method findLibrary = ClassLoader.class.getDeclaredMethod("findLibrary", new Class[] {String.class});
+		Method findLibrary = findDeclaredMethod(this.getClass().getClassLoader().getClass(), "findLibrary",
+				String.class);
 		findLibrary.setAccessible(true);
 		AbstractBundleTests.simpleResults.addEvent(findLibrary.invoke(this.getClass().getClassLoader(), new Object[] {"nativecode.txt"}));
 	}
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/BundleInstallUpdateTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/BundleInstallUpdateTests.java
index de830bc..4c4a5a4 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/BundleInstallUpdateTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/BundleInstallUpdateTests.java
@@ -437,6 +437,17 @@
 		is.close();
 	}
 
+	public static Method findDeclaredMethod(Class<?> clazz, String method, Class... args) throws NoSuchMethodException {
+		do {
+			try {
+				return clazz.getDeclaredMethod(method, args);
+			} catch (NoSuchMethodException e) {
+				clazz = clazz.getSuperclass();
+			}
+		} while (clazz != null);
+		throw new NoSuchMethodException(method);
+	}
+
 	public void testEscapeZipRoot() throws IOException, BundleException, InvalidSyntaxException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
 		String entry1 = "../../escapedZipRoot1.txt";
 		String entry2 = "dir1/../../../escapedZipRoot2.txt";
@@ -463,8 +474,8 @@
 		} catch (ClassNotFoundException e) {
 			// expected
 		}
-		ClassLoader cl = testBundle.adapt(BundleWiring.class).getClassLoader();
-		Method findLibrary = ClassLoader.class.getDeclaredMethod("findLibrary", String.class);
+		Object cl = testBundle.adapt(BundleWiring.class).getClassLoader();
+		Method findLibrary = findDeclaredMethod(cl.getClass(), "findLibrary", String.class);
 		findLibrary.setAccessible(true);
 		assertNull("Found library.", findLibrary.invoke(cl, "nativeCode"));
 
diff --git a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/ClassLoadingBundleTests.java b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/ClassLoadingBundleTests.java
index 793aa05..0e7f0d3 100644
--- a/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/ClassLoadingBundleTests.java
+++ b/bundles/org.eclipse.osgi.tests/src/org/eclipse/osgi/tests/bundles/ClassLoadingBundleTests.java
@@ -1876,7 +1876,8 @@
 		ClassLoader cl = this.getClass().getClassLoader();
 		Enumeration resources = null;
 		try {
-			Method findMethod = ClassLoader.class.getDeclaredMethod("findResources", new Class[] {String.class});
+			Method findMethod = BundleInstallUpdateTests.findDeclaredMethod(cl.getClass(), "findResources",
+					String.class);
 			findMethod.setAccessible(true);
 
 			resources = (Enumeration) findMethod.invoke(cl, new Object[] {"test/doesnotexist.txt"});