(本文是介绍依赖注入容器Spring和分析JUnit源码的准备知识)

Java Annotation(标注)

java.lang.annotation.Annotation是全部Java标注的父接口。它除了override/改写Object的equals(Object)、hashCode()和toString()外,仅有一个方法

Class<? extends Annotation> annotationType()

返回本标注的的类型。

JDK中定义的标注java.lang.Override为:
@Target(value=METHOD)
@Retention(value=SOURCE)
public @interface Override{}

再如java.lang. FunctionalInterface

@Documented
@Retention(value=RUNTIME)
@Target(value=TYPE)
public @interface FunctionalInterface
......

这些样例反映标注的一些基本内容:

①定义一个标注。如public @interface MyAnnotation。不须要extends Annotation而是使用@ interface。

有一些用于修饰自己定义标注的标注——称为元标注/meta-annotation,如Override样例中的@Target、@Retention和@Documented。其后是括号,括号里是使用逗号切割的名值对。(name=value)

@Target说明被修饰的标注的适用场合/目标。

其值由枚举java.lang.annotation.ElementType限定,包含ANNOTATION_TYPE , CONSTRUCTOR , FIELD , LOCAL_VARIABLE , METHOD ,PACKAGE , PARAMETER , TYPE和TYPE_PARAMETER。

比如Target自己的适用场合是@Target(value=ANNOTATION_TYPE);假设标注仅仅有单一属性成员(名值对),而并成员名为"value="能够省略为@Target(ANNOTATION_TYPE)。

@Retention说明被修饰的标注的保留策略,由RetentionPolicy枚举。

  • SOURCE仅仅会保留在程序源代码里。或者说仅对编译器有效,如@Override。编译器检查
  • 默认或指定CLASS时,指定该标注写入class文件,可是不会把这些信息载入到JVM中;
  • RUNTIME表示能够通过反射机制获得该标注。

@Documented指明该标注被反映在JavaDoc中。

②标注用来修饰源码的元素(类,方法。属性,參数。本地变量,包,元标注)。
③标注的使用有一个明白的底线它们不可影响程序代码的运行。不管添加或删除标注,代码的运行不会有不论什么影响。

可是,程序能够解析RUNTIME标注。并决定代码的行为


样例:JUnit4自己定义的annotation

org.junit.Test

org.junit.Ignore @Target({ElementType.METHOD, ElementType.TYPE})

@Before和@After标示的方法仅仅能各有一个,代替了JUnit曾经版本号中的setUp和tearDown方法

org.junit.BeforeClass @Target(ElementType.METHOD)

org.junit.Before @Target(ElementType.METHOD)

org.junit.AfterClass @Target(ElementType.METHOD)

org.junit.After @Target(ElementType.METHOD)

org.junit.runner.RunWith

org.junit.runners.Suite.SuiteClasses

org.junit.runners.Parameterized.Parameters

解析RUNTIME标注

①定义一个标注。
package MyTest;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation{
String value();
}

②标注的使用类

package MyTest;
public class HelloWorld {
@MyAnnotation("Test")
public double add(double m,double n){
return m+n;
}
@MyAnnotation("Ignore")
public double add2(double m,double n){
return m+n;
}
}
③解析RetentionPolicy.RUNTIME标注。

通过c获得全部public方法,对每一个方法获取其标注Annotation[],找到自己定义的MyAnnotation,假设其value()的值为"Test",(Double)method.invoke(h,1,2.0);
package MyTest;
import java.lang.annotation.*;
import java.lang.reflect.*;
public class TestHelloWorld{
public static void main(String[] args)throws Exception{
//直接使用反射机制
Class<?> c = MyTest.HelloWorld.class;
HelloWorld h =(HelloWorld)c.newInstance();
Method[] methods = c.getMethods();
for(Method method:methods){
Annotation[] annotations = method.getDeclaredAnnotations();
for(Annotation annotation : annotations){
if(annotation instanceof MyAnnotation){
MyAnnotation myAnnotation = (MyAnnotation) annotation;
System.out.println("value: " + myAnnotation.value());
if(myAnnotation.value().equals("Test") ){
double d = (Double)method.invoke(h,1,2.0);
System.out.println(d);
}
}
}
}
}
}

输出:

value: Test

3.0

value: Ignore







1.2.4 Java Annotation 提要的更多相关文章

  1. Java Annotation概述

    @(Java)[Annotation|Java] Java Annotation概述 用途 编译器的相关信息,如用于检测错误和一些警告 编译时和部署时的处理,如一些软件用于自动生成代码之类的 运行时处 ...

  2. Java Annotation 注解

    java_notation.html div.oembedall-githubrepos { border: 1px solid #DDD; list-style-type: none; margin ...

  3. paip.Java Annotation注解的作用and 使用

    paip.Java Annotation注解的作用and 使用 作者Attilax 艾龙,  EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog. ...

  4. Java Annotation认知(包括框架图、详细介绍、示例说明)

    摘要 Java Annotation是JDK5.0引入的一种注释机制. 网上很多关于Java Annotation的文章,看得人眼花缭乱.Java Annotation本来很简单的,结果说的人没说清楚 ...

  5. Java Annotation原理分析(一)

    转自:http://blog.csdn.net/blueheart20/article/details/18725801 小引: 在当下的Java语言层面上,Annotation已经被应用到了语言的各 ...

  6. Java Annotation 及几个常用开源项目注解原理简析

    PDF 版: Java Annotation.pdf, PPT 版:Java Annotation.pptx, Keynote 版:Java Annotation.key 一.Annotation 示 ...

  7. Java Annotation 机制源码分析与使用

    1 Annotation 1.1 Annotation 概念及作用      1.  概念 An annotation is a form of metadata, that can be added ...

  8. Java Annotation手册

    Java Annotation手册 作者:cleverpig(作者的Blog:http://blog.matrix.org.cn/page/cleverpig) 原文:http://www.matri ...

  9. Java Annotation 必须掌握的特性

    什么是Annotation? Annotation翻译为中文即为注解,意思就是提供除了程序本身逻辑外的额外的数据信息.Annotation对于标注的代码没有直接的影响,它不可以直接与标注的代码产生交互 ...

随机推荐

  1. powerdesigner 连接mysql提示“connection test failed”

    powerdesigner  连接mysql提示“connection test failed”,该如何解决: 1.把64位的jdk换成32位的jdk(VM只支持32的jre) 2.系统变量:  CL ...

  2. 【Todo】Zookeeper系列文章

    http://nileader.blog.51cto.com/1381108/1068033

  3. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  4. [TS] Implement a singly linked list in TypeScript

    In a singly linked list each node in the list stores the contents of the node and a reference (or po ...

  5. progerssbar-style 属性分析

    先看如下代码 <ProgressBar android:id="@+id/stateProgressBar" android:orientation="horizo ...

  6. 4. Spring Boot 过滤器、监听器

    转自:https://blog.csdn.net/catoop/article/details/50501688

  7. Oracle 12C R2 on Linux 7.X Data Guard 搭建文档

    1.查看主机和数据库信息   [oracle@oracle1 ~]$ sqlplus / as sysdba   SQL*Plus: Release 12.2.0.1.0 Production on ...

  8. C# for 和 foreach的执行效率

    for和foreach哪个执行效率快,相信很多人都会说当然是foreach快啊,在我实验之前我也是这么认为的,直到今天.费话不多说,下面是测试的结果,区分Debug和Release,数据采用int[] ...

  9. PythonNET网络编程3

    IO IO input output 在内存中存在数据交换的操作都可以认为是IO操作 和终端交互 : input print 和磁盘交互 : read write 和网络交互 : recv send ...

  10. amazeui学习笔记--css(常用组件1)--小徽章Badge

    amazeui学习笔记--css(常用组件1)--小徽章Badge 一.总结 1.am-badge:添加am-badge来声明小徽章对象 <span class="am-badge a ...