Notes Analysis: annotation get property values
Using reflection techniques : java.lang.reflect.AnnotatedElement Interface
Interface reflected implementation classes:
the AccessibleObject, Class, the Constructor, Field,, Method,, the Package
abstract methods:
Boolean isAnnotationPresent (Class annotationClass)
determines the specified type (Constructor, Class, Method ...) on whether the annotation containing the specified
parameters:
Class annotationClas: Class transmitting specified annotation file object; want to judge if there is no annotation Book, passed Book.class
return value:
Boolean: containing the specified annotation, returns true; not specified annotation return to false
T getAnnotation (Class annotationClass)
Gets the specified type (Constructor, Class, Method ...) annotation
parameters:
Class annotationClass: Specifies the file transfer Class annotations; want to obtain Book annotations, passing Book.class
return value:
T: returns is specified annotation
annotation does not exist, return null
Meta-annotation of use:
package com.ccc.demo08Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.METHOD,ElementType.TYPE})//声明注解的使用位置:成员方法上,类/接口上
@Retention(RetentionPolicy.RUNTIME)//声明注解的声明周期(.java,.class,运行后的内存中)
public @interface Book {
//书名
public abstract String value();
//价格,默认值为 100
public abstract double price() default 100;
//多位作者
public abstract String[] authors();
}
Analytical element analysis method on :( annotation, the annotation on annotation parser class)
package com.ccc.demo08Annotation;
import com.itheima.demo05Annotation.MyAnnotation01;
import com.itheima.demo05Annotation.MyAnnotation03;
import org.junit.Test;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
@Book(value = "三国演义",price = 88,authors = "罗贯中")
public class ParseAnnotation {
@Book(value = "水浒传",authors = "施耐庵")
@MyAnnotation03("aaa")
public void method(){}
Notes on the analytical method for
analysis:
- Gets the current class (ParseAnnotation) of the class file object
- Get all members of the methods in the current class, returns an array of Method
- Through the array, each acquiring a Method Object
- Use Method isAnnotationPresent object determination, whether there is a specified method Book annotations
- If the method specified Book annotations, using the specified method for obtaining Book Notes on getAnnotation
- Use annotation name attribute name () Gets annotation property values
@Test
public void getAnnotationMethod() throws ClassNotFoundException {
//1.获取当前类ParseAnnotation的class文件对象
Class clazz = Class.forName("com.itheima.demo08Annotation.ParseAnnotation");
//2.获取当前类中的所有成员方法,返回一个Method数组
//Method[] methods = clazz.getMethods();
Method[] methods = clazz.getDeclaredMethods();
//3.遍历数组,获取每一个Method对象
for (Method m : methods) {
//System.out.println(m.getName()+"-->"+m.isAnnotationPresent(Book.class));
//4.使用Method对象中的方法isAnnotationPresent判断,方法上是否有指定的Book注解
if(m.isAnnotationPresent(Book.class)){
//5.如果方法指定的Book注解,使用getAnnotation获取方法上指定的Book注解
Book book = m.getAnnotation(Book.class);
//6.使用注解名.属性名()获取注解的属性值
System.out.println(book.value());
System.out.println(book.price());
System.out.println(Arrays.toString(book.authors()));
}
}
}
Analytical notes on class
analysis:
- Gets the current class (ParseAnnotation) of the class file object
- Use isAnnotationPresent determines whether the class has a specified annotation Book
- If there Book annotations, use getAnnotation method to get to the Book Notes
- Use annotation name attribute name (), get property values annotation
@Test
public void getAnnotationClass(){
//1.获取当前类ParseAnnotation的class文件对象
Class clazz = ParseAnnotation.class;
//2.使用isAnnotationPresent判断类上是否有指定的Book注解
boolean b = clazz.isAnnotationPresent(Book.class);
//System.out.println(b);
//3.如果有Book注解,使用getAnnotation方法获取到Book注解
if(b){
//Annotation是所有接口的父接口,而Annotation没有book类型的属性,
//所以得向下转型为book类型
Book book = (Book)clazz.getAnnotation(Book.class);
//4.使用注解名.属性名(),获取注解的属性值
String value = book.value();
System.out.println(value);
double price = book.price();
System.out.println(price);
String[] authors = book.authors();
System.out.println(Arrays.toString(authors));
}
}
}