@Test public void testJavassistClassCreation() throws Exception { ExampleClass instance = classByExtensionBenchmark.benchmarkJavassist(); assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS))); assertThat(instance.getClass().getSuperclass(), CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)); assertThat(classByExtensionBenchmark.benchmarkJavassist().getClass(), not(CoreMatchers.<Class<?>>is(instance.getClass()))); assertReturnValues(instance); }
@Benchmark public ExampleClass benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); proxyFactory.setSuperclass(baseClass); proxyFactory.setFilter(new MethodFilter() { public boolean isHandled(Method method) { return method.getDeclaringClass() == baseClass; } }); @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 { return proceed.invoke(self, args); } }); return (ExampleClass) instance; }
ClassByExtensionBenchmark { @Benchmark public ExampleClass benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); proxyFactory.setSuperclass(baseClass); proxyFactory.setFilter(new MethodFilter() { public boolean isHandled(Method method) { return method.getDeclaringClass() == baseClass; } }); @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 { return proceed.invoke(self, args); } }); return (ExampleClass) instance; } }
ClassByExtensionBenchmark { @Benchmark public ExampleClass benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); proxyFactory.setSuperclass(baseClass); proxyFactory.setFilter(new MethodFilter() { public boolean isHandled(Method method) { return method.getDeclaringClass() == baseClass; } }); @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 { return proceed.invoke(self, args); } }); return (ExampleClass) instance; }  }
ClassByExtensionBenchmark { @Benchmark public ExampleClass benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); proxyFactory.setSuperclass(baseClass); proxyFactory.setFilter(new MethodFilter() { public boolean isHandled(Method method) { return method.getDeclaringClass() == baseClass; } }); @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 { return proceed.invoke(self, args); } }); return (ExampleClass) instance; }  @Setup void setup(); @Benchmark ExampleClass baseline(); @Benchmark ExampleClass benchmarkByteBuddyWithProxy(); @Benchmark ExampleClass benchmarkByteBuddyWithProxyAndReusedDelegator(); @Benchmark ExampleClass benchmarkByteBuddyWithProxyWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithProxyAndReusedDelegatorWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithAccessor(); @Benchmark ExampleClass benchmarkByteBuddyWithAccessorAndReusedDelegator(); @Benchmark ExampleClass benchmarkByteBuddyWithAccessorWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithAccessorAndReusedDelegatorWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithPrefix(); @Benchmark ExampleClass benchmarkByteBuddyWithPrefixAndReusedDelegator(); @Benchmark ExampleClass benchmarkByteBuddyWithPrefixWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithPrefixAndReusedDelegatorWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddySpecialized(); @Benchmark ExampleClass benchmarkCglib(); @Benchmark ExampleClass benchmarkJavassist(); }
ClassByExtensionBenchmark { @Benchmark public ExampleClass benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); proxyFactory.setSuperclass(baseClass); proxyFactory.setFilter(new MethodFilter() { public boolean isHandled(Method method) { return method.getDeclaringClass() == baseClass; } }); @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 { return proceed.invoke(self, args); } }); return (ExampleClass) instance; }  @Setup void setup(); @Benchmark ExampleClass baseline(); @Benchmark ExampleClass benchmarkByteBuddyWithProxy(); @Benchmark ExampleClass benchmarkByteBuddyWithProxyAndReusedDelegator(); @Benchmark ExampleClass benchmarkByteBuddyWithProxyWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithProxyAndReusedDelegatorWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithAccessor(); @Benchmark ExampleClass benchmarkByteBuddyWithAccessorAndReusedDelegator(); @Benchmark ExampleClass benchmarkByteBuddyWithAccessorWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithAccessorAndReusedDelegatorWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithPrefix(); @Benchmark ExampleClass benchmarkByteBuddyWithPrefixAndReusedDelegator(); @Benchmark ExampleClass benchmarkByteBuddyWithPrefixWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddyWithPrefixAndReusedDelegatorWithTypePool(); @Benchmark ExampleClass benchmarkByteBuddySpecialized(); @Benchmark ExampleClass benchmarkCglib(); @Benchmark ExampleClass benchmarkJavassist(); static final Class<? extends ExampleClass> BASE_CLASS; }