@Test public void testJavassistClassCreation() throws Exception { ExampleInterface instance = classByImplementationBenchmark.benchmarkJavassist(); assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS)); assertThat(instance.getClass().getSuperclass(), CoreMatchers.<Class<?>>is(Object.class)); assertThat(classByImplementationBenchmark.benchmarkJavassist().getClass(), not(CoreMatchers.<Class<?>>is(instance.getClass()))); assertReturnValues(instance); }
@Benchmark public ExampleInterface benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); 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; }
ClassByImplementationBenchmark { @Benchmark public ExampleInterface benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); 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; } }
ClassByImplementationBenchmark { @Benchmark public ExampleInterface benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); 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; }  }
ClassByImplementationBenchmark { @Benchmark public ExampleInterface benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); 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; }  @Setup void setup(); @Benchmark ExampleInterface baseline(); @Benchmark ExampleInterface benchmarkByteBuddy(); @Benchmark ExampleInterface benchmarkByteBuddyWithTypePool(); @Benchmark ExampleInterface benchmarkCglib(); @Benchmark ExampleInterface benchmarkJavassist(); @Benchmark ExampleInterface benchmarkJdkProxy(); }
ClassByImplementationBenchmark { @Benchmark public ExampleInterface benchmarkJavassist() throws Exception { ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return newClassLoader(); } }; proxyFactory.setUseCache(false); proxyFactory.setUseWriteReplace(false); 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; }  @Setup void setup(); @Benchmark ExampleInterface baseline(); @Benchmark ExampleInterface benchmarkByteBuddy(); @Benchmark ExampleInterface benchmarkByteBuddyWithTypePool(); @Benchmark ExampleInterface benchmarkCglib(); @Benchmark ExampleInterface benchmarkJavassist(); @Benchmark ExampleInterface benchmarkJdkProxy(); static final Class<? extends ExampleInterface> BASE_CLASS; static final String DEFAULT_REFERENCE_VALUE; static final boolean DEFAULT_BOOLEAN_VALUE; static final byte DEFAULT_BYTE_VALUE; static final short DEFAULT_SHORT_VALUE; static final char DEFAULT_CHAR_VALUE; static final int DEFAULT_INT_VALUE; static final long DEFAULT_LONG_VALUE; static final float DEFAULT_FLOAT_VALUE; static final double DEFAULT_DOUBLE_VALUE; }