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
RetentionPolicy
|
Compiled into .class No Reflection |
|
Available for Reflection |
|
Only in Source Dropped for Compilation |
ElementType
|
Class, Interface, Enum |
|
Class fields |
|
Methods no Constructor |
|
Any Param of a Method/Constructor |
|
Only Constructors |
|
only Local Vars |
|
valid for other Annotations |
|
Annotation for Package |
PACKAGE
annotations should only be in package-info.java
|
|
Extract Annotation from Class
getAnnotation(Class<A> aClass)
|
in this class or from parent |
getDeclaredAnnotation(Class<A> aClass)
|
only declared in this class |
|
array of Annotations |
getDeclaredAnnotations()
|
array of Annotations |
Extract Fields and Methods
get[Declared]Field(String name)
|
|
get[Declared]Method(String name, Class<?>... parameterTypes)
|
|
|
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 information
|
|
|
|
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 Functions
|
|
|
|
invoke(Object obj, Object... args)
|
Call Method |
Constructors
Object.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