Java注解的简单了解

38 篇文章 1 订阅
20 篇文章 0 订阅

部分信息来自《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));
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值