您的位置:首页 > 编程语言 > Java开发

Java注解的简单了解

2013-09-02 18:44 309 查看
部分信息来自《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));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: