Spring AOP: как получить значение аннотации вне зависимости от позиции параметра в методе?
Всем привет! Есть код валидации аннотации. Как получить значение аннотации если параметр не первый? || не отрабатывает, и уверен, что это можно сделать как-то проще...
@Component
@Aspect
public class CheckExistValidator {
@PersistenceContext
private EntityManager entityManager;
@Around("execution(* *(@CheckExist (*), ..))) || execution(* *(.., @CheckExist(*))) || execution(* *(*, @CheckExist (*), ..)))")
private Object verify(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Annotation[][] annotationMatrix = methodSignature.getMethod().getParameterAnnotations();
Object[] args = joinPoint.getArgs();
long id = 0;
Class<?> entityClass = null;
boolean isEntityExist;
for (Object arg : args) {
if (!isCanParse(arg)) {
continue;
}
id = Long.parseLong(arg.toString());
}
for (Annotation[] annotations : annotationMatrix) {
for (Annotation annotation : annotations) {
if (annotation instanceof CheckExist) {
entityClass = ((CheckExist) annotation).entityClass();
}
}
}
isEntityExist = entityManager.find(entityClass, id) != null;
if (isEntityExist) {
return joinPoint.proceed();
} else {
throw new NotFoundException(String.format("Сущность с указанным id - %d не существует!", id));
}
}
private boolean isCanParse(Object arg) {
long result = 0;
try {
result = Long.parseLong(arg.toString());
} catch (Exception ignored) {
}
return result != 0;
}
}