use of org.aspectj.lang.reflect.MethodSignature in project Hotchpotch by carryxyh.
the class Check method validator.
/**
* 拦截所有使用Validator注解的接口,并进行参数合法有效性验证
* @param joinPoint
* @return
* @throws Throwable
*/
@Around("@annotation(com.dfire.validator.annotation.Validator)")
public Object validator(ProceedingJoinPoint joinPoint) throws Throwable {
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
Object[] objects = joinPoint.getArgs();
LocalVariableTableParameterNameDiscoverer u = new LocalVariableTableParameterNameDiscoverer();
// 获得接口实现类的所有方法
Method[] methods = joinPoint.getTarget().getClass().getDeclaredMethods();
Map<String, Integer> paramMap = new HashMap<>();
for (Method method1 : methods) {
// 判断接口名与参数是否一致
if (StringUtils.equals(method.getName(), method1.getName()) && method.getParameterTypes().length == method1.getParameterTypes().length && checkMethod(method, method1)) {
String[] params = u.getParameterNames(method1);
for (int i = 0; i < params.length; i++) {
paramMap.put(params[i], i);
}
break;
}
}
// 取得需要验证的注解对象
Validator interfaceValidator = method.getAnnotation(Validator.class);
// 取得明细验证规则
Check[] params = interfaceValidator.value();
int index = 0;
for (int i = 0; i < params.length; i++) {
Check p = params[i];
Integer idx = paramMap.get(p.name());
index = idx != null ? idx : i;
// 如未指定验证参数名字,且当前规则所在索引大于参数个数时,报出异常
if (index >= objects.length) {
throw new Exception("未指定name或name错误 的注解索引已 大于 参数个数,请检查!注:name需与实现参数名一致!");
}
// 验证参数对象
Object o = objects[index];
Class<? extends ValidatorAdapter> vda = p.adapter();
// 如未指定适配器,则默认使用oval验证对象
if (vda.getName().equals(ValidatorAdapter.class.getName())) {
if (o != null) {
// 当验证对象不为null,使用oval验证框架验证
net.sf.oval.Validator validator = new net.sf.oval.Validator();
List<ConstraintViolation> ret = validator.validate(o);
if (ret != null && ret.size() > 0) {
return processResult(p.name(), ret.get(0).getErrorCode(), ret.get(0).getMessage(), method.getReturnType());
}
}
} else {
ValidatorAdapter adapter = (ValidatorAdapter) Class.forName(vda.getName()).newInstance();
// 根据注解相应设置的条件调用相应的验证方法
if ((p.v().length == 0 && !adapter.validate(o)) || (p.v().length == 1 && !adapter.validate(o, p.v()[0])) || (p.v().length == 2 && !adapter.validate(o, p.v()[0], p.v()[1]))) {
String message = StringUtils.isBlank(p.message()) ? adapter.getMessage() : p.message();
return processResult(p.name(), p.errorCode(), message, method.getReturnType());
}
}
}
// 调用目标方法
return joinPoint.proceed();
}
use of org.aspectj.lang.reflect.MethodSignature in project trafficController by amitkhosla.
the class QueueAnnotationsHandler method consume.
/**
* This method handles {@link Consumer} annotated flows.
* This method on call will register a consumer (if not already registered) and return the value to caller.
* @param joinPoint Join point of the caller method
* @param queueConsumer Consumer
* @return Output of the method
* @throws Throwable Throws in case of failure in processing
*/
@Around("execution(@org.ak.trafficController.messaging.annotations.Consumer * *(..)) && @annotation(queueConsumer)")
public Object consume(ProceedingJoinPoint joinPoint, Consumer queueConsumer) throws Throwable {
// System.out.println("called2....");
Object objectHavingMethod = joinPoint.getTarget();
MethodSignature signature = ((MethodSignature) joinPoint.getSignature());
Method method = signature.getMethod();
if (!consumeMethodsProcessed.containsKey(method)) {
boolean batch = queueConsumer.batch();
String queueName = getQueueName(queueConsumer.name(), joinPoint, batch, CONSUMER);
initializeQueue(queueConsumer, objectHavingMethod, method, batch, queueName);
consumeMethodsProcessed.put(method, true);
}
return joinPoint.proceed();
}
use of org.aspectj.lang.reflect.MethodSignature in project data-prep by Talend.
the class AsyncAspect method getGroupId.
/**
* Return the async execution group id from the given proceeding join point.
*
* @param pjp the proceeding join point.
* @return the async execution group id from the proceeding join point.
*/
private String getGroupId(ProceedingJoinPoint pjp) {
MethodSignature ms = (MethodSignature) pjp.getSignature();
Method m = ms.getMethod();
final AsyncOperation asyncOperationAnnotation = m.getAnnotation(AsyncOperation.class);
// try with the groupIdGeneratorBean first
Class<? extends GroupIdGenerator> generatorClass = asyncOperationAnnotation.groupIdGeneratorBean();
if (generatorClass != AsyncOperation.DEFAULT.class) {
try {
final GroupIdGenerator generatorBean = applicationContext.getBean(generatorClass);
return generatorBean.getGroupId(pjp);
} catch (Exception e) {
LOGGER.warn("could not get the async group id form {} because {}, let's try with the group id generator class", pjp.toLongString(), e);
}
}
// as a fallback, let's try with the generator class
generatorClass = asyncOperationAnnotation.groupIdGeneratorClass();
try {
final GroupIdGenerator idGenerator = generatorClass.newInstance();
return idGenerator.getGroupId(pjp);
} catch (Exception e) {
LOGGER.warn("could not get the async group id form {} because {}, let's try with the group id generator class", pjp.toLongString(), e);
}
return null;
}
use of org.aspectj.lang.reflect.MethodSignature in project data-prep by Talend.
the class AnnotationGroupIdGenerator method getGroupId.
/**
* @see GroupIdGenerator#getGroupId(ProceedingJoinPoint)
*/
@Override
public String getGroupId(ProceedingJoinPoint pjp) {
// look for the @AsyncGroupId annotated parameter
int idParameterIndex = AnnotationUtils.getAnnotatedParameterIndex(pjp, AsyncGroupId.class);
// to get the @AsyncGroupId parameter value
final String asyncGroupId;
if (idParameterIndex >= 0) {
MethodSignature ms = (MethodSignature) pjp.getSignature();
final Class groupIdParameterType = ms.getParameterTypes()[idParameterIndex];
if (AsyncGroupKey.class.isAssignableFrom(groupIdParameterType)) {
final AsyncGroupKey paramValue = (AsyncGroupKey) pjp.getArgs()[idParameterIndex];
asyncGroupId = paramValue.getAsyncGroupKey();
} else {
asyncGroupId = String.valueOf(pjp.getArgs()[idParameterIndex]);
}
} else {
asyncGroupId = null;
}
return asyncGroupId;
}
use of org.aspectj.lang.reflect.MethodSignature in project data-prep by Talend.
the class AspectHelper method getAnnotation.
public static <T> T getAnnotation(ProceedingJoinPoint pjp, Class<T> annotationClass) {
final MethodSignature methodSignature = ((MethodSignature) pjp.getSignature());
final Annotation[] annotations = methodSignature.getMethod().getAnnotations();
T lookupAnnotation = null;
for (Annotation annotation : annotations) {
if (annotation.annotationType().equals(annotationClass)) {
lookupAnnotation = (T) annotation;
break;
}
}
return lookupAnnotation;
}
Aggregations