java工具类注释 see_分享springframework提供的注释AnnotationUtils工具类获取类、或类方法的注释对象内容...

一、前言

通过spring-core提供额org.springframework.core.annotation.AnnotationUtils工具类提供的findAnnotation、getAnnotation等方法查找类、及其方法的注释对象内容。

二、源码分享package org.springframework.core.annotation;@b@@b@import java.lang.annotation.Annotation;@b@import java.lang.reflect.AnnotatedElement;@b@import java.lang.reflect.Method;@b@import java.util.List;@b@import java.util.Map;@b@import java.util.WeakHashMap;@b@@b@import org.springframework.core.BridgeMethodResolver;@b@import org.springframework.util.Assert;@b@@b@/**@b@ * General utility methods for working with annotations, handling bridge methods (which the compiler@b@ * generates for generic declarations) as well as super methods (for optional "annotation inheritance").@b@ * Note that none of this is provided by the JDK's introspection facilities themselves.@b@ *@b@ * 

As a general rule for runtime-retained annotations (e.g. for transaction control, authorization or service@b@ * exposure), always use the lookup methods on this class (e.g., {@link #findAnnotation(Method, Class)}, {@link@b@ * #getAnnotation(Method, Class)}, and {@link #getAnnotations(Method)}) instead of the plain annotation lookup@b@ * methods in the JDK. You can still explicitly choose between lookup on the given class level only ({@link@b@ * #getAnnotation(Method, Class)}) and lookup in the entire inheritance hierarchy of the given method ({@link@b@ * #findAnnotation(Method, Class)}).@b@ *@b@ * @author Rob Harrop@b@ * @author Juergen Hoeller@b@ * @author Sam Brannen@b@ * @author Mark Fisher@b@ * @author Chris Beams@b@ * @since 2.0@b@ * @see java.lang.reflect.Method#getAnnotations()@b@ * @see java.lang.reflect.Method#getAnnotation(Class)@b@ */@b@public abstract class AnnotationUtils {@b@@b@/** The attribute name for annotations with a single element */@b@static final String VALUE = "value";@b@@b@private static final Map, Boolean> annotatedInterfaceCache = new WeakHashMap, Boolean>();@b@@b@@b@/**@b@ * Get a single {@link Annotation} of {@code annotationType} from the supplied@b@ * Method, Constructor or Field. Meta-annotations will be searched if the annotation@b@ * is not declared locally on the supplied element.@b@ * @param ae the Method, Constructor or Field from which to get the annotation@b@ * @param annotationType the annotation class to look for, both locally and as a meta-annotation@b@ * @return the matching annotation or {@code null} if not found@b@ * @since 3.1@b@ */@b@public static  T getAnnotation(AnnotatedElement ae, Class annotationType) {@b@T ann = ae.getAnnotation(annotationType);@b@if (ann == null) {@b@for (Annotation metaAnn : ae.getAnnotations()) {@b@ann = metaAnn.annotationType().getAnnotation(annotationType);@b@if (ann != null) {@b@break;@b@}@b@}@b@}@b@return ann;@b@}@b@@b@/**@b@ * Get all {@link Annotation Annotations} from the supplied {@link Method}.@b@ * 

Correctly handles bridge {@link Method Methods} generated by the compiler.@b@ * @param method the method to look for annotations on@b@ * @return the annotations found@b@ * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)@b@ */@b@public static Annotation[] getAnnotations(Method method) {@b@return BridgeMethodResolver.findBridgedMethod(method).getAnnotations();@b@}@b@@b@/**@b@ * Get a single {@link Annotation} of {@code annotationType} from the supplied {@link Method}.@b@ * 

Correctly handles bridge {@link Method Methods} generated by the compiler.@b@ * @param method the method to look for annotations on@b@ * @param annotationType the annotation class to look for@b@ * @return the annotations found@b@ * @see org.springframework.core.BridgeMethodResolver#findBridgedMethod(Method)@b@ */@b@public static  A getAnnotation(Method method, Class annotationType) {@b@Method resolvedMethod = BridgeMethodResolver.findBridgedMethod(method);@b@A ann = resolvedMethod.getAnnotation(annotationType);@b@if (ann == null) {@b@for (Annotation metaAnn : resolvedMethod.getAnnotations()) {@b@ann = metaAnn.annotationType().getAnnotation(annotationType);@b@if (ann != null) {@b@break;@b@}@b@}@b@}@b@return ann;@b@}@b@@b@/**@b@ * Get a single {@link Annotation} of {@code annotationType} from the supplied {@link Method},@b@ * traversing its super methods if no annotation can be found on the given method itself.@b@ * 

Annotations on methods are not inherited by default, so we need to handle this explicitly.@b@ * @param method the method to look for annotations on@b@ * @param annotationType the annotation class to look for@b@ * @return the annotation found, or {@code null} if none found@b@ */@b@public static 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值