[JAVA] 注解学习@interface
一直都看框架级的代码中都是各种annotation,一起来看看到底怎么弄的
例子1:直接定义一个annotation,并使用之:
package com.base.annotation.example; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; /**
* Created by guangyi on 15/12/8.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTarget {
String value();
} package com.base.annotation.example; import java.lang.reflect.Method; /**
* Created by guangyi on 15/12/8.
*/
public class MyTargetTest {
@MyTarget("test")
public void doSomething() {
System.out.println("hello world");
}
public static void main(String[] args) throws NoSuchMethodException{
Method method = MyTargetTest.class.getMethod("doSomething", null);
if(method.isAnnotationPresent(MyTarget.class)) {
System.out.println(method.getAnnotation(MyTarget.class)); // 打印@com.base.annotation.example.MyTarget(value=test)
}
}
}
例子2:复杂类型的,各种注解类型
package com.base.annotation.example.example2; /**
* Created by guangyi on 15/12/8.
*/ public class EnumTest {
public static enum TrafficLamp {
RED, GREEN, YELLOW
}
} package com.base.annotation.example.example2; import com.base.annotation.example.MyTarget; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; /**
* Created by guangyi on 15/12/8.
*/
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String hello() default "hello";
String world();
int[] array() default {1, 2, 3, 4};
EnumTest.TrafficLamp lamp();
MyTarget myAnnotation() default @MyTarget("ddd");
Class style() default String.class;
} package com.base.annotation.example.example2; import com.base.annotation.example.MyTarget; import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; /**
* Created by guangyi on 15/12/8.
*/
@MyAnnotation(hello = "beijing", world = "shanghai", array = {5, 6, 7, 8}, lamp = EnumTest.TrafficLamp.GREEN, style = int.class)
public class MyTest {
@MyAnnotation(myAnnotation = @MyTarget("baby"), world = "shanghai", array = {10, 11, 12}, lamp = EnumTest.TrafficLamp.YELLOW)
@Deprecated
@SuppressWarnings("")
public void output() {
System.out.println("output something!");
} public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException{
MyTest myTest = new MyTest();
Class<MyTest> c = MyTest.class;
Method method = c.getMethod("output", new Class[]{});
if(MyTest.class.isAnnotationPresent(MyAnnotation.class)) {
System.out.println("have annotation");
}
if(method.isAnnotationPresent(MyAnnotation.class)) {
method.invoke(myTest, null);
MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
String hello = myAnnotation.hello();
String world = myAnnotation.world();
System.out.println(hello + ", " + world);
System.out.println(myAnnotation.array().length);
System.out.println(myAnnotation.myAnnotation().value());
System.out.println(myAnnotation.style());
System.out.println(myAnnotation.lamp());
}
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation.annotationType().getName());
}
}
}
示例3:看原始注解的定义,可以看到也是使用注解,另外,使用value作为注解属性名,则可以省略注解名字不写,例如:@Rentention(RententionPolicy.RUNTIME):
package java.lang; import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; @Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
String[] value();
} package java.lang; import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; @Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(value={CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})
public @interface Deprecated {
} package java.lang.annotation; @Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
} package java.lang.annotation; @Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
参考:http://blog.csdn.net/liuwenbo0920/article/details/7290586/
[JAVA] 注解学习@interface的更多相关文章
- Java注解学习
一.注解定义 JVM5.0定义了4个标准的元注解: @Target, @Retention, @Documented @Inherited 1. @Target 作用:用于描述注解的使用范围 取值El ...
- java 注解 学习
周末闲来无事,想要研究一下注解方面的知识,曾经看过几次,都忘记了,这次学习下,而且写篇文章记录下, 1.元注解 元注解是指注解的注解.包含 @Retention @Target @Document ...
- Java注解学习笔记
我们平常写Java代码,对其中的注解并不是很陌生,比如说写继承关系的时候经常用到@Override来修饰方法.但是@Override是用来做什么的,为什么写继承方法的时候要加上它,不加行不行.如果对J ...
- java注解学习(1)注解的作用和三个常用java内置注解
今天,记录一下自己学习的关于注解方面的知识. Annotation是从JDK5.0开始引入的新技术 Annotation的作用: -不是程序本身,可以对程序做出解释(这一点和注释没什么区别) -可以被 ...
- java注解学习笔记总结
注解的理解 ① jdk 5.0 新增的功能 ② Annotation 其实就是代码里的特殊标记, 这些标记可以在编译, 类加载, 运行时被读取, 并执行相应的处理.通过使用 Annotation,程序 ...
- java注解使用总结
2005年,sun公司推出了jdk1.5,同时推出的注解功能吸引了很多人的目光,使用注解编写代码,能够减轻java程序员繁琐配置的痛苦. 使用注解可以编写出更加易于维护,bug更少的代码. 注解是什么 ...
- 分享:自定义JAVA注解
元注解 元注解指用来定义注解的注解,例如:@Retention @Target Inherited @Documented等等.最为重要和经常使用的是@Retention @Target. @Rete ...
- 深入学习JAVA注解-Annotation(学习过程)
JAVA注解-Annotation学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...
- Java注解系统学习与实战
背景 为什么要再次梳理一下java注解,显而易见,因为重要啊.也是为研究各大类开源框架做铺垫,只有弄清楚Java注解相关原理,才能看懂大部分框架底层的设计. 缘起 注解也叫做元数据,是JDK1.5版本 ...
随机推荐
- System.Web.Http.Cors配置跨域访问的两种方式
System.Web.Http.Cors配置跨域访问的两种方式 使用System.Web.Http.Cors配置跨域访问,众多大神已经发布了很多文章,我就不在详细描述了,作为小白我只说一下自己的使用心 ...
- [asp.net core] Tag Helpers 简介(转)
原文地址 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/intro What are Tag Helpers? ...
- (转)CentOS下开机启动查看管理命令:chkconfig用法
CentOS下开机启动查看管理命令:chkconfig用法 CentOS下开机启动查看管理的命令是:chkconfig 1. 开机启动列表查看: chkconfig --list 说明 ...
- 阿里提前批校招内推offer经历
经过一个半月的阿里内推面试,今天终于收到了阿里的offer邮件 .阿里的内推面试一共有四轮,本人是7月19号投的内推邮件,8月28号收到了offer的邮件.首先本人谈谈内推的看法.内推是公司招聘人才的 ...
- vim的编译安装及其插件YouCompleteMe安装
相关的环境: win 7 x64 vs2013 community python 2.7.10 AMD64 python 3.5 AMD64 LLVM 3.5 cmake 3.5 YouCompl ...
- SLF4J: Class path contains multiple SLF4J bindings.
库冲突导致的异常,由于多次引入SLF4j包导致. It seems you have several implementation of SLF4J; you should exclude all t ...
- Sql Server创建函数
在使用数据库的过程中,往往我们需要对有的数据先进行计算,然后再查询出来,所以我们就需要创建函数来完成这项任务,在数据库的Programmability(如图1)下面的Function中创建函数(如图2 ...
- 取两个String数组的交集
import org.testng.annotations.Test; import java.util.HashMap; import java.util.LinkedList; import ja ...
- 【Python网络爬虫二】使用urllib2抓去网页内容
在Python中通过导入urllib2组件,来完成网页的抓取工作.在python3.x中被改为urllib.request. 爬取具体的过程类似于使用程序模拟IE浏览器的功能,把URL作为HTTP请求 ...
- Git的checkout, reset, revert
不管是修改还是新建文件,都必须通过git add把这次修改从工作区加到暂存区: commit只是提交暂存区的修改,还没add到暂存区处于工作区的修改是不会commit的: git checkout ...