Java Code Examples for net.sf.cglib.proxy.MethodProxy#invokeSuper()

The following examples show how to use net.sf.cglib.proxy.MethodProxy#invokeSuper() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InterceptorOfASingleElement.java    From java-client with Apache License 2.0 6 votes vote down vote up
/**
 * Look at {@link MethodInterceptor#intercept(Object, Method, Object[], MethodProxy)}.
 */
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
    throws Throwable {

    if (method.getName().equals("toString") && args.length == 0) {
        return locator.toString();
    }

    if (Object.class.equals(method.getDeclaringClass())) {
        return proxy.invokeSuper(obj, args);
    }

    if (WrapsDriver.class.isAssignableFrom(method.getDeclaringClass()) && method.getName()
        .equals("getWrappedDriver")) {
        return driver;
    }

    WebElement realElement = locator.findElement();
    return getObject(realElement, method, args);
}
 
Example 2
Source File: CglibProxy.java    From retry with Apache License 2.0 6 votes vote down vote up
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
    int times = 0;

    while (times < RetryConstant.MAX_TIMES) {
        try {
            //通过代理子类调用父类的方法
            return methodProxy.invokeSuper(o, objects);
        } catch (Exception e) {
            times++;

            if (times >= RetryConstant.MAX_TIMES) {
                throw new RuntimeException(e);
            }
        }
    }

    return null;
}
 
Example 3
Source File: MjProxy.java    From Mars-Java with MIT License 6 votes vote down vote up
/**
 * 绑定代理
 *
 * @param o
 * @param method
 * @param args
 * @param methodProxy
 * @return obj
 * @throws Throwable
 */
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
    MarsGet marsGet = method.getAnnotation(MarsGet.class);
    MarsSelect marsSelect = method.getAnnotation(MarsSelect.class);
    MarsUpdate marsUpdate = method.getAnnotation(MarsUpdate.class);

    int count = checkAnnot(marsGet, marsSelect, marsUpdate);

    if (count == 0) {
        return methodProxy.invokeSuper(o, args);
    } else if (count == 1) {
        return executeMethod(args,method,marsGet,marsSelect,marsUpdate);
    } else {
        throw new Exception(method.getName() + "方法上不允许有多个sql注解");
    }
}
 
Example 4
Source File: SubjectInterceptor.java    From JavaDesignPattern with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy)
    throws Throwable {
  preAction();
  Object result = proxy.invokeSuper(obj, args);
  postAction();
  return result;
}
 
Example 5
Source File: CglibProxyFactory.java    From mybatis with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
  final String methodName = method.getName();
  try {
    synchronized (lazyLoader) {
      if (WRITE_REPLACE_METHOD.equals(methodName)) {
        Object original = null;
        if (constructorArgTypes.isEmpty()) {
          original = objectFactory.create(type);
        } else {
          original = objectFactory.create(type, constructorArgTypes, constructorArgs);
        }
        PropertyCopier.copyBeanProperties(type, enhanced, original);
        if (lazyLoader.size() > 0) {
          return new CglibSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs);
        } else {
          return original;
        }
      } else {
    	//这里是关键,延迟加载就是调用ResultLoaderMap.loadAll()
        if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
          if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
            lazyLoader.loadAll();
          } else if (PropertyNamer.isProperty(methodName)) {
          	//或者调用ResultLoaderMap.load()
            final String property = PropertyNamer.methodToProperty(methodName);
            if (lazyLoader.hasLoader(property)) {
              lazyLoader.load(property);
            }
          }
        }
      }
    }
    return methodProxy.invokeSuper(enhanced, args);
  } catch (Throwable t) {
    throw ExceptionUtil.unwrapThrowable(t);
  }
}
 
Example 6
Source File: RenderMointorIntercetor.java    From gecco with MIT License 5 votes vote down vote up
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
	if(method.getName().equals("inject")) {
		try {
			Object o = proxy.invokeSuper(obj, args);
			return o;
		} catch(RenderException ex) {
			RenderMonitor.incrException(ex.getSpiderBeanClass().getName());
			throw ex;
		}
	} else {
		return proxy.invokeSuper(obj, args);
	}
}
 
Example 7
Source File: CglibDynamicProxy.java    From AndroidFrame with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
    // 在代理真实对象操作前 我们可以添加一些自己的操作
    System.out.println("前置代理,增强处理");

    methodProxy.invokeSuper(o, args);

    // 在代理真实对象操作后 我们也可以添加一些自己的操作
    System.out.println("后置代理,增强处理");
    return null;
}
 
Example 8
Source File: TargetInterceptor.java    From oim-fx with MIT License 5 votes vote down vote up
@Override
public Object intercept(Object obj, Method method, Object[] params, MethodProxy proxy) throws Throwable {
	System.out.println("调用前");  
       Object result = proxy.invokeSuper(obj, params);  
       System.out.println(" 调用后"+result);  
       return result; 
}
 
Example 9
Source File: NulsMethodInterceptor.java    From nuls with MIT License 5 votes vote down vote up
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
    if (method.isBridge()) {
        return methodProxy.invokeSuper(o, objects);
    }
    return interceptor.intercept(o, method, objects, methodProxy);
}
 
Example 10
Source File: CglibProxyFactory.java    From mybaties with Apache License 2.0 5 votes vote down vote up
@Override
public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
  final String methodName = method.getName();
  try {
    synchronized (lazyLoader) {
      if (WRITE_REPLACE_METHOD.equals(methodName)) {
        Object original = null;
        if (constructorArgTypes.isEmpty()) {
          original = objectFactory.create(type);
        } else {
          original = objectFactory.create(type, constructorArgTypes, constructorArgs);
        }
        PropertyCopier.copyBeanProperties(type, enhanced, original);
        if (lazyLoader.size() > 0) {
          return new CglibSerialStateHolder(original, lazyLoader.getProperties(), objectFactory, constructorArgTypes, constructorArgs);
        } else {
          return original;
        }
      } else {
    	//这里是关键,延迟加载就是调用ResultLoaderMap.loadAll()
        if (lazyLoader.size() > 0 && !FINALIZE_METHOD.equals(methodName)) {
          if (aggressive || lazyLoadTriggerMethods.contains(methodName)) {
            lazyLoader.loadAll();
          } else if (PropertyNamer.isProperty(methodName)) {
          	//或者调用ResultLoaderMap.load()
            final String property = PropertyNamer.methodToProperty(methodName);
            if (lazyLoader.hasLoader(property)) {
              lazyLoader.load(property);
            }
          }
        }
      }
    }
    return methodProxy.invokeSuper(enhanced, args);
  } catch (Throwable t) {
    throw ExceptionUtil.unwrapThrowable(t);
  }
}
 
Example 11
Source File: MvcCglibProxy.java    From Mars-Java with MIT License 5 votes vote down vote up
/**
 * 绑定代理
 */
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	LogAop c = null;

	MarsLog marsLog = method.getAnnotation(MarsLog.class);
	if(marsLog != null){
		c = new LogAop(cls,method.getName());
		c.startMethod(args);
	}

	try{
		Object o1 = null;
		if(cls.isInterface()){
			o1 = ExecuteRef.executeRef(cls,method,args);
		} else {
			o1 = methodProxy.invokeSuper(o,args);
		}
		if(c != null){
			c.endMethod(args,o1);
		}
		return o1;
	} catch (Throwable e) {
		if(c != null) {
			c.exp(e);
		}
		throw e;
	}
}
 
Example 12
Source File: CglibProxy.java    From tangyuan2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
	// System.out.println("前置代理: " + method.getName());
	// 通过代理类调用父类中的方法
	Object result = proxy.invokeSuper(obj, args);
	// System.out.println("后置代理: " + method.getName());
	return result;
}
 
Example 13
Source File: StandardJaxrsActionProxy.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Object intercept( Object o, Method method, Object[] objects, MethodProxy methodProxy ) throws Throwable {
    Object result = methodProxy.invokeSuper(o, objects);
    try{
        //尝试记录审计日志
        if( Config.logLevel().audit().enable() ){
            tryToRecordAuditLog( o, method, objects, methodProxy );
        }
    }catch(Exception e){
        e.printStackTrace();
    }
    return result;
}
 
Example 14
Source File: TransactionInterceptor.java    From snakerflow with Apache License 2.0 5 votes vote down vote up
/**
 * 方法执行的拦截器,拦截条件为:
 * 1、数据库访问类是DBTransaction的实现类
 * 2、方法名称匹配初始化的事务方法列表
 */
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
	Object result = null;
	TransactionStatus status = null;
	if(isMatch(method.getName())) {
		if(log.isDebugEnabled()) {
			log.debug("intercept method is[name="  + method.getName() + "]");
		}
		try {
			status = getTransaction();
			AssertHelper.notNull(status);
			//调用具体无事务支持的业务逻辑
			result = proxy.invokeSuper(obj, args);
			//如果整个执行过程无异常抛出,则提交TransactionStatus持有的transaction对象
			if(status.isNewTransaction()) {
				commit(status);
			}
		} catch (Exception e) {
			rollback(status);
			throw new SnakerException(e);
		}
	} else {
		if(log.isDebugEnabled()) {
			log.debug("****don't intercept method is[name="  + method.getName() + "]");
		}
		result = proxy.invokeSuper(obj, args);
	}
	return result;
}
 
Example 15
Source File: MarsBeanProxy.java    From Mars-Java with MIT License 4 votes vote down vote up
/**
 * 绑定代理
 */
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
	AopModel aopModel = null;
	AopModel tractionModel = null;
	RedisLock redisLock = null;
	Boolean hasLock = false;
	/* 分布式锁,解锁时的标识 */
	String val = UUID.randomUUID().toString();
	try {

		MarsAop marsAop = method.getAnnotation(MarsAop.class);
		Traction traction = method.getAnnotation(Traction.class);
		redisLock = method.getAnnotation(RedisLock.class);

		tractionModel = ExecTraction.getAopModel(traction);
		aopModel = ExecAop.getAopModel(marsAop);

		/* 加分布式锁 */
		hasLock = ExecRedisLock.lock(redisLock,val);
		if(!hasLock){
			return null;
		}

		/* 开启事务 */
		ExecTraction.beginTraction(tractionModel);

		/* 执行aop的开始方法 */
		ExecAop.startMethod(args, aopModel);

		/* 执行方法本体 */
		Object o1 = methodProxy.invokeSuper(o, args);

		/* 执行aop的结束方法 */
		ExecAop.endMethod(args,o1,aopModel);

		/* 提交事务 */
		ExecTraction.commit(tractionModel);
		return o1;
	} catch (Throwable e) {
		/* 回滚事务 */
		ExecTraction.rollback(tractionModel,e);
		/* AOP处理异常 */
		ExecAop.exp(aopModel, e);
		throw e;
	} finally {
		/* 解分布式锁, 如果失败了就重试,十次之后还失败,就不管了,10秒后会自动解锁 */
		if(hasLock){
			for(int i = 0;i<10;i++){
				Boolean hasUnlock = ExecRedisLock.unlock(redisLock,val);
				if(hasUnlock){
					break;
				}
			}
		}
	}
}
 
Example 16
Source File: CglibProxyFactory.java    From mybatis with Apache License 2.0 4 votes vote down vote up
@Override
public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
  final Object o = super.invoke(enhanced, method, args);
  return (o instanceof AbstractSerialStateHolder) ? o : methodProxy.invokeSuper(o, args);
}
 
Example 17
Source File: JedisClusterMethodInterceptor.java    From pepper-metrics with Apache License 2.0 4 votes vote down vote up
@Override
protected Object innerInvoke(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
    return proxy.invokeSuper(obj, args);
}
 
Example 18
Source File: CglibProxyFactory.java    From mybaties with Apache License 2.0 4 votes vote down vote up
@Override
public Object intercept(Object enhanced, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
  final Object o = super.invoke(enhanced, method, args);
  return (o instanceof AbstractSerialStateHolder) ? o : methodProxy.invokeSuper(o, args);
}
 
Example 19
Source File: DefaultMethodInterceptor.java    From nuls with MIT License 3 votes vote down vote up
/**
 * 拦截方法
 * Intercept method
 *
 * @param obj         方法所属对象/Method owner
 * @param method      方法定义/Method definition
 * @param params      方法参数列表/Method parameter list
 * @param methodProxy 方法代理器
 * @return 返回拦截的方法的返回值,可以对该值进行处理和替换/Returns the return value of the intercepting method, which can be processed and replaced.
 * @throws Throwable 该方法可能抛出异常,请谨慎处理/This method may throw an exception, handle with care.
 */
@Override
public Object intercept(Object obj, Method method, Object[] params, MethodProxy methodProxy) throws Throwable {
    if (null == method.getDeclaredAnnotations() || method.getDeclaredAnnotations().length == 0) {
        return methodProxy.invokeSuper(obj, params);
    }
    return BeanMethodInterceptorManager.doInterceptor(method.getDeclaredAnnotations(), obj, method, params, methodProxy);
}
 
Example 20
Source File: CGLibProxyHandler.java    From Algorithms with Apache License 2.0 3 votes vote down vote up
/**
 * 回调方法
 *
 * @param o
 * @param method
 * @param objects
 * @param methodProxy
 * @return
 * @throws Throwable
 */
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
    System.out.println("before method invoke...");
    System.out.println(method);
    Object res = methodProxy.invokeSuper(o, objects);
    System.out.println("after method invoke...");
    return res;
}