撸撸java注解

Annotation 组成部分

Annotation.java

package java.lang.annotation;
public interface Annotation {

    boolean equals(Object obj);

    int hashCode();

    String toString();

    Class<? extends Annotation> annotationType();
}

ElementType.java

package java.lang.annotation;

public enum ElementType {
    TYPE,               /* 类、接口(包括注释类型)或枚举声明  */

    FIELD,              /* 字段声明(包括枚举常量)  */

    METHOD,             /* 方法声明  */

    PARAMETER,          /* 参数声明  */

    CONSTRUCTOR,        /* 构造方法声明  */

    LOCAL_VARIABLE,     /* 局部变量声明  */

    ANNOTATION_TYPE,    /* 注释类型声明  */

    PACKAGE             /* 包声明  */
}

RetentionPolicy.java

package java.lang.annotation;
public enum RetentionPolicy {
    SOURCE,            /* Annotation信息仅存在于编译器处理期间,编译器处理完之后就没有该Annotation信息了  */

    CLASS,             /* 编译器将Annotation存储于类对应的.class文件中。默认行为  */

    RUNTIME            /* 编译器将Annotation存储于class文件中,并且可由JVM读入 */
}

注解类

@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation1 {
}

在反射中使用 Annotation

在反射的 Class, Method, Field 等函数中,有许多于 Annotation 相关的接口。

这也意味着,我们可以在反射中解析并使用 Annotation。

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

/**
 * Annotation在反射函数中的使用示例
 */
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
    String[] value() default "unknown";
}


public class Person {

    /**
     * empty()方法同时被 "@Deprecated" 和 "@MyAnnotation(value={"a","b"})"所标注
     * (01) @Deprecated,意味着empty()方法,不再被建议使用
     * (02) @MyAnnotation, 意味着empty() 方法对应的MyAnnotation的value值是默认值"unknown"
     */
    @MyAnnotation
    @Deprecated
    public void empty(){
        System.out.println("\nempty");
    }

    /**
     * sombody() 被 @MyAnnotation(value={"girl","boy"}) 所标注,
     * @MyAnnotation(value={"girl","boy"}), 意味着MyAnnotation的value值是{"girl","boy"}
     */
    @MyAnnotation(value={"girl","boy"})
    public void somebody(String name, int age){
        System.out.println("\nsomebody: "+name+", "+age);
    }
}



import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class AnnotationTest {

    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        // 新建Person
        Person person = new Person();
        // 获取Person的Class实例
        Class<Person> c = Person.class;
        // 获取 somebody() 方法的Method实例
        Method somebody = c.getMethod("somebody", new Class[]{String.class, int.class});

        // 执行该方法
        somebody.invoke(person, new Object[]{"lily", 18});
        iteratorAnnotations(somebody);

        // 获取 somebody() 方法的Method实例
        Method mEmpty = c.getMethod("empty", new Class[]{});
        // 执行该方法
        mEmpty.invoke(person, new Object[]{});
        iteratorAnnotations(mEmpty);

    }

    private static void iteratorAnnotations(Method somebody) {
        if (somebody.isAnnotationPresent(MyAnnotation.class)) {
            MyAnnotation annotation = somebody.getAnnotation(MyAnnotation.class);
            String[] values = annotation.value();
            for (String str : values)
                System.out.printf(str + ", ");

        }
    }


}

自定义注解使用案例1-模拟springmvc的requestMapping注解

package springmvc;

import java.lang.reflect.Method;

public class ParserResult {
    private String path;
    private String classpath;
    private Method method;

    public String getPath() {
        return path;
    }

    public Method getMethod() {
        return method;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public String getClasspath() {
        return classpath;
    }

    public void setClasspath(String classpath) {
        this.classpath = classpath;
    }
}
package springmvc;

import java.lang.annotation.*;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@interface RequestMapping {
    String path() default "";
}
package springmvc;
@RequestMapping(path="/root")
public class RequestMappingObject {
    @RequestMapping(path = "method1")
    public void method1(){

    }

    @RequestMapping(path = "method2")
    public void method2(){

    }

    @RequestMapping(path = "method3")
    public void method3(){

    }
}
package springmvc;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class RequestMappingParser<T> {
    private final Class<T> tClass;

    public RequestMappingParser(Class<T> tClass) {
        this.tClass = tClass;
    }

    public List<ParserResult> parse() {
        Package packages = tClass.getPackage();
        //反射获取类的注解路径
        RequestMapping annotationField = tClass.getAnnotation(RequestMapping.class);
        String rootPath = annotationField != null ? annotationField.path() : "";
        if (!rootPath.startsWith("/")) {
            rootPath = "/";
        }
        //反射或者所有的方法
        List<ParserResult> list = new ArrayList<ParserResult>();
        Method[] methods = this.tClass.getMethods();
        for (Method method : methods) {
            ParserResult parserResult = new ParserResult();
            if (method == null) {
                continue;
            }
            RequestMapping annotation = method.getAnnotation(RequestMapping.class);
            if (annotation == null) {
                continue;
            }
            String path = annotation.path();
            if ("".equals(path)) {
                continue;
            }
            if(path.startsWith("/")){
                parserResult.setPath(rootPath+path);
            }else{
                parserResult.setPath(rootPath+"/"+path);
            }
            Class<?> declaringClass = method.getDeclaringClass();
            String declaringClassName = declaringClass.getName();
            parserResult.setClasspath(declaringClassName);
            list.add(parserResult);
        }
        return list;

    }

}
package springmvc;

import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class RequetMappingTest {
    public static void main(String[] args) {
        RequestMappingParser requestMappingParser = new RequestMappingParser(RequestMappingObject.class);

        List parse = requestMappingParser.parse();
        System.out.println(JSONObject.toJSONString(parse));
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值