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);
}
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();
}
}
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);
}
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;
}
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();
}
}
Aggregations