当前位置: 首页>>代码示例>>Java>>正文


Java InvocationHandler类代码示例

本文整理汇总了Java中net.sf.cglib.proxy.InvocationHandler的典型用法代码示例。如果您正苦于以下问题:Java InvocationHandler类的具体用法?Java InvocationHandler怎么用?Java InvocationHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


InvocationHandler类属于net.sf.cglib.proxy包,在下文中一共展示了InvocationHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: newProxyInstance

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
/**
 * 生成代理对象
 * @param targetClass  被代理对象的类型(类或接口)
 * @param target       被代理对象实例
 * @return             代理对象
 */
@SuppressWarnings("unchecked")
public <T> T newProxyInstance(final Class<T> targetClass, final Object target) {
    return (T) Enhancer.create(targetClass, new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            before(targetClass, method, args);
            Object ret = null;
            try {
                ret = method.invoke(target, args);
            } catch (Exception e) {
                exception(targetClass, method, args, e);
            }
            after(targetClass, method, args);
            return ret;
        }
    });
}
 
开发者ID:huhuics,项目名称:tauren,代码行数:24,代码来源:ProxyInterceptor.java

示例2: newProxyInstance

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
@Override
@SuppressWarnings("unchecked")
public <T> T newProxyInstance(final Class<T> targetClass, final Object target) {
    return (T) Enhancer.create(targetClass, new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

            /**
             * 如果没有@Transaction,则直接调用原方法
             */
            if (!method.isAnnotationPresent(Transaction.class)) {
                return method.invoke(target, args);
            }

            before(targetClass, method, args);
            Object ret = null;
            try {
                ret = method.invoke(target, args);
            } catch (Exception e) {
                exception(targetClass, method, args, e);
            }
            after(targetClass, method, args);
            return ret;
        }
    });
}
 
开发者ID:huhuics,项目名称:tauren,代码行数:27,代码来源:TransactionPartialInterceptor.java

示例3: doBuild

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
/**
 * Build CGLIB <i>implementee</i> bean.
 * 
 * @param implementation
 * @param implementorBeanFactory
 * @return
 */
protected Object doBuild(
		Implementation<?> implementation,
		ImplementorBeanFactory implementorBeanFactory)
{
	InvocationHandler invocationHandler = new CglibImplementeeInvocationHandler(
			implementation, implementorBeanFactory,
			implementeeMethodInvocationFactory);

	Enhancer enhancer = new Enhancer();
	enhancer.setInterfaces(new Class[] { CglibImplementee.class });
	enhancer.setSuperclass(implementation.getImplementee());
	enhancer.setCallback(invocationHandler);

	return enhancer.create();
}
 
开发者ID:ximplementation,项目名称:ximplementation-spring,代码行数:23,代码来源:CglibImplementeeBeanBuilder.java

示例4: getSuperPoweredRequest

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
private ServletWebRequest getSuperPoweredRequest(ServletWebRequest request, HttpServletRequest superPoweredMockRequest) {
    if (isSuperPowered(request)) {
        return request;
    }
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(SingularServletWebRequest.class);
    enhancer.setInterfaces(new Class[]{SuperPowered.class});
    enhancer.setCallback(new InvocationHandler() {
        @Override
        public Object invoke(Object o, Method method, Object[] objects)
                throws InvocationTargetException, IllegalAccessException {
            if ("getContainerRequest".equals(method.getName())) {
                return superPoweredMockRequest;
            }
            return method.invoke(request, objects);
        }
    });
    return (ServletWebRequest) enhancer.create();
}
 
开发者ID:opensingular,项目名称:singular-server,代码行数:20,代码来源:SingularTestRequestCycleListener.java

示例5: getDomInvocationHandler

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
@Nullable
public static DomInvocationHandler getDomInvocationHandler(DomElement proxy) {
  if (proxy instanceof DomFileElement) {
    return null;
  }
  if (proxy instanceof DomInvocationHandler) {
    return (DomInvocationHandler)proxy;
  }
  final InvocationHandler handler = AdvancedProxy.getInvocationHandler(proxy);
  if (handler instanceof StableInvocationHandler) {
    //noinspection unchecked
    final DomElement element = ((StableInvocationHandler<DomElement>)handler).getWrappedElement();
    return element == null ? null : getDomInvocationHandler(element);
  }
  if (handler instanceof DomInvocationHandler) {
    return (DomInvocationHandler)handler;
  }
  return null;
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:20,代码来源:DomManagerImpl.java

示例6: getDomInvocationHandler

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
@Nullable
public static DomInvocationHandler getDomInvocationHandler(DomElement proxy) {
  if (proxy instanceof DomFileElement) {
    return null;
  }
  if (proxy instanceof DomInvocationHandler) {
    return (DomInvocationHandler)proxy;
  }
  final InvocationHandler handler = AdvancedProxy.getInvocationHandler(proxy);
  if (handler instanceof StableInvocationHandler) {
    final DomElement element = ((StableInvocationHandler<DomElement>)handler).getWrappedElement();
    return element == null ? null : getDomInvocationHandler(element);
  }
  if (handler instanceof DomInvocationHandler) {
    return (DomInvocationHandler)handler;
  }
  return null;
}
 
开发者ID:lshain-android-source,项目名称:tools-idea,代码行数:19,代码来源:DomManagerImpl.java

示例7: getProxyClass

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
public static Class<?> getProxyClass(Class<?> clazz) {
    Enhancer e = new Enhancer();
    if (clazz.isInterface()) {
     e.setSuperclass(clazz);
    } else {
     e.setSuperclass(clazz);
     e.setInterfaces(clazz.getInterfaces());
    }
    e.setCallbackTypes(new Class[]{
        InvocationHandler.class,
        NoOp.class,
    });
    e.setCallbackFilter(BAD_OBJECT_METHOD_FILTER);
    e.setUseFactory(true);
    e.setNamingPolicy(new LithiumTestProxyNamingPolicy());
    return e.createClass();
}
 
开发者ID:lithiumtech,项目名称:multiverse-test,代码行数:18,代码来源:FunctionalTestClassLoader.java

示例8: getDomInvocationHandler

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
@Nullable
public static DomInvocationHandler getDomInvocationHandler(DomElement proxy)
{
	if(proxy instanceof DomFileElement)
	{
		return null;
	}
	if(proxy instanceof DomInvocationHandler)
	{
		return (DomInvocationHandler) proxy;
	}
	final InvocationHandler handler = AdvancedProxy.getInvocationHandler(proxy);
	if(handler instanceof StableInvocationHandler)
	{
		//noinspection unchecked
		final DomElement element = ((StableInvocationHandler<DomElement>) handler).getWrappedElement();
		return element == null ? null : getDomInvocationHandler(element);
	}
	if(handler instanceof DomInvocationHandler)
	{
		return (DomInvocationHandler) handler;
	}
	return null;
}
 
开发者ID:consulo,项目名称:consulo-xml,代码行数:25,代码来源:DomManagerImpl.java

示例9: newProxyInstance

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> T newProxyInstance(Class<T> clazz, final Object target) {
    Enhancer enhancer = new Enhancer();
    enhancer.setSuperclass(clazz);
    enhancer.setCallback(new InvocationHandler() {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("invoked by proxy");
            return method.invoke(target, args);
        }
    });

    return (T) enhancer.create();
}
 
开发者ID:huhuics,项目名称:tauren,代码行数:15,代码来源:ProxyUtil.java

示例10: testExtendClass

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
public void testExtendClass() throws Throwable {
  final List<String> invocations = new ArrayList<String>();
  Implementation implementation = AdvancedProxy.createProxy(Implementation.class, new Class[]{Interface3.class}, new InvocationHandler(){
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      invocations.add(method.getName());
      if (Object.class.equals(method.getDeclaringClass())) {
        return method.invoke(this, args);
      }
      return Implementation.class.getMethod("getField").invoke(proxy);
    }
  }, "239");
  implementation.hashCode();
  implementation.method();
  assertEquals("239", implementation.getFoo());
  implementation.setField("42");
  assertEquals("42", implementation.getBar());
  assertEquals("42", implementation.toString());
  assertEquals(Arrays.asList("hashCode", "getFoo", "getFoo", "getBar"), invocations);

  assertEquals("42", Interface1.class.getMethod("getFoo").invoke(implementation));

  assertEquals("42", Interface3.class.getMethod("bar").invoke(implementation));

  assertEquals("42", Interface1.class.getMethod("foo").invoke(implementation));
  assertEquals("42", Interface2.class.getMethod("foo").invoke(implementation));
  assertEquals("42", Interface2.class.getMethod("foo").invoke(implementation));
  assertEquals("42", Implementation.class.getMethod("foo").invoke(implementation));
}
 
开发者ID:jskierbi,项目名称:intellij-ce-playground,代码行数:30,代码来源:ProxyTest.java

示例11: getProxyFactory

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
public static Class getProxyFactory(Class persistentClass, Class[] interfaces)
		throws HibernateException {
	Enhancer e = new Enhancer();
	e.setSuperclass( interfaces.length == 1 ? persistentClass : null );
	e.setInterfaces(interfaces);
	e.setCallbackTypes(new Class[]{
		InvocationHandler.class,
		NoOp.class,
  		});
 		e.setCallbackFilter(FINALIZE_FILTER);
 		e.setUseFactory(false);
	e.setInterceptDuringConstruction( false );
	return e.createClass();
}
 
开发者ID:cacheonix,项目名称:cacheonix-core,代码行数:15,代码来源:CGLIBLazyInitializer.java

示例12: createProxy

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
private URLClassLoader createProxy(final ClassLoader cl) throws MalformedURLException {
	Enhancer e = new Enhancer();
	e.setSuperclass(URLClassLoader.class);
	e.setCallback(new InvocationHandler() {

		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
			if (method.getName().equals("getResources") && "META-INF/persistence.xml".equals((String) args[0])) {
				final String persistenceContent = generateXml();

				final File file = getTempFile();
				BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
				bufferedWriter.write(persistenceContent);
				bufferedWriter.close();
				final URL url = file.toURI().toURL();
				return java.util.Collections.enumeration(new HashSet<URL>(Arrays.asList(url)));
			} else {
				return method.invoke(cl, args);
			}
		}
	});

	URL[] arrayDeURL = new URL[] { new URL("http://google.com.br") };
	Class<?>[] array = new Class<?>[] { arrayDeURL.getClass() };

	URLClassLoader f = (URLClassLoader) e.create(array, new Object[] { arrayDeURL });
	return f;
}
 
开发者ID:geraldox100,项目名称:testyourquery,代码行数:28,代码来源:RuntimePersistenceGenerator.java

示例13: createInterceptorFor

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
public Object createInterceptorFor(Object instanceToIntercept)
	{
		if (getInterceptorClassToBeUsed() != null)
		{
			try
			{


//				Class[] parameterTypes = {Object.class};
//				Object[] parameters = {instanceToIntercept};
//              Constructor constructor = getInterceptorClassToBeUsed().getConstructor(parameterTypes);
//				InvocationHandler handler = (InvocationHandler) constructor.newInstance(parameters);
                // use helper class to instantiate
                InvocationHandler handler = (InvocationHandler) ClassHelper.newInstance(
                                            getInterceptorClassToBeUsed(), Object.class, instanceToIntercept);
				Class[] interfaces = computeInterfaceArrayFor(instanceToIntercept.getClass());
				Object result =
					Proxy.newProxyInstance(
						ClassHelper.getClassLoader(),
						interfaces,
						handler);
				return result;
			}
			catch (Throwable t)
			{
				LoggerFactory.getDefaultLogger().error("can't use Interceptor " + getInterceptorClassToBeUsed().getName() +
					"for " + instanceToIntercept.getClass().getName(), t);
				return instanceToIntercept;
			}
		}
		else
		{
			return instanceToIntercept;
		}
	}
 
开发者ID:KualiCo,项目名称:ojb,代码行数:36,代码来源:InterceptorFactory.java

示例14: getInvocationHandler

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
public static InvocationHandler getInvocationHandler(Object proxy) {
	return (InvocationHandler)((Factory)proxy).getCallback(0);
}
 
开发者ID:lithiumtech,项目名称:multiverse-test,代码行数:4,代码来源:FunctionalTestClassLoader.java

示例15: copyDelegateFields

import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private static void copyDelegateFields(Object target, InvocationHandler source) {
	Object obj = ((RemoteProxy)source).obj;
	if (obj == null) {
		return;
	}
	if (!(obj.getClass().getClassLoader() instanceof FunctionalTestClassLoader)) {
		throw new ClassLoaderException("Failed wrapping field on remote object - wrong classloader type");
	}
	FunctionalTestClassLoader classLoader = (FunctionalTestClassLoader)obj.getClass().getClassLoader();
	
	// get the source fields
	Field[] fields = getAllFields(obj.getClass());
	
	// get the target proxy parent fields
	Field[] targetFields = getAllFields(target.getClass().getSuperclass());
	
	for(Field field : fields) {
		if (!Modifier.isStatic(field.getModifiers())) {
			/**
			 * The @TestVisible annotation tells the framework to automatically bring the value 
			 * from the child classloader into the proxy for easy access during a test.
			 * 
			 * @TestVisible
			 * @Autowired
			 * private final MyInterface myDependencyInjectedValue = null;
			 * 
			 * ...
			 * {
			 * 		...
			 * 		myTestClass.myDependencyInjectedValue.getValue();
			 * 		...
			 * }
			 * 
			 */
			Class<? extends Annotation> testVisible;
			testVisible = (Class<? extends Annotation>)getClassFromClassLoader(TestVisible.class, classLoader);
			if (field.isAnnotationPresent(testVisible)) {
				// copy the values over for final fields					
				Field targetField = null;
				try {
					for(Field f : targetFields) {
						if (f.getName().equals(field.getName())) {
							targetField = f;
						}
					}
				} catch (Exception e) {
					 // benign
				}
				if (targetField != null) {
					copyProxiedValue(field, obj, targetField, target, classLoader);
				}
			}
		}
	}
}
 
开发者ID:lithiumtech,项目名称:multiverse-test,代码行数:57,代码来源:FunctionalTestClassLoader.java


注:本文中的net.sf.cglib.proxy.InvocationHandler类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。