Java gets annotations on a class or method
Acquisition method
Class
- Class.getAnnotations() gets all annotations, both self declared and inherited
- Class. Getannotation (class < a > annotationclass) gets the specified annotation, which can be self declared or inherited
- Class. Getdeclaraedannotations() gets the annotations declared by itself
Method
- Method.getAnnotations() gets all annotations, both self declared and inherited
- Method. Getannotation (class < a > annotationclass) gets the specified annotation, which can be self declared or inherited
- Method. Getdeclaraedannotations() gets the annotations declared by itself
Examples
//Custom annotation A @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface A { String value() default ""; } //Custom annotation B @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) public @interface B { String value() default ""; } //Using annotations @A("testClass") public class TestService { @B("testMethod") public void sayHello() { System.out.println("hello word"); } } //obtain public static void main(String[] args) throws NoSuchMethodException { Class<?> clazz = TestService.class; System.out.println(Arrays.toString(clazz.getAnnotations())); System.out.println(clazz.getAnnotation(A.class)); System.out.println(Arrays.toString(clazz.getDeclaredAnnotations())); System.out.println(); Method method = clazz.getMethod("sayHello", null); System.out.println(Arrays.toString(method.getAnnotations())); System.out.println(method.getAnnotation(B.class)); System.out.println(Arrays.toString(method.getDeclaredAnnotations())); }
be careful
- Annotations can only be Inherited by subclasses if they are defined as @ Inherited
- getAnnotations() and getdeclaraedannotations () return an empty array when the current class or method is not annotated with any annotations
- If the annotation queried by the method getannotation (class < a > annotationtype) does not exist, null will be returned
- The subclass overrides the method of the parent class, and the annotation cannot be inherited
Unable to get annotation information
Custom annotation
Check annotation definition:
When @ Retention(value = RetentionPolicy.RUNTIME) is not defined for annotation, annotation information cannot be obtained.
Annotation types can be annotated by themselves. Java5.0 defines four standard meta annotation types: Target, Retention, Documented and Inherited, which are used to describe other annotation types. These types and the classes they support are in Java You can find it in the lang.annotation package.
- @Usage of Target: indicates the type of program element to which the annotation type applies. If the Target meta annotation does not exist in the annotation type declaration, the declared type can be used on any program element. If such meta annotations exist, the compiler enforces the specified usage restrictions.
- ElementType.ANNOTATION_TYPE applies to annotation type declarations
- ElementType.CONSTRUCTOR construct method declaration
- ElementType.FIELD applies to field declarations (including enumeration constants)
- ElementType.LOCAL_VARIABLE applies to local variable declarations
- ElementType.METHOD applies to method declarations
- ElementType.PACKAGE applies to package declarations
- ElementType.PARAMETER applies to parameter declarations
- ElementType.TYPE applies to class, interface (including annotation types), or enumeration declarations
- @Usage of Retention: indicates how long annotation of annotation type should be retained. If there is no Retention annotation in the annotation type declaration, the Retention policy defaults to retentionpolicy CLASS
- RetentionPolicy. The class compiler records comments in the class file, but the JVM does not need to keep comments at run time
- RetentionPolicy. The runtime compiler will record the comments in the class file, and the JVM will keep the comments at run time, so they can be read reflexively
- RetentionPolicy.SOURCE comments to be discarded by the compiler
- @Usage of documented: indicates that a certain type of annotation will be documented through javadoc and similar default tools. Declarations of these types should be annotated with this type: their annotation affects the use of elements annotated by their clients. If a type declaration is annotated with documented, its annotation becomes part of the public API of the annotation element. Documented is a comment that has no members.
- @Inherited usage: indicates that the annotation type is automatically inherited by subclasses. Inherited is also a comment without members
Note that anything other than the @ Inherited annotation class is invalid. Also note that this meta annotation is only valid for annotations Inherited from superclasses; Invalid comment on implemented interface.
Class proxied
The annotation information cannot be obtained when the current class is proxied. How to obtain the annotation information?
Under normal circumstances, our class is com spc.job. canal. Canalhandler is like this, but if the class represented by AOP is class com spc.job. canal. CanalHandler$$EnhancerBySpringCGLIB$$5770166e
Solution 1
Retrieve a Class object that is not represented:
Class<?> clazz = bean.getClass(); String name = clazz.getName(); if (name.contains("$$EnhancerBySpringCGLIB") || name.contains("$$5770166e")) { name = name.substring(0, name.indexOf("$$")); } try { clazz = Class.forName(name); } catch (ClassNotFoundException e) { throw new RuntimeException(e.getMessage()); } System.out.println(Arrays.toString(clazz.getAnnotations())); for (Method method : clazz.getMethods()) { System.out.println(Arrays.toString(method.getAnnotations())); }
Solution 2
Use the annotation utils provided in Spring to read annotations
//A is the annotation information A a = AnnotationUtils.findAnnotation(method, A.class);