1.12.1 Obtaining Annotations at Run Time by Use of Reflection 运用反射在运行时获得注解
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; // A simple annotation type. @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String stringValue(); int intValue(); } public class MainClass { // Annotate a method. @MyAnnotation(stringValue = "Annotation Example", intValue = 100) public static void myMethod() { } public static void main(String[] a) { try { MainClass ob = new MainClass(); Class c = ob.getClass(); Method m = c.getMethod("myMethod"); MyAnnotation anno = m.getAnnotation(MyAnnotation.class); System.out.println(anno.stringValue() + " " + anno.intValue()); } catch (NoSuchMethodException exc) { System.out.println("Method Not Found."); } } } //Annotation Example 100
1.12.2 Reflection: getMethod( ) with parameters 反射: 带参数调用getMethod()
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; // A simple annotation type. @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String stringValue(); int intValue(); } public class MainClass { // Annotate a method. @MyAnnotation(stringValue = "Annotation Example", intValue = 100) public static void myMethod(String str, int i) { } public static void main(String[] a) { try { MainClass ob = new MainClass(); Class c = ob.getClass(); Method m = c.getMethod("myMethod", String.class, int.class); MyAnnotation anno = m.getAnnotation(MyAnnotation.class); System.out.println(anno.stringValue() + " " + anno.intValue()); } catch (NoSuchMethodException exc) { System.out.println("Method Not Found."); } } } //Annotation Example 100
新奇的是, int.class 居然在编译器眼里是合法的!
1.12.3 Obtaining All Annotations: getAnnotations() 获得类的所有的注解
import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; // A simple annotation type. @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String stringValue(); int intValue(); } @Retention(RetentionPolicy.RUNTIME) @interface What { String description(); } @What(description = "An annotation test class") @MyAnnotation(stringValue = "for class", intValue = 100) public class MainClass { // Annotate a method. @What(description = "An annotation test method") @MyAnnotation(stringValue = "Annotation Example", intValue = 100) public static void myMethod(String str, int i) { } public static void main(String[] arg) { try { MainClass ob = new MainClass(); Annotation[] annos = ob.getClass().getAnnotations(); System.out.println("All annotations for Meta2:"); for(Annotation a : annos) System.out.println(a); } catch (Exception exc) { } } } /* All annotations for Meta2: @MyAnnotation(stringValue=for class, intValue=100) @What(description=An annotation test class) */
1.12.4 Getting all annotations for a method 获得方法的所有注解
import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Method; // A simple annotation type. @Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String stringValue(); int intValue(); } @Retention(RetentionPolicy.RUNTIME) @interface What { String description(); } @What(description = "An annotation test class") @MyAnnotation(stringValue = "for class", intValue = 100) public class MainClass { // Annotate a method. @What(description = "An annotation test method") @MyAnnotation(stringValue = "Annotation Example", intValue = 100) public static void myMethod() { } public static void main(String[] arg) { try { MainClass ob = new MainClass(); Method m = ob.getClass( ).getMethod("myMethod"); Annotation[] annos = m.getAnnotations(); System.out.println("All annotations for myMeth:"); for(Annotation a : annos) System.out.println(a); } catch (Exception exc) { } } } /* All annotations for myMeth: @What(description=An annotation test method) @MyAnnotation(stringValue=Annotation Example, intValue=100) */
相关推荐
Android annotations 注解 使用,一些使用的注解实现更好的编程
功能:完全注解框架,一切皆为注解:声明控件,绑定控件,...缺点:文档是全英文的加上功能比较少没有具体研究,由于一切都是注解,感觉效率不高,不过根据官方介绍说并不是使用的反射加载,所以效率比一般注解高很多。
annotations-api.jar注解
类、方法、变量、参数、包都可以被注解,可用来将信息元数据与程序元素进行关联 An annotation is a form of metadata, that can be added to Java source code. Classes, methods, variables, parameters and ...
Java JSOUP 标注方案,不用自己费劲解析了
无注解之属性的默认值 2.2.. 映射主键属性 2.2.4. 映射继承关系 2.2.4.1. 每个类一张表 2.2.4.2. 每个类层次结构一张表 2.2.4.3. 连接的子类 2.2.4.4. 从父类继承的属性 2.2.5. 映射实体Bean的关联关系 2.2....
最新注解JAR包. 找了N久才找到的.
赠送jar包:swagger-annotations-2.1.2.jar; 赠送原API文档:swagger-annotations-2.1.2-javadoc.jar; 赠送源代码:swagger-annotations-2.1.2-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-2.1.2....
赠送jar包:swagger-annotations-1.5.20.jar; 赠送原API文档:swagger-annotations-1.5.20-javadoc.jar; 赠送源代码:swagger-annotations-1.5.20-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-...
jackson-annotations, 对于Jackson数据处理器,核心注解( 仅依赖于 概述这个项目包含了Jackson数据处理器的通用注释,用于值和处理程序类型。 惟一不包括的注释是需要依赖于 Databind包的注释。项目包含版本 2.0和上...
赠送jar包:swagger-annotations-1.6.2.jar; 赠送原API文档:swagger-annotations-1.6.2-javadoc.jar; 赠送源代码:swagger-annotations-1.6.2-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-1.6.2....
开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-2.8.6开发工具 jackson-annotations-...
赠送jar包:audience-annotations-0.5.0.jar; 赠送原API文档:audience-annotations-0.5.0-javadoc.jar; 赠送源代码:audience-annotations-0.5.0-sources.jar; 赠送Maven依赖信息文件:audience-annotations-...
使用注解Annotation时用的jar包。 包括: hibernate-commons-annotations-3.3.0.ga.jar hibernate-entitymanager.jar ejb3-persistence.jar hibernate-annotations.jar
Android注解支持,Android support library从19.1版本开始引入了一个新的注解库,support-annotations.zip
赠送jar包:swagger-annotations-1.5.24.jar; 赠送原API文档:swagger-annotations-1.5.24-javadoc.jar; 赠送源代码:swagger-annotations-1.5.24-sources.jar; 赠送Maven依赖信息文件:swagger-annotations-...
Hibernate注解jar包 介绍:Hibernate Annotations注解使用的jar包。
2 ... 2 ... 5 ... 2 ... 7 ... 8 ...
赠送jar包:jackson-annotations-2.9.0.jar; 赠送原API文档:jackson-annotations-2.9.0-javadoc.jar; 赠送源代码:jackson-annotations-2.9.0-sources.jar; 赠送Maven依赖信息文件:jackson-annotations-2.9.0....
主要介绍了Java8新特性之重复注解(repeating annotations)浅析,这个新特性只是修改了程序的可读性,是比较小的一个改动,需要的朋友可以参考下