注解是从JDK5开始支持,是Java对元数据的一种特殊支持。与注释有一定区别,可以理解为代码上的特殊标记,通过这些标记我们可以在编译,类加载,运行等程序类的生命周期内被读取、执行相应的处理。通过注解开发人员可以在不改变原有代码和逻辑的情况下在源代码中嵌入补充信息。注解是Java语言的一种强大的功能。
import java.lang.annotation.*;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,
ElementType.CONSTRUCTOR,
ElementType.FIELD,
ElementType.PARAMETER,
ElementType.TYPE,
ElementType.ANNOTATION_TYPE})
public @interface TestAnnotation {
String value();
}
@TestAnnotation("Class Student")
public class Student {
@TestAnnotation("constance field")
public static final String CLASS_NAME = "Student";
@TestAnnotation("field")
private String name;
public Student(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.lang.annotation.*;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,
ElementType.CONSTRUCTOR,
ElementType.FIELD,
ElementType.PARAMETER,
ElementType.TYPE,
ElementType.ANNOTATION_TYPE})
public @interface TestAnnotationTwo {
String message();
String[] names() default {};
}
@TestAnnotationTwo(message = "Hello!")
public class StudentTwo {
@TestAnnotationTwo(message = "Hi!", names = {"holo", "la"})
private String id;
@TestAnnotationTwo(message = "yes")
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.lang.annotation.*;
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD,
ElementType.CONSTRUCTOR,
ElementType.FIELD,
ElementType.PARAMETER,
ElementType.TYPE,
ElementType.ANNOTATION_TYPE})
public @interface TestAnnotation {
String value();
}
@TestAnnotation("Class Student")
public class Student {
@TestAnnotation("constance field")
public static final String CLASS_NAME = "Student";
@TestAnnotation("field")
private String name;
@TestAnnotation("Constructor")
public Student(String name) {
this.name = name;
}
@TestAnnotation("getter")
public String getName() {
return name;
}
@TestAnnotation("setter")
public void setName(String name) {
this.name = name;
}
@TestAnnotation("public method")
public void printName() {
System.out.println(name);
}
@TestAnnotation("public method with parameter")
public void printName(@TestAnnotation("parameter") String name) {
System.out.println(name);
}
}
public void learnClassAnnotation() {
Class> clazzStudent = Student.class;
for (Annotation annotation : clazzStudent.getAnnotations()) {
if (annotation instanceof TestAnnotation) {
System.out.println(((TestAnnotation) annotation).value());
}
if (annotation.annotationType() == TestAnnotation.class) {
System.out.println(((TestAnnotation) annotation).value());
}
}
}
Class Student
Class Student
public void learnFieldAnnotation() {
Class> clazz = Student.class;
List fields = Arrays.asList(clazz.getDeclaredFields());
fields.forEach(field -> {
System.out.println("\n\n------------------------field " + field.getName());
for (Annotation annotation : field.getAnnotations()) {
if (annotation instanceof TestAnnotation) {
System.out.println(((TestAnnotation) annotation).value());
}
if (annotation.annotationType() == TestAnnotation.class) {
System.out.println(((TestAnnotation) annotation).value());
}
}
});
}
------------------------field CLASS_NAME
constance field
constance field
------------------------field name
field
field
public void learnConstructorAnnotation() {
Class> clazz = Student.class;
List> constructors = Arrays.asList(clazz.getDeclaredConstructors());
constructors.forEach(constructor -> {
System.out.println("\n\n------------------------constructor " + constructor.getName());
for (Annotation annotation : constructor.getAnnotations()) {
if (annotation instanceof TestAnnotation) {
System.out.println(((TestAnnotation) annotation).value());
}
if (annotation.annotationType() == TestAnnotation.class) {
System.out.println(((TestAnnotation) annotation).value());
}
}
});
}
------------------------constructor Student
Constructor
Constructor
public void learnMethodAnnotation() {
Class> clazz = Student.class;
List methods = Arrays.asList(clazz.getMethods());
methods.forEach(method -> {
System.out.println("\n\n------------------------method " + method.getName());
for (Annotation annotation : method.getAnnotations()) {
if (annotation instanceof TestAnnotation) {
System.out.println(((TestAnnotation) annotation).value());
}
if (annotation.annotationType() == TestAnnotation.class) {
System.out.println(((TestAnnotation) annotation).value());
}
}
});
}
------------------------method getName
getter
getter
------------------------method setName
setter
setter
------------------------method printName
public method
public method
------------------------method printName
public method with parameter
public method with parameter
------------------------method wait
------------------------method wait
------------------------method wait
------------------------method equals
------------------------method toString
------------------------method hashCode
------------------------method getClass
------------------------method notify
------------------------method notifyAll
public void learnParameterAnnotation() {
Class> clazz = Student.class;
List methods = Arrays.asList(clazz.getMethods());
for (Method method : methods) {
System.out.println("\n\n-----------------------------------------------method " + method.getName());
for (Parameter parameter : method.getParameters()) {
System.out.println("------------------------parameter " + parameter.getName());
for (Annotation annotation : parameter.getAnnotations()) {
if (annotation instanceof TestAnnotation) {
System.out.println(((TestAnnotation) annotation).value());
}
if (annotation.annotationType() == TestAnnotation.class) {
System.out.println(((TestAnnotation) annotation).value());
}
}
}
}
}
-----------------------------------------------method getName
-----------------------------------------------method setName
------------------------parameter arg0
-----------------------------------------------method printName
-----------------------------------------------method printName
------------------------parameter arg0
parameter
parameter
-----------------------------------------------method wait
-----------------------------------------------method wait
------------------------parameter arg0
------------------------parameter arg1
-----------------------------------------------method wait
------------------------parameter arg0
-----------------------------------------------method equals
------------------------parameter arg0
-----------------------------------------------method toString
-----------------------------------------------method hashCode
-----------------------------------------------method getClass
-----------------------------------------------method notify
-----------------------------------------------method notifyAll
1.通过 instanceof 操作,判断annotation对象是否是自定义注解类
annotation instanceof TestAnnotation
2.通过 Annotation 的 annotationType() 方法获取到annotation对象的类型,并同自定义注解类进行比对
annotation.annotationType() == TestAnnotation.class
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
/**
* Created by xuyh(Johnsonmoon) at 2018/1/3 11:43.
*/
public class AnnotationUtils {
/**
* get declared annotations of the given class
*
* @param clazz given class
* @return annotations
*/
public static Annotation[] getAnnotations(Class> clazz) {
return clazz.getAnnotations();
}
/**
* get declared annotations of the given field
*
* @param field given field
* @return annotations
*/
public static Annotation[] getAnnotations(Field field) {
return field.getAnnotations();
}
/**
* get declared annotations of the given method
*
* @param method given method
* @return annotations
*/
public static Annotation[] getAnnotations(Method method) {
return method.getAnnotations();
}
/**
* get declared annotations of the given constructor
*
* @param constructor given constructor
* @return annotations
*/
public static Annotation[] getAnnotations(Constructor constructor) {
return constructor.getAnnotations();
}
/**
* get declared annotations of the given parameter
*
* @param parameter given parameter
* @return annotations
*/
public static Annotation[] getAnnotations(Parameter parameter) {
return parameter.getAnnotations();
}
/**
* Is field has annotation of annotationType
*
* @param field given field
* @param annotationType given annotation type
* @return true/false
*/
public static boolean hasAnnotation(Field field, Class> annotationType) {
Annotation[] annotations = field.getAnnotations();
if (annotations == null || annotations.length == 0)
return false;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return true;
}
return false;
}
/**
* Is clazz has annotation of annotationType
*
* @param clazz given clazz
* @param annotationType given annotation type
* @return true/false
*/
public static boolean hasAnnotation(Class> clazz, Class> annotationType) {
Annotation[] annotations = clazz.getAnnotations();
if (annotations == null || annotations.length == 0)
return false;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return true;
}
return false;
}
/**
* Is method has annotation of annotationType
*
* @param method given method
* @param annotationType given annotation type
* @return true/false
*/
public static boolean hasAnnotation(Method method, Class> annotationType) {
Annotation[] annotations = method.getAnnotations();
if (annotations == null || annotations.length == 0)
return false;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return true;
}
return false;
}
/**
* Is constructor has annotation of annotationType
*
* @param constructor given constructor
* @param annotationType given annotation type
* @return true/false
*/
public static boolean hasAnnotation(Constructor> constructor, Class> annotationType) {
Annotation[] annotations = constructor.getAnnotations();
if (annotations == null || annotations.length == 0)
return false;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return true;
}
return false;
}
/**
* Is parameter has annotation of annotationType
*
* @param parameter given parameter
* @param annotationType given annotation type
* @return true/false
*/
public static boolean hasAnnotation(Parameter parameter, Class> annotationType) {
Annotation[] annotations = parameter.getAnnotations();
if (annotations == null || annotations.length == 0)
return false;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return true;
}
return false;
}
/**
* Get annotation of given annotation type declared in given field.
*
* @param field given field
* @param annotationType given annotation type
* @return annotation instance
*/
public static Annotation getAnnotation(Field field, Class> annotationType) {
Annotation[] annotations = field.getAnnotations();
if (annotations == null || annotations.length == 0)
return null;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return annotation;
}
return null;
}
/**
* Get annotation of given annotation type declared in given clazz.
*
* @param clazz given clazz
* @param annotationType given annotation type
* @return annotation instance
*/
public static Annotation getAnnotation(Class> clazz, Class> annotationType) {
Annotation[] annotations = clazz.getAnnotations();
if (annotations == null || annotations.length == 0)
return null;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return annotation;
}
return null;
}
/**
* Get annotation of given annotation type declared in given method.
*
* @param method given method
* @param annotationType given annotation type
* @return annotation instance
*/
public static Annotation getAnnotation(Method method, Class> annotationType) {
Annotation[] annotations = method.getAnnotations();
if (annotations == null || annotations.length == 0)
return null;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return annotation;
}
return null;
}
/**
* Get annotation of given annotation type declared in given constructor.
*
* @param constructor given constructor
* @param annotationType given annotation type
* @return annotation instance
*/
public static Annotation getAnnotation(Constructor> constructor, Class> annotationType) {
Annotation[] annotations = constructor.getAnnotations();
if (annotations == null || annotations.length == 0)
return null;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return annotation;
}
return null;
}
/**
* Get annotation of given annotation type declared in given parameter.
*
* @param parameter given parameter
* @param annotationType given annotation type
* @return annotation instance
*/
public static Annotation getAnnotation(Parameter parameter, Class> annotationType) {
Annotation[] annotations = parameter.getAnnotations();
if (annotations == null || annotations.length == 0)
return null;
for (Annotation annotation : annotations) {
if (annotation.annotationType() == annotationType)
return annotation;
}
return null;
}
}