/** * Creates a proxy class and returns an instance of that class. * * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. * @param mh the method handler for the proxy class. * @since 3.4 */ public Object create(Class[] paramTypes, Object[] args, MethodHandler mh) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = create(paramTypes, args); ((Proxy)obj).setHandler(mh); return obj; }
final Proxy proxy = (Proxy) (constructorParamTypes.length == 0 ? factory.createClass().getConstructor().newInstance() : factory.create(constructorParamTypes, constructorArguments)); proxy.setHandler((self, overridden, proceed, args) -> { final UnitOfWork unitOfWork = overridden.getAnnotation(UnitOfWork.class);
/** * Creates a proxy class and returns an instance of that class. * * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. * @param mh the method handler for the proxy class. * @since 3.4 */ public Object create(Class<?>[] paramTypes, Object[] args, MethodHandler mh) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = create(paramTypes, args); ((Proxy)obj).setHandler(mh); return obj; }
/** * Constructs a partial instance of abstract type {@code cls}, passing {@code args} into its * constructor. * * <p>The returned object will throw an {@link UnsupportedOperationException} from any * unimplemented methods. */ public static <T> T of(Class<T> cls, Object... args) { checkIsValidPartial(cls); try { Constructor<?> constructor = cls.getDeclaredConstructors()[0]; ProxyFactory factory = new ProxyFactory(); factory.setSuperclass(cls); factory.setFilter(new MethodFilter() { @Override public boolean isHandled(Method m) { return Modifier.isAbstract(m.getModifiers()); } }); @SuppressWarnings("unchecked") T partial = (T) factory.create( constructor.getParameterTypes(), args, new ThrowingMethodHandler()); return partial; } catch (Exception e) { throw new RuntimeException("Failed to instantiate " + cls, e); } }
@Override protected Object createObjectFromNoArgsConstructor(final Constructor<?> constructor) { try { return proxyFactory.create(new Class[0], new Class[0]); } catch (final InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { LOGGER.debug("Exception:", e); // ignore, we want to try all constructors // if all constructors fail, it will be handled by caller return null; } }
private Object createProxy() { try { return proxyFactory.create(new Class[]{}, new Object[]{}, this); } catch (Exception e) { throw new RuntimeException("Error creating Proxy for " + clazz + ": " + e.getMessage(), e); } }
/** * Creates a proxy class and returns an instance of that class. * * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. * @param mh the method handler for the proxy class. * @since 3.4 */ public Object create(Class[] paramTypes, Object[] args, MethodHandler mh) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = create(paramTypes, args); ((Proxy)obj).setHandler(mh); return obj; }
/** * Creates a proxy class and returns an instance of that class. * * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. * @param mh the method handler for the proxy class. * @since 3.4 */ public Object create(Class[] paramTypes, Object[] args, MethodHandler mh) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = create(paramTypes, args); ((ProxyObject)obj).setHandler(mh); return obj; }
/** * Creates a proxy class and returns an instance of that class. * * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. * @param mh the method handler for the proxy class. * @since 3.4 */ public Object create(Class[] paramTypes, Object[] args, MethodHandler mh) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = create(paramTypes, args); ((Proxy)obj).setHandler(mh); return obj; }
/** * Creates a proxy class and returns an instance of that class. * * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. * @param mh the method handler for the proxy class. * @since 3.4 */ public Object create(Class[] paramTypes, Object[] args, MethodHandler mh) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = create(paramTypes, args); ((ProxyObject)obj).setHandler(mh); return obj; }
/** * Creates a proxy class and returns an instance of that class. * * @param paramTypes parameter types for a constructor. * @param args arguments passed to a constructor. * @param mh the method handler for the proxy class. * @since 3.4 */ public Object create(Class[] paramTypes, Object[] args, MethodHandler mh) throws NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException { Object obj = create(paramTypes, args); ((Proxy)obj).setHandler(mh); return obj; }
private <T> Object createInstance(Class<T> type, MethodInvocation<? super T> handler, ProxyFactory factory) { try { return factory.create(null, null, new MethodInvocationAdapter<>(handler)); } catch (ReflectiveOperationException | IllegalArgumentException e) { logger.error("An error occurs when create a proxy for type " + type, e); throw new ProxyCreationException(e); } }
@Override public Object run() throws Exception { ClassLoader currentCCL = null; if (neutralCCL) { currentCCL = Thread.currentThread().getContextClassLoader(); } try { return proxyFactory.create(c.getParameterTypes(), args, methodInterceptor); } catch (InvocationTargetException ite) { Throwable targetException = ite.getTargetException(); Logger.getLogger().debug(c.getDeclaringClass().getName(), c.getName(), targetException); if (targetException instanceof Exception) { throw (Exception) targetException; } throw new RuntimeException(targetException); } finally { if (neutralCCL) { Thread.currentThread().setContextClassLoader(currentCCL); } } }
private Entity proxifyEntity(final Entity entity) { if (entity == null) { return null; } final ProxyFactory factory = new ProxyFactory(); factory.setSuperclass(entity.getClass()); factory.setFilter(new AllMethodFilter()); try { return (Entity) factory.create(new Class<?>[0], new Object[0], new LazyMethodHandler(entity, lazyLoader)); } catch (final Exception e) { throw new RuntimeException("Error when proxifying object", e); } }
private <T> T createProxy( T parentTap, Class<T> type, Class... interfaces ) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass( type ); if( interfaces.length != 0 ) proxyFactory.setInterfaces( interfaces ); try { return (T) proxyFactory.create( new Class[]{}, new Object[]{}, getClassLoaderMethodHandler( parentTap ) ); } catch( Exception exception ) { throw new RuntimeException( "failed to create proxy", exception ); } }
private ProviderFactory createProviderFactoryProxy( Class factoryClass ) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass( factoryClass ); proxyFactory.setInterfaces( new Class[]{ProviderFactory.class} ); try { return (ProviderFactory) proxyFactory.create( new Class[]{}, new Object[]{}, getProviderFactoryMethodHandler() ); } catch( Exception exception ) { throw new RuntimeException( "failed to create proxy", exception ); } }
private Entity proxifyEntity(final Entity entity) { if (entity == null) { return null; } final ProxyFactory factory = new ProxyFactory(); factory.setSuperclass(entity.getClass()); factory.setFilter(new AllMethodFilter()); try { return (Entity) factory.create(new Class<?>[0], new Object[0], new LazyMethodHandler(entity, lazyLoader)); } catch (final Exception e) { throw new RuntimeException("Error when proxifying object", e); } }
private Entity proxifyEntity(final Entity entity) { if (entity == null) { return null; } final ProxyFactory factory = new ProxyFactory(); Class<?> classForProxy = entity.getClass(); //It's not possible to create a Proxy on a Proxy //Here Entity can already be an Hibernate Proxy if (ProxyFactory.isProxyClass(classForProxy)) { classForProxy = classForProxy.getSuperclass(); } factory.setSuperclass(classForProxy); factory.setFilter(new AllMethodFilter()); try { return (Entity) factory.create(new Class<?>[0], new Object[0], new LazyMethodHandler(entity, lazyLoader)); } catch (final Exception e) { throw new RuntimeException("Error when proxifying object", e); } }
public Object createProxy() { ProxyFactory factory = new ProxyFactory(); factory.setSuperclass(clazz); factory.setInterfaces(interfaces); try { return factory.create(new Class[0], new Object[0], new JavassistInvocationHandler(methodInterceptors)); } catch (Exception e) { throw new MirrorException("Proxy could not be created", e); } } }
private static Counters.Counter proxyCounter(Counter c) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass(Counters.Counter.class); proxyFactory.setFilter(CCMethodHandler.FILTER); CCMethodHandler handler = new CCMethodHandler(c); try { return (Counters.Counter) proxyFactory.create(new Class[0], new Object[0], handler); } catch (Exception e) { throw new CrunchRuntimeException(e); } }