@Aspect
public class ThrowOnNullFirstArgAspect {
@Pointcut("" +
"within(@org.springframework.stereotype.Controller *) || " +
"within(@(@org.springframework.stereotype.Controller *) *)")
private void isController() {}
@Around("isController()")
public Object executeAroundController(ProceedingJoinPoint point) throws Throwable {
throwIfNullFirstArgIsPassed(point);
return point.proceed();
}
private void throwIfNullFirstArgIsPassed(ProceedingJoinPoint point) {
if (!(point.getSignature() instanceof MethodSignature)) {
return;
}
if (point.getArgs().length > 0 && point.getArgs()[0] == null) {
throw new IllegalStateException("The first argument is not allowed to be null");
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter