Search in sources :

Example 61 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project dubbo by alibaba.

the class ProxyTest method testCglibProxy.

@Test
public void testCglibProxy() throws Exception {
    ITest test = (ITest) Proxy.getProxy(ITest.class).newInstance(new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println(method.getName());
            return null;
        }
    });
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(test.getClass());
    enhancer.setCallback(new MethodInterceptor() {

        public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
            return null;
        }
    });
    try {
        enhancer.create();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
        Assert.fail();
    }
}
Also used : MethodInterceptor(net.sf.cglib.proxy.MethodInterceptor) Enhancer(net.sf.cglib.proxy.Enhancer) MethodProxy(net.sf.cglib.proxy.MethodProxy) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler) Test(org.junit.Test)

Example 62 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project tomee by apache.

the class MdbProxy method destroyProxy.

public static void destroyProxy(final Object proxy) {
    final InvocationHandler handler = Proxy.getInvocationHandler(proxy);
    if (MdbProxy.class.isInstance(handler)) {
        final MdbInvocationHandler mdbInvocationHandler = (MdbInvocationHandler) handler;
        mdbInvocationHandler.destroy();
    }
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler)

Example 63 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project jo-client-platform by jo-source.

the class RemotingServiceProviderFactory method getService.

private static Object getService(final IServiceId<?> serviceId, final Object brokerId) {
    final Class<?> serviceType = serviceId.getServiceType();
    final InvocationHandler invocationHandler = new RemoteMethodInvocationHandler(brokerId, serviceId);
    return Proxy.newProxyInstance(serviceType.getClassLoader(), new Class[] { serviceType }, invocationHandler);
}
Also used : InvocationHandler(java.lang.reflect.InvocationHandler)

Example 64 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project kernel by exoplatform.

the class TestContainerUtil method testGetServletContextName.

public void testGetServletContextName() {
    final AtomicReference<String> scn = new AtomicReference<String>("myContextName");
    final AtomicReference<String> scp = new AtomicReference<String>("/myContextPath");
    ServletContext context = (ServletContext) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { ServletContext.class }, new InvocationHandler() {

        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if ("getServletContextName".equals(method.getName()))
                return scn.get();
            else if ("getContextPath".equals(method.getName()))
                return scp.get();
            return null;
        }
    });
    assertEquals("myContextName", ContainerUtil.getServletContextName(context));
    scn.set(null);
    assertEquals("myContextPath", ContainerUtil.getServletContextName(context));
    scp.set("");
    assertEquals("", ContainerUtil.getServletContextName(context));
    scp.set("/a/b");
    assertEquals("a", ContainerUtil.getServletContextName(context));
    scp.set("/a2/b/");
    assertEquals("a2", ContainerUtil.getServletContextName(context));
    scp.set("a3/b");
    assertEquals("a3", ContainerUtil.getServletContextName(context));
    scp.set("a4/b/");
    assertEquals("a4", ContainerUtil.getServletContextName(context));
    scp.set(null);
    assertNull(ContainerUtil.getServletContextName(context));
}
Also used : ServletContext(javax.servlet.ServletContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Method(java.lang.reflect.Method) InvocationHandler(java.lang.reflect.InvocationHandler)

Example 65 with InvocationHandler

use of java.lang.reflect.InvocationHandler in project jo-client-platform by jo-source.

the class CancelServicesDecoratorProviderImpl method getDecorator.

@Override
public <SERVICE_TYPE> IDecorator<SERVICE_TYPE> getDecorator(final IServiceId<SERVICE_TYPE> id) {
    Assert.paramNotNull(id, "id");
    final Class<? extends SERVICE_TYPE> serviceType = id.getServiceType();
    return new IDecorator<SERVICE_TYPE>() {

        @SuppressWarnings("unchecked")
        @Override
        public SERVICE_TYPE decorate(final SERVICE_TYPE original) {
            if (services.contains(serviceType)) {
                final InvocationHandler invocationHandler = new CancelInvocationHandler(original);
                return (SERVICE_TYPE) Proxy.newProxyInstance(serviceType.getClassLoader(), new Class[] { serviceType }, invocationHandler);
            } else {
                return original;
            }
        }
    };
}
Also used : AbstractCapServiceInvocationHandler(org.jowidgets.cap.common.tools.proxy.AbstractCapServiceInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler) IDecorator(org.jowidgets.util.IDecorator)

Aggregations

InvocationHandler (java.lang.reflect.InvocationHandler)411 Method (java.lang.reflect.Method)232 Test (org.junit.Test)70 InvocationTargetException (java.lang.reflect.InvocationTargetException)54 Proxy (java.lang.reflect.Proxy)28 ArrayList (java.util.ArrayList)25 Map (java.util.Map)23 IOException (java.io.IOException)19 Field (java.lang.reflect.Field)19 HashMap (java.util.HashMap)18 AccessibleObject (java.lang.reflect.AccessibleObject)16 List (java.util.List)16 BindingProvider (javax.xml.ws.BindingProvider)14 PersistentClass (org.hibernate.mapping.PersistentClass)12 RootClass (org.hibernate.mapping.RootClass)12 Before (org.junit.Before)10 Connection (java.sql.Connection)9 LinkedHashMap (java.util.LinkedHashMap)9 AbstractQueryFacade (org.jboss.tools.hibernate.runtime.common.AbstractQueryFacade)8 DexMakerTest (com.android.dx.DexMakerTest)7