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:com.quatico.base.aem.test.api.setup.SetupFactory.java

public T getSetup(final Object... implementors) {
    final Map<Class<?>, Object> interfaces = new HashMap<>();
    for (Object implementor : implementors) {
        List<Class<?>> implementorInterfaces = ClassUtils.getAllInterfaces(implementor.getClass());

        assert !implementorInterfaces.isEmpty();

        for (Class<?> implementorInterface : implementorInterfaces) {
            interfaces.put(implementorInterface, implementor);
        }//  w  ww  .  j a  v  a  2s.c o m
    }

    @SuppressWarnings("unchecked")
    T result = (T) Proxy.newProxyInstance(this.interfaze.getClassLoader(), new Class<?>[] { this.interfaze },
            new InvocationHandler() {
                private final Object fProxyObject = new Object() {
                    @Override
                    public String toString() {
                        return "Proxy for: " + SetupFactory.this.interfaze.getDeclaringClass().getName(); //$NON-NLS-1$
                    }
                };

                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if (method.getDeclaringClass().equals(Object.class)) {
                        method.invoke(this.fProxyObject, args);
                    }

                    if (interfaces.containsKey(method.getDeclaringClass())) {
                        return method.invoke(interfaces.get(method.getDeclaringClass()), args);
                    }

                    throw new UnsupportedOperationException(
                            "Created proxy has not received an implementation that supports this method: " //$NON-NLS-1$
                                    + method.getName());
                }
            });

    return result;
}

From source file:com.facebook.infrastructure.db.ColumnFamily.java

public static ICompactSerializer2<ColumnFamily> serializerWithIndexes() {
    return (ICompactSerializer2<ColumnFamily>) Proxy.newProxyInstance(ColumnFamily.class.getClassLoader(),
            new Class[] { ICompactSerializer2.class },
            new ColumnIndexInvocationHandler<ColumnFamily>(serializer_));
}

From source file:org.makersoft.shards.ShardImpl.java

public ShardImpl(Set<ShardId> shardIds, SqlSessionFactory sqlSessionFactory) {
    this.shardIds = shardIds;
    this.sqlSessionFactory = sqlSessionFactory;
    this.exceptionTranslator = new MyBatisExceptionTranslator(
            sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), true);
    this.sqlSessionProxy = (SqlSession) Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(),
            new Class[] { SqlSession.class }, new SqlSessionInterceptor());
}

From source file:com.clarionmedia.jockey.authentication.AuthenticationProvider.java

public HttpClient now(Context context, OnAuthenticationListener onAuthenticationListener) {
    checkPreconditions(context);//from   w w w .  j a v a2  s  . co m
    mAuthenticator = buildAuthenticator(context);
    mAuthenticator.setOnAuthenticationListener(onAuthenticationListener);

    mAuthenticator.authenticateAsync();

    return (HttpClient) Proxy.newProxyInstance(HttpClient.class.getClassLoader(),
            new Class<?>[] { HttpClient.class }, new HttpClientProxy(mAuthenticator));
}

From source file:org.bytesoft.bytejta.supports.spring.ManagedConnectionFactoryPostProcessor.java

public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

    Class<?> clazz = bean.getClass();
    ClassLoader cl = clazz.getClassLoader();

    Class<?>[] interfaces = clazz.getInterfaces();

    if (XADataSource.class.isInstance(bean)) {
        ManagedConnectionFactoryHandler interceptor = new ManagedConnectionFactoryHandler(bean);
        interceptor.setIdentifier(beanName);
        return Proxy.newProxyInstance(cl, interfaces, interceptor);
    } else if (XAConnectionFactory.class.isInstance(bean)) {
        ManagedConnectionFactoryHandler interceptor = new ManagedConnectionFactoryHandler(bean);
        interceptor.setIdentifier(beanName);
        return Proxy.newProxyInstance(cl, interfaces, interceptor);
    } else if (ManagedConnectionFactory.class.isInstance(bean)) {
        ManagedConnectionFactoryHandler interceptor = new ManagedConnectionFactoryHandler(bean);
        interceptor.setIdentifier(beanName);
        return Proxy.newProxyInstance(cl, interfaces, interceptor);
    } else {// w w w .ja v  a  2s .  c o  m
        return bean;
    }
}

From source file:com.eviware.soapui.plugins.PluginProxies.java

@SuppressWarnings("unchecked")
static <T> T createProxyFor(T delegate) {

    if (delegate instanceof JComponent) {
        log.warn("Can't proxy JComponent derived classes");
        return delegate;
    }//from   w w  w . ja va2 s . com

    Collection<Class> interfaces = ClassUtils.getAllInterfaces(delegate.getClass());
    if (interfaces.isEmpty()) {
        // this shouldn't really happen, unless reflection is being used in some odd way
        log.warn("Can't proxy instance of {} because the class doesn't implement any interfaces",
                delegate.getClass());
        return delegate;
    }
    interfaces.add(PluginProxy.class);
    return (T) Proxy.newProxyInstance(PluginProxies.class.getClassLoader(),
            interfaces.toArray(new Class[interfaces.size()]), new DelegatingHandler<T>(delegate));
}

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

@SuppressWarnings("unchecked")
public RepositoryWorkerReturn<Repository> open() {
    try {/*from  w  w w  . ja va  2 s  .  com*/
        String tmpDir = System.getProperty("java.io.tmpdir");
        File dir = new File(tmpDir, UUID.randomUUID().toString().replaceAll("-", ""));
        Git.init().setDirectory(dir).setBare(false).call();
        repositories.add(dir);
        File file = new File(dir, ".git");
        Repository repository = new FileRepository(file);
        return (RepositoryWorkerReturn<Repository>) Proxy.newProxyInstance(this.getClass().getClassLoader(),
                new Class[] { RepositoryWorkerReturn.class },
                new ReturnHolder(new RepositoryWokerImpl(dir, repository), repository));
    } catch (Exception e) {
        throw new RuntimeException("Error creating git repository ", e);
    }
}

From source file:ar.com.zauber.commons.spring.beans.factory.impl.ProxyFactoryBean.java

/** @see FactoryBean#getObject() */
public final Object getObject() throws Exception {
    final Class<?>[] classes = new Class[interfaces.size()];
    int i = 0;/*from  www .ja  v a2s .  com*/
    for (Class<?> c : interfaces) {
        classes[i] = c;
        i++;
    }
    return Proxy.newProxyInstance(getClass().getClassLoader(), (Class<?>[]) classes, ih);
}

From source file:org.chromium.sdk.internal.protocolparser.dynamicimpl.ParserRootImpl.java

ParserRootImpl(Class<R> rootClass, Map<Class<?>, TypeHandler<?>> type2TypeHandler)
        throws JsonProtocolModelParseException {
    this.rootClass = rootClass;
    ParseInterfaceSession session = new ParseInterfaceSession(type2TypeHandler);
    session.run(rootClass);/*from  ww w  . ja  v  a 2 s  .c  o m*/
    this.invocationHandler = session.createInvocationHandler();
    Object result = Proxy.newProxyInstance(rootClass.getClassLoader(), new Class<?>[] { rootClass },
            invocationHandler);
    this.instance = (R) result;
}

From source file:de.perdian.commons.i18n.polyglot.PolyglotImpl.java

@Override
public T getInstance(Locale locale) {
    if (locale == null) {
        throw new IllegalArgumentException("Parameter 'locale' must not be null");
    } else {//from  w w  w.  j  a  v  a 2s .c om
        T cachedInstance = this.getLocaleToInstanceCache().get(locale);
        if (cachedInstance == null) {
            Class<T> instanceClass = this.getInstanceClass();
            cachedInstance = instanceClass.cast(Proxy.newProxyInstance(instanceClass.getClassLoader(),
                    new Class[] { instanceClass }, this.createInvocationHandler(locale)));
            this.getLocaleToInstanceCache().put(locale, cachedInstance);
        }
        return cachedInstance;
    }
}