Skip to content

Instantly share code, notes, and snippets.

@elegantcoder
Last active September 25, 2020 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elegantcoder/b008af8c7b81c88ddf857f572ab739d4 to your computer and use it in GitHub Desktop.
Save elegantcoder/b008af8c7b81c88ddf857f572ab739d4 to your computer and use it in GitHub Desktop.
[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