mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 23:04:50 +02:00
8285401: Proxy class initializer should use 3-arg Class.forName
to avoid unnecessary class initialization
Reviewed-by: rriggs, mchung
This commit is contained in:
parent
37a513003c
commit
e0382c5523
2 changed files with 76 additions and 4 deletions
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 1999, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 1999, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
|
@ -73,6 +73,7 @@ final class ProxyGenerator extends ClassWriter {
|
|||
private static final String JLR_UNDECLARED_THROWABLE_EX = "java/lang/reflect/UndeclaredThrowableException";
|
||||
|
||||
private static final String LJL_CLASS = "Ljava/lang/Class;";
|
||||
private static final String LJL_CLASSLOADER = "Ljava/lang/ClassLoader;";
|
||||
private static final String LJLR_METHOD = "Ljava/lang/reflect/Method;";
|
||||
private static final String LJLR_INVOCATION_HANDLER = "Ljava/lang/reflect/InvocationHandler;";
|
||||
|
||||
|
@ -597,6 +598,13 @@ final class ProxyGenerator extends ClassWriter {
|
|||
mv.visitTryCatchBlock(L_startBlock, L_endBlock, L_NoClassHandler,
|
||||
JL_CLASS_NOT_FOUND_EX);
|
||||
|
||||
// Put ClassLoader at local variable index 0, used by
|
||||
// Class.forName(String, boolean, ClassLoader) calls
|
||||
mv.visitLdcInsn(Type.getObjectType(dotToSlash(className)));
|
||||
mv.visitMethodInsn(INVOKEVIRTUAL, JL_CLASS,
|
||||
"getClassLoader", "()" + LJL_CLASSLOADER, false);
|
||||
mv.visitVarInsn(ASTORE, 0);
|
||||
|
||||
mv.visitLabel(L_startBlock);
|
||||
for (List<ProxyMethod> sigmethods : proxyMethods.values()) {
|
||||
for (ProxyMethod pm : sigmethods) {
|
||||
|
@ -844,7 +852,8 @@ final class ProxyGenerator extends ClassWriter {
|
|||
|
||||
/**
|
||||
* Generate code for initializing the static field that stores
|
||||
* the Method object for this proxy method.
|
||||
* the Method object for this proxy method. A class loader is
|
||||
* anticipated at local variable index 0.
|
||||
*/
|
||||
private void codeFieldInitialization(MethodVisitor mv, String className) {
|
||||
codeClassForName(mv, fromClass);
|
||||
|
@ -890,13 +899,18 @@ final class ProxyGenerator extends ClassWriter {
|
|||
* Generate code to invoke the Class.forName with the name of the given
|
||||
* class to get its Class object at runtime. The code is written to
|
||||
* the supplied stream. Note that the code generated by this method
|
||||
* may cause the checked ClassNotFoundException to be thrown.
|
||||
* may cause the checked ClassNotFoundException to be thrown. A class
|
||||
* loader is anticipated at local variable index 0.
|
||||
*/
|
||||
private void codeClassForName(MethodVisitor mv, Class<?> cl) {
|
||||
mv.visitLdcInsn(cl.getName());
|
||||
mv.visitInsn(ICONST_0); // false
|
||||
mv.visitVarInsn(ALOAD, 0); // classLoader
|
||||
mv.visitMethodInsn(INVOKESTATIC,
|
||||
JL_CLASS,
|
||||
"forName", "(Ljava/lang/String;)Ljava/lang/Class;", false);
|
||||
"forName",
|
||||
"(Ljava/lang/String;Z" + LJL_CLASSLOADER + ")Ljava/lang/Class;",
|
||||
false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue