Java Code Examples for javassist.util.proxy.ProxyFactory#create()
The following examples show how to use
javassist.util.proxy.ProxyFactory#create() .
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: ProxyUtils.java From chrome-devtools-java-client with Apache License 2.0 | 6 votes |
/** * Creates a proxy class to a given abstract clazz supplied with invocation handler for * un-implemented/abstrat methods. * * @param clazz Proxy to class. * @param paramTypes Ctor param types. * @param args Ctor args. * @param invocationHandler Invocation handler. * @param <T> Class type. * @return Proxy instance. */ @SuppressWarnings("unchecked") public static <T> T createProxyFromAbstract( Class<T> clazz, Class[] paramTypes, Object[] args, InvocationHandler invocationHandler) { ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass(clazz); proxyFactory.setFilter(method -> Modifier.isAbstract(method.getModifiers())); try { return (T) proxyFactory.create( paramTypes, args, (o, method, method1, objects) -> invocationHandler.invoke(o, method, objects)); } catch (Exception e) { LOGGER.error("Failed creating proxy from abstract class", e); throw new RuntimeException("Failed creating proxy from abstract class", e); } }
Example 2
Source File: Partial.java From FreeBuilder with Apache License 2.0 | 6 votes |
/** * 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); } }
Example 3
Source File: ProxyBuilder.java From docx-stamper with MIT License | 6 votes |
/** * Creates a proxy object out of the specified root object and the specified interfaces * and implementations. * * @return a proxy object that is still of type T but additionally implements all specified * interfaces. * @throws ProxyException if the proxy could not be created. */ public T build() throws ProxyException { if (this.root == null) { throw new IllegalArgumentException("root must not be null!"); } if (this.interfacesToImplementations.isEmpty()) { // nothing to proxy return this.root; } try { ProxyMethodHandler methodHandler = new ProxyMethodHandler(root, interfacesToImplementations); ProxyFactory proxyFactory = new ProxyFactory(); proxyFactory.setSuperclass(root.getClass()); proxyFactory.setInterfaces(interfacesToImplementations.keySet().toArray(new Class[]{})); return (T) proxyFactory.create(new Class[0], new Object[0], methodHandler); } catch (Exception e) { throw new ProxyException(e); } }
Example 4
Source File: TransactionalClassProxy.java From seed with Mozilla Public License 2.0 | 6 votes |
/** * Create a transactional proxy around the provided {@link TransactionalLink}. * * @param <T> the interface used to create the proxy. * @param clazz the class representing the transacted resource. * @param transactionalLink the link to access the instance of the transacted resource. * @return the proxy. */ @SuppressWarnings("unchecked") public static <T> T create(Class<T> clazz, final TransactionalLink<T> transactionalLink) { try { ProxyFactory factory = new ProxyFactory(); factory.setSuperclass(clazz); if (AutoCloseable.class.isAssignableFrom(clazz)) { factory.setInterfaces(new Class<?>[]{IgnoreAutoCloseable.class}); } factory.setFilter(method -> !method.getDeclaringClass().equals(Object.class)); return (T) factory.create(new Class<?>[0], new Object[0], new TransactionalClassProxy<>(transactionalLink)); } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { throw SeedException.wrap(e, TransactionErrorCode.UNABLE_TO_CREATE_TRANSACTIONAL_PROXY).put("class", clazz.getName()); } }
Example 5
Source File: JavassistProxifier.java From vraptor4 with Apache License 2.0 | 5 votes |
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); } }
Example 6
Source File: ThriftClientImpl.java From thrift-pool-client with Artistic License 2.0 | 4 votes |
/** * {@inheritDoc} * * <p> * iface. * </p> */ @SuppressWarnings("unchecked") @Override public <X extends TServiceClient> X iface(Class<X> ifaceClass, Function<TTransport, TProtocol> protocolProvider, int hash) { List<ThriftServerInfo> servers = serverInfoProvider.get(); if (servers == null || servers.isEmpty()) { throw new NoBackendException(); } hash = Math.abs(hash); hash = Math.max(hash, 0); ThriftServerInfo selected = servers.get(hash % servers.size()); logger.trace("get connection for [{}]->{} with hash:{}", ifaceClass, selected, hash); TTransport transport = poolProvider.getConnection(selected); TProtocol protocol = protocolProvider.apply(transport); ProxyFactory factory = new ProxyFactory(); factory.setSuperclass(ifaceClass); factory.setFilter(m -> ThriftClientUtils.getInterfaceMethodNames(ifaceClass).contains( m.getName())); try { X x = (X) factory.create(new Class[] { org.apache.thrift.protocol.TProtocol.class }, new Object[] { protocol }); ((Proxy) x).setHandler((self, thisMethod, proceed, args) -> { boolean success = false; try { Object result = proceed.invoke(self, args); success = true; return result; } finally { if (success) { poolProvider.returnConnection(selected, transport); } else { poolProvider.returnBrokenConnection(selected, transport); } } }); return x; } catch (NoSuchMethodException | IllegalArgumentException | InstantiationException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException("fail to create proxy.", e); } }