当前位置:   article > 正文

java-Annotation总结_[ljava.lang.annotation.annotation; at sun.reflect.

[ljava.lang.annotation.annotation; at sun.reflect.unsafefieldaccessorimpl.th

java-annotation

  • java.lang.annotation.Annotation Annotation主类,任何@xxx 在通过getAnnotation()得到的接口类是Annotation,通过方法可以获取到Annotation的类,注意不是通过getClass得到
 Class<? extends Annotation> annotationType();
  • 1
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@FSConstraint(validatedBy =FSNotEmptyContraintValidator.class)
@interface notNull{
   String message();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
public class User{
  @NotNull(message="name can not be empty")
  private String name;
  @NotNull(message="age can not be empty")
  private int age;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
obj=new User();
Field[] fs=obj.getClass().getDeclaredFields();
for(Field f:fs){//遍历获取到User对象上的所有的Field
 	Annotation[] ans=f.getAnnotations(); //拿到Field的所有的annotaion,也就是@NotNull
			for(Annotation an:ans){
			 //对于 NotNull 这个Annotation需要到注解的注解
			 //例如NotNull的注解@FSConstraint(validatedBy =FSNotEmptyContraintValidator.class)
			 //使用下面
					Annotation[] ansofAn=an.annotationType().getAnnotations(); 
					//不要是用an.getClass().getAnnotations(); 
		}
}				   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • java.lang.annotation.Target 表明了annotation 可以放在哪些类型上,和ElementType进行配合使用
  • java.lang.annotation.ElementType 指明Annotation可以放在哪些类型上
    如下
/** Class, interface (including annotation type), or enum declaration */
    TYPE,

    /** Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /** Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • java.lang.annotation.Inherited

父类中的某个annotation如果有@Inherited, 则这个annotation会被子类查到,注意父类不可以是接口

例子说明如下:

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited //这里加了@Inherited 
public @interface Component {
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
@Component
public class Service {
}
  • 1
  • 2
  • 3
public class AService extends Service {
}
  • 1
  • 2
  public static void main(String[] args) {
        AService aService=new AService();  
        System.out.println(aService.getClass().getAnnotation(Component.class));
    }
  • 1
  • 2
  • 3
  • 4

AService 继承了Service,而Service有注解Component,并且Component里有@Inherited,这里可以打印出来,如果去掉@Inherited,则打印结果为空

  • java.lang.annotation.Retention 表明了注解保留的方式,和RetentionPolicy配合一起使用
  • java.lang.annotation.RetentionPolicy如下
public enum RetentionPolicy {
    /**
     * Annotations are to be discarded by the compiler.
     *只是在source上保留,complier编译的时候丢弃
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     * 编译成class会保修,但在运行时候会丢弃
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     * 运行的时候也会保留,如果是上面的2种,是不能通过aService.getClass().getAnnotation(Component.class) 获取到的
     */
    RUNTIME
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • java.lang.annotation.Repeatable 表明 annotation 是否能被重复的使用
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Repeatable(Components.class)
public @interface Component {
    String name();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Components {
    Component[] value();
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

使用注解方式1

@Component("a")
@Component("A")
public class AService extends Service {
}
  • 1
  • 2
  • 3
  • 4

使用注解方式2

@Components({@Component(name="b"),@Component(name="B")})
public class BService extends Service {
}
  • 1
  • 2
  • 3

注解参数可以支持的类型

@Interface Foo{
  <类型> value()
}
  • 1
  • 2
  • 3

注解参数的可支持数据类型

  1. 所有基本数据类型(int,float,boolean,byte,double,char,long,short)
  2. String类型
  3. Class类型
  4. enum类型
  5. Annotation类型
  6. 以上所有类型的数组

注解参数可以支持的类型的default值

上述的类型可以支持default值如下

public @interface Foo {
    <类型> value() default <类型的default>;
}
  • 1
  • 2
  • 3

对于可支持数据类型的1-4和5的默认值都很好做,但对于Annotation的默认值缺不好做

例如:
有个注解

public @interface Config {
    boolean ignoreUnknown() default false;
    int steps() default 1;
}
  • 1
  • 2
  • 3
  • 4
public @interface Foo {
    Config value() default <default>;
}
  • 1
  • 2
  • 3

这里Config的default值不能是null,{},可以写成

public @interface Foo {
    Config[] value() default {};  
}
  • 1
  • 2
  • 3
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/306641?site
推荐阅读
相关标签
  

闽ICP备14008679号