博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java注解的简单了解
阅读量:7234 次
发布时间:2019-06-29

本文共 2614 字,大约阅读时间需要 8 分钟。

部分信息来自《Thinking In Java》

注解也成为元数据。什么是元数据?就是“关于数据的数据”

注解为我们在代码中添加信息提供了一种形式化的方法,使我们可以在稍后某个时刻非常方便的使用这些数据。

它可以用来完整的描述程序所需的信息,能够让编译器来测试和验证格式,存储有关程序的额外信息。

定义一个注解:

注解的样子和接口很像

 

package me.benzeph.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {}

定义注解时,需要一些元注解(关于注解的注解)例如:@Target和@Retention。

 

@Target用来定义你的注解应用于什么地方。例如:方法,域。

 

@Target(ElementType.METHOD)@Target(ElementType.CONSTRUCTOR)@Target(ElementType.PARAMETER)...

@Retention用来定义你的注解在哪个级别可用。例如:源代码中,类文件中或者运行时。

 

 

@Retention(RetentionPolicy.RUNTIME)@Retention(RetentionPolicy.CLASS)@Retention(RetentionPolicy.SOURCE)

如果一个注解中没有一个元素。那么它就称为标记注解。例如@Test。

 

在注解中,一般都会包含一些元素以表示某些值。当分析处理注解时,程序或工具可以利用这些值。

 

package me.benzeph.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {	public int value();}

 

package me.benzeph.annotation;public class UseAnnotation {	@MyAnnotation(value = 10)	public void useMyAnnotation() {	}}

如果没有读取注解的工具,那么注解也不会比注释更有用。

 

利用两个反射方法去解析一个含有某个注解的方法。

 

package me.benzeph.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation {	int value();}

 

package me.benzeph.annotation;public class UseAnnotation {	@MyAnnotation(value = 10)	public void useMyAnnotation() {	}}

 

package me.benzeph.annotation;import java.lang.reflect.Method;public class MyAnnotationParser {	public boolean findMethodByValue(int value) {		for (Method method : UseAnnotation.class.getDeclaredMethods()) {			MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);			if ((myAnnotation != null) && (myAnnotation.value() == value)) {				return true;			}		}		return false;	}}

 

package me.benzeph.annotation;import org.junit.Before;import org.junit.Test;import static org.junit.Assert.assertFalse;import static org.junit.Assert.assertTrue;public class MyAnnotationParserTest {	private MyAnnotationParser parser;	@Before	public void setUp() throws Exception {		parser = new MyAnnotationParser();	}	@Test	public void shouldReturnTrue() {		assertTrue(parser.findMethodByValue(10));	}	@Test	public void shouldReturnFalse() {		assertFalse(parser.findMethodByValue(5));	}}

 

 

转载地址:http://wplfm.baihongyu.com/

你可能感兴趣的文章
Installing Chocolatey
查看>>
python3+spark2.1+kafka0.8+sparkStreaming
查看>>
jstl自己定义函数的使用
查看>>
使用Visual Studio Code调试React Native报错
查看>>
FineUI 将不再内置 ExtJS (严格遵守 ExtJS 的开源规则)
查看>>
javascript 中contentWindow和 frames和iframe之间通信
查看>>
取得正在运行的Activity
查看>>
UVA 103 Stacking Boxes 套箱子 DAG最长路 dp记忆化搜索
查看>>
二分-hdu-4768-Flyer
查看>>
IE下target获得焦点时存在虚线的问题
查看>>
Web App开发入门
查看>>
PHP实现金额数字转换成大写函数
查看>>
IE读取并显示本地图像文件的方法
查看>>
自学android半年,已从.net转型成android程序员,分享下这个过程
查看>>
ImageView显示网络图片
查看>>
linux防止sshd被爆破(安装denyhosts)
查看>>
【P4语言学习】Parser解析器
查看>>
python问题:AttributeError: 'module' object has no attribute 'SSL_ST_INIT'(转)
查看>>
测试人员职业规划
查看>>
Twenty Newsgroups Classification任务之二seq2sparse(3)
查看>>