Search in sources :

Example 21 with Signature

use of org.aspectj.lang.Signature in project rxlib by RockyLOMO.

the class ValidateUtil method onProcess.

/**
 * Annotation expression只对method有效
 *
 * @param joinPoint
 * @param msg
 * @return
 * @throws Throwable
 */
@Override
protected Object onProcess(ProceedingJoinPoint joinPoint, StringBuilder msg) throws Throwable {
    Class targetType = joinPoint.getTarget().getClass();
    Signature signature = joinPoint.getSignature();
    Executable member;
    if (signature instanceof ConstructorSignature) {
        member = ((ConstructorSignature) signature).getConstructor();
    } else {
        member = ((MethodSignature) signature).getMethod();
    }
    msg.setPrefix(String.format("[Valid] %s.%s ", targetType.getSimpleName(), signature.getName()));
    EnableValid attr = member.getAnnotation(EnableValid.class);
    if (attr == null) {
        attr = (EnableValid) targetType.getAnnotation(EnableValid.class);
        if (attr == null) {
            msg.appendLine("skip validate..");
            return joinPoint.proceed();
        }
    }
    int flags = attr.value();
    boolean validateValues = hasFlags(flags, EnableValid.ParameterValues);
    if (hasFlags(flags, EnableValid.Method)) {
        if (signature instanceof ConstructorSignature) {
            ConstructorSignature cs = (ConstructorSignature) signature;
            validateConstructor(cs.getConstructor(), joinPoint.getArgs(), validateValues);
            return super.onProcess(joinPoint, msg);
        }
        MethodSignature ms = (MethodSignature) signature;
        return validateMethod(ms.getMethod(), joinPoint.getTarget(), joinPoint.getArgs(), validateValues, p -> super.onProcess(joinPoint, msg));
    }
    if (validateValues) {
        for (Object parameterValue : joinPoint.getArgs()) {
            validateBean(parameterValue);
        }
    }
    msg.appendLine("validate ok..").setPrefix(null);
    return super.onProcess(joinPoint, msg);
}
Also used : ConstructorSignature(org.aspectj.lang.reflect.ConstructorSignature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) ConstructorSignature(org.aspectj.lang.reflect.ConstructorSignature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Executable(java.lang.reflect.Executable) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint)

Example 22 with Signature

use of org.aspectj.lang.Signature in project vip by guangdada.

the class MultiSourceExAop method around.

@Around("cut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
    Signature signature = point.getSignature();
    MethodSignature methodSignature = null;
    if (!(signature instanceof MethodSignature)) {
        throw new IllegalArgumentException("该注解只能用于方法");
    }
    methodSignature = (MethodSignature) signature;
    Object target = point.getTarget();
    Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
    DataSource datasource = currentMethod.getAnnotation(DataSource.class);
    if (datasource != null) {
        DataSourceContextHolder.setDataSourceType(datasource.name());
        log.debug("设置数据源为:" + datasource.name());
    } else {
        DataSourceContextHolder.setDataSourceType(DSEnum.DATA_SOURCE_GUNS);
        log.debug("设置数据源为:dataSourceCurrent");
    }
    try {
        return point.proceed();
    } finally {
        log.debug("清空数据源信息!");
        DataSourceContextHolder.clearDataSourceType();
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) DataSource(com.ikoori.vip.common.annotion.DataSource) Around(org.aspectj.lang.annotation.Around)

Example 23 with Signature

use of org.aspectj.lang.Signature in project uPortal by Jasig.

the class RequestCacheAspect method createCacheKey.

protected CacheKey createCacheKey(ProceedingJoinPoint pjp, RequestCache requestCache) {
    final Signature signature = pjp.getSignature();
    final Class<?> declaringType = signature.getDeclaringType();
    final String signatureLongString = signature.toLongString();
    final boolean[] keyMask = requestCache.keyMask();
    final Object[] args = pjp.getArgs();
    final Object[] keyArgs;
    if (keyMask.length == 0) {
        keyArgs = args;
    } else if (keyMask.length != args.length) {
        throw new AnnotationFormatError("RequestCache.keyMask has an invalid length on: " + signature.toLongString());
    } else {
        keyArgs = new Object[args.length];
        for (int i = 0; i < args.length; i++) {
            if (keyMask[i]) {
                keyArgs[i] = args[i];
            }
        }
    }
    return CacheKey.build(signatureLongString, declaringType, keyArgs);
}
Also used : Signature(org.aspectj.lang.Signature) AnnotationFormatError(java.lang.annotation.AnnotationFormatError)

Example 24 with Signature

use of org.aspectj.lang.Signature in project FS-Blog by JamesZBL.

the class AspectUtil method getAnnotation.

/**
 * 获取连接点的制定类型的注解
 *
 * @param joinPoint 连接点
 * @param clazz     注解类
 *
 * @return 注解
 */
public static Annotation getAnnotation(ProceedingJoinPoint joinPoint, Class clazz) {
    try {
        // 拦截的对象
        Object object = joinPoint.getTarget();
        Signature signature = joinPoint.getSignature();
        // 拦截方法名
        String methodName = signature.getName();
        Class[] parameterTypes = ((MethodSignature) signature).getMethod().getParameterTypes();
        try {
            // 反射目标方法
            Method method = object.getClass().getDeclaredMethod(methodName, parameterTypes);
            // 获取注解
            return method.getDeclaredAnnotation(clazz);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    } catch (Throwable throwable) {
        throwable.printStackTrace();
    }
    return null;
}
Also used : Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method)

Example 25 with Signature

use of org.aspectj.lang.Signature in project spring-boot by Linda-Tan.

the class MultiDataSourceEx method around.

@Around("cut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
    Signature signature = point.getSignature();
    MethodSignature methodSignature = null;
    if (!(signature instanceof MethodSignature)) {
        throw new IllegalArgumentException("该注解只能用于方法");
    }
    methodSignature = (MethodSignature) signature;
    Object target = point.getTarget();
    Method currentMethod = target.getClass().getMethod(methodSignature.getName(), methodSignature.getParameterTypes());
    DataSource datasource = currentMethod.getAnnotation(DataSource.class);
    DynamicDataSource.setDataSourceType(datasource.name());
    log.info("设置数据源为:" + datasource.name());
    try {
        return point.proceed();
    } finally {
        log.debug("清空数据源信息!");
        DynamicDataSource.clearDataSourceType();
    }
}
Also used : MethodSignature(org.aspectj.lang.reflect.MethodSignature) Signature(org.aspectj.lang.Signature) MethodSignature(org.aspectj.lang.reflect.MethodSignature) Method(java.lang.reflect.Method) DynamicDataSource(com.junliang.spring.config.DynamicDataSource) Around(org.aspectj.lang.annotation.Around)

Aggregations

Signature (org.aspectj.lang.Signature)28 MethodSignature (org.aspectj.lang.reflect.MethodSignature)17 Method (java.lang.reflect.Method)12 Around (org.aspectj.lang.annotation.Around)6 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)4 JoinPoint (org.aspectj.lang.JoinPoint)3 CodeSignature (org.aspectj.lang.reflect.CodeSignature)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Test (org.testng.annotations.Test)2 DataSource (com.ikoori.vip.common.annotion.DataSource)1 BussinessLog (com.ikoori.vip.common.annotion.log.BussinessLog)1 AbstractDictMap (com.ikoori.vip.common.constant.dictmap.base.AbstractDictMap)1 ShiroUser (com.ikoori.vip.server.core.shiro.ShiroUser)1 RequestLockable (com.jim.framework.annotationlock.annotation.RequestLockable)1 DynamicDataSource (com.junliang.spring.config.DynamicDataSource)1 User (gemma.gsec.model.User)1 AnnotationFormatError (java.lang.annotation.AnnotationFormatError)1 Executable (java.lang.reflect.Executable)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1