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);
}
}
}
Aggregations