package cn.itcast.pointcut;
import cn.itcast.annotation.NullDefaultValue;
import cn.itcast.pojo.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
@Aspect
@Component
@Slf4j
public class PointCut {
@Pointcut(“execution(public * cn.itcast.controller.UserController.*(…))”)
//PointCut签名
public void PointCut() {
}
@Around(value = "PointCut()")
public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
log.info("@Around环绕通知:" + proceedingJoinPoint.getSignature().toString());
//
// Object obj = null;
// try {
// Object[] args = proceedingJoinPoint.getArgs();
// for (Object arg : args) {
// if(arg instanceof List){
// List list = (List) arg;
// for (int i = list.size() - 1; i >= 0; i–) {
// if(((Student)list.get(i)).getId()%2 == 0) list.remove(i);
// }
// }
// if(arg instanceof List){
// List list = (List) arg;
// for (Object o : list) {
// System.out.println(o);
// }
// }
// }
// obj = proceedingJoinPoint.proceed(); //可以加参数
//
// } catch (Throwable throwable) {
// throwable.printStackTrace();
// }
// log.info("@Around环绕通知执行结束");
Object[] args = proceedingJoinPoint.getArgs();
for (Object arg : args) {
setNullDefaultValue(arg);
}
return proceedingJoinPoint.proceed(args);
}
public void setNullDefaultValue(Object object) {
Class<?> objClass = object.getClass();
if (objClass.getName().equals(Object.class.getName())) {
return;
}
Field[] objFields = objClass.getDeclaredFields();
for (Field objField : objFields) {
objField.setAccessible(true);
if (objField.isAnnotationPresent(NullDefaultValue.class)) {
String fieldName = objField.getName();
String getMethodStr = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
Method getMethod = objClass.getMethod(getMethodStr);
Object result = getMethod.invoke(object);
if (result == null) {
NullDefaultValue annotation = objField.getAnnotation(NullDefaultValue.class);
String defaultValue = annotation.value();
boolean required = annotation.required();
Class<?> fieldTypeClass = Class.forName(objField.getType().getName());
String setMethodStr = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
Method setMethod = objClass.getMethod(setMethodStr,fieldTypeClass);
if (!required) {
ObjectMapper objectMapper = new ObjectMapper();
//1.如果用json,json用反序列化时按照@JsonProperty反序列化
Object obj = null;
try {
obj = objectMapper.readValue(defaultValue, fieldTypeClass);
setMethod.invoke(object, obj);
} catch (Exception e) {
try {
Method valueOfMethod = fieldTypeClass.getMethod("valueOf", String.class);
valueOfMethod.invoke(object, defaultValue);
} catch (Exception e1) {
}
}
} else {
Method valueOfMethod = fieldTypeClass.getMethod("valueOf", String.class);
setMethod.invoke(object, defaultValue);
}
}
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (ClassNotFoundException e) {
}
}
}
}
}
注解:
package cn.itcast.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface NullDefaultValue {
String value() default “”;
boolean required() default false;
}
package cn.itcast.pointcut;
import cn.itcast.annotation.NotNullArg;
import cn.itcast.annotation.NullDefaultValue;
import cn.itcast.pojo.Student;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
@Aspect
@Component
@Slf4j
public class PointCut {
@Pointcut(“execution(public * cn.itcast.controller.UserController.*(…))”)
//PointCut签名
public void PointCut() {
}
@Around(value = "PointCut()")
public Object doAroundAdvice(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = signature.getMethod();
Object[] args = proceedingJoinPoint.getArgs();
Annotation[][] annotations = method.getParameterAnnotations();
for (int i = 0; i < annotations.length; i++) {
Object param = args[i];
Annotation[] paramAnn = annotations[i];
//参数为空,直接下一个参数
for (Annotation annotation : paramAnn) {
//这里判断当前注解是否为Test.class
if (annotation.annotationType().equals(NotNullArg.class)) {
//校验该参数,验证一次退出该注解
setNullDefaultValue(param);
break;
}
}
}
return proceedingJoinPoint.proceed(args);
}
public void setNullDefaultValue(Object object) {
Class<?> objClass = object.getClass();
if (objClass.getName().equals(Object.class.getName())) {
return;
}
Field[] objFields = objClass.getDeclaredFields();
for (Field objField : objFields) {
objField.setAccessible(true);
if (objField.isAnnotationPresent(NullDefaultValue.class)) {
String fieldName = objField.getName();
String getMethodStr = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
try {
Method getMethod = objClass.getMethod(getMethodStr);
Object result = getMethod.invoke(object);
if (result == null) {
NullDefaultValue annotation = objField.getAnnotation(NullDefaultValue.class);
String defaultValue = annotation.value();
boolean required = annotation.required();
Class<?> fieldTypeClass = Class.forName(objField.getType().getName());
String setMethodStr = "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
Method setMethod = objClass.getMethod(setMethodStr, fieldTypeClass);
if (!required) {
ObjectMapper objectMapper = new ObjectMapper();
//1.如果用json,json用反序列化时按照@JsonProperty反序列化
Object obj = null;
try {
obj = objectMapper.readValue(defaultValue, fieldTypeClass);
setMethod.invoke(object, obj);
} catch (Exception e) {
try {
Method valueOfMethod = fieldTypeClass.getMethod("valueOf", String.class);
valueOfMethod.invoke(object, defaultValue);
} catch (Exception e1) {
}
}
} else {
Method valueOfMethod = fieldTypeClass.getMethod("valueOf", String.class);
setMethod.invoke(object, defaultValue);
}
}
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
} catch (ClassNotFoundException e) {
}
}
}
}
}
package cn.itcast.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface NotNullArg {
}