java Annotation注解的运用
2011-02-17 14:45
423 查看
1、对于其概念和定义就不说了,直接看一下代码。
2、
Description.java
package com.lixx.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Description {
String value();
}
Name.javapackage com.lixx.annotation;
import java.lang.annotation.Documented;
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)
@Documented
public @interface Name {
String originate();
String community();
}
JavaEyer.javapackage com.lixx.annotation;
@Description(value="javaeye,做最棒的软件开发交流社区")
public class JavaEyer {
private String name;
@Name(originate="创始人:robbin",community="javaEye")
public String getName() {
return name;
}
@Name(originate="创始人:江南白衣",community="springside")
public void setName(String name) {
this.name = name;
}
}
TestAnnotation.javapackage com.lixx.annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
public class TestAnnotation {
public static void main(String[] args) throws ClassNotFoundException{
ClassNotFoundException ex ;
String CLASS_NAME = "com.lixx.annotation.JavaEyer";
try {
Class test = Class.forName(CLASS_NAME);
Method[] method = test.getMethods();
boolean flag = test.isAnnotationPresent(Description.class);
if(flag){
Description des = (Description)test.getAnnotation(Description.class);
System.out.println("描述为:"+des.value());
}
/**
* 将javaeyer类中所有用到@Name的全部方法保存到set集合中去
*/
Set<Method> set = new HashSet<Method>();
for(Method methd : method){
boolean otherFlag = methd.isAnnotationPresent(Name.class);
if(otherFlag){
set.add(methd);
}
}
for(Method m : set){
Name name = m.getAnnotation(Name.class);
System.out.println(name.originate()+" "+"创建的社区:"+name.community());
}
return ;
} catch (ClassNotFoundException e) {
ex = e;
}
if(null != ex){
throw ex;
}
}
}
可以试着运行下....
2、
Description.java
package com.lixx.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Description {
String value();
}
Name.javapackage com.lixx.annotation;
import java.lang.annotation.Documented;
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)
@Documented
public @interface Name {
String originate();
String community();
}
JavaEyer.javapackage com.lixx.annotation;
@Description(value="javaeye,做最棒的软件开发交流社区")
public class JavaEyer {
private String name;
@Name(originate="创始人:robbin",community="javaEye")
public String getName() {
return name;
}
@Name(originate="创始人:江南白衣",community="springside")
public void setName(String name) {
this.name = name;
}
}
TestAnnotation.javapackage com.lixx.annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
public class TestAnnotation {
public static void main(String[] args) throws ClassNotFoundException{
ClassNotFoundException ex ;
String CLASS_NAME = "com.lixx.annotation.JavaEyer";
try {
Class test = Class.forName(CLASS_NAME);
Method[] method = test.getMethods();
boolean flag = test.isAnnotationPresent(Description.class);
if(flag){
Description des = (Description)test.getAnnotation(Description.class);
System.out.println("描述为:"+des.value());
}
/**
* 将javaeyer类中所有用到@Name的全部方法保存到set集合中去
*/
Set<Method> set = new HashSet<Method>();
for(Method methd : method){
boolean otherFlag = methd.isAnnotationPresent(Name.class);
if(otherFlag){
set.add(methd);
}
}
for(Method m : set){
Name name = m.getAnnotation(Name.class);
System.out.println(name.originate()+" "+"创建的社区:"+name.community());
}
return ;
} catch (ClassNotFoundException e) {
ex = e;
}
if(null != ex){
throw ex;
}
}
}
可以试着运行下....
相关文章推荐
- java 自定义注解@annotation 运用到方法上
- Java 注解 Annotation
- 一张图了解 Java 注解(Annotation)
- 深入理解Java:注解(Annotation)--注解处理器
- java 中JUint4 中使用注解(annotation)
- 深入理解Java:注解(Annotation)基本概念
- Java注解(Annotation)详解(二)——自定义注解
- java注解(annotation)的执行顺序
- Java 注解Annotation使用解析
- 深入理解Java:注解(Annotation)自定义注解入门
- 深入理解Java:注解(Annotation)基本概念
- 深入理解Java:注解(Annotation)自定义注解入门
- 11.Java注解(Annotation)
- 深入理解Java:注解(Annotation)自定义注解入门
- Java 注解(annotation)
- 深入理解Java:注解(Annotation)基本概念
- java注解应用实例 - Annotation, 自定义注解, 注解类规则
- Java Annotation(注解)
- 深入理解Java:注解(Annotation)
- Java学习之注解Annotation实现原理