Java Code Examples for javassist.util.proxy.ProxyFactory#createClass()

The following examples show how to use javassist.util.proxy.ProxyFactory#createClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JavassistClientProxyFactory.java    From bowman with Apache License 2.0 6 votes vote down vote up
private static <T> T createProxyInstance(Class<T> entityType, MethodHandlerChain handlerChain) {
	ProxyFactory factory = new ProxyFactory();
	if (ProxyFactory.isProxyClass(entityType)) {
		factory.setInterfaces(getNonProxyInterfaces(entityType));
		factory.setSuperclass(entityType.getSuperclass());
	}
	else {
		factory.setSuperclass(entityType);
	}
	factory.setFilter(handlerChain);
	
	Class<?> clazz = factory.createClass();
	T proxy = instantiateClass(clazz);
	((Proxy) proxy).setHandler(handlerChain);
	return proxy;
}
 
Example 2
Source File: JavassistProxyFactory.java    From ymate-platform-v2 with Apache License 2.0 6 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> T createProxy(final Class<?> targetClass, final List<IProxy> proxies) {
    ProxyFactory factory = new ProxyFactory();
    factory.setSuperclass(targetClass);
    Class<?> clazz = factory.createClass();
    try {
        Object targetObj = clazz.newInstance();
        ((ProxyObject) targetObj).setHandler(new MethodHandler() {
            @Override
            public Object invoke(final Object self, Method thisMethod, final Method proceed, final Object[] args) throws Throwable {
                return new AbstractProxyChain(JavassistProxyFactory.this, targetClass, self, thisMethod, args, proxies) {
                    @Override
                    protected Object doInvoke() throws Throwable {
                        return proceed.invoke(getTargetObject(), getMethodParams());
                    }
                }.doProxyChain();
            }
        });
        return (T) targetObj;
    } catch (Exception e) {
        throw new Error(e);
    }
}
 
Example 3
Source File: JavassistLazyInitializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static Class getProxyFactory(
		Class persistentClass,
        Class[] interfaces) throws HibernateException {
	// note: interfaces is assumed to already contain HibernateProxy.class

	try {
		ProxyFactory factory = new ProxyFactory();
		factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
		factory.setInterfaces( interfaces );
		factory.setFilter( FINALIZE_FILTER );
		return factory.createClass();
	}
	catch ( Throwable t ) {
		LogFactory.getLog( BasicLazyInitializer.class ).error(
				"Javassist Enhancement failed: "
				+ persistentClass.getName(), t
		);
		throw new HibernateException(
				"Javassist Enhancement failed: "
				+ persistentClass.getName(), t
		);
	}
}
 
Example 4
Source File: BootstrapProxyFactory.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
/**
 * @param bootstrap dropwizard bootstrap object
 * @param context   guicey configuration context
 * @return dropwizard bootstrap proxy object
 */
@SuppressWarnings("unchecked")
public static Bootstrap create(final Bootstrap bootstrap, final ConfigurationContext context) {
    try {
        final ProxyFactory factory = new ProxyFactory();
        factory.setSuperclass(Bootstrap.class);
        final Class proxy = factory.createClass();

        final Bootstrap res = (Bootstrap) proxy.getConstructor(Application.class).newInstance(new Object[]{null});
        ((Proxy) res).setHandler((self, thisMethod, proceed, args) -> {
            // intercept only bundle addition
            if (thisMethod.getName().equals("addBundle")) {
                context.registerDropwizardBundles((ConfiguredBundle) args[0]);
                return null;
            }
            // other methods called as is
            return thisMethod.invoke(bootstrap, args);
        });
        return res;
    } catch (Exception e) {
        throw new IllegalStateException("Failed to create Bootstrap proxy", e);
    }
}
 
Example 5
Source File: TrivialClassCreationBenchmark.java    From byte-buddy with Apache License 2.0 6 votes vote down vote up
/**
 * Performs a benchmark for a trivial class creation using javassist proxies.
 *
 * @return The created instance, in order to avoid JIT removal.
 */
@Benchmark
public Class<?> benchmarkJavassist() {
    ProxyFactory proxyFactory = new ProxyFactory() {
        protected ClassLoader getClassLoader() {
            return newClassLoader();
        }
    };
    proxyFactory.setUseCache(false);
    proxyFactory.setUseWriteReplace(false);
    proxyFactory.setSuperclass(baseClass);
    proxyFactory.setFilter(new MethodFilter() {
        public boolean isHandled(Method method) {
            return false;
        }
    });
    return proxyFactory.createClass();
}
 
Example 6
Source File: ProxyHelper.java    From stevia with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@SuppressWarnings("rawtypes")
public static <T> T create(final Object obj, final ProxyCallback onSuccess, final ProxyCallback onException) throws Exception {
	ProxyFactory factory = new ProxyFactory();
	Class<T> origClass = (Class<T>) obj.getClass();
	factory.setSuperclass(origClass);
	Class clazz = factory.createClass();
	MethodHandler handler = new MethodHandler() {

		@Override
		public Object invoke(Object self, Method overridden, Method forwarder,
				Object[] args) throws Throwable {
			try {
				Object returnVal = overridden.invoke(obj, args);
				if (null != onSuccess) onSuccess.execute();
				return returnVal;
			} catch (Throwable tr) {
				if (null != onException) onException.execute();
				throw tr;
			}
		}
	};
	Object instance = clazz.newInstance();
	((ProxyObject) instance).setHandler(handler);
	return (T) instance;
}
 
Example 7
Source File: ProxyDetectionTest.java    From graphql-spqr with Apache License 2.0 5 votes vote down vote up
@Test
public void testJavassistProxy() throws IllegalAccessException, InstantiationException {
    ProxyFactory f = new ProxyFactory();
    f.setSuperclass(User.class);
    f.setFilter(m -> !m.getName().equals("finalize"));
    Class proxyClass = f.createClass();
    assertTrue(ClassUtils.isProxy(proxyClass));
}
 
Example 8
Source File: TypesTest.java    From monasca-common with Apache License 2.0 5 votes vote down vote up
@Test(enabled = false)
public void shouldDeProxyJavassistProxy() {
  ProxyFactory proxyFactory = new ProxyFactory();
  proxyFactory.setSuperclass(ArrayList.class);
  Class<?> proxy = proxyFactory.createClass();

  assertEquals(Types.deProxy(proxy), ArrayList.class);
}
 
Example 9
Source File: JavassistPartialObjectFactory.java    From spearal-java with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> createValue(SpearalContext context, Class<?> key, Object unused) {
	context.getSecurizer().checkDecodable(key);
	
	ProxyFactory proxyFactory = new ProxyFactory();
	proxyFactory.setFilter(new PartialObjectFilter(context, key));
	proxyFactory.setSuperclass(key);
	proxyFactory.setInterfaces(new Class<?>[] { ExtendedPartialObjectProxy.class });
	return proxyFactory.createClass();
}
 
Example 10
Source File: JavaHandlerModule.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> load(final Class<?> frameClass) throws Exception {
	final Class<?> handlerClass = getHandlerClass(frameClass);
	ProxyFactory proxyFactory = new ProxyFactory() {
		@Override
		protected ClassLoader getClassLoader() {

			return new URLClassLoader(new URL[0], handlerClass.getClassLoader());
		}
	};
	proxyFactory.setUseCache(false);
	proxyFactory.setSuperclass(handlerClass);
	return proxyFactory.createClass();
}
 
Example 11
Source File: JavassistLazyInitializer.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static HibernateProxy getProxy(
		final String entityName,
        final Class persistentClass,
        final Class[] interfaces,
        final Method getIdentifierMethod,
        final Method setIdentifierMethod,
        AbstractComponentType componentIdType,
        final Serializable id,
        final SessionImplementor session) throws HibernateException {
	// note: interface is assumed to already contain HibernateProxy.class
	try {
		final JavassistLazyInitializer instance = new JavassistLazyInitializer(
				entityName,
		        persistentClass,
		        interfaces,
		        id,
		        getIdentifierMethod,
		        setIdentifierMethod,
		        componentIdType,
		        session
		);
		ProxyFactory factory = new ProxyFactory();
		factory.setSuperclass( interfaces.length == 1 ? persistentClass : null );
		factory.setInterfaces( interfaces );
		factory.setFilter( FINALIZE_FILTER );
		Class cl = factory.createClass();
		final HibernateProxy proxy = ( HibernateProxy ) cl.newInstance();
		( ( ProxyObject ) proxy ).setHandler( instance );
		instance.constructed = true;
		return proxy;
	}
	catch ( Throwable t ) {
		LogFactory.getLog( BasicLazyInitializer.class ).error(
				"Javassist Enhancement failed: " + entityName, t
		);
		throw new HibernateException(
				"Javassist Enhancement failed: "
				+ entityName, t
		);
	}
}
 
Example 12
Source File: OutlineBuilder.java    From datamill with ISC License 4 votes vote down vote up
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);
}