< prev index next >
src/java.base/share/classes/java/lang/invoke/MethodHandleImpl.java
Print this page
rev 58565 : 8238358: Implementation of JEP 371: Hidden Classes
Reviewed-by: duke
Contributed-by: mandy.chung@oracle.com, lois.foltan@oracle.com, david.holmes@oracle.com, harold.seigel@oracle.com, serguei.spitsyn@oracle.com, alex.buckley@oracle.com, jamsheed.c.m@oracle.com
@@ -25,11 +25,10 @@
package java.lang.invoke;
import jdk.internal.access.JavaLangInvokeAccess;
import jdk.internal.access.SharedSecrets;
-import jdk.internal.org.objectweb.asm.AnnotationVisitor;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
import jdk.internal.vm.annotation.ForceInline;
@@ -38,10 +37,11 @@
import sun.invoke.empty.Empty;
import sun.invoke.util.ValueConversions;
import sun.invoke.util.VerifyType;
import sun.invoke.util.Wrapper;
+import java.lang.invoke.MethodHandles.Lookup;
import java.lang.reflect.Array;
import java.nio.ByteOrder;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -1155,14 +1155,28 @@
// Cache the result of makeInjectedInvoker once per argument class.
MethodHandle bccInvoker = CV_makeInjectedInvoker.get(hostClass);
return restoreToType(bccInvoker.bindTo(vamh), mh, hostClass);
}
- private static MethodHandle makeInjectedInvoker(Class<?> hostClass) {
+ private static MethodHandle makeInjectedInvoker(Class<?> targetClass) {
try {
- Class<?> invokerClass = UNSAFE.defineAnonymousClass(hostClass, INJECTED_INVOKER_TEMPLATE, null);
- assert checkInjectedInvoker(hostClass, invokerClass);
+ /*
+ * The invoker class defined to the same class loader as the lookup class
+ * but in an unnamed package so that the class bytes can be cached and
+ * reused for any @CSM.
+ *
+ * @CSM must be public and exported if called by any module.
+ */
+ String name = targetClass.getName() + "$$InjectedInvoker";
+ if (targetClass.isHiddenClass()) {
+ // use the original class name
+ name = name.replace('/', '_');
+ }
+ Class<?> invokerClass = new Lookup(targetClass)
+ .makeHiddenClassDefiner(name, INJECTED_INVOKER_TEMPLATE)
+ .defineClass(true);
+ assert checkInjectedInvoker(targetClass, invokerClass);
return IMPL_LOOKUP.findStatic(invokerClass, "invoke_V", INVOKER_MT);
} catch (ReflectiveOperationException ex) {
throw uncaughtException(ex);
}
}
@@ -1254,14 +1268,10 @@
MethodVisitor mv = cw.visitMethod(ACC_STATIC, "invoke_V",
"(Ljava/lang/invoke/MethodHandle;[Ljava/lang/Object;)Ljava/lang/Object;",
null, null);
- // Suppress invoker method in stack traces.
- AnnotationVisitor av0 = mv.visitAnnotation(InvokerBytecodeGenerator.HIDDEN_SIG, true);
- av0.visitEnd();
-
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact",
"([Ljava/lang/Object;)Ljava/lang/Object;", false);
< prev index next >