Example usage for java.lang.reflect Proxy newProxyInstance

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

Introduction

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

Prototype

private static Object newProxyInstance(Class<?> caller, 
            Constructor<?> cons, InvocationHandler h) 

Source Link

Usage

From source file:org.resthub.rpc.AMQPProxyFactoryBean.java

@Override
public void afterPropertiesSet() {
    super.afterPropertiesSet();

    if (null == this.serviceInterface || !this.serviceInterface.isInterface()) {
        throw new IllegalArgumentException("Property 'serviceInterface' is required");
    }// w ww.j ava  2 s  .c  o  m
    if (null == this.serializationHandler) {
        this.serializationHandler = new DefaultSerializationHandler();
    }
    AMQPProxy handler = new AMQPProxy(this);
    this.serviceProxy = Proxy.newProxyInstance(this.serviceInterface.getClassLoader(),
            new Class[] { this.serviceInterface }, handler);
}

From source file:org.jcurl.core.jnlp.FileDialogWebstart.java

/**
 * Dynamic Interface Wrapper (or dynamic Delegate) using
 * {@link Proxy#newProxyInstance(ClassLoader, Class[], InvocationHandler)}
 * mapping only identical methods./*from   w  w w  .  j  ava  2s  . c  o m*/
 * <p>
 * The two interfaces must have identical methods!
 * </p>
 * 
 * @param src
 *            the source instance (the one to wrap)
 * @param dstT
 *            destination interface type
 * @return target interface instance.
 */
static Object wrap(final Object src, final Class<?> dstT) {
    if (log.isDebugEnabled())
        log.debug("wrap(" + src + ", " + dstT + ")");
    if (src == null)
        return null;
    return Proxy.newProxyInstance(FileDialogWebstart.class.getClassLoader(), new Class[] { dstT },
            new InvocationHandler() {
                public Object invoke(final Object proxy, final Method method, final Object[] args)
                        throws Throwable {
                    final Class<?> srcT = src.getClass();
                    // Wrap ALL Methods (also getClass, equals, toString,
                    // ...)
                    final Method dstM = srcT.getMethod(method.getName(), method.getParameterTypes());
                    if (log.isDebugEnabled()) {
                        log.debug("proxy: " + proxy.getClass());
                        log.debug("method: " + method);
                        log.debug("args: " + args);
                        log.debug("srcT: " + srcT);
                        log.debug("dstM: " + dstM);
                    }
                    final Object ret = dstM.invoke(src, args);
                    if (log.isDebugEnabled())
                        log.debug("ret: " + ret);
                    return ret;
                }
            });
}

From source file:org.gitistics.test.RepositoryWokerImpl.java

@SuppressWarnings("unchecked")
private <T> RepositoryWorkerReturn<T> proxy(T object) {
    return (RepositoryWorkerReturn<T>) Proxy.newProxyInstance(this.getClass().getClassLoader(),
            new Class[] { RepositoryWorkerReturn.class }, new ReturnHolder(this, object));
}

From source file:org.bytesoft.openjtcc.supports.dubbo.internal.RemoteInvocationServiceFactoryImpl.java

@Override
public RemoteInvocationService getRemoteInvocationService(TerminalKey terminalKey) {
    RemoteInvocationEndpointSelector handler = new RemoteInvocationEndpointSelector();
    handler.setTerminalKey(terminalKey);
    handler.setApplicationContext(this.applicationContext);
    return (RemoteInvocationService) Proxy.newProxyInstance(RemoteInvocationService.class.getClassLoader(),
            new Class[] { RemoteInvocationService.class }, handler);
}

From source file:org.zenoss.zep.dao.impl.EventDetailsConfigDaoImpl.java

public EventDetailsConfigDaoImpl(DataSource ds) {
    this.template = (SimpleJdbcOperations) Proxy.newProxyInstance(SimpleJdbcOperations.class.getClassLoader(),
            new Class<?>[] { SimpleJdbcOperations.class }, new SimpleJdbcTemplateProxy(ds));
}

From source file:ar.com.zauber.labs.kraken.providers.wikipedia.model.impl.LazyWikiPageRetriever.java

/** @see WikiPageRetriever#get(Language, String) */
public final WikiPage get(final Language language, final String pageTitle) {
    return (WikiPage) Proxy.newProxyInstance(getClass().getClassLoader(), new Class<?>[] { WikiPage.class },
            new LazyWikiPageInvocationHandler(language, pageTitle, wikiPageRetriever));
}

From source file:io.teak.sdk.Amazon.java

public void init(Context context) {
    skuDetailsRequestMap = new HashMap<>();
    skuPriceMap = new HashMap<>();
    try {//w  w w . ja v  a  2  s  .co m
        Class<?> purchasingListenerClass = Class.forName("com.amazon.device.iap.PurchasingListener");
        InvocationHandler handler = new PurchasingListenerInvocationHandler();
        Object proxy = Proxy.newProxyInstance(purchasingListenerClass.getClassLoader(),
                new Class[] { purchasingListenerClass }, handler);

        Class<?> purchasingServiceClass = Class.forName("com.amazon.device.iap.PurchasingService");
        Method m = purchasingServiceClass.getMethod("registerListener", Context.class, purchasingListenerClass);
        m.invoke(null, context, proxy);

        if (Teak.isDebug) {
            Field sandbox = purchasingServiceClass.getDeclaredField("IS_SANDBOX_MODE");

            Log.d(LOG_TAG, "Amazon In-App Purchasing 2.0 registered.");
            Log.d(LOG_TAG, "   Sandbox Mode: " + sandbox.getBoolean(null));
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "Reflection error: " + Log.getStackTraceString(e));
        Teak.sdkRaven.reportException(e);
    }
}

From source file:net.sf.morph.context.support.ChainContextCreator.java

/**
 * Creates a {@link org.apache.commons.chain.Context} instance out of any
 * Object.  The context instance is backed by the given object and by a
 * Map, so that if properties are not accessible via the object, they will
 * be via the Map.//from w w w . j a v  a  2  s. c o  m
 * 
 * @param object the object to be exposed as a context
 * @returns a {@link org.apache.commons.chain.Context} instance backed by
 * the given object 
 * @throws IllegalArgumentException if object is <code>null</code>
 */
public Context createContext(Object object) throws IllegalArgumentException {
    if (object == null) {
        throw new IllegalArgumentException("An object from which a context is to be created must be specified");
    }

    Context context = (Context) Proxy.newProxyInstance(Context.class.getClassLoader(),
            new Class[] { Context.class, HierarchicalContext.class }, new ChainInvocationHandler(object));
    return context;
}

From source file:com.googlecode.pondskum.client.listener.CompositeConnectionListener.java

@Override
public void handleEvent(final DefaultHttpClient httpClient, final HttpResponse response)
        throws ConnectionListenerException {
    for (ConnectionListener connectionListener : connectionListeners) {

        //proxy the entity content so it can be queried more than once.
        if (response.getEntity() != null) {
            HttpEntity proxiedEntity = (HttpEntity) Proxy.newProxyInstance(getClass().getClassLoader(),
                    new Class<?>[] { HttpEntity.class },
                    new HttpResponseInvocationHandler(response.getEntity()));
            response.setEntity(proxiedEntity);
        }/*from   w  ww.ja va  2 s.  c  om*/

        connectionListener.handleEvent(httpClient, response);
    }
}

From source file:org.akita.proxy.ProxyInvocationHandler.java

public Object bind(Class<?> clazz) {
    Class<?>[] clazzs = { clazz };
    Object newProxyInstance = Proxy.newProxyInstance(clazz.getClassLoader(), clazzs, this);
    return newProxyInstance;
}