@SuppressWarnings("unchecked")
public static <T> T createProxy(final T underlying, final GenericInvocationHandler<T> invocationHandler) {
// TODO refactor - to get rid of endless try - catch
final ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
try {
final Enhancer enhancer = createProxy(underlying, invocationHandler, ProxyNamingPolicy.INSTANCE, currentThreadClassLoader);
return (T) enhancer.create();
} catch (CodeGenerationException e) {
// fallback, as proxied class might implement non-public interfaces
// that have trouble with our ProxyNamingPolicy (for example for SQLite)
try {
final Enhancer enhancer = createProxy(underlying, invocationHandler, null, currentThreadClassLoader);
return (T) enhancer.create();
} catch (CodeGenerationException ex) {
//
// well, wildfly (8.1.CR1) just doesn't like currentThreadClassLoader => needs to use the default one
//
try {
final Enhancer enhancer = createProxy(underlying, invocationHandler, ProxyNamingPolicy.INSTANCE, null);
return (T) enhancer.create();
} catch (CodeGenerationException exc) {
// fallback, as proxied class might implement non-public interfaces
// that have trouble with our ProxyNamingPolicy (for example for SQLite)
final Enhancer enhancer = createProxy(underlying, invocationHandler, null, null);
return (T) enhancer.create();
}
}
}
}