Created
November 14, 2012 16:09
-
-
Save Fuud/4073013 to your computer and use it in GitHub Desktop.
NonnullAspect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.fxdd.common.aspects.notnull; | |
import java.lang.annotation.Annotation; | |
import org.aspectj.lang.JoinPoint; | |
import org.aspectj.lang.Signature; | |
import org.aspectj.lang.annotation.After; | |
import org.aspectj.lang.annotation.AfterReturning; | |
import org.aspectj.lang.annotation.Aspect; | |
import org.aspectj.lang.annotation.Before; | |
import org.aspectj.lang.reflect.CodeSignature; | |
import org.aspectj.lang.reflect.ConstructorSignature; | |
import org.aspectj.lang.reflect.MethodSignature; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import javax.annotation.Nonnull; | |
@Aspect | |
public class NonnullAspect { | |
private static final Logger logger = LoggerFactory.getLogger(NonnullAspect.class); | |
@Before("execution(* *..*.*(@javax.annotation.Nonnull (*), ..)) && args(value, ..)") | |
public void notNull_0(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 0); | |
} | |
@Before("execution(* *..*.*(*, @javax.annotation.Nonnull (*), ..)) && args(*, value, ..)") | |
public void notNull_1(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 1); | |
} | |
@Before("execution(* *..*.*(*, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, value, ..)") | |
public void notNull_2(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 2); | |
} | |
@Before("execution(* *..*.*(*, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, value, ..)") | |
public void notNull_3(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 3); | |
} | |
@Before("execution(* *..*.*(*, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, value, ..)") | |
public void notNull_4(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 4); | |
} | |
@Before("execution(* *..*.*(*, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, value, ..)") | |
public void notNull_5(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 5); | |
} | |
@Before("execution(* *..*.*(*, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, value, ..)") | |
public void notNull_6(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 6); | |
} | |
@Before("execution(* *..*.*(*, *, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, *, value, ..)") | |
public void notNull_7(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 7); | |
} | |
@Before("execution(* *..*.*(*, *, *, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, *, *, value, ..)") | |
public void notNull_8(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 8); | |
} | |
@Before("execution(* *..*.*(*, *, *, *, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, *, *, *, value, ..)") | |
public void notNull_9(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 9); | |
} | |
@Before("execution(* *..*.*(*, *, *, *, *, *, *, *, *, *, .., @javax.annotation.Nonnull (*), ..))") | |
public void notNull_moreThan9(JoinPoint point) throws Throwable { | |
final MethodSignature signature = (MethodSignature) point.getSignature(); | |
final Annotation[][] parameterAnnotations = signature.getMethod().getParameterAnnotations(); | |
for (int i=10; i< parameterAnnotations.length; i++){ | |
for (final Annotation annotation : parameterAnnotations[i]) { | |
if (annotation instanceof Nonnull){ | |
checkNotNull(point, point.getArgs()[i], i); | |
} | |
} | |
} | |
} | |
@Before("execution(*..*.new(@javax.annotation.Nonnull (*), ..)) && args(value, ..)") | |
public void notNull_new_0(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 0); | |
} | |
@Before("execution(*..*.new(*, @javax.annotation.Nonnull (*), ..)) && args(*, value, ..)") | |
public void notNull_new_1(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 1); | |
} | |
@Before("execution(*..*.new(*, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, value, ..)") | |
public void notNull_new_2(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 2); | |
} | |
@Before("execution(*..*.new(*, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, value, ..)") | |
public void notNull_new_3(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 3); | |
} | |
@Before("execution(*..*.new(*, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, value, ..)") | |
public void notNull_new_4(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 4); | |
} | |
@Before("execution(*..*.new(*, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, value, ..)") | |
public void notNull_new_5(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 5); | |
} | |
@Before("execution(*..*.new(*, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, value, ..)") | |
public void notNull_new_6(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 6); | |
} | |
@Before("execution(*..*.new(*, *, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, *, value, ..)") | |
public void notNull_new_7(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 7); | |
} | |
@Before("execution(*..*.new(*, *, *, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, *, *, value, ..)") | |
public void notNull_new_8(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 8); | |
} | |
@Before("execution(*..*.new(*, *, *, *, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, *, *, *, value, ..)") | |
public void notNull_new_9(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 9); | |
} | |
@Before("execution(*..*.new(*, *, *, *, *, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, *, *, *, *, value, ..)") | |
public void notNull_new_10(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 10); | |
} | |
@Before("execution(*..*.new(*, *, *, *, *, *, *, *, *, *, *, @javax.annotation.Nonnull (*), ..)) && args(*, *, *, *, *, *, *, *, *, *, *, value, ..)") | |
public void notNull_new_11(JoinPoint point, Object value) throws Throwable { | |
checkNotNull(point, value, 11); | |
} | |
@Before("execution(*..*.new(*, *, *, *, *, *, *, *, *, *, *, *, .., @javax.annotation.Nonnull (*), ..))") | |
public void notNull_new_moreThan11(JoinPoint point) throws Throwable { | |
final ConstructorSignature signature = (ConstructorSignature) point.getSignature(); | |
final Annotation[][] parameterAnnotations = signature.getConstructor().getParameterAnnotations(); | |
for (int i=10; i< parameterAnnotations.length; i++){ | |
for (final Annotation annotation : parameterAnnotations[i]) { | |
if (annotation instanceof Nonnull){ | |
checkNotNull(point, point.getArgs()[i], i); | |
} | |
} | |
} | |
} | |
@AfterReturning(pointcut="execution(@javax.annotation.Nonnull * *..*.*(..))", returning="value") | |
public void notNull_returnValue(JoinPoint point, Object value) throws Throwable { | |
if (value==null){ | |
final String msg = "method return null"; | |
final NullPointerException exception = new NullPointerException(msg); | |
logger.error(msg, exception); | |
throw exception; | |
} | |
} | |
private void checkNotNull(JoinPoint point, Object value, int parameterIndex) { | |
if (value == null) { | |
final String parameterName = getParameterName(point, parameterIndex); | |
final NullPointerException exception = new NullPointerException(parameterName); | |
logger.error(parameterName + " is null", exception); | |
throw exception; | |
} | |
} | |
private String getParameterName(JoinPoint point, int index) { | |
final Signature signature = point.getSignature(); | |
final CodeSignature code = (CodeSignature) signature; | |
final String name = code.getParameterNames()[index]; | |
return name == null ? String.valueOf(index) : name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment