Java Reflection Cheat Sheet

Java reflection is one of the powerful features of java. Reflection allows to inspect and manipulate meta java at runtime. We can access Java classes, methods, attributes, annotations at runtime and objects can be instantiated, methods invoked.
Reflection is a very powerful feature, it will come handy in many a situations and can do the unexpected. Java reflection requires multiple articles and I will be writing about them in future. This one is a cheat sheet, a quick reference kind of document for reflection.

Get Class Object:
Class zooClass = ZooImpl.class;
Get Class Object Using Class Canonical Name as Argument:
String className = "com.javapapers.corejava.ZooImpl";
Class zooClass = Class.forName(className);
Get Canonical Name From a Class Object:
String classCanoName = zooClass.getCanonicalName();
Get All Constructors of a Class:
Constructor[] constructorArr = zooClass.getConstructors();
Get Constructor with Parameter as String:
Constructor constructor = zooClass.getConstructor(String.class);
Get All Parameters of a Constructor:
Class[] parameterArr = constructor.getParameterTypes();
Instantiate an Object using a Constructor:
ZooImpl zooObject = (ZooImpl)constructor.newInstance(“My Zoo”);
Get Parent Class:
Class parentClass = zooClass.getSuperclass();
Get Package:
Package package = zooClass.getPackage();
Get Modifiers:
int modifiers = zooClass.getModifiers();
Modifier.isAbstract(int modifiers)
Modifier.isFinal(int modifiers)
Modifier.isPrivate(int modifiers)
Get Interfaces Implemented by a Class:
Class[] interfaceArr = zooClass.getInterfaces();
Get Methods: (including inherited)
Method[] methodArr = zooClass.getMethods();
Get Methods: (inherited not included)
Method[] methodArr = zooClass.getDeclaredMethods();
Get a Method:
Method method = zooClass.getMethod(“removeAnimal”, new Class[]{String.class});
Invoke a Method:
Object methodReturnValue = method.invoke(zooObj, “Animal Name”);
Get Fields:
Field[] fieldArr = zooClass.getFields();
Get Public Field:
Field field = zooClass.getField(“SimpleFieldName”);
Get Field Value:
Class zooClass = zooObj.getClass()
Field zooNamefield = zooClass.getField(“zooName”);
Object fieldValue = field.get(zooObj);
Set Field Value:
Object fieldValue = “My Zoo”;
zooNamefield.set(zooObj, fieldValue);
Get Annotations: (including inherited) Should have Retention Policy set as RUNTIME to access annotations at runtime.
Annotation[] annotationArr = zooClass.getAnnotations();
Get Annotations: (inherited not included)
Annotation[] annotationArr = zooClass.getDeclaredAnnotations();
Get Annotation by Type:
Annotation annotation = zooClass.getAnnotation(ZooManager.class);
Check is Annotation:
boolean flag = annotation.isAnnotation();
Check if an Annotation is Present:
boolean flag = zooClass.isAnnotationPresent(ZooManager.class);
Get Public Members:
Class[] publicMemberArr = zooClass.getClasses();
Get All Members:
Class[] memberArr = zooClass.getDeclaredClasses();
GetElements of an Enum Class:
Object[] elementArr = enumClass.getEnumConstants();
Is an Instance of this Class:
boolean flag = zooClass.isInstance(zooObj);
Check if a Class is Synthetic Class:
boolean flag = zooClass.isSynthetic();

0 comments:

Post a Comment