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.apache.servicemix.nmr.spring.BundleExtTest.java

public void test() {
    final long bundleId = 32;
    BundleExtUrlPostProcessor processor = new BundleExtUrlPostProcessor();
    processor.setBundleContext((BundleContext) Proxy.newProxyInstance(getClass().getClassLoader(),
            new Class[] { BundleContext.class }, new InvocationHandler() {
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    if ("getBundle".equals(method.getName())) {
                        return (Bundle) Proxy.newProxyInstance(getClass().getClassLoader(),
                                new Class[] { Bundle.class }, new InvocationHandler() {
                                    public Object invoke(Object proxy, Method method, Object[] args)
                                            throws Throwable {
                                        if ("getBundleId".equals(method.getName())) {
                                            return bundleId;
                                        }
                                        return null;
                                    }//from  w  ww  .  ja v  a 2  s  . c  o m
                                });
                    }
                    return null; //To change body of implemented methods use File | Settings | File Templates.
                }
            }));
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { "bundle.xml" },
            false);
    ctx.addBeanFactoryPostProcessor(processor);
    ctx.refresh();
    Object str = ctx.getBean("string");
    System.err.println(str);
    assertNotNull(str);
    assertEquals("bundle://" + bundleId + "///schema.xsd", str);
}

From source file:com.palantir.leader.proxy.AwaitingLeadershipProxy.java

@SuppressWarnings("unchecked")
public static <T> T newProxyInstance(Class<T> interfaceClass, Supplier<T> delegateSupplier,
        LeaderElectionService leaderElectionService) {
    AwaitingLeadershipProxy proxy = new AwaitingLeadershipProxy(delegateSupplier, leaderElectionService,
            interfaceClass);//from  www .ja  v  a2 s.c  o  m
    proxy.tryToGainLeadership();
    return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(),
            new Class<?>[] { interfaceClass, Closeable.class }, proxy);
}

From source file:org.apache.hivemind.examples.impl.ProxyLoggingInterceptorFactory.java

public void createInterceptor(InterceptorStack stack, Module invokingModule, List parameters) {
    Log log = stack.getServiceLog();//from   w w  w.j  av  a2s. c  om

    InvocationHandler handler = new ProxyLoggingInvocationHandler(log, stack.peek());

    Object interceptor = Proxy.newProxyInstance(invokingModule.getClassResolver().getClassLoader(),
            new Class[] { stack.getServiceInterface() }, handler);

    stack.push(interceptor);
}

From source file:io.dyn.core.context.EventedProxyFactoryBean.java

@SuppressWarnings({ "unchecked" })
@Override/*from   ww  w .j a  va2s . com*/
public P getObject() throws Exception {
    return (P) Proxy.newProxyInstance(eventProxyType.getClassLoader(), new Class<?>[] { eventProxyType },
            evented.invocationHandler());
}

From source file:org.hyperic.snmp.SNMPSessionCache.java

static SNMPSession newInstance(SNMPSession session, int expire) throws SNMPException {
    SNMPSessionCache handler = new SNMPSessionCache(session, expire);

    SNMPSession sessionCache;/*from   w  ww .j  av a 2 s  .c  om*/

    try {
        sessionCache = (SNMPSession) Proxy.newProxyInstance(SNMPSession.class.getClassLoader(),
                new Class[] { SNMPSession.class }, handler);
    } catch (Exception e) {
        throw new SNMPException(e.getMessage());
    }

    return sessionCache;
}

From source file:com.epam.reportportal.junit.JUnitProvider.java

@Override
public IListenerHandler get() {
    if (reportPortalService.getParameters().getEnable()) {
        return new ParallelRunningHandler(parallelRunningContext, reportPortalService);
    }/*from w  ww  .  ja v  a 2  s  . c om*/

    return (IListenerHandler) Proxy.newProxyInstance(this.getClass().getClassLoader(),
            new Class[] { IListenerHandler.class }, new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                    Class<?> returnType = method.getReturnType();

                    if (ClassUtils.isAssignable(returnType, Boolean.class, true)) {
                        return false;
                    }

                    return null;
                }
            });
}

From source file:jp.go.nict.langrid.servicecontainer.executor.jsonrpc.DynamicJsonRpcServiceExecutor.java

public static <T> T create(String invocationName, long invocationId, Endpoint endpoint,
        Class<T> interfaceClass) {
    return interfaceClass.cast(Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
            new Class[] { interfaceClass, StreamingNotifier.class },
            new DynamicJsonRpcServiceExecutor(invocationName, invocationId, endpoint)));
}

From source file:com.examples.with.different.packagename.ClassPublicInterface.java

public static <L> void bindEventsToMethod(final Object target, final String methodName,
        final Object eventSource, final Class<L> listenerType, final String... eventTypes) {
    final L listener = listenerType.cast(Proxy.newProxyInstance(target.getClass().getClassLoader(),
            new Class[] { listenerType }, new EventBindingInvocationHandler(target, methodName, eventTypes)));
    addEventListener(eventSource, listenerType, listener);
}

From source file:com.amazonaws.hal.client.HalJsonResourceUnmarshallerTest.java

@Test
public void testNested() throws Exception {
    HalResource halResource = parseHalResourceFromClasspath("report.resource");

    Assert.assertNotNull(halResource);//from w w  w  . j  a  va2  s. c o  m

    Report report = (Report) Proxy.newProxyInstance(Report.class.getClassLoader(),
            new Class<?>[] { Report.class },
            new HalResourceInvocationHandler(halResource, halResource._getSelfHref(), null));

    Assert.assertNotNull(report.getColumns());
    Assert.assertEquals(4, report.getColumns().size());
    Assert.assertNotNull(report.getColumns().get("day"));
    Assert.assertEquals("date", report.getColumns().get("day").getDisplayName());

    Assert.assertNotNull(report.getRows());
    Assert.assertEquals(30, report.getRows().size());
    Assert.assertEquals(4, report.getRows().get(0).size());
    Assert.assertEquals("2014-02-10T00:00:00Z", report.getRows().get(0).get(0));
    Assert.assertEquals("Android", report.getRows().get(0).get(1));
    Assert.assertEquals(1003921, report.getRows().get(0).get(2));
    Assert.assertEquals(6.34, report.getRows().get(0).get(3));
}

From source file:com.googlecode.jdbcproc.daofactory.guice.StoredProcedureDaoProvider.java

@SuppressWarnings("unchecked")
public T get() {/*from   w  ww . jav a 2s  .c  o m*/
    try {
        return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                new Class[] { interfaze },
                new StoredProcedureDaoInvocationHandler(interfaze, jdbcTemplate, daoMethodInfo));
    } catch (SQLException ex) {
        throw new RuntimeException(ex);
    }
}