Java自定义注解Annotation及取注解值的例子

来源:互联网 发布:googlenet in caffe 编辑:程序博客网 时间:2024/05/29 19:15

1.基本概念如元注解如何使用请看这篇文章,姐姐觉得写的不错啊,拿这个学习的,然后自己测试了一把


2.自定义各种作用范围的注解

a.定义在包上的注解

package annotation;import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Target(ElementType.PACKAGE)@Retention(RetentionPolicy.RUNTIME)public @interface TestPackage {public String name() default "TestPackage";}

b.定义在类上的注解
package annotation;import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Target(ElementType.TYPE)@Retention(RetentionPolicy.RUNTIME)public @interface TestType {public String name() default "TestType";}

c.定义在构造器上的注解
package annotation;import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Target(ElementType.CONSTRUCTOR)@Retention(RetentionPolicy.RUNTIME)public @interface TestContructor {public String name() default "TestContructor";}



d.定义在成员变量上的注解
package annotation;import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface TestField {public String name() default "TestField";}


e.定义在方法上的注解
package annotation;import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface TestMethod {public String name() default "TestMethod";}


f.定义在方法入参上的注解
package annotation;import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Target(ElementType.PARAMETER)@Retention(RetentionPolicy.RUNTIME)public @interface TestParameter {public String name() default "TestParameter";}

g.定义在方法里面局部变量的注解,这个暂时没有找到取注解值的办法,如果知道的可以留言教一下姐姐,呵呵
package annotation;import java.lang.annotation.Target;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;@Target(ElementType.LOCAL_VARIABLE)@Retention(RetentionPolicy.RUNTIME)public @interface TestLocalVariable {public String name() default "TestLocalVariable";}

h.包注解不能写在类里面,必须写在一个文件里面package-info.java,这个带横杠无法生成,要用记事本生成,里面写入注解
@TestPackage(name="包注解")package annotation;

3.测试

package annotation;import java.lang.annotation.Annotation;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.Method;import java.lang.reflect.Type;@TestType(name = "类")public class Test {private static final String constructors = null;@TestField(name = "属性")private String s;@TestContructor(name = "构造器")public Test(String s) {this.s = s;};public Test() {};@TestMethod(name = "方法")public void test(@TestParameter(name = "参数") String a) {System.out.println(a);@TestLocalVariable(name = "本地变量")String s = "";System.out.println(s);}public static void main(String[] args) throws Exception {// 获取Test类对象Class<Test> clazz = Test.class; //可以通过I/O操作或配置项获得包名          Package pkg = Package.getPackage("annotation");          //获得指定包上的注解          System.out.println(Package.getPackage("annotation").getAnnotation(TestPackage.class).name());          //获得包上的注解          for(Annotation an:Package.getPackage("annotation").getAnnotations()){              if(an instanceof TestPackage){                  System.out.println(((TestPackage) an).name());                      }          }                  // 获取类对象为testtype注解的值System.out.println("获取TestType注解的值------"+clazz.getAnnotation(TestType.class).name());// 获取类对象为@TestContructor某个成员变量注解的值System.out.println("获取指定TestContructor注解的值------"+clazz.getDeclaredConstructor(String.class).getAnnotation(TestContructor.class).name());// 获取所有成员变量@SuppressWarnings("rawtypes")Constructor[] constructorss = clazz.getDeclaredConstructors();for (Constructor constructor : constructorss) {if (constructor.isAnnotationPresent(TestContructor.class)) {System.out.println("获取TestContructor注解的值------"+((TestContructor) constructor.getAnnotation(TestContructor.class)).name());}}// 获取类对象为testfield某个成员变量注解的值System.out.println("获取指定TestField注解的值------"+clazz.getDeclaredField("s").getAnnotation(TestField.class).name());// 获取所有成员变量Field[] fields = clazz.getDeclaredFields();for (Field field : fields) {if (field.isAnnotationPresent(TestField.class)) {System.out.println("获取TestField注解的值------"+field.getAnnotation(TestField.class).name());}}// 获取类对象为TestMethod某个方法注解的值Method method = clazz.getDeclaredMethod("test", String.class);System.out.println("获取指定TestMethod注解的值------"+method.getAnnotation(TestMethod.class).name());// 获取所有方法的值Method[] methods = clazz.getDeclaredMethods();System.out.println(methods.length);for (Method m : methods) {if (m.isAnnotationPresent(TestMethod.class)) {System.out.println("获取TestMethod注解的值------"+m.getAnnotation(TestMethod.class).name());System.out.println(m.getParameterAnnotations().length);for (Annotation[] parameterAnnotation : m.getParameterAnnotations()) {for (Annotation annotation : parameterAnnotation) {if (annotation instanceof TestParameter) {TestParameter param = (TestParameter) annotation;System.out.println("获取TestParameter注解的值------"+param.name());}}}}}}}


4.结果如下


5.完事了~~~~~

原创粉丝点击