类对象处理类-ClassUtil

import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.SynthesizingMethodParameter;
import org.springframework.web.method.HandlerMethod;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * 类工具类
 *
 * @author L.cm
 */
public class ClassUtil extends org.springframework.util.ClassUtils {

   private static final ParameterNameDiscoverer PARAMETER_NAME_DISCOVERER = new DefaultParameterNameDiscoverer();

   /**
    * 获取方法参数信息
    *
    * @param constructor    构造器
    * @param parameterIndex 参数序号
    * @return {MethodParameter}
    */
   public static MethodParameter getMethodParameter(Constructor<?> constructor, int parameterIndex) {
      MethodParameter methodParameter = new SynthesizingMethodParameter(constructor, parameterIndex);
      methodParameter.initParameterNameDiscovery(PARAMETER_NAME_DISCOVERER);
      return methodParameter;
   }

   /**
    * 获取方法参数信息
    *
    * @param method         方法
    * @param parameterIndex 参数序号
    * @return {MethodParameter}
    */
   public static MethodParameter getMethodParameter(Method method, int parameterIndex) {
      MethodParameter methodParameter = new SynthesizingMethodParameter(method, parameterIndex);
      methodParameter.initParameterNameDiscovery(PARAMETER_NAME_DISCOVERER);
      return methodParameter;
   }

   /**
    * 获取Annotation
    *
    * @param method         Method
    * @param annotationType 注解类
    * @param <A>            泛型标记
    * @return {Annotation}
    */
   public static <A extends Annotation> A getAnnotation(Method method, Class<A> annotationType) {
      Class<?> targetClass = method.getDeclaringClass();
      // The method may be on an interface, but we need attributes from the target class.
      // If the target class is null, the method will be unchanged.
      Method specificMethod = ClassUtil.getMostSpecificMethod(method, targetClass);
      // If we are dealing with method with generic parameters, find the original method.
      specificMethod = BridgeMethodResolver.findBridgedMethod(specificMethod);
      // 先找方法,再找方法上的类
      A annotation = AnnotatedElementUtils.findMergedAnnotation(specificMethod, annotationType);
      ;
      if (null != annotation) {
         return annotation;
      }
      // 获取类上面的Annotation,可能包含组合注解,故采用spring的工具类
      return AnnotatedElementUtils.findMergedAnnotation(specificMethod.getDeclaringClass(), annotationType);
   }

   /**
    * 获取Annotation
    *
    * @param handlerMethod  HandlerMethod
    * @param annotationType 注解类
    * @param <A>            泛型标记
    * @return {Annotation}
    */
   public static <A extends Annotation> A getAnnotation(HandlerMethod handlerMethod, Class<A> annotationType) {
      // 先找方法,再找方法上的类
      A annotation = handlerMethod.getMethodAnnotation(annotationType);
      if (null != annotation) {
         return annotation;
      }
      // 获取类上面的Annotation,可能包含组合注解,故采用spring的工具类
      Class<?> beanType = handlerMethod.getBeanType();
      return AnnotatedElementUtils.findMergedAnnotation(beanType, annotationType);
   }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值