Sweetener - Reflections
The main problem of working with reflection is that, you can only refer to the properties of the given class. If the property you are looking for is declared in the base class, you have a problem, and you are forced to move iteratively throughout the class hierarchy.
This problem does not exists in any method of Reflections
class.
Accessing field value
getFieldValue
Person person = new Person("Peter", "Hunt", 41, new Company("Jsolve", new Address("street1", "city1")), prepareListOfCategories("B", "D"));
Object name = Reflections.getFieldValue(person, "name");
// name is Peter
It’s also possible getting nested value:
Person person = new Person("Peter", "Hunt", 41, new Company("Jsolve", new Address("street1", "city1")), prepareListOfCategories("B", "D"));
Object companyName = Reflections.getFieldValue(person, "company.name");
// company name is Jsolve
setFieldValue
private static final String NAME = "John";
...
Person person = new Person();
Reflections.setFieldValue(person, "name", NAME);
// person.getName() == "John"
It’s also possible setting nested value, even if the nested object is null. In this case, null object is replaced by a new object.
private static final String COMPANY_NAME = "Jsolve";
...
Person person = new Person();
Reflections.setFieldValue(person, "company.name", COMPANY_NAME);
// person.getCompany().getName() == "Jsolve"
Of course it is also possible to set the properties of the base class of the given class.
Obtaining class elements
Obtaining all class elements
The class Reflections
also includes getters for fields, annotations, constructors, methods, classes of the given object. These methods retrieve data from the hierarchy of classes, not just from the current class.
public static List<Class<?>> getClasses(Object object);
public static List<Field> getFields(Object object);
public static List<Annotation> getAnnotations(Object object);
public static List<Constructor<?>> getConstructors(Object object);
public static List<Method> getMethods(Object object);
Obtaining annotated class elements
You can also get those elements that are annotated by certain annotation. To do so use one of the following methods:
public static List<Field> getFieldsAnnotatedBy(Object object, final Class<? extends Annotation> annotation);
public static List<Method> getMethodsAnnotatedBy(Object object, final Class<? extends Annotation> annotation);
For example, if you would like to get all deprecated methods of java.util.Date
class you can write something like this:
Date date = new Date();
List<Method> deprecatedMethods = Reflections.getMethodsAnnotatedBy(date, Deprecated.class);
Obtaining certain class elements
You can specify which elements should be returned by passing a condition. The following list consists all available methods to do so:
public static List<Class<?>> getClassesSatisfyingCondition(Object object, Condition<Class<?>> condition);
public static List<Field> getFieldsSatisfyingCondition(Object object, Condition<Field> condition);
public static List<Annotation> getAnnotationsSatisfyingCondition(Object object, Condition<Annotation> condition);
public static List<Constructor<?>> getConstructorsSatisfyingCondition(Object object, Condition<Constructor<?>> condition);
public static List<Method> getMethodsSatisfyingCondition(Object object, Condition<Method> condition);
They might look difficult at first but the usage is really simple. For example if you are interested in list of getter methods of Hero
class just write:
Hero hero = new Hero();
Condition<Method> getterMethodsCondition = new Condition<Method>() {
@Override
public boolean isSatisfied(Method method) {
return method.getName().startsWith("get");
}
};
List<Method> getters = Reflections.getMethodsSatisfyingCondition(hero, getterMethodsCondition);