Cheatography
https://cheatography.com
Stuff related to Java Annotations and Annotation Processing
Define an Annotation@Retention(RUNTIME) //RetentionPolicy
@Target(TYPE) //ElementType
public @interface MyAnnot {
boolean value() default false;
String name();
}
|
Annotation for Classes, Eums and Interfaces available for Reflection
RetentionPolicyCLASS
| Compiled into .class No Reflection | RUNTIME
| Available for Reflection | SOURCE
| Only in Source Dropped for Compilation |
ElementTypeTYPE
| Class, Interface, Enum | FIELD
| Class fields | METHOD
| Methods no Constructor | PARAMETER
| Any Param of a Method/Constructor | CONSTRUCTOR
| Only Constructors | LOCAL_VARIABLE
| only Local Vars | ANNOTATION_TYPE
| valid for other Annotations | PACKAGE
| Annotation for Package |
PACKAGE annotations should only be in package-info.java
| | Extract Annotation from ClassgetAnnotation(Class<A> aClass)
| in this class or from parent | getDeclaredAnnotation(Class<A> aClass)
| only declared in this class | getAnnotations()
| array of Annotations | getDeclaredAnnotations()
| array of Annotations |
Extract Fields and Methodsget[Declared]Field(String name)
| extracts a Field | get[Declared]Method(String name, Class<?>... parameterTypes)
| extracts a Method | getConstructors()
| extracts an Array of Constructor | get[Declared]Fields()
| extracts an Array of Field | get[Declared]Methods()
| extracts an Array of Method |
getDeclared... always points to exactly this Object only including private , without Declared any super can also match but only public ones.
Extract Field informationgetName()
| getType()
| getModifiers()
| get(Object obj)
| extract field value from obj | set(Object obj, Object val)
| set field to value |
Primitives are available e.g.: getInt(obj) , setInt(obj, val)
Method FunctionsgetName()
| getParameterTypes()
| Class Array of Types
| getReturnType()
| invoke(Object obj, Object... args)
| Call Method |
ConstructorsObject.class.newInstance()
| utilizes Default Constructor of an Class object | const.newInstance(Object.... args)
| utilizes one of the extracted Constructors |
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by cs8898