Annotation

Annotation

 

 

 

package com.jason.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Retention(RetentionPolicy.RUNTIME)  
public  @interface IValidator {
    String fieldname() default "";  
    String validatorname();  
}

 

 

package com.jason.annotation;

public class Person {
    @IValidator(fieldname = "username",validatorname = "com.jason.annotation.NameValidator")
    private String name;
    @IValidator(fieldname = "userpassword",validatorname = "com.jason.annotation.NameValidator")
    private String password;
    @IValidator(fieldname = "userage",validatorname = "com.jason.annotation.NameValidator")
    private int age;
    private String address;
    
    
    public Person() {
        super();
    }
    
    public void output(){  
           System.out.println("output is running");  
       }  

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPassword() {
        return password;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getAddress() {
        return address;
    }
}

 

 

package com.jason.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MyReflection {
    public MyReflection() {
        super();
    }
    
    public static void main(String[] args) throws Exception {  
            Class<Person> c = Person.class;//
            Method method = c.getMethod("output", new Class[]{});
            if(method.isAnnotationPresent(IValidator.class)){
                IValidator annotation = method.getAnnotation(IValidator.class);
                System.out.println(annotation.fieldname()); 
                System.out.println(annotation.validatorname());
            }
           
            Annotation[] annotations = method.getAnnotations();
            for(Annotation annotation: annotations){
                System.out.println(annotation.annotationType().getName());  
            }  
            System.out.println("------------------------");  
            Field[] fields = c.getDeclaredFields();
            for(Field field:fields){
                if(field.isAnnotationPresent(IValidator.class)){
                    IValidator annotation = field.getAnnotation(IValidator.class); 
                    System.out.println(annotation.fieldname());
                    System.out.println(annotation.validatorname());
                }
                Annotation[] anns = field.getAnnotations();
                for(Annotation annotation: anns){
                    System.out.println(annotation.annotationType().getName());  
                }  
            }  
          
            
            
        }  
}

 

你可能感兴趣的:(annotation)