diff --git a/java/src/main/java/io/github/toolfactory/jvm/function/catalog/ConstructorInvokeFunction.java b/java/src/main/java/io/github/toolfactory/jvm/function/catalog/ConstructorInvokeFunction.java index d6383900..ce83b615 100644 --- a/java/src/main/java/io/github/toolfactory/jvm/function/catalog/ConstructorInvokeFunction.java +++ b/java/src/main/java/io/github/toolfactory/jvm/function/catalog/ConstructorInvokeFunction.java @@ -67,24 +67,28 @@ public static class ForJava9 extends Abst { public ForJava9(Map context) throws Throwable { ObjectProvider functionProvider = ObjectProvider.get(context); - Class accessorImplClass = Class.forName("jdk.internal.reflect.NativeConstructorAccessorImpl"); + Class accessorImplClass = Class.forName(retrieveNativeAccessorClassName()); Method method = accessorImplClass.getDeclaredMethod("newInstance0", Constructor.class, Object[].class); ConsulterSupplyFunction getConsulterFunction = functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); MethodHandles.Lookup consulter = getConsulterFunction.apply(accessorImplClass); methodHandle = consulter.unreflect(method); } + protected String retrieveNativeAccessorClassName() { + return "jdk.internal.reflect.NativeConstructorAccessorImpl"; + } + } - public static class ForJava22 extends Abst { + public static class ForJava22 extends ForJava9 { public ForJava22(Map context) throws Throwable { - ObjectProvider functionProvider = ObjectProvider.get(context); - Class accessorImplClass = Class.forName("jdk.internal.reflect.DirectConstructorHandleAccessor$NativeAccessor"); - Method method = accessorImplClass.getDeclaredMethod("newInstance0", Constructor.class, Object[].class); - ConsulterSupplyFunction getConsulterFunction = functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); - MethodHandles.Lookup consulter = getConsulterFunction.apply(accessorImplClass); - methodHandle = consulter.unreflect(method); + super(context); + } + + @Override + protected String retrieveNativeAccessorClassName() { + return "jdk.internal.reflect.DirectConstructorHandleAccessor$NativeAccessor"; } } diff --git a/java/src/main/java/io/github/toolfactory/jvm/function/catalog/MethodInvokeFunction.java b/java/src/main/java/io/github/toolfactory/jvm/function/catalog/MethodInvokeFunction.java index bcafecda..fe8c1227 100644 --- a/java/src/main/java/io/github/toolfactory/jvm/function/catalog/MethodInvokeFunction.java +++ b/java/src/main/java/io/github/toolfactory/jvm/function/catalog/MethodInvokeFunction.java @@ -65,7 +65,7 @@ public ForJava7(Map context) throws Throwable { public static class ForJava9 extends Abst { public ForJava9(Map context) throws Throwable { - Class accessorImplClass = Class.forName("jdk.internal.reflect.NativeMethodAccessorImpl"); + Class accessorImplClass = Class.forName(retrieveNativeAccessorClassName()); Method invoker = accessorImplClass.getDeclaredMethod("invoke0", Method.class, Object.class, Object[].class); ObjectProvider functionProvider = ObjectProvider.get(context); ConsulterSupplyFunction consulterSupplyFunction = functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); @@ -74,18 +74,21 @@ public ForJava9(Map context) throws Throwable { methodHandle = consulter.unreflect(invoker); } + protected String retrieveNativeAccessorClassName() { + return "jdk.internal.reflect.NativeMethodAccessorImpl"; + } + } - public static class ForJava22 extends Abst { + public static class ForJava22 extends ForJava9 { public ForJava22(Map context) throws Throwable { - Class accessorImplClass = Class.forName("jdk.internal.reflect.DirectMethodHandleAccessor$NativeAccessor"); - Method invoker = accessorImplClass.getDeclaredMethod("invoke0", Method.class, Object.class, Object[].class); - ObjectProvider functionProvider = ObjectProvider.get(context); - ConsulterSupplyFunction consulterSupplyFunction = functionProvider.getOrBuildObject(ConsulterSupplyFunction.class, context); - MethodHandles.Lookup consulter = consulterSupplyFunction.apply(accessorImplClass); - functionProvider.getOrBuildObject(SetAccessibleFunction.class, context).accept(invoker, true); - methodHandle = consulter.unreflect(invoker); + super(context); + } + + @Override + protected String retrieveNativeAccessorClassName() { + return "jdk.internal.reflect.DirectMethodHandleAccessor$NativeAccessor"; } }