Search in sources :

Example 16 with MethodHandler

use of javassist.util.proxy.MethodHandler in project kernel by exoplatform.

the class ContainerUtil method createProxy.

/**
 * Creates a proxy of the given super class whose instance will be created accessed lazily thanks to a provider
 * @param superClass the super class of the proxy to create
 * @param provider the provider that will create the instance lazily
 * @return a proxy of the given super class
 * @throws UnproxyableResolutionException if any issue occurs while creating the proxy
 */
public static <T> T createProxy(final Class<T> superClass, final Provider<T> provider) throws UnproxyableResolutionException {
    PrivilegedExceptionAction<T> action = new PrivilegedExceptionAction<T>() {

        public T run() throws Exception {
            // We first make sure that there is no non-static, final methods with public, protected or default visibility
            Method[] methods = superClass.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                Method m = methods[i];
                int modifiers = m.getModifiers();
                if (Modifier.isFinal(modifiers) && !Modifier.isPrivate(modifiers) && !Modifier.isStatic(modifiers)) {
                    throw new UnproxyableResolutionException("Cannot create a proxy for the class " + superClass.getName() + " because it has at least one non-static, final method with public, protected or default visibility");
                }
            }
            try {
                ProxyFactory factory = new ProxyFactory();
                factory.setSuperclass(superClass);
                factory.setFilter(MethodFilterHolder.METHOD_FILTER);
                MethodHandler handler = new ProxyMethodHandler<T>(provider);
                return superClass.cast(factory.create(new Class<?>[0], new Object[0], handler));
            } catch (Exception e) {
                throw new UnproxyableResolutionException("Cannot create a proxy for the class " + superClass.getName(), e);
            }
        }
    };
    try {
        return SecurityHelper.doPrivilegedExceptionAction(action);
    } catch (PrivilegedActionException e) {
        Throwable cause = e.getCause();
        if (cause instanceof UnproxyableResolutionException) {
            throw (UnproxyableResolutionException) cause;
        } else {
            throw new UnproxyableResolutionException("Cannot create a proxy for the class " + superClass.getName(), cause);
        }
    }
}
Also used : ProxyFactory(javassist.util.proxy.ProxyFactory) PrivilegedActionException(java.security.PrivilegedActionException) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) Method(java.lang.reflect.Method) PrivilegedActionException(java.security.PrivilegedActionException) DefinitionException(org.exoplatform.container.context.DefinitionException) UnproxyableResolutionException(javax.enterprise.inject.UnproxyableResolutionException) IOException(java.io.IOException) MethodHandler(javassist.util.proxy.MethodHandler) UnproxyableResolutionException(javax.enterprise.inject.UnproxyableResolutionException)

Example 17 with MethodHandler

use of javassist.util.proxy.MethodHandler 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 18 with MethodHandler

use of javassist.util.proxy.MethodHandler 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)

Example 19 with MethodHandler

use of javassist.util.proxy.MethodHandler in project jbosstools-hibernate by jbosstools.

the class GenericExporterFacadeTest method setUp.

@Before
public void setUp() throws Exception {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass(GenericExporter.class);
    Class<?> proxyClass = proxyFactory.createClass();
    genericExporter = (GenericExporter) proxyClass.newInstance();
    ((ProxyObject) genericExporter).setHandler(new MethodHandler() {

        @Override
        public Object invoke(Object self, Method m, Method proceed, Object[] args) throws Throwable {
            if (methodName == null) {
                methodName = m.getName();
            }
            if (arguments == null) {
                arguments = args;
            }
            return proceed.invoke(self, args);
        }
    });
    genericExporterFacade = new AbstractGenericExporterFacade(FACADE_FACTORY, genericExporter) {
    };
    reset();
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) MethodHandler(javassist.util.proxy.MethodHandler) ProxyFactory(javassist.util.proxy.ProxyFactory) ProxyObject(javassist.util.proxy.ProxyObject) Method(java.lang.reflect.Method) AbstractGenericExporterFacade(org.jboss.tools.hibernate.runtime.common.AbstractGenericExporterFacade) Before(org.junit.Before)

Example 20 with MethodHandler

use of javassist.util.proxy.MethodHandler in project jbosstools-hibernate by jbosstools.

the class Hbm2DDLExporterFacadeTest method setUp.

@Before
public void setUp() throws Exception {
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setSuperclass(Hbm2DDLExporter.class);
    Class<?> proxyClass = proxyFactory.createClass();
    hbm2ddlExporter = (Hbm2DDLExporter) proxyClass.newInstance();
    ((ProxyObject) hbm2ddlExporter).setHandler(new MethodHandler() {

        @Override
        public Object invoke(Object self, Method m, Method proceed, Object[] args) throws Throwable {
            if (methodName == null) {
                methodName = m.getName();
            }
            if (arguments == null) {
                arguments = args;
            }
            return proceed.invoke(self, args);
        }
    });
    hbm2DDLExporterFacade = new AbstractHbm2DDLExporterFacade(FACADE_FACTORY, hbm2ddlExporter) {
    };
    reset();
}
Also used : ProxyObject(javassist.util.proxy.ProxyObject) AbstractHbm2DDLExporterFacade(org.jboss.tools.hibernate.runtime.common.AbstractHbm2DDLExporterFacade) MethodHandler(javassist.util.proxy.MethodHandler) ProxyFactory(javassist.util.proxy.ProxyFactory) ProxyObject(javassist.util.proxy.ProxyObject) Method(java.lang.reflect.Method) Before(org.junit.Before)

Aggregations

MethodHandler (javassist.util.proxy.MethodHandler)33 ProxyFactory (javassist.util.proxy.ProxyFactory)32 Method (java.lang.reflect.Method)31 ProxyObject (javassist.util.proxy.ProxyObject)26 Before (org.junit.Before)24 ArtifactCollector (org.hibernate.tool.hbm2x.ArtifactCollector)6 AbstractArtifactCollectorFacade (org.jboss.tools.hibernate.runtime.common.AbstractArtifactCollectorFacade)6 AbstractForeignKeyFacade (org.jboss.tools.hibernate.runtime.common.AbstractForeignKeyFacade)6 AbstractGenericExporterFacade (org.jboss.tools.hibernate.runtime.common.AbstractGenericExporterFacade)6 AbstractHbm2DDLExporterFacade (org.jboss.tools.hibernate.runtime.common.AbstractHbm2DDLExporterFacade)6 IArtifactCollector (org.jboss.tools.hibernate.runtime.spi.IArtifactCollector)6 MethodFilter (javassist.util.proxy.MethodFilter)4 Proxy (javassist.util.proxy.Proxy)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ODatabaseObject (com.orientechnologies.orient.core.db.object.ODatabaseObject)1 OSerializationException (com.orientechnologies.orient.core.exception.OSerializationException)1 Element (com.tinkerpop.blueprints.Element)1 IOException (java.io.IOException)1 Proxy (java.lang.reflect.Proxy)1 URLClassLoader (java.net.URLClassLoader)1