Example usage for java.lang.reflect Proxy getProxyClass

List of usage examples for java.lang.reflect Proxy getProxyClass

Introduction

In this page you can find the example usage for java.lang.reflect Proxy getProxyClass.

Prototype

@Deprecated
@CallerSensitive
public static Class<?> getProxyClass(ClassLoader loader, Class<?>... interfaces)
        throws IllegalArgumentException 

Source Link

Document

Returns the java.lang.Class object for a proxy class given a class loader and an array of interfaces.

Usage

From source file:com.netflix.paas.config.base.DynamicProxyArchaeusConfigurationFactory.java

@SuppressWarnings({ "unchecked", "static-access" })
public <T> T get(Class<T> configClass, DynamicPropertyFactory propertyFactory,
        AbstractConfiguration configuration) throws Exception {
    final Map<String, Supplier<?>> methods = ConfigurationProxyUtils.getMethodSuppliers(configClass,
            propertyFactory, configuration);

    if (configClass.isInterface()) {
        Class<?> proxyClass = Proxy.getProxyClass(configClass.getClassLoader(), new Class[] { configClass });

        return (T) proxyClass.getConstructor(new Class[] { InvocationHandler.class })
                .newInstance(new Object[] { new InvocationHandler() {
                    @Override/*from   w w  w .ja  v a 2  s .  c  o m*/
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                        return supplier.get();
                    }
                } });
    } else {
        final Enhancer enhancer = new Enhancer();
        final Object obj = (T) enhancer.create(configClass, new net.sf.cglib.proxy.InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                if (supplier == null) {
                    return method.invoke(proxy, args);
                }
                return supplier.get();
            }
        });

        ConfigurationProxyUtils.assignFieldValues(obj, configClass, propertyFactory, configuration);
        return (T) obj;
    }
}

From source file:com.netflix.paas.config.base.JavaAssistArchaeusConfigurationFactory.java

@SuppressWarnings({ "unchecked", "static-access" })
public <T> T get(Class<T> configClass, DynamicPropertyFactory propertyFactory,
        AbstractConfiguration configuration) throws Exception {
    final Map<String, Supplier<?>> methods = ConfigurationProxyUtils.getMethodSuppliers(configClass,
            propertyFactory, configuration);

    if (configClass.isInterface()) {
        Class<?> proxyClass = Proxy.getProxyClass(configClass.getClassLoader(), new Class[] { configClass });

        return (T) proxyClass.getConstructor(new Class[] { InvocationHandler.class })
                .newInstance(new Object[] { new InvocationHandler() {
                    @Override//w w  w . j  a  v  a2 s.c  o  m
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                        if (supplier == null) {
                            return method.invoke(proxy, args);
                        }
                        return supplier.get();
                    }
                } });
    } else {
        final Enhancer enhancer = new Enhancer();
        final Object obj = (T) enhancer.create(configClass, new net.sf.cglib.proxy.InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                if (supplier == null) {
                    return method.invoke(proxy, args);
                }
                return supplier.get();
            }
        });

        ConfigurationProxyUtils.assignFieldValues(obj, configClass, propertyFactory, configuration);
        return (T) obj;
    }
}

From source file:org.apache.batchee.container.util.TCCLObjectInputStream.java

@Override
protected Class resolveProxyClass(final String[] interfaces) throws IOException, ClassNotFoundException {
    final Class[] cinterfaces = new Class[interfaces.length];
    for (int i = 0; i < interfaces.length; i++) {
        cinterfaces[i] = Class.forName(interfaces[i], false, tccl);
    }/*from   w  w  w  .  j  ava2 s  .  c o  m*/

    try {
        return Proxy.getProxyClass(tccl, cinterfaces);
    } catch (IllegalArgumentException e) {
        throw new ClassNotFoundException(null, e);
    }
}

From source file:ipc.RPC.java

private static synchronized RpcEngine getProtocolEngine(Class protocol, Configuration conf) {
    RpcEngine engine = PROTOCOL_ENGINES.get(protocol);
    if (engine == null) {
        Class<?> impl = conf.getClass(ENGINE_PROP + "." + protocol.getName(), WritableRpcEngine.class);
        engine = (RpcEngine) ReflectionUtils.newInstance(impl, conf);
        if (protocol.isInterface())
            PROXY_ENGINES.put(Proxy.getProxyClass(protocol.getClassLoader(), protocol), engine);
        PROTOCOL_ENGINES.put(protocol, engine);
    }//from   w w w .ja  v  a  2s  .co m
    return engine;
}

From source file:org.atteo.moonshine.websocket.jsonmessages.HandlerDispatcher.java

public <T> SenderProvider<T> addSender(Class<T> klass) {
    checkState(klass.isInterface(), "Provided Class object must represent an interface");

    for (Method method : klass.getMethods()) {
        registerSenderMethod(method);//w  w w  .j a v a 2  s.  co m
    }
    @SuppressWarnings("unchecked")
    final Class<T> proxyClass = (Class<T>) Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(),
            klass);

    class SenderInvocationHandler implements InvocationHandler {
        private final Session session;

        public SenderInvocationHandler(Session session) {
            this.session = session;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            String request = encoderObjectMapper.writeValueAsString(args[0]);
            session.getBasicRemote().sendText(request);
            return null;
        }
    }

    return new SenderProvider<T>() {
        @Override
        public T get(Session session) {
            try {
                return proxyClass.getConstructor(new Class<?>[] { InvocationHandler.class })
                        .newInstance(new SenderInvocationHandler(session));
            } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
                    | IllegalArgumentException | InvocationTargetException ex) {
                throw new RuntimeException(ex);
            }
        }
    };
}

From source file:com.alibaba.wasp.ipc.WaspRPC.java

private static synchronized RpcEngine getProtocolEngine(Class protocol, Configuration conf) {
    RpcEngine engine = PROTOCOL_ENGINES.get(protocol);
    if (engine == null) {
        // check for a configured default engine
        Class<?> defaultEngine = conf.getClass(RPC_ENGINE_PROP, ProtobufRpcEngine.class);

        // check for a per interface override
        Class<?> impl = conf.getClass(RPC_ENGINE_PROP + "." + protocol.getName(), defaultEngine);
        LOG.debug("Using " + impl.getName() + " for " + protocol.getName());
        engine = (RpcEngine) ReflectionUtils.newInstance(impl, conf);
        if (protocol.isInterface())
            PROXY_ENGINES.put(Proxy.getProxyClass(protocol.getClassLoader(), protocol), engine);
        PROTOCOL_ENGINES.put(protocol, engine);
    }/*w  w  w  .  j a va  2s.com*/
    return engine;
}

From source file:org.apache.hadoop.hbase.ipc.HBaseClientRPC.java

static synchronized RpcClientEngine getProtocolEngine(Class<? extends IpcProtocol> protocol,
        Configuration conf) {/*from   w  w w.  j a  va  2 s. c  o m*/
    RpcClientEngine engine = PROTOCOL_ENGINES.get(protocol);
    if (engine == null) {
        // check for a configured default engine
        Class<?> defaultEngine = conf.getClass(RPC_ENGINE_PROP, ProtobufRpcClientEngine.class);

        // check for a per interface override
        Class<?> impl = conf.getClass(RPC_ENGINE_PROP + "." + protocol.getName(), defaultEngine);
        LOG.debug("Using " + impl.getName() + " for " + protocol.getName());
        engine = (RpcClientEngine) ReflectionUtils.newInstance(impl, conf);
        if (protocol.isInterface()) {
            PROXY_ENGINES.put(Proxy.getProxyClass(protocol.getClassLoader(), protocol), engine);
        }
        PROTOCOL_ENGINES.put(protocol, engine);
    }
    return engine;
}

From source file:org.mule.transport.soap.axis.extensions.MuleMsgProvider.java

protected Class getServiceClass(String s, SOAPService soapService, MessageContext messageContext)
        throws AxisFault {
    Service component = connector.getMuleContext().getRegistry().lookupService(soapService.getName());
    try {//from w  w w .  ja v  a  2s.c om
        Class[] classes = AxisServiceProxy.getInterfacesForComponent(component);
        return Proxy.getProxyClass(Thread.currentThread().getContextClassLoader(), classes);
    } catch (Exception e) {
        throw new AxisFault("Failed to implementation class for component: " + e.getMessage(), e);
    }
}

From source file:org.apache.openejb.client.EjbObjectInputStream.java

@Override
protected Class resolveProxyClass(final String[] interfaces) throws IOException, ClassNotFoundException {
    final Class[] cinterfaces = new Class[interfaces.length];
    for (int i = 0; i < interfaces.length; i++) {
        cinterfaces[i] = getClassloader().loadClass(interfaces[i]);
    }/*  w w w. j a  v a2  s .c  o m*/

    try {
        return Proxy.getProxyClass(getClassloader(), cinterfaces);
    } catch (IllegalArgumentException e) {
        throw new ClassNotFoundException(null, e);
    }
}

From source file:ProxyFactory.java

private synchronized Constructor getConstructor() {
    Constructor ctor = ctorRef == null ? null : (Constructor) ctorRef.get();

    if (ctor == null) {
        try {//from ww w .  j a  v  a  2s .  c  o  m
            ctor = Proxy.getProxyClass(getClass().getClassLoader(), interfaces)
                    .getConstructor(new Class[] { InvocationHandler.class });
        } catch (NoSuchMethodException e) {
            throw new InternalError(e.toString());
        }

        ctorRef = new SoftReference(ctor);
    }

    return ctor;
}