Java自定义Annotation学习
2012-02-03 00:08
260 查看
本次学习的目标是为了获取如下Java类成员中ID的值: Java代码 package com.perficient.annotation;
public class WebPage
{
@Identifier(id= "A")
public String
buttonA;
@Identifier(id= "B")
public String
buttonB;
}
package com.perficient.annotation;public class WebPage { @Identifier(id= "A") public String buttonA; @Identifier(id= "B") public String buttonB;}[/pre]
创建自定义的Annotation: Java代码 package com.perficient.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Identifier
{
public String
id();
}
package com.perficient.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME)
public @interface Identifier { public String id(); }[/pre]
注意:Target一定要给予正确的值,如果是类成员的话用Field,是方法要用Method
编写测试类 Java代码 package com.perficient.annotation;
import java.lang.reflect.Field;
public class AnnotationTest
{
public static void main(String[]
args) throws ClassNotFoundException
{
String className = "com.perficient.annotation.WebPage";
Class<?> test = Class.forName(className);
Field[] fields = test.getFields();
for (Field
field : fields){
System.out.println("The
field Name is:" + field);
boolean flag
= field.isAnnotationPresent(Identifier.class);
if (flag)
{
Identifier idt = (Identifier) field
.getAnnotation(Identifier.class);
System.out.println("Id
is:" + idt.id());
} else {
System.out.println("The
annotation can't be found");
}
}
}
}
package com.perficient.annotation;import java.lang.reflect.Field;public class AnnotationTest { public static void main(String[] args) throws ClassNotFoundException { String className = "com.perficient.annotation.WebPage"; Class<?> test = Class.forName(className);
Field[] fields = test.getFields(); for (Field field : fields){ System.out.println("The field Name is:" + field); boolean flag = field.isAnnotationPresent(Identifier.class); if (flag) { Identifier
idt = (Identifier) field .getAnnotation(Identifier.class); System.out.println("Id is:" + idt.id()); } else { System.out.println("The annotation can't be found"); }
} }}[/pre]
注意:如果是Method,需要使用getDeclaredMethods(),如果用getMethods()不会返回我们所需要的结果
输出结果: Java代码The field Name is:public java.lang.String
com.perficient.annotation.WebPage.buttonA
Id is:A
The field Name is:public java.lang.String
com.perficient.annotation.WebPage.buttonB
Id is:B
public class WebPage
{
@Identifier(id= "A")
public String
buttonA;
@Identifier(id= "B")
public String
buttonB;
}
package com.perficient.annotation;public class WebPage { @Identifier(id= "A") public String buttonA; @Identifier(id= "B") public String buttonB;}[/pre]
创建自定义的Annotation: Java代码 package com.perficient.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Identifier
{
public String
id();
}
package com.perficient.annotation;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;@Target(ElementType.FIELD) @Retention(RetentionPolicy.RUNTIME)
public @interface Identifier { public String id(); }[/pre]
注意:Target一定要给予正确的值,如果是类成员的话用Field,是方法要用Method
编写测试类 Java代码 package com.perficient.annotation;
import java.lang.reflect.Field;
public class AnnotationTest
{
public static void main(String[]
args) throws ClassNotFoundException
{
String className = "com.perficient.annotation.WebPage";
Class<?> test = Class.forName(className);
Field[] fields = test.getFields();
for (Field
field : fields){
System.out.println("The
field Name is:" + field);
boolean flag
= field.isAnnotationPresent(Identifier.class);
if (flag)
{
Identifier idt = (Identifier) field
.getAnnotation(Identifier.class);
System.out.println("Id
is:" + idt.id());
} else {
System.out.println("The
annotation can't be found");
}
}
}
}
package com.perficient.annotation;import java.lang.reflect.Field;public class AnnotationTest { public static void main(String[] args) throws ClassNotFoundException { String className = "com.perficient.annotation.WebPage"; Class<?> test = Class.forName(className);
Field[] fields = test.getFields(); for (Field field : fields){ System.out.println("The field Name is:" + field); boolean flag = field.isAnnotationPresent(Identifier.class); if (flag) { Identifier
idt = (Identifier) field .getAnnotation(Identifier.class); System.out.println("Id is:" + idt.id()); } else { System.out.println("The annotation can't be found"); }
} }}[/pre]
注意:如果是Method,需要使用getDeclaredMethods(),如果用getMethods()不会返回我们所需要的结果
输出结果: Java代码The field Name is:public java.lang.String
com.perficient.annotation.WebPage.buttonA
Id is:A
The field Name is:public java.lang.String
com.perficient.annotation.WebPage.buttonB
Id is:B
相关文章推荐
- java 学习自定义注解Annotation
- java annotation注解学习,自定义注解
- 深入JAVA注解(Annotation):自定义注解
- Java -自定义Annotation
- Java Annotation 学习(1)
- JAVA学习11_Java注解Annotation详解
- java如何自定义annotation
- 深入理解Java:注解(Annotation)自定义注解入门
- 深入理解Java注解(Annotation)--自定义注解
- 深入理解Java:注解(Annotation)自定义注解入门
- 深入理解Java:注解(Annotation)自定义注解入门
- 【Java学习笔记】HashSet中加入自定义的类的对象
- 使用Java自定义Annotation
- JAVA-Struts2学习之自定义拦截器
- java提高篇(49)--注解(Annotation)自定义注解入门
- javaweb中对于自定义标签的学习
- 深入理解Java:注解(Annotation)自定义注解入门
- 深入理解Java:注解(Annotation)自定义注解入门
- 深入理解Java:注解(Annotation)自定义注解入门
- Java进阶之Annotation(注解)——自定义注解