Search in sources :

Example 1 with ExampleInterface

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;
}
Also used : ProxyFactory(javassist.util.proxy.ProxyFactory) MethodFilter(javassist.util.proxy.MethodFilter) StubMethod(net.bytebuddy.implementation.StubMethod) Method(java.lang.reflect.Method) Proxy(java.lang.reflect.Proxy) MethodHandler(javassist.util.proxy.MethodHandler) URLClassLoader(java.net.URLClassLoader) ExampleInterface(net.bytebuddy.benchmark.specimen.ExampleInterface)

Example 2 with ExampleInterface

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);
}
Also used : ExampleInterface(net.bytebuddy.benchmark.specimen.ExampleInterface) Test(org.junit.Test)

Example 3 with ExampleInterface

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();
}
Also used : Enhancer(net.sf.cglib.proxy.Enhancer) CallbackHelper(net.sf.cglib.proxy.CallbackHelper) FixedValue(net.sf.cglib.proxy.FixedValue) StubMethod(net.bytebuddy.implementation.StubMethod) Method(java.lang.reflect.Method) ExampleInterface(net.bytebuddy.benchmark.specimen.ExampleInterface)

Example 4 with ExampleInterface

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);
}
Also used : ExampleInterface(net.bytebuddy.benchmark.specimen.ExampleInterface) Test(org.junit.Test)

Example 5 with ExampleInterface

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);
}
Also used : Proxy(java.lang.reflect.Proxy) ExampleInterface(net.bytebuddy.benchmark.specimen.ExampleInterface) Test(org.junit.Test)

Aggregations

ExampleInterface (net.bytebuddy.benchmark.specimen.ExampleInterface)8 Test (org.junit.Test)6 Method (java.lang.reflect.Method)2 Proxy (java.lang.reflect.Proxy)2 StubMethod (net.bytebuddy.implementation.StubMethod)2 URLClassLoader (java.net.URLClassLoader)1 MethodFilter (javassist.util.proxy.MethodFilter)1 MethodHandler (javassist.util.proxy.MethodHandler)1 ProxyFactory (javassist.util.proxy.ProxyFactory)1 CallbackHelper (net.sf.cglib.proxy.CallbackHelper)1 Enhancer (net.sf.cglib.proxy.Enhancer)1 FixedValue (net.sf.cglib.proxy.FixedValue)1