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
src/java.base/share/classes/java/lang/reflect
test/jdk/java/lang/reflect/Proxy
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
58
test/jdk/java/lang/reflect/Proxy/LazyInitializationTest.java
Normal file
58
test/jdk/java/lang/reflect/Proxy/LazyInitializationTest.java
Normal file
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Copyright (c) 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
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8285401
|
||||
* @summary Avoid initialization of parameter types in proxy construction
|
||||
* @run testng LazyInitializationTest
|
||||
*/
|
||||
public final class LazyInitializationTest {
|
||||
private static volatile boolean initialized = false;
|
||||
|
||||
interface Intf {
|
||||
void m(Parameter parameter);
|
||||
}
|
||||
|
||||
static class Parameter {
|
||||
static {
|
||||
initialized = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLazyInitialization() {
|
||||
Intf value = (Intf) Proxy.newProxyInstance(LazyInitializationTest.class.getClassLoader(),
|
||||
new Class<?>[]{ Intf.class },
|
||||
(proxy, method, args) -> null);
|
||||
Assert.assertFalse(initialized, "parameter type initialized unnecessarily");
|
||||
|
||||
value.m(new Parameter());
|
||||
Assert.assertTrue(initialized, "parameter type initialized after instantiation");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user