`
sunqitang
  • 浏览: 74922 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

annotation(@Retention)

阅读更多

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


在使用Retention时必须要提供一个RetentionPolicy的枚举类型参数。

RetentionPolicy有三个枚举内容:CLASS RUNTIME SOURCE
     SOURCE, //编译程序处理完Annotation信息后就完成任务
     CLASS,  //编译程序将Annotation储存于class档中,缺省
     RUNTIME //编译程序将Annotation储存于class檔中,可由VM读入(通过反射机制)。这个功能搭配反射是非常强大的。

 

当使用@Retention时要记得加参数和加入"包"。

 

例子:

   第一步:新建一个annotation,名字为:MyAnnotation.java。注意引入了Retention和RetentionPolicy这两个包

package test;

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

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	String hello() default "sunqitang";
	String world();
}

   第二步:建立一个MyTest.java 来使用上面的annotation。

public class MyTest {
	@MyAnnotation(world="Hello,world!",hello="Hello,beijing!")
	public void output(){
		System.out.println("output is running");
	}
}

  第三步:用反射机制来调用注解中的内容

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

public class MyReflection  {
	public static void main(String[] args) throws Exception {
		Class<MyTest> c = MyTest.class;//获得要调用的类
		Method method = c.getMethod("output", new Class[]{});//获得要调用的方法,output为要调用方法名字,new Class[]{}为所需要的参数。由于为空,所以使用这个方式
		if(method.isAnnotationPresent(MyAnnotation.class)){//是否有类型为MyAnnotation的注解
			MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);//获得注解
			System.out.println(annotation.hello()); //调用注解内容
			System.out.println(annotation.world());//调用注解内容
		}
		System.out.println("------------------------");
		Annotation[] annotations = method.getAnnotations();//获得所有注解。必须是在runtime类型的。
		for(Annotation annotation: annotations){//遍历所以注解的名称
			System.out.println(annotation.annotationType().getName());
		}
	}
}

 运行结果:

Hello,beijing!
Hello,world!
------------------------
test.MyAnnotation
 
分享到:
评论
1 楼 wteqiao 2014-11-07  
[color=orange][/color]  

相关推荐

    Java注解之Retention、Documented、Inherited介绍

    主要介绍了Java注解之Retention、Documented、Inherited注解介绍,本文内容和相关文章是系列文章,需要的朋友可以参考下

    观看韩顺平学习整理java的笔记到异常

    基本的 Annotation 介绍 16 @Override 注解 16 @Override 使用说明 17 @Deprecated 注解 17 @SuppressWarnings 注解 17 元注解 19 @Retention 注解 19 @Target 注解 19 @Documented注解 20 @Inherited 注解 20 异常-...

    Java中三种标准注解和四种元注解.pdf

     2.@Retention,  3.@Documented,  4.@Inherited  这些类型和它们所⽀持的类在java.lang.annotation包中可以找到。下⾯我们看⼀下每个元注解的作⽤和相应分参数的使⽤说明。  @Target:  @Target说明了...

    java8源码-Annotation_demo:Annotation_demo

    java8 源码 Java注解 简介 由于无论在Java后台或者Android开发中我们经常遇到注解这个...通过@Target进行添加到注解中,说明了Annotation所修饰的对象范围:Annotation可被用于 packages、types(类、接口、枚举、Ann

    java元注解.docx

    在Java中,元注解(Meta-Annotation)是一种特殊类型的注解,用于注解其他注解。元注解提供对注解的元数据进行定义和控制的能力。Java中提供了几个预定义的元注解,用于定制和修改注解的行为。让我们详细介绍一下...

    JavaSE-注解与反射(框架底层实现机制)

    @Retention:描述注解的生命周期,传入value参数指定 (runtime&gt;class&gt;sources) @Documented:是否生成注解在Javadoc种 @Inherited:子类可以继承父类的注解 自定义注解 @interface 注解名{} 属性为注解的参数:...

    java7源码-Android-Annotation-Study:Android注解生成代码

    Android-Annotation-Study Android注解生成代码 Android注解生成代码老早前就开始用了,在以前Eclipse ADT时代就已经开始使用AvailableAnnotations注解框架,极大简化了Android开发。只是一开始并不了解其原理,感觉...

    Java语言高级部分之注解是什么?

    元注解(JDK的元Annotation)4.1.@Target4.2.@Retention4.3.@Documented4.4.@Inherited4.5.@Result四、在程序中使用(解析)注解五、案例——简单的测试框架 一、注解是什么?  从JDK5开始,Java增加对元数据的支持...

    @SpringBootApplication注解到底做了什么,你真的了解吗?

    @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = ...

    ExtendJavascriptInterface:Android的一个小类,由Android 4.2下的javascript安全调用

    导入java.lang.annotation.Retention; 导入java.lang.annotation.RetentionPolicy; 导入java.lang.annotation.Target; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD}) 公共@...

    \java超强笔记(超级经典)

    四种元注释:java.lang.annotation中的类 元注释:注释注释的注释,用来限定注释的特征 @Terget 用来限定某个注释的使用范围,可以对什么元素进行注释 @Retention 用来描述注释的有效范围 @Inherited ...

    Java内功修炼系列:注解(Annotation)

    3.1 @Retention 3.2 @Target 3.3 @Documented 3.4 @Inherited 3.5 @Repeatable 四 Java 预置的注解 4.1 @Deprecated 4.2 @Override 4.3 @SuppressWarnings 4.4 @SafeVarargs 4.5 @FunctionalInterface 五 注解的属性...

    Java版水果管理系统源码-huihe_2020summer:2020假期spring学习,vue留给你们了,有兴趣自己可以看官方文档,中文很

    注解(Annotation)又称 Java 标注,是 JDK5.0 引入的一种注释机制。 Java 语言中的类、方法、变量、参数和包等都可以被标注。 内置的注解 作用在代码的注解是 @Override - 检查该方法是否是重写方法。如果发现其...

    Java 高级特性.doc

    @Retention(RetentionPolicy.RUNTIME) //在运行是加载Annotation到JVM中 public @interface MyAnnotation { public String value() default "yellow"; public int[] array() default {1,2}; public Meta...

    Android代码-RxEventBus

    A EventBus based on RxJava2, using Retention.CLASS annotation. Getting Started Subscriber register subscriber @Override public void onStart() { super.onStart(); RxEventBus.getDefault().register...

    day021-反射和注解笔记和代码.rar

    枚举是一种类,注解(指的是注解Annotation)是一种接口; 每个数组都是 Class字节码类中的一个具体 对象 基本的 Java 类型(boolean、byte、char、short、int、long、float 和 double)和关键字 void 也表示...

    疯狂JAVA讲义

    第1章 Java概述 1 1.1 Java语言的发展简史 2 1.2 Java的竞争对手及各自优势 4 1.2.1 C#简介和优势 4 1.2.2 Ruby简介和优势 4 1.2.3 Python的简介和优势 5 ...学生提问:当我们使用编译C程序时,不仅需要指定存放...

Global site tag (gtag.js) - Google Analytics