8207814: (proxy) upgrade the proxy class generator

Reviewed-by: mchung
This commit is contained in:
Roger Riggs 2019-08-21 16:19:17 -04:00
parent 5c25e2a6fb
commit de8d01d4d3
7 changed files with 3576 additions and 1783 deletions

View File

@ -420,17 +420,16 @@ public final class Method extends Executable {
@Override
String toShortString() {
StringBuilder sb = new StringBuilder("method ");
sb.append(getDeclaringClass().getTypeName()).append('.');
sb.append(getName());
sb.append('(');
StringJoiner sj = new StringJoiner(",");
return "method " + getDeclaringClass().getTypeName() +
'.' + toShortSignature();
}
String toShortSignature() {
StringJoiner sj = new StringJoiner(",", getName() + "(", ")");
for (Class<?> parameterType : getParameterTypes()) {
sj.add(parameterType.getTypeName());
}
sb.append(sj);
sb.append(')');
return sb.toString();
return sj.toString();
}
/**

View File

@ -49,6 +49,7 @@ import jdk.internal.reflect.CallerSensitive;
import jdk.internal.reflect.Reflection;
import jdk.internal.loader.ClassLoaderValue;
import sun.reflect.misc.ReflectUtil;
import sun.security.action.GetBooleanAction;
import sun.security.action.GetPropertyAction;
import sun.security.util.SecurityConstants;
@ -99,7 +100,7 @@ import static java.lang.module.ModuleDescriptor.Modifier.SYNTHETIC;
* <li>A proxy class extends {@code java.lang.reflect.Proxy}.
*
* <li>A proxy class implements exactly the interfaces specified at its
* creation, in the same order. Invoking {@link Class#getInterfaces getInterfaces}
* creation, in the same order. Invoking {@link Class#getInterfaces() getInterfaces}
* on its {@code Class} object will return an array containing the same
* list of interfaces (in the order specified at its creation), invoking
* {@link Class#getMethods getMethods} on its {@code Class} object will return
@ -295,6 +296,13 @@ public class Proxy implements java.io.Serializable {
private static final ClassLoaderValue<Constructor<?>> proxyCache =
new ClassLoaderValue<>();
/**
* System property to revert to generation of proxy class files for version 1.5 (V49).
* Set to "true" to generate v49 class file format.
*/
private static final boolean PROXY_GENERATOR_V49 =
GetBooleanAction.privilegedGetProperty("jdk.proxy.ProxyGenerator.v49");
/**
* the invocation handler for this proxy instance.
* @serial
@ -531,8 +539,9 @@ public class Proxy implements java.io.Serializable {
/*
* Generate the specified proxy class.
*/
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces.toArray(EMPTY_CLASS_ARRAY), accessFlags);
byte[] proxyClassFile = PROXY_GENERATOR_V49
? ProxyGenerator_v49.generateProxyClass(proxyName, interfaces, accessFlags)
: ProxyGenerator.generateProxyClass(loader, proxyName, interfaces, accessFlags);
try {
Class<?> pc = JLA.defineClass(loader, proxyName, proxyClassFile,
null, "__dynamic_proxy__");
@ -1116,6 +1125,5 @@ public class Proxy implements java.io.Serializable {
return ih;
}
private static final Class<?>[] EMPTY_CLASS_ARRAY = new Class<?>[0];
private static final String PROXY_PACKAGE_PREFIX = ReflectUtil.PROXY_PACKAGE;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,568 @@
/*
* Copyright (c) 2011, 2019, 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.
*/
/*
* @test
* @summary Proxy Generator Combo tests
* @library /test/langtools/tools/javac/lib .
* @modules jdk.compiler/com.sun.tools.javac.api
* jdk.compiler/com.sun.tools.javac.code
* jdk.compiler/com.sun.tools.javac.comp
* jdk.compiler/com.sun.tools.javac.main
* jdk.compiler/com.sun.tools.javac.tree
* jdk.compiler/com.sun.tools.javac.util
* @build combo.ComboTestHelper
* @run main/othervm ProxyGeneratorCombo
* @run main/othervm -Djdk.proxy.ProxyGenerator.v49=true ProxyGeneratorCombo
*/
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
import combo.ComboInstance;
import combo.ComboParameter;
import combo.ComboTask.Result;
import combo.ComboTestHelper;
public class ProxyGeneratorCombo extends ComboInstance<ProxyGeneratorCombo> {
// The unique number to qualify interface names, unique across multiple runs
private static int uniqueId = 0;
/**
* Class Access kinds.
*/
enum ClassAccessKind implements ComboParameter {
PUBLIC("public"),
PACKAGE(""),
;
String classAccessTemplate;
ClassAccessKind(String classAccessTemplate) {
this.classAccessTemplate = classAccessTemplate;
}
@Override
public String expand(String optParameter) {
return classAccessTemplate;
}
}
/**
* Signatures of methods to be tested.
*/
enum MethodsKind implements ComboParameter {
NONE(""),
ZERO("#{METHODACCESS} void zero() #{EXCEPTION};"),
ONE("#{METHODACCESS} void one(#{ARG[0]} a) #{EXCEPTION};"),
TWO("#{METHODACCESS} void one(#{ARG[0]} b);\n" +
"#{METHODACCESS} void two(#{ARG[0]} a, #{ARG[1]} b);"),
THREE("#{METHODACCESS} void one(#{ARG[0]} a);\n" +
"#{METHODACCESS} void two(#{ARG[0]} a, #{ARG[1]} b);\n" +
"#{METHODACCESS} void three(#{ARG[0]} a, #{ARG[1]} b, #{ARG[0]} c);");
String methodsKindTemplate;
MethodsKind(String methodsKindTemplate) {
this.methodsKindTemplate = methodsKindTemplate;
}
@Override
public String expand(String optParameter) {
return methodsKindTemplate;
}
}
/**
* Type of arguments to insert in method signatures
*/
enum ArgumentKind implements ComboParameter {
BOOLEAN("boolean"),
BYTE("byte"),
CHAR("char"),
SHORT("short"),
INT("int"),
LONG("long"),
FLOAT("float"),
DOUBLE("double"),
STRING("String");
String argumentsKindTemplate;
ArgumentKind(String argumentsKindTemplate) {
this.argumentsKindTemplate = argumentsKindTemplate;
}
@Override
public String expand(String optParameter) {
return argumentsKindTemplate;
}
}
/**
* Exceptions to be added to zero and one methods.
*/
enum ExceptionKind implements ComboParameter {
NONE(null),
EXCEPTION(java.lang.Exception.class),
RUNTIME_EXCEPTION(java.lang.RuntimeException.class),
ILLEGAL_ARGUMENT_EXCEPTION(java.lang.IllegalArgumentException.class),
IOEXCEPTION(java.io.IOException.class),
/**
* Used only for throw testing, is empty for throws clause in the source,
*/
UNDECLARED_EXCEPTION(Exception1.class),
;
Class<? extends Throwable> exceptionKindClass;
ExceptionKind(Class<? extends Throwable> exceptionKindClass) {
this.exceptionKindClass = exceptionKindClass;
}
@Override
public String expand(String optParameter) {
return exceptionKindClass == null || exceptionKindClass == Exception1.class
? "" : "throws " + exceptionKindClass.getName();
}
}
/**
* Extra interfaces to be added.
*/
enum MultiInterfacesKind implements ComboParameter {
NONE(new Class<?>[0]),
INTERFACE_WITH_EXCEPTION(new Class<?>[] {InterfaceWithException.class}),
;
Class<?>[] multiInterfaceClasses;
MultiInterfacesKind(Class<?>[] multiInterfaceClasses) {
this.multiInterfaceClasses = multiInterfaceClasses;
}
@Override
// Not used for expansion only execution
public String expand(String optParameter) {
throw new RuntimeException("NYI");
}
Class<?>[] classes() {
return multiInterfaceClasses;
}
}
@Override
public int id() {
return ++uniqueId;
}
protected void fail(String msg, Throwable thrown) {
super.fail(msg);
thrown.printStackTrace();
}
/**
* Test interface with a "one(int)" method.
*/
interface InterfaceWithException {
// The signature must match the ONE MethodsKind above
void one(int a) throws RuntimeException, IOException;
}
/**
* Main to generate combinations and run the tests.
* @param args unused
* @throws Exception In case of failure
*/
public static void main(String... args) throws Exception {
// Test variations of access declarations
new ComboTestHelper<ProxyGeneratorCombo>()
.withDimension("CLASSACCESS", ClassAccessKind.values())
.withDimension("METHODACCESS", new ClassAccessKind[]{ClassAccessKind.PUBLIC})
.withDimension("METHODS", ProxyGeneratorCombo::saveMethod,
new MethodsKind[] {MethodsKind.NONE, MethodsKind.ZERO, MethodsKind.ONE})
.withDimension("ARG[0]", new ArgumentKind[] {ArgumentKind.INT})
.withDimension("EXCEPTION", ProxyGeneratorCombo::saveException,
new ExceptionKind[]{ExceptionKind.NONE})
.run(ProxyGeneratorCombo::new);
// Test variations of argument types
new ComboTestHelper<ProxyGeneratorCombo>()
.withDimension("CLASSACCESS", new ClassAccessKind[]{ClassAccessKind.PUBLIC})
.withDimension("METHODACCESS", new ClassAccessKind[]{ClassAccessKind.PUBLIC})
.withDimension("METHODS", ProxyGeneratorCombo::saveMethod,
MethodsKind.values())
.withArrayDimension("ARG", ProxyGeneratorCombo::saveArg, 2,
ArgumentKind.values())
.withDimension("EXCEPTION", ProxyGeneratorCombo::saveException,
new ExceptionKind[]{ExceptionKind.NONE})
.withFilter(ProxyGeneratorCombo::filter)
.run(ProxyGeneratorCombo::new);
// Test for conflicts in Exceptions on methods with the same signatures
new ComboTestHelper<ProxyGeneratorCombo>()
.withDimension("CLASSACCESS", new ClassAccessKind[]{ClassAccessKind.PUBLIC})
.withDimension("METHODACCESS", new ClassAccessKind[]{ClassAccessKind.PUBLIC})
.withDimension("METHODS", ProxyGeneratorCombo::saveMethod, new MethodsKind[] {
MethodsKind.ZERO})
.withDimension("EXCEPTION", ProxyGeneratorCombo::saveException,
ExceptionKind.values())
.withDimension("MULTI_INTERFACES", ProxyGeneratorCombo::saveInterface,
new MultiInterfacesKind[] {MultiInterfacesKind.NONE})
.run(ProxyGeneratorCombo::new);
}
/**
* Basic template.
*/
String template = "#{CLASSACCESS} interface #{TESTNAME} {\n" +
"#{METHODS}" +
"}";
// Saved values of Combo values
private MultiInterfacesKind currInterface = MultiInterfacesKind.NONE;
private MethodsKind currMethod = MethodsKind.NONE;
private ExceptionKind currException = ExceptionKind.NONE;
private ArgumentKind[] currArgs = new ArgumentKind[0];
void saveInterface(ComboParameter s) {
currInterface = (MultiInterfacesKind)s;
}
void saveMethod(ComboParameter s) {
currMethod = (MethodsKind)s;
}
void saveException(ComboParameter s) {
currException = (ExceptionKind)s;
}
void saveArg(ComboParameter s, int index) {
if (index >= currArgs.length) {
currArgs = Arrays.copyOf(currArgs, index + 1);
}
currArgs[index] = (ArgumentKind)s;
}
/**
* Filter out needless tests (mostly with more variations of arguments than needed).
* @return true to run the test, false if not
*/
boolean filter() {
if ((currMethod == MethodsKind.NONE || currMethod == MethodsKind.ZERO) &&
currArgs.length >= 2) {
return currArgs[0] == ArgumentKind.INT &&
currArgs[1] == ArgumentKind.INT;
}
if (currMethod == MethodsKind.ONE &&
currArgs.length >= 2 ) {
return currArgs[0] == currArgs[1];
}
return true;
}
/**
* Generate the source file and compile.
* Generate a proxy for the interface and test the resulting Proxy
* for the methods, exceptions and handling of a thrown exception
* @throws IOException catch all IOException
*/
@Override
public void doWork() throws IOException {
String cp = System.getProperty("test.classes");
String ifaceName = "Interface_" + this.id();
newCompilationTask()
.withSourceFromTemplate(ifaceName, template.replace("#{TESTNAME}", ifaceName))
.withOption("-d")
.withOption(cp)
.generate(this::checkCompile);
try {
ClassLoader loader = ClassLoader.getSystemClassLoader();
Class<?> tc = Class.forName(ifaceName);
InvocationHandler handler =
new ProxyHandler(currException.exceptionKindClass);
// Construct array of interfaces for the proxy
Class<?>[] interfaces = new Class<?>[currInterface.classes().length + 1];
interfaces[0] = tc;
System.arraycopy(currInterface.classes(), 0,
interfaces, 1,
currInterface.classes().length);
Object proxy = Proxy.newProxyInstance(loader, interfaces, handler);
if (!Proxy.isProxyClass(proxy.getClass())) {
fail("generated proxy is not a proxy class");
return;
}
for (Class<?> i : interfaces) {
if (!i.isAssignableFrom(proxy.getClass())) {
fail("proxy is not assignable to " + i.getName());
}
}
try {
String s = proxy.toString();
} catch (Exception ex) {
ex.printStackTrace();
fail("proxy.toString() threw an exception");
}
checkDeclaredProxyExceptions(proxy, interfaces);
if (currMethod == MethodsKind.ZERO && currException != ExceptionKind.NONE) {
checkThrowsException(proxy, interfaces);
}
} catch (Exception ex) {
throw new RuntimeException("doWork unexpected", ex);
}
}
/**
* Check that the exceptions declared on the proxy match the declarations for
* exceptions from the interfaces.
*
* @param proxy a proxy object
* @param interfaces the interfaces that defined it
*/
void checkDeclaredProxyExceptions(Object proxy, Class<?>[] interfaces) {
var allMethods = allMethods(Arrays.asList(interfaces));
Method[] methods = proxy.getClass().getDeclaredMethods();
for (Method m : methods) {
String sig = toShortSignature(m);
var imethods = allMethods.get(sig);
if (imethods != null) {
var expectedEx = Set.copyOf(Arrays.asList(m.getExceptionTypes()));
var exs = Set.copyOf(extractExceptions(imethods));
if (!expectedEx.equals(exs)) {
System.out.printf("mismatch on exceptions for method %s:%nExpected: " +
"%s%nActual: %s%n",
sig, expectedEx, exs);
fail("Exceptions declared on proxy don't match interface methods");
}
}
}
}
void checkThrowsException(Object proxy, Class<?>[] interfaces) {
ProxyHandler ph = (ProxyHandler)(Proxy.getInvocationHandler(proxy));
try {
Method m = proxy.getClass().getDeclaredMethod("zero");
m.invoke(proxy);
fail("Missing exception: " + ph.exceptionClass);
} catch (NoSuchMethodException nsme) {
System.out.printf("No method 'zero()' to test exceptions with%n");
for (var cl : interfaces) {
System.out.printf(" i/f %s: %s%n", cl, Arrays.toString(cl.getMethods()));
}
Method[] methods = proxy.getClass().getMethods();
System.out.printf(" Proxy methods: %s%n", Arrays.toString(methods));
fail("No such method test bug", nsme);
} catch (InvocationTargetException actual) {
ph.checkThrownException(actual.getTargetException());
} catch (IllegalAccessException iae) {
fail("IllegalAccessException", iae);
}
}
/**
* Exceptions known to be supported by all methods with the same signature.
* @return a list of universal exception types
*/
private static List<Class<?>> extractExceptions(List<Method> methods) {
// for all methods with the same signature
// start with the exceptions from the first method
// while there are any exceptions remaining
// look at the next method
List<Class<?>> exceptions = null;
for (Method m : methods) {
var e = m.getExceptionTypes();
if (e.length == 0)
return emptyClassList();
List<Class<?>> elist = Arrays.asList(e);
if (exceptions == null) {
exceptions = elist; // initialize to first method exceptions
} else {
// for each exception
// if it is compatible (both ways) with any of the existing exceptions continue
// else remove the current exception
var okExceptions = new HashSet<Class<?>>();
for (int j = 0; j < exceptions.size(); j++) {
var ex = exceptions.get(j);
for (int i = 0; i < elist.size();i++) {
var ci = elist.get(i);
if (ci.isAssignableFrom(ex)) {
okExceptions.add(ex);
}
if (ex.isAssignableFrom(ci)) {
okExceptions.add(ci);
}
}
}
if (exceptions.isEmpty()) {
// The empty set terminates the search for a common set of exceptions
return emptyClassList();
}
// Use the new set for the next iteration
exceptions = List.copyOf(okExceptions);
}
}
return (exceptions == null) ? emptyClassList() : exceptions;
}
/**
* An empty correctly typed list of classes.
* @return An empty typed list of classes
*/
@SuppressWarnings("unchecked")
static List<Class<?>> emptyClassList() {
return Collections.EMPTY_LIST;
}
/**
* Accumulate all of the unique methods.
*
* @param interfaces a list of interfaces
* @return a map from signature to List of methods, unique by signature
*/
private static Map<String, List<Method>> allMethods(List<Class<?>> interfaces) {
Map<String, List<Method>> methods = new HashMap<>();
for (Class<?> c : interfaces) {
for (Method m : c.getMethods()) {
if (!Modifier.isStatic(m.getModifiers())) {
String sig = toShortSignature(m);
methods.computeIfAbsent(sig, s -> new ArrayList<Method>())
.add(m);
}
}
}
return methods;
}
/**
* The signature of a method without the return type.
* @param m a Method
* @return the signature with method name and parameters
*/
static String toShortSignature(Method m) {
StringJoiner sj = new StringJoiner(",", m.getName() + "(", ")");
for (Class<?> parameterType : m.getParameterTypes()) {
sj.add(parameterType.getTypeName());
}
return sj.toString();
}
/**
* Report any compilation errors.
* @param res the result
*/
void checkCompile(Result<?> res) {
if (res.hasErrors()) {
fail("invalid diagnostics for source:\n" +
res.compilationInfo() +
"\nFound error: " + res.hasErrors());
}
}
/**
* The Handler for the proxy includes the method to invoke the proxy
* and the expected exception, if any.
*/
class ProxyHandler implements InvocationHandler {
private final Class<? extends Throwable> exceptionClass;
ProxyHandler(Class<? extends Throwable> throwable) {
this.exceptionClass = throwable;
}
/**
* Invoke a method on the proxy or return a value.
* @param proxy the proxy instance that the method was invoked on
* @param method a method
* @param args some args
* @return
* @throws Throwable a throwable
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("toString")) {
return "Proxy" + System.identityHashCode(proxy);
}
if (method.getName().equals("zero")) {
if (exceptionClass != null) {
throw exceptionClass.getDeclaredConstructor().newInstance();
}
}
return "meth: " + method.toString();
}
/**
* Check that the expected exception was thrown.
* Special case is handled for Exception1 which does not appear in the
* throws clause of the method so UndeclaredThrowableException is expected.
*/
void checkThrownException(Throwable thrown) {
if (exceptionClass == Exception1.class &&
thrown instanceof UndeclaredThrowableException &&
((UndeclaredThrowableException)thrown).getUndeclaredThrowable() instanceof Exception1) {
// Exception1 caused UndeclaredThrowableException
return;
} else if (exceptionClass == Exception1.class) {
fail("UndeclaredThrowableException", thrown);
}
if (exceptionClass != null &&
!exceptionClass.equals(thrown.getClass())) {
throw new RuntimeException("Wrong exception thrown: expected: " + exceptionClass +
", actual: " + thrown.getClass());
}
}
}
/**
* Exception to be thrown as a test of InvocationTarget.
*/
static class Exception1 extends Exception {
private static final long serialVersionUID = 1L;
Exception1() {}
}
}

View File

@ -0,0 +1,93 @@
/*
* Copyright (c) 2014, 2019, 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.
*/
package org.openjdk.bench.java.lang.reflect.proxy;
import org.openjdk.jmh.annotations.*;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
@Warmup(iterations = 5)
@Measurement(iterations = 10)
@Fork(value = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class ProxyBench {
/**
* On Dell T7610:
*
* Benchmark w/ -Djdk.proxy.ProxyGenerator.v49=true
* Benchmark Mode Cnt Score Error Units
* ProxyBench.getProxyClass1i avgt 10 20.472 +/- 0.209 ns/op
* ProxyBench.getProxyClass4i avgt 10 57.353 +/- 0.461 ns/op
* ProxyBench.newProxyInstance1i avgt 10 31.459 +/- 0.516 ns/op
* ProxyBench.newProxyInstance4i avgt 10 66.580 +/- 0.983 ns/op
*
* Benchmark Mode Cnt Score Error Units
* ProxyBench.getProxyClass1i avgt 10 21.291 +/- 0.475 ns/op
* ProxyBench.getProxyClass4i avgt 10 61.481 +/- 4.709 ns/op
* ProxyBench.newProxyInstance1i avgt 10 30.177 +/- 0.761 ns/op
* ProxyBench.newProxyInstance4i avgt 10 68.302 +/- 1.344 ns/op
*/
interface PkgPrivate1 {
void m1();
}
interface PkgPrivate2 {
void m2();
}
static final InvocationHandler handler = (proxy, method, args) -> null;
static final ClassLoader loader1 = null;
static final Class<?>[] interfaces1 = {Runnable.class};
static final ClassLoader loader4 = PkgPrivate1.class.getClassLoader();
static final Class<?>[] interfaces4 = {Runnable.class, Callable.class,
PkgPrivate1.class, PkgPrivate2.class};
@Benchmark
public Class<?> getProxyClass1i() {
return Proxy.getProxyClass(loader1, interfaces1);
}
@Benchmark
public Class<?> getProxyClass4i() {
return Proxy.getProxyClass(loader4, interfaces4);
}
@Benchmark
public Object newProxyInstance1i() {
return Proxy.newProxyInstance(loader1, interfaces1, handler);
}
@Benchmark
public Object newProxyInstance4i() {
return Proxy.newProxyInstance(loader4, interfaces4, handler);
}
}

View File

@ -0,0 +1,162 @@
/*
* Copyright (c) 2014, 2019, 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.
*/
package org.openjdk.bench.java.lang.reflect.proxy;
import java.util.List;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.CompilerControl;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.concurrent.TimeUnit;
/**
* Benchmark measuring java.lang.reflect.ProxyGenerator.generateProxyClass.
* It bypasses the cache of proxies to measure the time to construct a proxy.
*/
@Warmup(iterations = 5)
@Measurement(iterations = 10)
@Fork(value = 1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class ProxyPerf {
/**
* Sample results from a Dell T7610.
* Benchmark Mode Cnt Score Error Units
* ProxyPerf.genIntf_1 avgt 10 35325.428 +/- 780.459 ns/op
* ProxyPerf.genIntf_1_V49 avgt 10 34309.423 +/- 727.188 ns/op
* ProxyPerf.genStringsIntf_3 avgt 10 46600.366 +/- 663.812 ns/op
* ProxyPerf.genStringsIntf_3_V49 avgt 10 45911.817 +/- 1598.536 ns/op
* ProxyPerf.genZeroParams avgt 10 33245.048 +/- 437.988 ns/op
* ProxyPerf.genZeroParams_V49 avgt 10 32954.254 +/- 1041.932 ns/op
* ProxyPerf.getPrimsIntf_2 avgt 10 43987.819 +/- 837.443 ns/op
* ProxyPerf.getPrimsIntf_2_V49 avgt 10 42863.462 +/- 1193.480 ns/op
*/
public interface Intf_1 {
public Object mL(Object o);
}
public interface Intf_2 {
public int m1I(int i);
public long m2IJ(int i, long l);
}
public interface Intf_3 {
public void mString(String s1);
public String m2String(String s1);
public String m2String(String s1, String s2);
}
private InvocationHandler handler;
private ClassLoader classloader;
private Method proxyGen;
private Method proxyGenV49;
@Setup
public void setup() {
try {
handler = (Object proxy, Method method, Object[] args) -> null;
classloader = ClassLoader.getSystemClassLoader();
Class<?> proxyGenClass = Class.forName("java.lang.reflect.ProxyGenerator");
proxyGen = proxyGenClass.getDeclaredMethod("generateProxyClass",
ClassLoader.class, String.class, java.util.List.class, int.class);
proxyGen.setAccessible(true);
// Init access to the old Proxy generator
Class<?> proxyGenClassV49 = Class.forName("java.lang.reflect.ProxyGenerator_v49");
proxyGenV49 = proxyGenClassV49.getDeclaredMethod("generateProxyClass",
String.class, java.util.List.class, int.class);
proxyGenV49.setAccessible(true);
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException("ProxyClass setup fails", ex);
}
}
@Benchmark
public void genZeroParams(Blackhole bh) throws Exception {
List<Class<?>> interfaces = List.of(Runnable.class);
bh.consume(proxyGen.invoke(null, classloader, "ProxyImpl", interfaces, 1));
}
@Benchmark
public void genIntf_1(Blackhole bh) throws Exception {
List<Class<?>> interfaces = List.of(Intf_1.class);
bh.consume(proxyGen.invoke(null, classloader, "ProxyImpl", interfaces, 1));
}
@Benchmark
public void getPrimsIntf_2(Blackhole bh) throws Exception {
List<Class<?>> interfaces = List.of(Intf_2.class);
bh.consume(proxyGen.invoke(null, classloader, "ProxyImpl", interfaces, 1));
}
@Benchmark
public void genStringsIntf_3(Blackhole bh) throws Exception {
List<Class<?>> interfaces = List.of(Intf_3.class);
bh.consume(proxyGen.invoke(null, classloader, "ProxyImpl", interfaces, 1));
}
// Generate using the V49inal generator for comparison
@Benchmark
public void genZeroParams_V49(Blackhole bh) throws Exception {
List<Class<?>> interfaces = List.of(Runnable.class);
bh.consume(proxyGenV49.invoke(null, "ProxyImpl", interfaces, 1));
}
@Benchmark
public void genIntf_1_V49(Blackhole bh) throws Exception {
List<Class<?>> interfaces = List.of(Intf_1.class);
bh.consume(proxyGenV49.invoke(null, "ProxyImpl", interfaces, 1));
}
@Benchmark
public void getPrimsIntf_2_V49(Blackhole bh) throws Exception {
List<Class<?>> interfaces = List.of(Intf_2.class);
bh.consume(proxyGenV49.invoke(null, "ProxyImpl", interfaces, 1));
}
@Benchmark
public void genStringsIntf_3_V49(Blackhole bh) throws Exception {
List<Class<?>> interfaces = List.of(Intf_3.class);
bh.consume(proxyGenV49.invoke(null, "ProxyImpl", interfaces, 1));
}
}