public class Reflection extends Object
Modifier and Type | Method and Description |
---|---|
static Object |
evalAccessPath(String path,
Object root)
Evaluates the given access path (dot separated getters) and returns the result.
|
static List<Field> |
getAllFields(Class<?> clazz)
Returns all fields defined by this class or one of its superclasses.
|
static Method |
getter(Class<?> clazz,
String property)
Returns the getter method according to the java beans specification for a
given property.
|
static boolean |
isSubclassOf(Class<?> superclass,
Class<?> classInQuestion)
Determines if the given superclass is the same or a superclass or superinterface of the given
classInQuestion.
|
static Method |
setter(Class<?> clazz,
String property,
Class<?> fieldType)
Returns the setter method according to the java beans specification for a
given property.
|
static String |
toFirstUpper(String string)
Converts the first character of a given string to upper case.
|
static void |
walkHierarchy(Class<?> clazz,
Callback<Class<?>> handler)
Calls the given handler for each superclass of the given one.
|
@Nullable public static String toFirstUpper(@Nullable String string)
Comes in handy when translating properties to getter/setter names.
string
- the name of the property to convert@Nonnull public static Method getter(@Nonnull Class<?> clazz, @Nonnull String property)
clazz
- the class in which the method should be searchedproperty
- the name of the property for which the getter is requestedIllegalArgumentException
- if the getter cannot be obtainedpublic static Method setter(Class<?> clazz, String property, Class<?> fieldType)
clazz
- the class in which the method should be searchedproperty
- the name of the property for which the setter is requestedfieldType
- determines the type of the field (which is accepted as parameter of the setter)IllegalArgumentException
- if the setter cannot be obtainedpublic static Object evalAccessPath(String path, Object root)
An access path can look like foo.bar.baz and represents: root.getFoo().getBar().getBaz()
.
If any of the getters returns null, null will also be the result of the evaluation.
path
- the access path to evaluateroot
- the root object on which the first getter is calledpublic static void walkHierarchy(@Nonnull Class<?> clazz, @Nonnull Callback<Class<?>> handler) throws Exception
Calls the handler for the given class and each of its superclasses until Object
is reached.
clazz
- the class to start fromhandler
- the handler to call for each superclassException
- thrown if the handler throws an exception@Nonnull public static List<Field> getAllFields(@Nonnull Class<?> clazz)
clazz
- the class to collect the fields forObject
)public static boolean isSubclassOf(@Nonnull Class<?> superclass, @Nonnull Class<?> classInQuestion)
This is essentially a shortcut for superclass.isAssignableFrom(classInQuestion)
which seems
to be more natural.
superclass
- the designated superclass or superinterfaceclassInQuestion
- the class in questionCopyright © 2018. All rights reserved.