Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[findFirstAnnotatedArgument] #java #springboot #@aspect
@Aspect
public class ControllerJsonStyleAdvice {
private Object findFirstAnnotatedArgument(JoinPoint joinPoint, Class annotation) {
Object[] args = joinPoint.getArgs();
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
for (int i = 0; i < parameterAnnotations.length; i++) {
for (int j = 0; j < parameterAnnotations[i].length; j++) {
if (annotation.isAssignableFrom(parameterAnnotations[i][j].getClass())) {
return args[i];
}
}
}
return null;
}
@Before(value = "commonPointcut()")
public void setupParams(JoinPoint joinPoint) throws Throwable {
Object requestBodyArgument = findFirstAnnotatedParameter(joinPoint, RequestBody.class);
/* ... */
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment