Java Reflection: Inspecting Class Components

Retrieving Constructors

The Java Reflection API provides specific mechanisms to access constructor metadata. getConstructor(Class... params) retrieves a single public constructor that matches the specified parameter types. getDeclaredConstructor(Class... params) returns a single constructor declared within the class, regradless of its visibility modifier (e.g., private). While getConstructors() returns an array of all public constructors (including those inherited from superclasses), getDeclaredConstructors() returns all constructors explicitly defined in the class, encompassing private and protected members, but excludes inherited ones.

Class> entityClass = Class.forName("com.example.data.Entity");
// Acquire a specific constructor matching specific argument types
Constructor> initConstructor = entityClass.getDeclaredConstructor(String.class, int.class);
// Disable access control checks to allow instantiation of non-public members
initConstructor.setAccessible(true);
// Create a new instance using the retrieved constructor
Object entityInstance = initConstructor.newInstance("identifier", 100);

Retrieving Methods

To inspect behaviors, reflection offers distinct methods for fetching method objects. getMethod(String name, Class... params) is used to retrieve a specific public method. getDeclaredMethod(String name, Class... params) accesses a method declared within the class regardless of its access modifier. For retrieving multiple methods, getMethods() fetches all public methods (including those inherited from superclasses), while getDeclaredMethods() returns all methods explicitly declared in the current class scope.

String content = "Reflection Demo";
// Retrieve the 'substring' method accepting two integer arguments
Method stringManipulator = content.getClass().getMethod("substring", int.class, int.class);

// Invoke the method dynamically; the first argument is the target instance
String result = (String) stringManipulator.invoke(content, 0, 10);
System.out.println(result);

Retrieving Fields

Accessing class state involves using Field objects. getField(String name) is restricted to retrieving public attributes. getDeclaredField(String name) bypasses visibility constraints for attributes defined in the class. To retrieve arrays of fields, getFields() returns all public fields (including those from superclasses), while getDeclaredFields() returns every field declared in the specific class, covering all access modifiers but excluding inherited fields.

DataObject dataObj = new DataObject();
Field[] attributes = dataObj.getClass().getDeclaredFields();

for (Field attribute : attributes) {
    attribute.setAccessible(true);
    
    // Extract the current value from the instance
    Object currentValue = attribute.get(dataObj);
    System.out.println("Name: " + attribute.getName() + ", Type: " + attribute.getType());
    
    // Modify the value if the field type is a String
    if (attribute.getType().equals(String.class)) {
        attribute.set(dataObj, "modified_value");
    }
}

Retrieving Annotations

Reflection is capable of inspecting runtime annotations attached to program elements. getAnnotation(Class annotationType) fetches a specific annotation object if it exists. isAnnotationPresent(Class annotationType) provides a boolean check to determine if an annotation is attached to the element with out necessarily retrieving the instance.

Field targetField = dataObj.getClass().getDeclaredField("config");
if (targetField.isAnnotationPresent(Deprecated.class)) {
    // Logic to handle the presence of the annotation
    System.out.println("Annotation detected on field");
}

Tags: java reflection API introspection metadata

Posted on Wed, 07 Oct 2026 16:15:59 +0000 by harsha