use of net.bytebuddy.benchmark.specimen.ExampleInterface in project byte-buddy by raphw.
the class ClassByImplementationBenchmark method benchmarkJavassist.
/**
* Performs a benchmark of an interface implementation using javassist proxies.
*
* @return The created instance, in order to avoid JIT removal.
* @throws java.lang.Exception If the reflective invocation causes an exception.
*/
@Benchmark
public ExampleInterface benchmarkJavassist() throws Exception {
ProxyFactory proxyFactory = new ProxyFactory();
proxyFactory.setUseCache(false);
ProxyFactory.classLoaderProvider = new ProxyFactory.ClassLoaderProvider() {
@Override
public ClassLoader get(ProxyFactory proxyFactory) {
return newClassLoader();
}
};
proxyFactory.setSuperclass(Object.class);
proxyFactory.setInterfaces(new Class<?>[] { baseClass });
proxyFactory.setFilter(new MethodFilter() {
public boolean isHandled(Method method) {
return true;
}
});
@SuppressWarnings("unchecked") Object instance = proxyFactory.createClass().getDeclaredConstructor().newInstance();
((javassist.util.proxy.Proxy) instance).setHandler(new MethodHandler() {
public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
Class<?> returnType = thisMethod.getReturnType();
if (returnType.isPrimitive()) {
if (returnType == boolean.class) {
return defaultBooleanValue;
} else if (returnType == byte.class) {
return defaultByteValue;
} else if (returnType == short.class) {
return defaultShortValue;
} else if (returnType == char.class) {
return defaultCharValue;
} else if (returnType == int.class) {
return defaultIntValue;
} else if (returnType == long.class) {
return defaultLongValue;
} else if (returnType == float.class) {
return defaultFloatValue;
} else {
return defaultDoubleValue;
}
} else {
return defaultReferenceValue;
}
}
});
return (ExampleInterface) instance;
}
use of net.bytebuddy.benchmark.specimen.ExampleInterface in project byte-buddy by raphw.
the class ClassByImplementationBenchmarkTest method testByteBuddyClassCreation.
@Test
public void testByteBuddyClassCreation() throws Exception {
ExampleInterface instance = classByImplementationBenchmark.benchmarkByteBuddy();
assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS));
assertThat(instance.getClass().getSuperclass(), CoreMatchers.<Class<?>>is(Object.class));
assertThat(classByImplementationBenchmark.benchmarkByteBuddy().getClass(), not(CoreMatchers.<Class<?>>is(instance.getClass())));
assertReturnValues(instance);
}
use of net.bytebuddy.benchmark.specimen.ExampleInterface in project byte-buddy by raphw.
the class ClassByImplementationBenchmark method benchmarkCglib.
/**
* Performs a benchmark of an interface implementation using cglib.
*
* @return The created instance, in order to avoid JIT removal.
*/
@Benchmark
public ExampleInterface benchmarkCglib() {
Enhancer enhancer = new Enhancer();
enhancer.setUseCache(false);
enhancer.setClassLoader(newClassLoader());
enhancer.setSuperclass(baseClass);
CallbackHelper callbackHelper = new CallbackHelper(Object.class, new Class[] { baseClass }) {
@Override
protected Object getCallback(Method method) {
if (method.getDeclaringClass() == baseClass) {
return new FixedValue() {
@Override
public Object loadObject() throws Exception {
return null;
}
};
} else {
return NoOp.INSTANCE;
}
}
};
enhancer.setCallbackFilter(callbackHelper);
enhancer.setCallbacks(callbackHelper.getCallbacks());
return (ExampleInterface) enhancer.create();
}
use of net.bytebuddy.benchmark.specimen.ExampleInterface in project byte-buddy by raphw.
the class ClassByImplementationBenchmarkTest method testByteBuddyClassCreationWithTypePool.
@Test
public void testByteBuddyClassCreationWithTypePool() throws Exception {
ExampleInterface instance = classByImplementationBenchmark.benchmarkByteBuddyWithTypePool();
assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS));
assertThat(instance.getClass().getSuperclass(), CoreMatchers.<Class<?>>is(Object.class));
assertThat(classByImplementationBenchmark.benchmarkByteBuddy().getClass(), not(CoreMatchers.<Class<?>>is(instance.getClass())));
assertReturnValues(instance);
}
use of net.bytebuddy.benchmark.specimen.ExampleInterface in project byte-buddy by raphw.
the class ClassByImplementationBenchmarkTest method testJdkProxyClassCreation.
@Test
public void testJdkProxyClassCreation() throws Exception {
ExampleInterface instance = classByImplementationBenchmark.benchmarkJdkProxy();
assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS));
assertThat(instance.getClass().getSuperclass(), CoreMatchers.<Class<?>>is(Proxy.class));
assertThat(classByImplementationBenchmark.benchmarkByteBuddy().getClass(), not(CoreMatchers.<Class<?>>is(instance.getClass())));
assertReturnValues(instance);
}
Aggregations