精通Java注解(2)- 注解也有使用限制?

286 阅读2分钟

JDK 11之前一共定义了11种target。可在java.lang.annotation.ElementType找到详细解释。 如果注解定义不符合@Target指定的目标,则会在代码编译阶段报错。

举例说明最常用的几种:

Type(类)
定义
//类/接口/注解/枚举
@Target({ElementType.TYPE})
public @interface TypeDescription {
    String value() default "";
}
使用
//类
@TypeDescription
public class Class {
    
}

//接口
@TypeDescription
public interface Interface {
    
}

//注解类
@TypeDescription
public @interface Annotation{
    
}

//枚举类
@TypeDescription
public enum Enum {
}
FIELD(字段)
定义
//字段
@Target({ElementType.FIELD})
public @interface FieldDescription {
    String value() default "";
}
使用
public class Class {
    //全局变量
    @FieldDescription
    private String field1;
}
METHOD(方法)
定义
//方法
@Target(value = ElementType.METHOD)
public @interface MethodDescription {
    String value() default "";
}
使用
public class Class {
    @MethodDescription
    public void method1() {
        //...
    }
}
PARAMETER(参数)
定义
//字段
@Target(value = ElementType.PARAMETER)
public @interface ParamDescription {
    String value() default "";
}
使用
public class Class {
     //构造函数参数
    public Class(@ParamDescription String param) {

    }

    //方法参数
    public void method1(@ParamDescription String param1) {
        //...
    }
}
CONSTRUCTOR(构造函数)
定义
//构造函数
@Target(value = ElementType.CONSTRUCTOR)
public @interface ConstructorDesc {
    String value() default "";
}
使用
public class Class {
    //构造函数
    public @ConstructorDesc Class(String param) {
        //...
    }
}

此外,还有几个不怎么常用的,简单说明:

LOCAL_VARIABLE(局部变量)
使用
public class Class {
    public void method1() {
       //局部变量
       @LocalVarDesc String localVar = "";
    }
}
ANNOTATION_TYPE(注解类)
使用
//注意区分和TYPE的区别,此注解仅能使用在注解类上
@AnnotationDesc
public @interface Annotation{
    
}
PACKAGE(包)

package-info主要有两个目的: 1.包级别的文档说明 2.包级别的注解只能在package-info.java中使用

java package-info详细说明: www.baeldung.com/java-packag…

使用
//package-info.java

@ParamDescription
package com.minivision;

import annotation.ParamDescription;
TYPE_PARAMETER(类型参数)
使用
//泛型定义使用例子1
public class GenericClass<@TypeParamDesc G> {
    //泛型定义使用例子2
    public <@TypeParamDesc T> T method(T param) {
        return null;
    }
}
TYPE_USE(JLS定义的N种类型)

官方释义:The TYPE_USE constant includes type declarations and type parameter declarations as a convenience for designers of type checkers which give semantics to annotation types.

JDK 8引入的通用类型,包含了:TYPE, TYPE_PARAMETER,FIELD,CONSTRUCTOR,LOCAL_VARIABLE,以及其他之前不曾包含的类型(千奇百怪)。详见JLS附录:docs.oracle.com/javase/spec…

使用
//可用于接口
@TypeUseDesc
public interface Interface {
    
}

//注解类
@TypeUseDesc
public @interface Annotation{
    
}

//枚举类
@TypeUseDesc
public enum Enum {
}

//也可用于泛型定义
public class GenericClass<@TypeUseDesc G> {
    public <@TypeUseDesc T> T method(T param) {
        return null;
    }
}

public class Class {
    //全局变量
    @TypeUseDesc
    private String field1;
    
    //构造函数
    public @TypeUseDesc Class(String param) {
        //...
    }
    
    //返回值也可以哦,连throws都可以定义注解
    public @TypeUseDesc String method2() throws @TypeUseDesc Exception {
        //局部变量
        @TypeUseDesc
        int local = 0;
        return "";
    }
}

MODULE(模块)
定义
@Target(ElementType.MODULE)
public @interface ModuleDesc {
    String value() default "";
}

使用
import annotation.ModuleDesc;

@ModuleDesc
module abc {
}
附录:11种Target详细说明
类型官方释义使用范围JDK版本
TypeClass, interface (including annotation type), or enum declaration类/接口(包括注解类)/枚举JDK 5
FIELDField declaration (includes enum constants)字段(实例变量)同上
METHODMethod declaration方法同上
PARAMETERFormal parameter declaration---同上
CONSTRUCTORConstructor declaration构造函数同上
LOCAL_VARIABLELocal variable declaration局部变量同上
ANNOTATION_TYPEAnnotation type declaration注解类同上
PACKAGEPackage declarationpackage-info.java同上
TYPE_PARAMETERType parameter declaration类型参数(用于泛型)JDK 8
TYPE_USEUse of a typeJLS 4.11章节涵盖的N种类型JDK 8
MODULEModule declaration.module-info.javaJDK 9

本章节为原创文章,转载请说明出处。