java定义自己的Annotation类型
// 自定义Annotation类型
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation
{
public String value() default ” “;
}
/*
ubuntu@xu-desktop:~$ java com.mldn.ReflectAnnotation
自定义Annotation:com.mldn.MyAnnotation value = www.k187.com
所有当前方法上使用的RUNTIME范围的Annotation:
@com.mldn.MyAnnotation(value=www.k187.com)
@java.lang.Deprecated()
*/
———@Target、@Documented类型:
1.@Target类型:
@Documented
@Retention(value=RUNTIME)
@Target(value=ANNOTATION_TYPE) // 只能使用在Annotation类型上
public @interface Target
{
public abstract ElementType[] value(); // 取值范围是个枚举类型;
}
// java中ElementType:元素类型定义:Target.value的取值:
public enum ElementType
extends Enum
1. TYPE
public static final ElementType TYPE;
Class, interface (including annotation type), or enum declaration
2.FIELD
public static final ElementType FIELD;
Field declaration (includes enum constants)
3.METHOD
public static final ElementType METHOD;
Method declaration
4.PARAMETER
public static final ElementType PARAMETER;
Parameter declaration
5.CONSTRUCTOR
public static final ElementType CONSTRUCTOR;
Constructor declaration
6.LOCAL_VARIABLE
public static final ElementType LOCAL_VARIABLE
Annotation type declaration
8.PACKAGE
public static final ElementType PACKAGE;
Package declaration
——定义Annotation的Target参数:
package com.mldn;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.reflect.Method;
public class ReflectAnnotation
{
public static void main(String[] args)
{
Class> c = null; // 声明反射的源头
Method me = null; // 方法
try
{
c = Class.forName(“com.mldn.AnnUse”); // 实例化
me = c.getDeclaredMethod(“toString”); // 通过反射获取方法
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (NoSuchMethodException e)
{
e.printStackTrace();
}
if (me.isAnnotationPresent(MyAnnotation.class)) // 当前元素上是否存在该类型的Annotation
{
MyAnnotation man = me.getAnnotation(MyAnnotation.class); // 获取该方法上使用的在运行时活动的该类型的Annotation;
String str = man.value(); // 取出当前Annotation的value值,注意:()!
System.out.println(“自定义Annotation:” + MyAnnotation.class.getName() + ” value = ” + str);
}
Annotation[] an = me.getAnnotations(); // 获取该方法上使用的所有在运行时活动的Annotation;
System.out.println(“所有当前方法上使用的RUNTIME范围的Annotation:”);
for (Annotation a : an)
{
System.out.println(a);
}
}
}
class AnnUse
{
@MyAnnotation(value = “www.k187.com”) // 使用自定义Annotation
@SuppressWarnings(“unchecked”) // 压制警告信息
@Deprecated // 不建议使用
@Override // 覆写的方法
public String toString()
{
// 覆写
return “This is a Override funcation!”;
}
}
// 自定义Annotation类型
@Target(value = ElementType.TYPE) // 表示只使用在类型声明上!class,interface( or annotation type),enum
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation
{
public String value() default ” “;
}
/*
ubuntu@xu-desktop:~$ javac -d . work/ReflectAnnotation.java
work/ReflectAnnotation.java:53: 注释类型不适用于该类型的声明 // 因为该类型是用在类型声明上的,而不是方法声明!
@MyAnnotation(value = “www.k187.com”) // 使用自定义Annotation
^
1 错误
*/
———@Target、@Documented类型:
1.@Target类型:
1. @Documented
2. @Retention(value=RUNTIME)
3. @Target(value=ANNOTATION_TYPE) // 只能使用在Annotation类型上
4. public @interface Target
5. {
6. public abstract ElementType[] value(); // 取值范围是个枚举类型;
7. }
复制代码
// java中ElementType:元素类型定义:Target.value的取值:
1. public enum ElementType
2. extends Enum
复制代码
1.TYPE
1. public static final ElementType TYPE;
复制代码
Class, interface (including annotation type), or enum declaration
2.FIELD
1. public static final ElementType FIELD;
复制代码
Field declaration (includes enum constants)
3.METHOD
1. public static final ElementType METHOD;
复制代码
Method declaration
4.PARAMETER
1. public static final ElementType PARAMETER;
复制代码
Parameter declaration
5.CONSTRUCTOR
1. public static final ElementType CONSTRUCTOR;
复制代码
Constructor declaration
6.LOCAL_VARIABLE
1. public static final ElementType LOCAL_VARIABLE
复制代码
Local variable declaration
7.ANNOTATION_TYPE
1. public static final ElementType ANNOTATION_TYPE;
复制代码
Annotation type declaration
8.PACKAGE
1. public static final ElementType PACKAGE;
复制代码
Package declaration
——定义Annotation的Target参数:
1. package com.mldn;
2.
3. import java.lang.annotation.Annotation;
4. import java.lang.annotation.Retention;
5. import java.lang.annotation.RetentionPolicy;
6. import java.lang.annotation.Target;
7. import java.lang.annotation.ElementType;
8. import java.lang.reflect.Method;
9.
10. public class ReflectAnnotation
11. {
12. public static void main(String[] args)
13. {
14. Class> c = null; // 声明反射的源头
15. Method me = null; // 方法
16.
17. try
18. {
19. c = Class.forName(“com.mldn.AnnUse”); // 实例化
20.
21. me = c.getDeclaredMethod(“toString”); // 通过反射获取方法
22. }
23. catch (ClassNotFoundException e)
24. {
25. e.printStackTrace();
26. }
27. catch (NoSuchMethodException e)
28. {
29. e.printStackTrace();
30. }
31.
32. if (me.isAnnotationPresent(MyAnnotation.class)) // 当前元素上是否存在该类型的Annotation
33. {
34. MyAnnotation man = me.getAnnotation(MyAnnotation.class); // 获取该方法上使用的在运行时活动的该类型的Annotation;
35.
36. String str = man.value(); // 取出当前Annotation的value值,注意:()!
37.
38. System.out.println(“自定义Annotation:” + MyAnnotation.class.getName() + ” value = ” + str);
39. }
40.
41. Annotation[] an = me.getAnnotations(); // 获取该方法上使用的所有在运行时活动的Annotation;
42.
43. System.out.println(“所有当前方法上使用的RUNTIME范围的Annotation:”);
44. for (Annotation a : an)
45. {
46. System.out.println(a);
47. }
48. }
49. }
50.
51. class AnnUse
52. {
53. @MyAnnotation(value = “www.k187.com”) // 使用自定义Annotation
54. @SuppressWarnings(“unchecked”) // 压制警告信息
55. @Deprecated // 不建议使用
56. @Override // 覆写的方法
57. public String toString()
58. {
59. // 覆写
60. return “This is a Override funcation!”;
61. }
62. }
复制代码
1. // 自定义Annotation类型
2. @Target(value = ElementType.TYPE) // 表示只使用在类型声明上!class,interface( or annotation type),enum
3. @Retention(value = RetentionPolicy.RUNTIME)
4. @interface MyAnnotation
5. {
6. public String value() default ” “;
7. }
8. /*
9. ubuntu@xu-desktop:~$ javac -d . work/ReflectAnnotation.java
10. work/ReflectAnnotation.java:53: 注释类型不适用于该类型的声明 // 因为该类型是用在类型声明上的,而不是方法声明!
11. @MyAnnotation(value = “www.k187.com”) // 使用自定义Annotation
12. ^
13. 1 错误
14. */
复制代码
声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 嗅谱网
转载请注明:转自《java定义自己的Annotation类型》
本文地址:http://www.xiupu.net/archives-150.html
关注公众号:
微信赞赏
支付宝赞赏