Java Code Examples for org.aspectj.lang.reflect.MethodSignature#getReturnType()

The following examples show how to use org.aspectj.lang.reflect.MethodSignature#getReturnType() . 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: DataSourceAspect.java    From x7 with Apache License 2.0 6 votes vote down vote up
@Around("cut() && @annotation(readOnly) ")
public Object around(ProceedingJoinPoint proceedingJoinPoint, ReadOnly readOnly) {

    Signature signature = proceedingJoinPoint.getSignature();
    MethodSignature ms = ((MethodSignature) signature);
    try {
        DataSourceContextHolder.set(DataSourceType.READ);
        if (ms.getReturnType() == void.class) {
            proceedingJoinPoint.proceed();
            return null;
        } else {
            return proceedingJoinPoint.proceed();
        }
    } catch (Throwable e) {
        throw new RuntimeException(ExceptionUtil.getMessage(e));
    }finally {
        DataSourceContextHolder.remove();
    }
}
 
Example 2
Source File: CacheAspectExecutor.java    From easyooo-framework with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public Object selectMergingByPrimaryKey(ProceedingJoinPoint pjp,
		Object bean) throws Throwable {
	if(!checkHasCache(bean)){
		return pjp.proceed();
	}
	MethodSignature method = (MethodSignature) pjp.getSignature();
	MergingStrategy ms = method.getMethod().getAnnotation(
			MergingStrategy.class);
	if (ms == null) {
		return pjp.proceed();
	}
	Class<Object> dtoClass = method.getReturnType();
	return dataProxy.selectMergingByPrimaryKey(bean,
			new DefaultDelegater<Object>(pjp), dtoClass);
}
 
Example 3
Source File: Pulsar.java    From Milkomeda with MIT License 4 votes vote down vote up
/**
 * 对使用了 @PulsarFlow 注解实现环绕切面
 *
 * @param joinPoint 切面连接点
 * @return 响应数据对象
 * @throws Throwable 可抛出异常
 */
@Around("@annotation(com.github.yizzuide.milkomeda.pulsar.PulsarFlow)")
Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    // 检测方法返回值
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    String invokeMethodName = joinPoint.getSignature().getName();
    if (methodSignature.getReturnType() != Object.class) {
        throw new ClassCastException("You must set [Object] return type on method " +
                invokeMethodName);
    }

    // 获取注解信息
    PulsarFlow pulsarFlow = getAnnotation(joinPoint, PulsarFlow.class);

    // 如果没有设置DeferredResult,则使用WebAsyncTask
    if (!pulsarFlow.useDeferredResult()) {
        // 返回异步任务(交给统一异常处理响应)
        /*if (null != timeoutCallback) {
            webAsyncTask.onTimeout(() -> this.timeoutCallback);
        }*/
        return new WebAsyncTask<>(new WebAsyncTaskCallable(joinPoint));
    }

    // 使用DeferredResult方式
    DeferredResult<Object> deferredResult = new DeferredResult<>();

    // 设置DeferredResult的错误处理(交给统一异常处理响应)
    /*if (null != timeoutCallback) {
        // 适配超时处理
        deferredResult.onTimeout(() -> deferredResult.setResult(this.timeoutCallback.get()));
    }
    if (null != errorCallback) {
        deferredResult.onError((throwable) -> deferredResult.setErrorResult(errorCallback.apply(throwable)));
    }*/

    // 创建增强DeferredResult
    PulsarDeferredResult pulsarDeferredResult = new PulsarDeferredResult();
    pulsarDeferredResult.setDeferredResult(deferredResult);

    // 准备设置DeferredResultID
    String id = pulsarFlow.id();
    String idValue = null;
    if (!StringUtils.isEmpty(id)) {
        // 解析表达式
        idValue = extractValue(joinPoint, id);
        pulsarDeferredResult.setDeferredResultID(idValue);
        // 注解设置成功,放入容器
        putDeferredResult(pulsarDeferredResult);
    }

    // 调用方法实现
    Object returnObj = joinPoint.proceed(injectParam(joinPoint, pulsarDeferredResult, pulsarFlow,
            StringUtils.isEmpty(idValue)));

    // 方法有返回值且不是DeferredResult,则不作DeferredResult处理
    if (null != returnObj && !(returnObj instanceof DeferredResult)) {
        // 通过注解存放过,则删除
        if (null != idValue) {
            removeDeferredResult(idValue);
        }
        return returnObj;
    }

    // 检查是否设置标识
    if (null == pulsarDeferredResult.getDeferredResultID()) {
        throw new IllegalArgumentException("You must invoke setDeferredResultID method of PulsarDeferredResult parameter on method " +
                invokeMethodName);
    }

    // 如果注解没有设置,在方法设置后放入容器
    if (null == idValue) {
        putDeferredResult(pulsarDeferredResult);
    }

    // 无论超时还是成功响应,删除这个DeferredResult
    deferredResult.onCompletion(() -> removeDeferredResult(pulsarDeferredResult.getDeferredResultID()));

    // 返回
    return deferredResult;
}
 
Example 4
Source File: FallbackOnlyAspect.java    From x7 with Apache License 2.0 4 votes vote down vote up
@Around("cut() && @annotation(fallbackOnly) ")
public Object around(ProceedingJoinPoint proceedingJoinPoint, FallbackOnly fallbackOnly) {

    long startTime = System.currentTimeMillis();

    Object[] args = proceedingJoinPoint.getArgs();

    Signature signature = proceedingJoinPoint.getSignature();
    String logStr = signature.getDeclaringTypeName() + "." + signature.getName();

    if (args == null || args.length == 0)
        throw new IllegalArgumentException(logStr + ", @fallbackOnly not support no args' method");

    Class<? extends Throwable>[] clzzArr = fallbackOnly.exceptions();

    try {
        MethodSignature ms = ((MethodSignature) signature);
        if (ms.getReturnType() == void.class) {
            proceedingJoinPoint.proceed();
            return null;
        } else {
            return proceedingJoinPoint.proceed();
        }
    } catch (Throwable e) {

        for (Class<? extends Throwable> clzz : clzzArr) {
            if (e.getClass() == clzz || e.getClass().isAssignableFrom(clzz)) {
                Class fallbackClzz = fallbackOnly.fallback();
                if (fallbackClzz == void.class)
                    break;
                try {
                    Object obj = fallbackClzz.newInstance();
                    String methodName = signature.getName();
                    Class[] clzzs= new Class[args.length];
                    for (int i=0; i<args.length; i++){
                        clzzs[i] = args[i].getClass();
                    }
                    fallbackClzz.getDeclaredMethod(methodName,clzzs).invoke(obj,args);
                }catch (Exception ee){
                    e.printStackTrace();
                }
                break;
            }
        }

        throw new RuntimeException(ExceptionUtil.getMessage(e));
    }

}
 
Example 5
Source File: WebAop.java    From x7 with Apache License 2.0 2 votes vote down vote up
@Around("cut()")
	public Object around(ProceedingJoinPoint proceedingJoinPoint) {

		org.aspectj.lang.Signature signature = proceedingJoinPoint.getSignature();
		MethodSignature ms = ((MethodSignature) signature);


		{

			long startTime = TimeUtil.now();

			try {
				Object obj = null;

				Class returnType = ms.getReturnType();
				if (returnType == void.class) {

					proceedingJoinPoint.proceed();
				} else {
					obj = proceedingJoinPoint.proceed();
				}


				long endTime = TimeUtil.now();
				long handledTimeMillis = endTime - startTime;
				System.out.println("________Transaction end, cost time: " + (handledTimeMillis) + "ms");
				if (obj instanceof ViewEntity){
					ViewEntity ve = (ViewEntity)obj;
					ve.setHandledTimeMillis(handledTimeMillis);
				}

				return obj;
			} catch (Throwable e) {

				System.out.println("________Transaction rollback:" + ExceptionUtil.getMessage(e));


//				if(e instanceof HystrixRuntimeException){
//					return ViewEntity.toast("服务繁忙, 请稍后");
//				}

				String msg = ExceptionUtil.getMessage(e);


				return ViewEntity.toast(msg);
			}
		}
	}
 
Example 6
Source File: PollableAspect.java    From mojito with Apache License 2.0 2 votes vote down vote up
/**
 * Gets the return type of the instrumented method.
 *
 * @param pjp contains the instrumented method
 * @return the return type of the instrumented method.
 */
private Class getFunctionReturnType(ProceedingJoinPoint pjp) {
    logger.debug("Get the return type of the instrumented method");
    MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
    return methodSignature.getReturnType();
}