Reflection in Java
January 16, 2022
January 16, 2022

int getModifiers();
boolean isArray();
boolean isEnum();
boolean isInstance(Object obj);
boolean isInterface();
boolean isPrimitive()
If we want to get information about attributes we can use following methods:
Field getField(String name); // gets only public attribute
Field[] getFields(); // gets only public attributes
Field getDeclaredField(String name);
Field[] getDeclaredFields();
To get reference of methods:
Method getMethod(String name, Class<?>... parameterTypes);
And to invoke it:
method.invoke(obj, arg1, arg2,...);
Advantages:
* Debuggers utilize the property of reflection to look at private members on classes
* Can dynamically obtain class instances at runtime, improve flexibility and dynamic compilation
Disadvantages:
* Performances - Reflective operations have slower performance than their non-reflective operations.
* Traceability - It is hard to find where is an error if a reflective method call fails