本文整理汇总了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;
}
});
}
示例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;
}
});
}
示例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();
}
示例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();
}
示例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;
}
示例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;
}
示例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();
}
示例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;
}
示例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();
}
示例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));
}
示例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();
}
示例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;
}
示例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;
}
}
示例14: getInvocationHandler
import net.sf.cglib.proxy.InvocationHandler; //导入依赖的package包/类
public static InvocationHandler getInvocationHandler(Object proxy) {
return (InvocationHandler)((Factory)proxy).getCallback(0);
}
示例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);
}
}
}
}
}