Java8-Annotations

简介: import java.lang.annotation.ElementType;import java.lang.annotation.Repeatable;import java.
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class Annotations1 {

    @Target({ElementType.TYPE_PARAMETER, ElementType.TYPE_USE})
    @interface MyAnnotation {

    }

    @Retention(RetentionPolicy.RUNTIME)
    @interface Hints {
        Hint[] value();
    }

    @Repeatable(Hints.class)
    @Retention(RetentionPolicy.RUNTIME)
    @interface Hint {
        String value();
    }

    @Hint("hint1")
    @Hint("hint2")
    class Person {

    }

    public static void main(String[] args) {
        Hint hint = Person.class.getAnnotation(Hint.class);
        System.out.println(hint);   // null

        Hints hints1 = Person.class.getAnnotation(Hints.class);
        System.out.println(hints1.value().length);  // 2

        Hint[] hints2 = Person.class.getAnnotationsByType(Hint.class);
        System.out.println(hints2.length);  // 2

    }
}
目录
相关文章
|
3月前
|
安全 Java 编译器
Java其他: 什么是Java中的注解(Annotation)?
Java其他: 什么是Java中的注解(Annotation)?
42 0
|
8月前
java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
266 0
java7 try-with-resources 很香
java7 try-with-resources 很香
|
SQL Java BI
Java - java.util.UnknownFormatConversionException: Conversion = ‘‘‘
Java - java.util.UnknownFormatConversionException: Conversion = ‘‘‘
721 0
|
安全 Java API
【小家java】聊聊Java中的Runtime类
【小家java】聊聊Java中的Runtime类
|
Java API C++
Java Annotation学习笔记
作为一个早期短暂从事过C++开发工作的程序员,我个人认为Annotation可能是Java与C++语言较大的不同点之一,这也是一个前C++程序员由衷认为Java可能、或许、maybe要比C++更好用的原因之一。本文通过一个小例子展示了自定义注解的全过程。
5436 0