當前位置: 首頁>>代碼示例>>Java>>正文


Java MethodHandler類代碼示例

本文整理匯總了Java中javassist.util.proxy.MethodHandler的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandler類的具體用法?Java MethodHandler怎麽用?Java MethodHandler使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


MethodHandler類屬於javassist.util.proxy包,在下文中一共展示了MethodHandler類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: blockHandler

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
@SuppressWarnings("unchecked")
private MethodHandler blockHandler(final Class clazz) {
    return (o, method, method1, args) -> {
        Pair<ThriftServerInfo, AbstractTrpcClient> borrowClient = ClientCluster.getBlockClient(clientArgs);
        Object client = borrowClient.getValue().getClient(clazz);
        boolean success = false;
        long startTime = System.currentTimeMillis();
        try {
            Object result = method.invoke(client, args);
            success = true;
            //將連接歸還對象池
            clientArgs.getPoolProvider().returnConnection(borrowClient.getKey(), borrowClient.getValue());
            return result;
        } catch (Exception e) {
            log.error("call rpc error", e);
            clientArgs.getPoolProvider().returnBrokenConnection(borrowClient.getKey(), borrowClient.getValue());
            throw e;
        } finally {
            log.info("call " + method.getName() + " " + success + " time: " + (System.currentTimeMillis() - startTime));
        }
    };
}
 
開發者ID:sofn,項目名稱:trpc,代碼行數:23,代碼來源:TrpcClientProxy.java

示例2: asyncHandler

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
@SuppressWarnings("unchecked")
private MethodHandler asyncHandler(final Class clazz) {
    return (o, method, method1, args) -> {
        Pair<ThriftServerInfo, AbstractTrpcClient> borrowClient = ClientCluster.getBlockClient(clientArgs);
        Object client = borrowClient.getValue().getClient(clazz);
        try {
            if (args.length > 0 && args[args.length - 1] instanceof AsyncMethodCallback) {
                //代理AsyncMethodCallback
                args[args.length - 1] = java.lang.reflect.Proxy.newProxyInstance(AsyncMethodCallback.class.getClassLoader(),
                        new Class[]{AsyncMethodCallback.class},
                        new AsyncMethodCallbackProxy(args[args.length - 1], clientArgs, borrowClient));
            }
            return method.invoke(client, args);
        } catch (Exception e) {
            log.error("call rpc error", e);
            throw e;
        }
    };
}
 
開發者ID:sofn,項目名稱:trpc,代碼行數:20,代碼來源:TrpcClientProxy.java

示例3: nullSafe

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
public static <T> T nullSafe(Class<T> klass, final Object instance){
    try {
        if (Modifier.isFinal(klass.getModifiers())) return (T)instance;
        ProxyFactory factory = new ProxyFactory();
        factory.setSuperclass(klass);
        Class clazz = factory.createClass();
        MethodHandler handler = (self, overridden, forwarder, args) -> {
            Object returnval = (instance != null) ? overridden.invoke(instance, args) : null;
            return nullSafe(overridden.getReturnType(), returnval);

        };
        Object proxyInstance = clazz.newInstance();
        ((ProxyObject) proxyInstance).setHandler(handler);
        return (T) proxyInstance;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
開發者ID:ChristinGorman,項目名稱:kjempenyttig,代碼行數:19,代碼來源:Elvis.java

示例4: newJavassistProxy

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
static NonTrivialBean newJavassistProxy() 
        throws InstantiationException, IllegalAccessException {
  ProxyFactory f = new ProxyFactory();
  f.setSuperclass(NonTrivialBean.class);
  Class<?> c = f.createClass();
  MethodHandler mi = new MethodHandler() {
    // delegate to source methods
    public Object invoke(Object self, Method m, Method proceed, 
        Object[] args) throws Throwable {
      return proceed.invoke(self, args);
    }
  };
  NonTrivialBean b = (NonTrivialBean) c.newInstance();
  ((Proxy) b).setHandler(mi);
  return b;
}
 
開發者ID:Salauyou,項目名稱:Java-Utils,代碼行數:17,代碼來源:TestBeanHelper.java

示例5: create

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
@SuppressWarnings("rawtypes")
public static <T> T create(final Object obj, final ProxyCallback onSuccess, final ProxyCallback onException) throws Exception {
	ProxyFactory factory = new ProxyFactory();
	Class<T> origClass = (Class<T>) obj.getClass();
	factory.setSuperclass(origClass);
	Class clazz = factory.createClass();
	MethodHandler handler = new MethodHandler() {

		@Override
		public Object invoke(Object self, Method overridden, Method forwarder,
				Object[] args) throws Throwable {
			try {
				Object returnVal = overridden.invoke(obj, args);
				if (null != onSuccess) onSuccess.execute();
				return returnVal;
			} catch (Throwable tr) {
				if (null != onException) onException.execute();
				throw tr;
			}
		}
	};
	Object instance = clazz.newInstance();
	((ProxyObject) instance).setHandler(handler);
	return (T) instance;
}
 
開發者ID:persado,項目名稱:stevia,代碼行數:26,代碼來源:ProxyHelper.java

示例6: isProxy

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
public static boolean isProxy(final Object element)
{
    if (null == element)
    {
        return false;
    }
    if (element instanceof ProxyObject)
    {
        final MethodHandler methodHandler = ((ProxyObject) element).getHandler();
        if (methodHandler instanceof LazyAssociationHandler)
        {
            return true;
        }
    }
    return false;
}
 
開發者ID:trajar,項目名稱:normandra,代碼行數:17,代碼來源:AssociationUtils.java

示例7: create

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
public static <T> T create(Class<T> classs) throws Exception {
    ProxyFactory factory = new ProxyFactory();
    factory.setSuperclass(classs);
    Class clazz = factory.createClass();
    MethodHandler handler = new MethodHandler() {

        public Object invoke(Object self, Method overridden, Method forwarder, Object[] args) throws Throwable {
            return forwarder.invoke(self, args);
        }
    };
    Object instance = clazz.newInstance();
    ((ProxyObject) instance).setHandler(handler);
    return (T) instance;
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:15,代碼來源:ProxyTest.java

示例8: postProcess

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
@Override
public <T> T postProcess(final T object, ResolutionContext context,
                         Resolution<?> resolution) throws ReflectiveOperationException {
    final Class<?> clazz = object.getClass();
    if (!hasProfileMethod(clazz)) {
        return null;
    }

    // The Proxy generated by javassist will have the same constructor args as on the real class
    // We can easily check and satisfy this by getting the instantiation method: if it is constructor injection
    // get the args from it. This approach requires some refactoring to support custom instantiation methods.
    Class<?>[] constructorTypes = getConstructorType(clazz);
    Object[] constructorArgs = resolveConstructorArgs(constructorTypes);

    ProxyFactory pf = new ProxyFactory();
    pf.setSuperclass(clazz);
    Class<?> proxyClass = pf.createClass(METHOD_FILTER);

    T proxy = (T) proxyClass
        .getConstructor(constructorTypes)
        .newInstance(constructorArgs);

    ((Proxy) proxy).setHandler(new MethodHandler() {
        @Override
        public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args)
                             throws ReflectiveOperationException {
            invocations.add(clazz.getSimpleName() + "#" + thisMethod.getName());
            return thisMethod.invoke(object, args);
        }
    });
    return proxy;
}
 
開發者ID:ljacqu,項目名稱:DependencyInjector,代碼行數:33,代碼來源:ProfilePostConstructHandler.java

示例9: createProxyInstance

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
private static <T> T createProxyInstance(Class<T> entityType, MethodHandler methodHandler) {
	ProxyFactory factory = new ProxyFactory();
	factory.setSuperclass(entityType);
	factory.setFilter(FILTER_INSTANCE);
	
	Class<?> clazz = factory.createClass();
	T proxy = instantiateClass(clazz);
	((Proxy) proxy).setHandler(methodHandler);
	return proxy;
}
 
開發者ID:BlackPepperSoftware,項目名稱:bowman,代碼行數:11,代碼來源:JavassistClientProxyFactory.java

示例10: createInternal

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
@SuppressWarnings("unchecked")
@Override
protected <E> E createInternal(final Class<E> klass) {
	
	try {
	
   		ProxyFactory factory = new ProxyFactory();
   		factory.setSuperclass(klass);
   		
   		factory.setFilter(new MethodFilter() {
   
   			@Override
               public boolean isHandled(Method m) {
   				return getPropName(m) != null;
   			}
   		});
   		
   		MethodHandler handler = new MethodHandler() {
   	        
   			@Override
   	        public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {
   				
   				return DefaultProxy.this.invoke(self, thisMethod, args);
   	        }
   	    };
   	    
   	    return (E) factory.create(new Class[0], new Object[0], handler);
   	    
	} catch(Exception e) {
		throw new BeanException(e);
	}
}
 
開發者ID:saoj,項目名稱:mentabean,代碼行數:33,代碼來源:DefaultProxy.java

示例11: assertExceptionThrown

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
/** TODO !!! describe !!! **/
public static <T> T assertExceptionThrown(final T object, final Class<? extends Throwable> expectedExceptionClass, final String message) {

	assertNotNull(object, message);

	return createProxy(object.getClass(), new MethodHandler() {

		@Override
		public Object invoke(Object self, Method thisMethod, Method proceed, Object[] args) throws Throwable {

			Throwable methodException = null;

			try {
				thisMethod.invoke(object, args);
			} catch (InvocationTargetException e) {
				methodException = e.getCause();
			}

			if (methodException == null || !expectedExceptionClass.isAssignableFrom(methodException.getClass())) {
				fail(object, "method " + thisMethod.getName() + "(...) was expected to throw " + expectedExceptionClass.getName()
						+ " but it "
						+ (methodException == null ? "did not" : "threw " + methodException.getClass().getName() + " instead"), message);
			}

			return null;
		}

	});
}
 
開發者ID:terems-org,項目名稱:webz-server,代碼行數:30,代碼來源:WebzTestUtils.java

示例12: proxy

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
public static <T> T proxy(Class<T> expected, MethodHandler handler, Class<?>[] paramTypes, Object[] args) {
    Class<?>[] interfaces = null;
    Class<?> superClass = null;
    if (expected.isInterface()) {
        interfaces = new Class[]{expected};
    } else {
        superClass = expected;
    }
    return proxy(expected, interfaces, superClass, handler, paramTypes, args);
}
 
開發者ID:jboss-openshift,項目名稱:ce-arq,代碼行數:11,代碼來源:BytecodeUtils.java

示例13: narrow

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
public static <T> T narrow(Class<T> expected, final T target) {
    return proxy(expected, new MethodHandler() {
        public Object invoke(Object self, Method method, Method proceed, Object[] args) throws Throwable {
            return method.invoke(target, args);
        }
    });
}
 
開發者ID:jboss-openshift,項目名稱:ce-arq,代碼行數:8,代碼來源:BytecodeUtils.java

示例14: toProxy

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
public static Archive<?> toProxy(final Archive<?> archive, final String newArchiveName) {
    Class<? extends Archive> expected = (archive instanceof EnterpriseArchive) ? EnterpriseArchive.class : WebArchive.class;
    return BytecodeUtils.proxy(expected, new MethodHandler() {
        public Object invoke(Object self, Method method, Method proceed, Object[] args) throws Throwable {
            if ("getName".equals(method.getName())) {
                return newArchiveName;
            } else {
                return method.invoke(archive, args);
            }
        }
    });
}
 
開發者ID:jboss-openshift,項目名稱:ce-arq,代碼行數:13,代碼來源:Archives.java

示例15: newInstance

import javassist.util.proxy.MethodHandler; //導入依賴的package包/類
/**
 * Creates a proxy class of bean and returns an instance of that class.
 *
 * @param context the activity context
 * @param beanRule the bean rule
 * @param constructorArgs the arguments passed to a constructor
 * @param constructorArgTypes the parameter types for a constructor
 * @return a new proxy bean object
 */
public static Object newInstance(ActivityContext context, BeanRule beanRule, Object[] constructorArgs, Class<?>[] constructorArgTypes) {
    try {
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setSuperclass(beanRule.getBeanClass());
        MethodHandler methodHandler = new JavassistDynamicBeanProxy(context, beanRule);
        return proxyFactory.create(constructorArgTypes, constructorArgs, methodHandler);
    } catch (Exception e) {
        throw new ProxyBeanInstantiationException(beanRule.getBeanClass(), e);
    }
}
 
開發者ID:aspectran,項目名稱:aspectran,代碼行數:20,代碼來源:JavassistDynamicBeanProxy.java


注:本文中的javassist.util.proxy.MethodHandler類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。