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.

java_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();

This Core Java tutorial was added on 12/04/2013.

Comments on "Java Reflection Cheat Sheet" Tutorial:

  1. Ayan says:

    Superlike!!!!

  2. vikas says:

    thnks……..super blog

  3. Praveen says:

    I would love to see some articles on Class loading and reloading :) Good work!

  4. Farrukh says:

    Very nice article. But i’m not sure this article in your manner explanation :) It’s would be perfect if table has detail columns (for example): method, where most used, etc…

  5. pratik says:

    Nice Topic :) Like it.

  6. java linux says:

    Good paper, thanks for sharing

  7. gopi.juvva@gmail.com says:

    Can I retrieve private properties of a class using reflection mechanism in other class?

  8. Ramnath says:

    Super Like!!
    Thanks alot for sharing the information with us. Its really helpful. Hope you will share more posts like this.

  9. Deepak Gakhar says:

    Great !!!

  10. Sriram says:

    Nice Work … Pls continue with what you are doing… thanks …

  11. Tom says:

    I’m sure there is a mistake:

    Get All Constructors of a Class:
    Constructor[] constructorArr = zooClass.getConstructors();

    The getConstructors method returns ONLY public constructors. To get ALL the constructors you should use the getDeclaredConstructors method. Correct me if I’m wrong.

  12. Sushant Sharma says:

    Hi Joe,

    Can you please add some articles on JSF.
    Not getting cleared with internal working of JSF components.

  13. Bhagwan says:

    Hi Joe,

    How can access the methods/fields of super class using reflection?
    Using getDeclaredFields(), I am getting fields of child class but not the parent.

  14. venkat says:

    can you add some articles for linkedlist,hshmap internally how put() and get() and add() working

  15. sandeep says:

    yaa u are right

Comments are closed for this "Java Reflection Cheat Sheet" tutorial.