标签:java自定义注解
【第一部分】
首先了解一下java1.5起默认的三个annotation类型:
@override:只能用在方法上,用来告诉人们这个方法是改写的父类的
@Deprecated:建议别人不要使用旧的api的时候使用的,编译的时候会产生警告信息,可以设定在程序的所有元素上。
@SuppressWarnings:这一类型可以暂时把一些警告信息消除。
【第二部分】
先讲一下怎么自己设计一个annotation,最好的就是读以下jdk自带的annotation源文件
1、源文件Documented.class
/*
* @(#)Documented.java 1.6 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang.annotation;
/**
* Indicates that annotations with a type are to be documented by javadoc
* and similar tools by default. This type should be used to annotate the
* declarations of types whose annotations affect the use of annotated
* elements by their clients. If a type declaration is annotated with
* Documented, its annotations become part of the public API
* of the annotated elements.
*
* @author Joshua Bloch
* @version 1.6, 11/17/05
* @since 1.5
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
2、Target.class
/*
* @(#)Target.java 1.6 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang.annotation;
/**
* Indicates the kinds of program element to which an annotation type
* is applicable. If a Target meta-annotation is not present on an
* annotation type declaration, the declared type may be used on any
* program element. If such a meta-annotation is present, the compiler
* will enforce the specified usage restriction.
*
* For example, this meta-annotation indicates that the declared type is
* itself a meta-annotation type. It can only be used on annotation type
* declarations:
* <pre>
* @Target(ElementType.ANNOTATION_TYPE)
* public @interface MetaAnnotationType {
* ...
* }
* </pre>
* This meta-annotation indicates that the declared type is intended solely
* for use as a member type in complex annotation type declarations. It
* cannot be used to annotate anything directly:
* <pre>
* @Target({})
* public @interface MemberType {
* ...
* }
* </pre>
* It is a compile-time error for a single ElementType constant to
* appear more than once in a Target annotation. For example, the
* following meta-annotation is illegal:
* <pre>
* @Target({ElementType.FIELD, ElementType.METHOD, ElementType.FIELD})
* public @interface Bogus {
* ...
* }
* </pre>
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
/*
* @(#)Retention.java 1.6 05/11/17
*
* Copyright 2006 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/
package java.lang.annotation;
/**
* Indicates how long annotations with the annotated type are to
* be retained. If no Retention annotation is present on
* an annotation type declaration, the retention policy defaults to
* <tt>RetentionPolicy.CLASS</tt>.
*
* <p>A Target meta-annotation has effect only if the meta-annotated
* type is use directly for annotation. It has no effect if the meta-annotated
* type is used as a member type in another annotation type.
*
* @author Joshua Bloch
* @since 1.5
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
RetentionPolicy value();
}
下面我们自己来写一个注解类使用
1、Description
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)
@Documented//作用是在生成javadoc文档的时候将该Annotation也写入到文档中。
@Retention(RetentionPolicy.RUNTIME)//指示注释类型的注释要保留多久。默认为 RetentionPolicy.CLASS。取值为枚举
/**
* 注解中定义的属性如果名称为 value, 此属性在使用时可以省写属性名
*/
public @interface Description {
String value() ;
}
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;
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
/**
* 使用了默认值
*/
public @interface Name {
String work() default "JAVA";
String community() default "BLOG";
}
@Description("吴海旭的社区测试")
public class hzu_Opensource {
@Name(work="sales",community="ITeye")
public String getName(){
return null ;
}
@Name(community="csdn",work="IT")
public String getName2(){
return "csdn" ;
}
@Name
public String getName3(){
return null ;
}
}
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
public class TestAnnotation {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
String CLASS_NAME = "hzu_Opensource" ;
Class test = Class.forName(CLASS_NAME) ;
boolean flag = test.isAnnotationPresent(Description.class) ;
if(flag){
Description des = (Description)test.getAnnotation(Description.class) ;
System.out.println("描述: " + des.value());
System.out.println("---------------");
}
Method[] method = test.getMethods() ;
Set<Method> set = new HashSet<Method>() ;
for(int i=0;i<method.length;i++){
boolean otherFlag = method[i].isAnnotationPresent(Name.class) ;
if(otherFlag) set.add(method[i]) ;
}
for(Method m:set){
Name name = m.getAnnotation(Name.class) ;
System.out.println(name.work());
System.out.println("社区:" + name.community());
}
}
}
描述: 吴海旭的社区测试 --------------- JAVA 社区:BLOG IT 社区:csdn sales 社区:ITeye
标签:java自定义注解
原文地址:http://blog.csdn.net/benjamin_whx/article/details/38228461