自定义枚举拦截工具

自定义枚举拦截工具,第1张

自定义枚举拦截工具

package com.jjfinder.retail.admin.aspect;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.Arrays;
import java.util.Objects;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import com.jjfinder.retail.common.annotation.Trim;
import com.jjfinder.retail.common.model.baseModel;


@Component
@Aspect
@Order(4)
public class ParamFilterAspect {
	public static final String SET = "set";
	public static final String GET = "get";
	@Around("execution(* com.jjfinder.retail.admin.*.controller..*(..))")
	public Object paramFilter(ProceedingJoinPoint jp) throws Throwable {
		Object[] args = jp.getArgs();
		for (Object obj : args) {
			if (baseModel.class.isAssignableFrom(obj.getClass())) {
				Field[] fields = obj.getClass().getDeclaredFields();
				for (Field field : fields) {
					Annotation[] annotations = field.getAnnotations();
					for (Annotation annotation : annotations) {
						if (Trim.class.isAssignableFrom(annotation.getClass())) {
							if (isNeedFilter(jp, annotation)) {
								Method setMethod = obj.getClass().getMethod(getFieldSetMethod(field.getName()), String.class);
								Method getMethod = obj.getClass().getMethod(getFieldGetMethod(field.getName()));
								String value = (String)getMethod.invoke(obj);
								setMethod.invoke(obj, value.trim());
							}
						}
					}
				}
			}
		}
		Object result = jp.proceed(args);
		return result;
	}

	public static Boolean isNeedFilter(ProceedingJoinPoint jp, Annotation annotation) {
		Class validClass = null;
		MethodSignature signature = (MethodSignature) jp.getSignature();
		Method method = signature.getMethod();
		Parameter[] parameters = method.getParameters();
		for (Parameter parameter : parameters) {
			if (parameter.isAnnotationPresent(Validated.class)) {
				Validated validate = parameter.getAnnotation(Validated.class);
				validClass = validate.value()[0];
				Trim fieldAnnotation = (Trim) annotation;
				Class[] groupClass = fieldAnnotation.groups();
				if (Objects.isNull(validClass) && (Objects.isNull(groupClass) || groupClass.length == 0)) return true;
				return Arrays.asList(groupClass).contains(validClass);
			}
		}
		return false;
	}

	public static String getFieldSetMethod(String field) {
		return SET + StringUtils.capitalize(field);
	}

	public static String getFieldGetMethod(String field) {
		return GET + StringUtils.capitalize(field);
	}
}
@documented
@Constraint(validatedBy = TrimValidator.class)
@Target({FIELD})
@Retention(RUNTIME)
@ReportAsSingleViolation
@NotNull
public @interface Trim {
    String message() default "{org.hibernate.validator.constraints.NotBlank.message}";

    Class[] groups() default {};

    Class[] payload() default {};

}
public class TrimValidator implements ConstraintValidator {
	
	@Override
	public void initialize(Trim annotation) {
	}

	@Override
	public boolean isValid(CharSequence charSequence, ConstraintValidatorContext constraintValidatorContext) {
		if (charSequence == null) {
			return true;
		}
		return charSequence.toString().trim().length() > 0;
	}
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5686396.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存