前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java 注解(Annotation)

Java 注解(Annotation)

作者头像
全栈程序员站长
发布2022-09-08 11:27:49
4030
发布2022-09-08 11:27:49
举报

大家好,又见面了,我是你们的朋友全栈君。

文章目录

Annotation工作方式

从Java5.0版发布以来,5.0平台提供了一个正式的annoatation功能:允许开发者定义、使用自己的annotation类型。此功能由一个定义annotation声明的语法,读取annotation的API,一个使用annotation修饰的class文件,一个annotation处理工具(apt)组成。

annotation并不直接影响代码语义,但是它能够工作的方式被看作类似程序的工具或者类库,它会反过来对正在运行的程序语义有所影响。 annotation可以从源文件、class文件或者以在运行时反射的多种方式被读取。

JDK5 内建Annotation

Java 注解(Annotation):

限定Override父类方法@Override

java.langOverride是个Marker annotation 用于标示的Annotation,Annotation名称本身即表示了要给工具程序的信息 a) Override 注解表示子类要重写(override) 父类的对应方法。

代码语言:javascript
复制
package sixtyNineth;

public class OverrideTest { 
   

	@Override
	public String toString() { 
   
		return "This is OverrideTest";
	}
	
	public static void main(String[] args) { 
   
		OverrideTest test = new OverrideTest();
		
		System.out.println(test);
	}
}

结果是: This is OverrideTest

标示方法为Deprecated @Deprectated

对编译程序说明某个方法已经不建议使用,即该方法是过时的。 java.lang.Deprecated也是个Marker annotation Deprecated这个名称在告知编译程序,被@Deprecated标示的方法是一个不建议被使用的方法 b) Deprecated 注解表示方法是不建议被使用的。

代码语言:javascript
复制
package sixtyNineth;

import java.sql.Date;

public class DeprecatedTest { 
   

	@Deprecated
	public void doSomething() { 
   
		System.out.println("do something");
	}

	public static void main(String[] args) { 
   
		
		DeprecatedTest test = new DeprecatedTest();
		
		test.doSomething();
		
		
		
		/*Date date = new Date(0); System.out.println(date.toLocaleString());*/
	}
}

结果是: do something

抑制编译程序警告@SuppressWarnings

对编译程序说明某个方法中若有警告讯息,则加以抑制 c) SuppressWarnings 注解表示抑制警告。

代码语言:javascript
复制
package sixtyNineth;

import java.util.Date;
import java.util.Map;
import java.util.TreeMap;

public class SuppressWarningTest { 
   

	@SuppressWarnings({ 
    "unchecked", "rawtypes" })
	public static void main(String[] args) { 
   
		Map map = new TreeMap();
		
		map.put("hello", new Date());
		
		System.out.println(map.get("hello"));
	}
}

结果是: Sun Jan 13 19:59:06 CST 2019

自定义Annotation类型

定义Marker Annotation,也就是Annotation名称本身即提供信息对于程序分析工具来说,主要是检查是否有Marker Annotation的出现,并作出对应的动作。

自定义注解:当注解中的属性名为 value 时,在对其赋值时可以不指定属性的名称 而直接写上属性值即可;除了 value 以外的其他值都需要使用 name=value 这种赋值 方式,即明确指定给谁赋值。

代码语言:javascript
复制
package sixtyNineth;

public @interface AnnotationTest { 
   
	
	String value1();

}
代码语言:javascript
复制
package sixtyNineth;

public @interface AnnotationTest1 { 
   

	String value();
}
代码语言:javascript
复制
package sixtyNineth;

@AnnotationTest(value1 = "hello")
public class AnnotationUsage { 
   
	
	@AnnotationTest1("world")
	public void method() { 
   
		System.out.println("usage of annotation");
	}
	
	public static void main(String[] args) { 
   
		AnnotationUsage usage = new AnnotationUsage();
		
		usage.method();
	}

}

结果是: usage of annotation

Single-value annotation

value成员设定默认值,用”default”关键词 数组方式的使用 枚举在Annotation中的应用

代码语言:javascript
复制
package sixtyNineth;

public @interface AnnotationTest1 { 
   

	String[] value1() default "hello";
	EnumTest value2();
}

enum EnumTest{ 
   
	Hello, World, Welcome
}
代码语言:javascript
复制
package sixtyNineth;

@AnnotationTest1(value2 = EnumTest.Welcome, value1 = "world")
public class AnnotationUsage { 
   
	
	@AnnotationTest1(value1 = { 
   "hello","haha"}, value2 = EnumTest.Hello)
	public void method() { 
   
		System.out.println("usage of annotation");
	}
	
	public static void main(String[] args) { 
   
		AnnotationUsage usage = new AnnotationUsage();
		
		usage.method();
	}

}

使用@interface自定义Annotation型态时,实际上是自动继承了java.lang.annotation.Annnotation接口由编译程序自动为您完成其它产生的细节,在定义Annotation型态时,不能继承其他的Annotation型态或者接口。 java.lang.annotation Interface Annotation

The common interface extended by all annotation types. Note that an interface that manually extends this one does not define an annotation type. Also note that this interface does not itself define an annotation type.

当 我 们 使 用 @interface 关 键 字 定 义 一 个 注 解 时 , 该 注 解 隐 含 地 继 承 了java.lang.annotation.Annotation 接口;如果我们定义了一个接口,并且让该接口继承自 Annotation,那么我们所定义的接口依然还是接口而不是注解; Annotation 本身是接口而不是注解。 (可以与 Enum 类比。)

定义Annotation型态时也可以使用包来管理类别方式类同于类的导入功能。

告知编译程序如何处理@Retention

java.lang.annotation.Retention型态可以在您定义Annotation型态时,指示编译程序该如何对待您的自定义的Annotation型态。 ==预设上编译程序会将Annotation信息留在.class档案中,==但是不被虚拟机读取,而仅用于编译程序或工具程序运行时提供信息。 在使用Retention型态时,需要提供java.lang.annotation.RetentionPolicy的枚举型态Package java.lang.annotation;

代码语言:javascript
复制
public enum RetentionPolicy
{ 
   
    SOURCE,//编译程序处理完Annotation信息后就完成任务
    CLASS,//编译程序将Annotation储存于class档中,缺省,不由VM读入
    RUNTIME //编译程序将Annotation储存于class档中,可由VM读入,通过反射的方式读取到
}

RetentionPolicy为SOURCE的例子是@SuppressWarnings 仅在编译时期告知编译程序来抑制警告,所以不必将这个信息储存于.class档案 RetentionPolicy为RUNTIME的时机,可以像是您使用Java设计一个程序代码分析工具,您让VM能读出Annotation信息,以便在分析程序时使用。 搭配反射(Reflectiong)机制,就可以达到这个目的

代码语言:javascript
复制
package Seventieth.First;

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

@Retention(RetentionPolicy.CLASS)
public @interface MyAnnotation { 
   

    String hello() default "Matthew";

    String world();
}
代码语言:javascript
复制
package Seventieth.First;

@MyAnnotation(hello = "beijing", world = "shanghai")
public class MyTest { 
   

    @MyAnnotation(hello = "tianjing", world = "shangdi")
    @Deprecated
    @SuppressWarnings("unchecked")
    public void output(){ 
   
        System.out.println("output something");
    }

    public static void main(String[] args) { 
   
        MyTest myTest = new MyTest();

        myTest.output();
    }

}

java.lang.reflect.AnnotatedElement接口 public Annotation getAnnotation(Class annotationType); public Annotation[] getAnnotations(); public Annotation[] getDeclaredAnnotations(); public boolean isAnnotationPresent(Class annotationType); Class、Constructor、Field、Method、Package等类别,都实现了AnnotationElement接口

代码语言:javascript
复制
package Seventieth.First;

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

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

    String hello() default "Matthew";

    String world();
}
代码语言:javascript
复制
package Seventieth.First;

@MyAnnotation(hello = "beijing", world = "shanghai")
public class MyTest { 
   

    @MyAnnotation(hello = "tianjing", world = "shangdi")
    @Deprecated
    @SuppressWarnings("unchecked")
    public void output(){ 
   
        System.out.println("output something");
    }

   /* public static void main(String[] args) { MyTest myTest = new MyTest(); myTest.output(); }*/

}
代码语言:javascript
复制
package Seventieth.First;

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

public class MyReflection {
    public static void main(String[] args) throws  Exception{

        MyTest myTest = new MyTest();

        Class<MyTest> c = MyTest.class;

        Method method = c.getMethod("output", new Class[]{});

        if(method.isAnnotationPresent(MyAnnotation.class)){
            method.invoke(myTest, new Object[]{});

            MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);

            String hello = myAnnotation.hello();
            String world = myAnnotation.world();

            System.out.println(hello + "," + world);
        }

        Annotation[] annotations = method.getAnnotations();

        for(Annotation annotation : annotations){
            System.out.println(annotation.annotationType().getName());
        }

    }
}

结果是: output something tianjing,shangdi Seventieth.First.MyAnnotation java.lang.Deprecated

当我们在@Retention(RetentionPolicy.RUNTIME)不用RUNTIME而用CLASS和SOURCE时,运行结果为空,因为注解存在了class文件当中,但是,VM不会反射读取出来,method.isAnnotationPresent(MyAnnotation.class)为false所以没有输出。 @Retention(RetentionPolicy.SOURCE) 因为SuppressWarnings的RetentionPolicy为SOURCE

限定annotation使用对象@Target

使用java.lang.annotation.Target可以定义其使用的时机,在定义时要指定java.lang.annotation.ElementType的枚举值之一 package java.lang.annotation; public enum ElementType{ TYPE,//适用class,interface,enum FIELD,//适用field METHOD,//适用method PARAMETER,//适用method上的parameter CONSTRUCTOR,//适用costructor LOCAL_VARIABLE,//适用局部变量 ABBOTATION_TYPE,//适用annotation型态 PACKAGE//适用package }

代码语言:javascript
复制
package Seventieth.First;

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

@Target(ElementType.METHOD)
public @interface MyTarget { 
   

    String value();
}
代码语言:javascript
复制
package Seventieth.First;

public class MyTargetTest { 
   

    @MyTarget("hello")
    public void doSomething(){ 
   

        System.out.println("hello world");

    }

}

如果把 @MyTarget(“hello”)放在类上面就会报错’@MyTarget’ not applicable to type,我们把 @Target(ElementType.METHOD)改为@Target(ElementType.TYPE)就不会报错了

要求为API文件@Documented

想要在使用者制作JavaDoc文件的同时,也一并将Annotation的讯息加入至API文件中使用java.lang.annotation.Documented

代码语言:javascript
复制
package Seventieth.First;

public @interface DocumentedAnnotation {
    String hello();
}
代码语言:javascript
复制
package Seventieth.First;

public class DocumentedTest {
    @DocumentedAnnotation(hello = "welcome")
    public void method(){
        System.out.println("hello world");
    }
}

生成java帮助文档在Tool》Generate JavaDoc 然后在弹出的界面点击Output directory后的按钮选择文档生成路径 接下来在底部的Locale输入框配置语言和编码集,如下图所示,语言用zh_CN,代表中文

在这里插入图片描述
在这里插入图片描述

点击ok生成帮助文档

在这里插入图片描述
在这里插入图片描述

这就是我生成的java帮助文档,点击DocumentTest拉到最下面有方法详细资料

在这里插入图片描述
在这里插入图片描述

我们将DoucumentedAnnotation.java更改一下

代码语言:javascript
复制
package Seventieth.First;

import java.lang.annotation.Documented;

@Documented
public @interface DocumentedAnnotation { 
   
    String hello();
}

再次生成帮助文档,我们发现发发详细资料变成如下图所示的样子

在这里插入图片描述
在这里插入图片描述

子类是否继承父类@Inherited

预设上夫类别中断Annotation并不会被继承至子类别中,可以在定义Annotation型态时加上java.lang.annotation.Inherited型态的Annotation

通过JUnit深入理解反射与注解的使用方式与场景

现在项目中导入jar包

代码语言:javascript
复制
package Seventieth.Secound;

import junit.framework.TestCase;

public class Test extends TestCase { 
   


    public void testAdd(){ 
   
        System.out.println("hello world");
    }

    public void testSubtract(){ 
   
        System.out.println("welcome");
    }

}

结果是: hello world welcome

方法必须以test开头,JUnit我们学过反射后应该大致了解其思路,首先获得这个类的Class对象,然后获得其所有的方法,存入Method[]然后遍历每一个方法,getName获得方法名,如果是以test开头就通过method.invoke执行这个方法

JUnit(3.8、 4.x): Keep the bar green to keep the code clean.

代码语言:javascript
复制
package Seventieth.Secound;

import org.junit.Test;

public class Test2 { 
   
    @Test
    public void hello(){ 
   
        System.out.println("hello world");
    }
}

JUnit4 的执行的一般流程: a) 首先获得待测试类所对应的 Class 对象。 b) 然后通过该 Class 对象获得当前类中所有 public 方法所对应的 Method 数组。 c) 遍历该 Method 数组,取得每一个 Method 对象 d) 调用每个 Method 对象的 isAnnotationPresent(Test.class)方法,判断该方法是否被 Test 注解所修饰。 e) 如果该方法返回 true,那么调用 method.invoke()方法去执行该方法,否则不执行。

单元测试不是为了证明你是对的,而是证明你没有错误。 Writing Secure Code(编写安全的代码): Input is evil。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/156667.html原文链接:https://javaforall.cn

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • Annotation工作方式
  • JDK5 内建Annotation
    • 限定Override父类方法@Override
      • 标示方法为Deprecated @Deprectated
        • 抑制编译程序警告@SuppressWarnings
        • 自定义Annotation类型
          • Single-value annotation
          • 告知编译程序如何处理@Retention
          • 限定annotation使用对象@Target
          • 要求为API文件@Documented
          • 子类是否继承父类@Inherited
          • 通过JUnit深入理解反射与注解的使用方式与场景
          相关产品与服务
          腾讯云代码分析
          腾讯云代码分析(内部代号CodeDog)是集众多代码分析工具的云原生、分布式、高性能的代码综合分析跟踪管理平台,其主要功能是持续跟踪分析代码,观测项目代码质量,支撑团队传承代码文化。
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
          http://www.vxiaotou.com