Search in sources :

Example 1 with UnproxyableResolutionException

use of javax.enterprise.inject.UnproxyableResolutionException 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)

Aggregations

IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 PrivilegedActionException (java.security.PrivilegedActionException)1 PrivilegedExceptionAction (java.security.PrivilegedExceptionAction)1 MethodHandler (javassist.util.proxy.MethodHandler)1 ProxyFactory (javassist.util.proxy.ProxyFactory)1 UnproxyableResolutionException (javax.enterprise.inject.UnproxyableResolutionException)1 DefinitionException (org.exoplatform.container.context.DefinitionException)1