package com.amap.klun.aspect;
import com.amap.qinling.entity.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import java.lang.reflect.Field;
@Aspect
@Component
public class StringBullTransJsonAspect {
@Pointcut(value = "@annotation(org.springframework.web.bind.annotation.PostMapping) || @annotation(org.springframework.web.bind.annotation.PutMapping)")
public void poincutAspect(){
}
@Before(value = "poincutAspect()")
public static <T extends BaseEntity> void aspectAround(JoinPoint joinPoint){
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
try {
Class<?> aClass = arg.getClass();
if(!(BaseEntity.class.isAssignableFrom(aClass))){
continue;
}
Field[] fields = aClass.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
if(!field.getType().getName().equals("java.lang.String")){continue;};
if(!field.isAnnotationPresent(ApiModelProperty.class)){continue;}
Boolean contains = field.getAnnotation(ApiModelProperty.class).value().contains("json");
if(!contains){continue;}
Object value = field.get(arg);
if(!ObjectUtils.isEmpty(value)){continue;}
field.set(arg,null);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
}