public static javassist.util.proxy.ProxyFactory buildJavassistProxyFactory( final Class persistentClass, final Class[] interfaces) { javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory() { @Override protected ClassLoader getClassLoader() { return persistentClass.getClassLoader(); } }; factory.setSuperclass( interfaces.length == 1 ? persistentClass : null ); factory.setInterfaces( interfaces ); factory.setFilter( EXCLUDE_FILTER ); return factory; }
Object readResolve() throws ObjectStreamException { try { int n = interfaces.length; Class[] infs = new Class[n]; for (int i = 0; i < n; i++) infs[i] = loadClass(interfaces[i]); ProxyFactory f = new ProxyFactory(); f.setSuperclass(loadClass(superClass)); f.setInterfaces(infs); Proxy proxy = (Proxy)f.createClass(filterSignature).newInstance(); proxy.setHandler(handler); return proxy; } catch (ClassNotFoundException e) { throw new java.io.InvalidClassException(e.getMessage()); } catch (InstantiationException e2) { throw new java.io.InvalidObjectException(e2.getMessage()); } catch (IllegalAccessException e3) { throw new java.io.InvalidClassException(e3.getMessage()); } } }
public <T> T create(Class<T> clazz, Class<?>[] constructorParamTypes, Object[] constructorArguments) { final ProxyFactory factory = new ProxyFactory(); factory.setSuperclass(clazz);
f.setSuperclass(loadClass(superClass)); f.setInterfaces(infs); Proxy proxy = (Proxy)f.createClass(filterSignature).getConstructor().newInstance();
public BasicProxyFactoryImpl(Class superClass, Class[] interfaces) { if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) { throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" ); } final javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory(); factory.setFilter( FINALIZE_FILTER ); if ( superClass != null ) { factory.setSuperclass( superClass ); } if ( interfaces != null && interfaces.length > 0 ) { factory.setInterfaces( interfaces ); } proxyClass = factory.createClass(); }
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { boolean isProxy = readBoolean(); if (isProxy) { String name = (String)readObject(); Class superClass = loader.loadClass(name); int length = readInt(); Class[] interfaces = new Class[length]; for (int i = 0; i < length; i++) { name = (String)readObject(); interfaces[i] = loader.loadClass(name); } length = readInt(); byte[] signature = new byte[length]; read(signature); ProxyFactory factory = new ProxyFactory(); // we must always use the cache and never use writeReplace when using // ProxyObjectOutputStream and ProxyObjectInputStream factory.setUseCache(true); factory.setUseWriteReplace(false); factory.setSuperclass(superClass); factory.setInterfaces(interfaces); Class proxyClass = factory.createClass(signature); return ObjectStreamClass.lookup(proxyClass); } else { return super.readClassDescriptor(); } }
@Override protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException { boolean isProxy = readBoolean(); if (isProxy) { String name = (String)readObject(); Class<?> superClass = loader.loadClass(name); int length = readInt(); Class<?>[] interfaces = new Class[length]; for (int i = 0; i < length; i++) { name = (String)readObject(); interfaces[i] = loader.loadClass(name); } length = readInt(); byte[] signature = new byte[length]; read(signature); ProxyFactory factory = new ProxyFactory(); // we must always use the cache and never use writeReplace when using // ProxyObjectOutputStream and ProxyObjectInputStream factory.setUseCache(true); factory.setUseWriteReplace(false); factory.setSuperclass(superClass); factory.setInterfaces(interfaces); Class<?> proxyClass = factory.createClass(signature); return ObjectStreamClass.lookup(proxyClass); } return super.readClassDescriptor(); }
/** * Constructs a Latke bean. * * @param beanManager the specified bean manager * @param name the specified bean name * @param beanClass the specified bean class * @param types the specified bean types * @param stereotypes the specified stereo types */ public Bean(final BeanManager beanManager, final String name, final Class<T> beanClass, final Set<Type> types, final Set<Class<? extends Annotation>> stereotypes) { this.beanManager = beanManager; this.name = name; this.beanClass = beanClass; this.types = types; this.stereotypes = stereotypes; this.configurator = beanManager.getConfigurator(); javassistMethodHandler = new JavassistMethodHandler(beanManager); final ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass(beanClass); proxyFactory.setFilter(javassistMethodHandler.getMethodFilter()); proxyClass = (Class<T>) proxyFactory.createClass(); annotatedType = new AnnotatedTypeImpl<T>(beanClass); fieldInjectionPoints = new HashSet<>(); initFieldInjectionPoints(); }
/** * 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 public <T> T proxify(Class<T> type, MethodInvocation<? super T> handler) { final ProxyFactory factory = new ProxyFactory(); factory.setFilter(IGNORE_BRIDGE_AND_OBJECT_METHODS); Class<?> rawType = extractRawType(type); if (type.isInterface()) { factory.setInterfaces(new Class[] { rawType }); } else { factory.setSuperclass(rawType); } Object instance = createInstance(type, handler, factory); logger.debug("a proxy for {} was created as {}", type, instance.getClass()); return (T) instance; }
private javassist.util.proxy.ProxyFactory buildJavassistProxyFactory() { final javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory() { @Override protected ClassLoader getClassLoader() { return persistentClass.getClassLoader(); } }; factory.setSuperclass(interfaces.length == 1 ? persistentClass : null); factory.setInterfaces(getInterfacesIncludingV2HibernateProxy(interfaces)); factory.setFilter(FINALIZE_FILTER); return factory; }
private static ProxyFactory createProxyFactory(Class<?> persistentClass, Class<?>[] interfaces) { ProxyFactory factory = new ProxyFactory(); factory.setSuperclass(interfaces.length == 1 ? persistentClass : null); factory.setInterfaces(interfaces); factory.setFilter(METHOD_FILTERS); if (WRITE_CLASSES_DIRECTORY != null) { factory.writeDirectory = WRITE_CLASSES_DIRECTORY; } return factory; }
public static <T> Class<T> createProxyClass(Class<T> proxyClass) { ProxyFactory proxyFactory = new ProxyFactory(); if (proxyClass != null) { proxyFactory.setSuperclass(proxyClass); } proxyFactory.setInterfaces(new Class<?>[]{LifecycleMixin.class, TargetInstanceProxy.class}); Class<T> clazz = proxyFactory.createClass(); return clazz; }
@Override public Class<?> load(final Class<?> frameClass) throws Exception { final Class<?> handlerClass = getHandlerClass(frameClass); ProxyFactory proxyFactory = new ProxyFactory() { protected ClassLoader getClassLoader() { return new URLClassLoader(new URL[0], handlerClass.getClassLoader()); } }; proxyFactory.setUseCache(false); proxyFactory.setSuperclass(handlerClass); return proxyFactory.createClass(); } });
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 static <T> Outline<T> buildOutline(Class<T> classToOutline, boolean camelCased) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass(classToOutline); Class<? extends T> outlineClass = proxyFactory.createClass(); return new OutlineImpl<>(objenesis.newInstance(outlineClass), camelCased); }
private Class<?> proxyClass(Class<?> mockedType, Class<?>... ancilliaryTypes) { final ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setFilter(new MethodFilter() { @Override public boolean isHandled(final Method m) { // ignore finalize() and als bridge methods return !m.getName().equals("finalize") || m.isBridge(); } }); proxyFactory.setSuperclass(mockedType); proxyFactory.setInterfaces(ancilliaryTypes); return proxyFactory.createClass(); }
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); } }
public BasicProxyFactoryImpl(Class superClass, Class[] interfaces) { if ( superClass == null && ( interfaces == null || interfaces.length < 1 ) ) { throw new AssertionFailure( "attempting to build proxy without any superclass or interfaces" ); } javassist.util.proxy.ProxyFactory factory = new javassist.util.proxy.ProxyFactory(); factory.setFilter( FINALIZE_FILTER ); if ( superClass != null ) { factory.setSuperclass( superClass ); } if ( interfaces != null && interfaces.length > 0 ) { factory.setInterfaces( interfaces ); } proxyClass = factory.createClass(); }