From e6cbfae1b59a4339275b88cfde8f7c3e22fdea75 Mon Sep 17 00:00:00 2001 From: Ernestas Kulik Date: Wed, 10 Jun 2020 09:06:10 +0200 Subject: [PATCH 6/8] =?UTF-8?q?test:=20RemoteTest:=20Don=E2=80=99t=20call?= =?UTF-8?q?=20URLClassLoader.addURL()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’s a protected method and some future version of Java will start restricting access via reflection, so let’s just use the legal way. --- test/RemoteTest.java | 8 +++----- test/outputs/run_remote.log.in | 20 ++++++++++---------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/test/RemoteTest.java b/test/RemoteTest.java index 040384f..e92e6f5 100644 --- a/test/RemoteTest.java +++ b/test/RemoteTest.java @@ -83,19 +83,17 @@ public class RemoteTest { input.close(); try { - Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class}); - method.setAccessible(true); - method.invoke(ClassLoader.getSystemClassLoader(), new Object[]{remoteJarUrl}); + ClassLoader loader = URLClassLoader.newInstance(new URL[]{remoteJarUrl}, ClassLoader.getSystemClassLoader()); /* Loaded these classes into cache. */ final String needed[] = {"SimpleTest", "ThreadUncaughtException", "ThreadCaughtException"}; for (String requiredClass : needed) { - if (null == Class.forName(requiredClass)) { + if (null == Class.forName(requiredClass, true, loader)) { System.out.println("Cannot get required class: " + requiredClass); System.exit(1); } } - testClassInstance = Class.forName(testClassName); + testClassInstance = Class.forName(testClassName, true, loader); } finally { server.stop(0); diff --git a/test/outputs/run_remote.log.in b/test/outputs/run_remote.log.in index 31ef171..00bf701 100644 --- a/test/outputs/run_remote.log.in +++ b/test/outputs/run_remote.log.in @@ -3,19 +3,19 @@ Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 42 ou at SimpleTest.throwIndexOutOfBoundsException(SimpleTest.java:24) [jar:http://localhost:54321/JarTest.jar!/SimpleTest.class] at SimpleTest.catchIndexOutOfBoundsException(SimpleTest.java:47) [jar:http://localhost:54321/JarTest.jar!/SimpleTest.class] at SimpleTest.throwAndCatchAllExceptions(SimpleTest.java:61) [jar:http://localhost:54321/JarTest.jar!/SimpleTest.class] - at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [jar:file:JAVA_AND_SYSTEM_SPECIFIC_PATH/rt.jar!/sun/reflect/NativeMethodAccessorImpl.class] - at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:LINENO) [jar:file:JAVA_AND_SYSTEM_SPECIFIC_PATH/rt.jar!/sun/reflect/NativeMethodAccessorImpl.class] - at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:LINENO) [jar:file:JAVA_AND_SYSTEM_SPECIFIC_PATH/rt.jar!/sun/reflect/DelegatingMethodAccessorImpl.class] - at java.lang.reflect.Method.invoke(Method.java:LINENO) [jar:file:JAVA_AND_SYSTEM_SPECIFIC_PATH/rt.jar!/java/lang/reflect/Method.class] - at RemoteTest.main(RemoteTest.java:110) [file:@CMAKE_BINARY_DIR@/test/RemoteTest.class] + at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [jrt:/java.base/jdk/internal/reflect/NativeMethodAccessorImpl.class] + at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:LINENO) [jrt:/java.base/jdk/internal/reflect/NativeMethodAccessorImpl.class] + at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:LINENO) [jrt:/java.base/jdk/internal/reflect/DelegatingMethodAccessorImpl.class] + at java.base/java.lang.reflect.Method.invoke(Method.java:LINENO) [jrt:/java.base/java/lang/reflect/Method.class] + at RemoteTest.main(RemoteTest.java:108) [file:@CMAKE_BINARY_DIR@/test/RemoteTest.class] executable: @CMAKE_BINARY_DIR@/test/RemoteTest.class Uncaught exception java.lang.NullPointerException in method SimpleTest.throwNullPointerException() Exception in thread "main" java.lang.NullPointerException at SimpleTest.throwNullPointerException(SimpleTest.java:36) [jar:http://localhost:54321/JarTest.jar!/SimpleTest.class] at SimpleTest.throwAndDontCatchException(SimpleTest.java:71) [jar:http://localhost:54321/JarTest.jar!/SimpleTest.class] - at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [jar:file:JAVA_AND_SYSTEM_SPECIFIC_PATH/rt.jar!/sun/reflect/NativeMethodAccessorImpl.class] - at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:LINENO) [jar:file:JAVA_AND_SYSTEM_SPECIFIC_PATH/rt.jar!/sun/reflect/NativeMethodAccessorImpl.class] - at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:LINENO) [jar:file:JAVA_AND_SYSTEM_SPECIFIC_PATH/rt.jar!/sun/reflect/DelegatingMethodAccessorImpl.class] - at java.lang.reflect.Method.invoke(Method.java:LINENO) [jar:file:JAVA_AND_SYSTEM_SPECIFIC_PATH/rt.jar!/java/lang/reflect/Method.class] - at RemoteTest.main(RemoteTest.java:112) [file:@CMAKE_BINARY_DIR@/test/RemoteTest.class] + at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [jrt:/java.base/jdk/internal/reflect/NativeMethodAccessorImpl.class] + at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:LINENO) [jrt:/java.base/jdk/internal/reflect/NativeMethodAccessorImpl.class] + at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:LINENO) [jrt:/java.base/jdk/internal/reflect/DelegatingMethodAccessorImpl.class] + at java.base/java.lang.reflect.Method.invoke(Method.java:LINENO) [jrt:/java.base/java/lang/reflect/Method.class] + at RemoteTest.main(RemoteTest.java:110) [file:@CMAKE_BINARY_DIR@/test/RemoteTest.class] executable: @CMAKE_BINARY_DIR@/test/RemoteTest.class -- 2.26.2