Skip to main content

Reflection Package

The org.apache.juneau.commons.reflect package provides comprehensive reflection utilities for working with classes, methods, fields, constructors, and annotations in a more convenient and type-safe way than the standard Java reflection APIs.

Key Classes

ReflectionUtils

Utility class providing convenient static methods for creating reflection info objects.

import static org.apache.juneau.commons.reflect.ReflectionUtils.*;

// Convert Class to ClassInfo
ClassInfo ci = info(MyClass.class);

// Convert Method to MethodInfo
Method m = MyClass.class.getMethod("myMethod");
MethodInfo mi = info(m);

// Convert Field to FieldInfo
Field f = MyClass.class.getField("myField");
FieldInfo fi = info(f);

// Convert Constructor to ConstructorInfo
Constructor<?> c = MyClass.class.getConstructor();
ConstructorInfo ci2 = info(c);

ClassInfo

Lightweight utility class for introspecting information about a class.

// Wrap a class in ClassInfo
ClassInfo classInfo = ClassInfo.of(MyClass.class);

// Get all methods in parent-to-child order, sorted alphabetically per class
for (MethodInfo methodInfo : classInfo.getAllMethods()) {
// Do something with it
}

// Get all class-level annotations in parent-to-child order
for (MyAnnotation annotation : classInfo.getAnnotations(MyAnnotation.class)) {
// Do something with it
}

// Get all fields
for (FieldInfo fieldInfo : classInfo.getAllFields()) {
// Do something with it
}

// Get all constructors
for (ConstructorInfo constructorInfo : classInfo.getConstructors()) {
// Do something with it
}

MethodInfo

Wrapper around java.lang.reflect.Method with additional convenience methods.

ClassInfo classInfo = ClassInfo.of(MyClass.class);

// Get a specific method
MethodInfo methodInfo = classInfo.getMethod("myMethod", String.class, int.class);

// Get method name
String name = methodInfo.getName();

// Get return type
Class<?> returnType = methodInfo.getReturnType();

// Get parameters
List<ParameterInfo> parameters = methodInfo.getParameters();

// Get annotations
List<MyAnnotation> annotations = methodInfo.getAnnotations(MyAnnotation.class);

// Invoke method
Object result = methodInfo.invoke(instance, "arg1", 42);

FieldInfo

Wrapper around java.lang.reflect.Field with additional convenience methods.

ClassInfo classInfo = ClassInfo.of(MyClass.class);

// Get a specific field
FieldInfo fieldInfo = classInfo.getField("myField");

// Get field name
String name = fieldInfo.getName();

// Get field type
Class<?> type = fieldInfo.getType();

// Get annotations
List<MyAnnotation> annotations = fieldInfo.getAnnotations(MyAnnotation.class);

// Get value
Object value = fieldInfo.get(instance);

// Set value
fieldInfo.set(instance, newValue);

ConstructorInfo

Wrapper around java.lang.reflect.Constructor with additional convenience methods.

ClassInfo classInfo = ClassInfo.of(MyClass.class);

// Get a specific constructor
ConstructorInfo constructorInfo = classInfo.getConstructor(String.class, int.class);

// Get parameters
List<ParameterInfo> parameters = constructorInfo.getParameters();

// Get annotations
List<MyAnnotation> annotations = constructorInfo.getAnnotations(MyAnnotation.class);

// Create instance
MyClass instance = constructorInfo.newInstance("arg1", 42);

ParameterInfo

Wrapper around java.lang.reflect.Parameter with additional convenience methods.

MethodInfo methodInfo = classInfo.getMethod("myMethod", String.class);

// Get parameters
List<ParameterInfo> parameters = methodInfo.getParameters();

for (ParameterInfo param : parameters) {
String name = param.getName();
Class<?> type = param.getType();
List<MyAnnotation> annotations = param.getAnnotations(MyAnnotation.class);
}

PackageInfo

Wrapper around java.lang.Package with additional convenience methods.

// Get package info
PackageInfo packageInfo = PackageInfo.of(MyClass.class.getPackage());

// Get package name
String name = packageInfo.getName();

// Get annotations
List<MyAnnotation> annotations = packageInfo.getAnnotations(MyAnnotation.class);

Annotatable

Common interface for all annotatable wrapper classes.

// All info classes implement Annotatable
Annotatable annotatable = ClassInfo.of(MyClass.class);
annotatable = MethodInfo.of(method);
annotatable = FieldInfo.of(field);
annotatable = ConstructorInfo.of(constructor);
annotatable = ParameterInfo.of(parameter);
annotatable = PackageInfo.of(package);

// Get annotations from any annotatable
List<MyAnnotation> annotations = annotatable.getAnnotations(MyAnnotation.class);

Common Patterns

Introspecting a Class

ClassInfo classInfo = ClassInfo.of(MyClass.class);

// Get all public methods
for (MethodInfo method : classInfo.getPublicMethods()) {
System.out.println(method.getName());
}

// Get all declared methods (including private)
for (MethodInfo method : classInfo.getDeclaredMethods()) {
System.out.println(method.getName());
}

// Get all methods including inherited
for (MethodInfo method : classInfo.getAllMethods()) {
System.out.println(method.getName());
}

Working with Annotations

ClassInfo classInfo = ClassInfo.of(MyClass.class);

// Get all annotations of a specific type
List<MyAnnotation> annotations = classInfo.getAnnotations(MyAnnotation.class);

// Check if annotation is present
boolean hasAnnotation = classInfo.hasAnnotation(MyAnnotation.class);

// Get single annotation (first one found)
MyAnnotation annotation = classInfo.getAnnotation(MyAnnotation.class);

Type Information

ClassInfo classInfo = ClassInfo.of(MyClass.class);

// Get superclass
ClassInfo superclass = classInfo.getSuperclass();

// Get all superclasses
List<ClassInfo> superclasses = classInfo.getSuperclasses();

// Get interfaces
List<ClassInfo> interfaces = classInfo.getInterfaces();

// Check if class is assignable from another
boolean assignable = classInfo.isAssignableFrom(OtherClass.class);

See Also

Discussion

Share feedback or follow-up questions for this page directly through GitHub.