关于java语言的Annotation,讲解的文章很多了。不再废话,直接上代码说明怎么使用Annotation.
MyAnnotation的java文件如下:
-
package Annotation;
-
-
import java.lang.annotation.Documented;
-
import java.lang.annotation.ElementType;
-
import java.lang.annotation.Inherited;
-
import java.lang.annotation.Retention;
-
import java.lang.annotation.RetentionPolicy;
-
import java.lang.annotation.Target;
-
-
//@Retention(RetentionPolicy.SOURCE)
-
@Retention(RetentionPolicy.RUNTIME)
-
public @interface MyAnnotation
-
{
-
String hello() default "namaste";
-
-
String world();//没有指定默认值,使用这个Annotation的时候,必须给world指定值
-
-
int[] array() default { 2, 4, 5, 6 };
-
-
Class style() default String.class;
-
}
以上文件定义了一个Annotation.
MyTest类的java文件如下:
-
package Annotation;
-
-
import java.lang.String;
-
-
@MyAnnotation(hello = "beijing", world = "shanghai", array={2, 3, 5, 7 }, style = int.class)
-
public class MyTest
-
{
-
@MyAnnotation(world = "shanghai", array={2, 4, 6, 8})
-
@SuppressWarnings("unchecked")
-
public void output()
-
{
-
System.out.println("output something!");
-
}
-
}
这个类以及其中的方法使用了MyAnnotation.
程序入口的类,代码如下:
-
package Annotation;
-
-
import java.lang.reflect.Method;
-
import java.lang.annotation.Annotation;
-
import java.lang.reflect.AnnotatedElement;
-
-
public class ExampleMain {
-
//打印MyAnnotation对象
-
static void printAnnotation(MyAnnotation annotation, String desc)
-
{
-
if (annotation == null)
-
return ;
-
if (desc != null)
-
System.out.println(desc);
-
-
System.out.println("hello is: " + annotation.hello());
-
System.out.println("world is: " + annotation.world());
-
if (annotation.array() != null)
-
{
-
System.out.print("array is: ");
-
for(int a : annotation.array())
-
{
-
System.out.print(a);
-
System.out.print("\t");
-
}
-
System.out.println("");
-
}
-
System.out.println("style is: " + annotation.style());
-
-
System.out.println("");
-
}
-
-
static void test1()
-
{
-
//如果MyTest类名上有注解@MyAnnotation修饰,则为true
-
if (!MyTest.class.isAnnotationPresent(MyAnnotation.class))
-
{
-
System.out.println("class MyTest DOES NOT have any annotation");
-
return ;
-
}
-
-
MyAnnotation annotation = MyTest.class.getAnnotation(MyAnnotation.class);
-
printAnnotation(annotation, "MyTest类上的Annotation如下:");
-
}
-
-
static void test2()
-
{
-
Class<MyTest> c = MyTest.class;
-
Method method = null;
-
try {
-
method = c.getMethod("output", new Class[] {});
-
}
-
catch (NoSuchMethodException ne){
-
System.out.println("MyTest 类没有output这个方法");
-
return ;
-
}
-
-
if (method.isAnnotationPresent(MyAnnotation.class))
-
{
-
//获取方法上注解@MyAnnotation的信息
-
MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
-
printAnnotation(annotation, "MyTest类的output方法的Annotation如下:");
-
}
-
-
//得到output方法上的所有注解,当然是被RetentionPolicy.RUNTIME修饰的
-
-
System.out.println("output方法上的所有注解: ");
-
Annotation[] annotations = method.getAnnotations();
-
for (Annotation annotation : annotations)
-
{
-
System.out.println("annotation type name : " + annotation.annotationType().getName());
-
}
-
}
-
-
public static void main(String[] args) throws Exception
-
{
-
//例子1: 获得一个类的Annotation
-
//test1();
-
-
//例子2:获得一个方法的Annotation
-
test2();
-
-
}
-
}
在main函数可以调用test1(),也可以调用test2().分别展示了怎样通过反射技术来获得MyTest类的Annotation。
阅读(863) | 评论(0) | 转发(0) |