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"; |
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(); |
Comments are closed for "Java Reflection Cheat Sheet".
Superlike!!!!
thnks……..super blog
I would love to see some articles on Class loading and reloading :) Good work!
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…
Nice Topic :) Like it.
Good paper, thanks for sharing
Can I retrieve private properties of a class using reflection mechanism in other class?
Super Like!!
Thanks alot for sharing the information with us. Its really helpful. Hope you will share more posts like this.
Great !!!
Nice Work … Pls continue with what you are doing… thanks …
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.
Hi Joe,
Can you please add some articles on JSF.
Not getting cleared with internal working of JSF components.
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.
can you add some articles for linkedlist,hshmap internally how put() and get() and add() working
yaa u are right