Search in sources :

Example 1 with MethodFilter

use of javassist.util.proxy.MethodFilter in project che by eclipse.

the class JUnitTestRunner method create3xTestListener.

private Object create3xTestListener(ClassLoader loader, Class<?> listenerClass, AbstractTestListener delegate) throws Exception {
    ProxyFactory f = new ProxyFactory();
    f.setSuperclass(Object.class);
    f.setInterfaces(new Class<?>[] { listenerClass });
    f.setFilter(new MethodFilter() {

        @Override
        public boolean isHandled(Method m) {
            String methodName = m.getName();
            switch(methodName) {
                case "startTest":
                case "endTest":
                case "addError":
                case "addFailure":
                    return true;
            }
            return false;
        }
    });
    Class<?> c = f.createClass();
    MethodHandler mi = new MethodHandler() {

        public Object invoke(Object self, Method method, Method proceed, Object[] args) throws Throwable {
            String testKey = args[0].getClass().toString();
            String testName = args[0].getClass().getName();
            String methodName = method.getName();
            switch(methodName) {
                case "startTest":
                    delegate.startTest(testKey, testName);
                    break;
                case "endTest":
                    delegate.endTest(testKey, testName);
                    break;
                case "addError":
                    delegate.addError(testKey, (Throwable) args[1]);
                    break;
                case "addFailure":
                    delegate.addFailure(testKey, (Throwable) args[1]);
                    break;
            }
            return null;
        }
    };
    Object listener = c.getConstructor().newInstance();
    ((javassist.util.proxy.Proxy) listener).setHandler(mi);
    return listener;
}
Also used : MethodHandler(javassist.util.proxy.MethodHandler) ProxyFactory(javassist.util.proxy.ProxyFactory) MethodFilter(javassist.util.proxy.MethodFilter) Method(java.lang.reflect.Method)

Example 2 with MethodFilter

use of javassist.util.proxy.MethodFilter 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 3 with MethodFilter

use of javassist.util.proxy.MethodFilter in project byte-buddy by raphw.

the class TrivialClassCreationBenchmark method benchmarkJavassist.

/**
     * Performs a benchmark for a trivial class creation using javassist proxies.
     *
     * @return The created instance, in order to avoid JIT removal.
     */
@Benchmark
public Class<?> benchmarkJavassist() {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setUseCache(false);
    ProxyFactory.classLoaderProvider = new ProxyFactory.ClassLoaderProvider() {

        @Override
        public ClassLoader get(ProxyFactory proxyFactory) {
            return newClassLoader();
        }
    };
    proxyFactory.setSuperclass(baseClass);
    proxyFactory.setFilter(new MethodFilter() {

        public boolean isHandled(Method method) {
            return false;
        }
    });
    return proxyFactory.createClass();
}
Also used : ProxyFactory(javassist.util.proxy.ProxyFactory) MethodFilter(javassist.util.proxy.MethodFilter) URLClassLoader(java.net.URLClassLoader) Method(java.lang.reflect.Method)

Example 4 with MethodFilter

use of javassist.util.proxy.MethodFilter in project che by eclipse.

the class JUnitTestRunner method create4xTestListener.

private Object create4xTestListener(ClassLoader loader, Class<?> listenerClass, AbstractTestListener delegate) throws Exception {
    ProxyFactory f = new ProxyFactory();
    f.setSuperclass(listenerClass);
    f.setFilter(new MethodFilter() {

        @Override
        public boolean isHandled(Method m) {
            String methodName = m.getName();
            switch(methodName) {
                case "testStarted":
                case "testFinished":
                case "testFailure":
                case "testAssumptionFailure":
                    return true;
            }
            return false;
        }
    });
    Class<?> c = f.createClass();
    MethodHandler mi = new MethodHandler() {

        @Override
        public Object invoke(Object self, Method m, Method method, Object[] args) throws Throwable {
            String methodName = m.getName();
            Object description = null;
            Throwable throwable = null;
            switch(methodName) {
                case "testStarted":
                case "testFinished":
                    description = args[0];
                    throwable = null;
                    break;
                case "testFailure":
                case "testAssumptionFailure":
                    description = args[0].getClass().getMethod("getDescription", new Class<?>[0]).invoke(args[0]);
                    throwable = (Throwable) args[0].getClass().getMethod("getException", new Class<?>[0]).invoke(args[0]);
                    break;
                default:
                    return null;
            }
            String testKey = (String) description.getClass().getMethod("getDisplayName", new Class<?>[0]).invoke(description);
            String testName = testKey;
            switch(methodName) {
                case "testStarted":
                    delegate.startTest(testKey, testName);
                    break;
                case "testFinished":
                    delegate.endTest(testKey, testName);
                    break;
                case "testFailure":
                    delegate.addFailure(testKey, throwable);
                    break;
                case "testAssumptionFailure":
                    delegate.addError(testKey, throwable);
                    break;
            }
            return null;
        }
    };
    Object listener = c.getConstructor().newInstance();
    ((javassist.util.proxy.Proxy) listener).setHandler(mi);
    return listener;
}
Also used : MethodHandler(javassist.util.proxy.MethodHandler) ProxyFactory(javassist.util.proxy.ProxyFactory) MethodFilter(javassist.util.proxy.MethodFilter) Method(java.lang.reflect.Method)

Example 5 with MethodFilter

use of javassist.util.proxy.MethodFilter in project byte-buddy by raphw.

the class ClassByExtensionBenchmark method benchmarkJavassist.

/**
     * Performs a benchmark of a class extension using javassist proxies.
     *
     * @return The created instance, in order to avoid JIT removal.
     * @throws java.lang.Exception If the invocation causes an exception.
     */
@Benchmark
public ExampleClass benchmarkJavassist() throws Exception {
    ProxyFactory proxyFactory = new ProxyFactory() {

        @Override
        protected ClassLoader getClassLoader() {
            return newClassLoader();
        }
    };
    proxyFactory.setUseCache(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;
}
Also used : MethodHandler(javassist.util.proxy.MethodHandler) ExampleClass(net.bytebuddy.benchmark.specimen.ExampleClass) ProxyFactory(javassist.util.proxy.ProxyFactory) MethodFilter(javassist.util.proxy.MethodFilter) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)5 MethodFilter (javassist.util.proxy.MethodFilter)5 ProxyFactory (javassist.util.proxy.ProxyFactory)5 MethodHandler (javassist.util.proxy.MethodHandler)4 URLClassLoader (java.net.URLClassLoader)2 Proxy (java.lang.reflect.Proxy)1 ExampleClass (net.bytebuddy.benchmark.specimen.ExampleClass)1 ExampleInterface (net.bytebuddy.benchmark.specimen.ExampleInterface)1 StubMethod (net.bytebuddy.implementation.StubMethod)1