public class Reflection extends Object
| Modifier and Type | Field and Description |
|---|---|
private static org.slf4j.Logger |
logger |
private static boolean |
TEST_BOOL |
private static byte |
TEST_BYTE |
private static char |
TEST_CHAR |
private static double |
TEST_DOUBLE |
private static float |
TEST_FLOAT |
private static int |
TEST_INT |
private static long |
TEST_LONG |
private static short |
TEST_SHORT |
| Constructor and Description |
|---|
Reflection() |
| Modifier and Type | Method and Description |
|---|---|
private static String |
concat(Object... objects)
Concat objects as Strings using an underlying StringBuilder.
|
static String |
fieldToString(Field field)
A 'toString' method for logging a field with its source class.
|
static <T,R> Field |
findField(Class<T> clazz,
BiConsumer<T,R> setter)
Find a field on a class whose value is returned by the given getter operation.
|
static <T,R> Field |
findField(Class<T> clazz,
Function<T,R> getter)
Find a field on a class whose value is returned by the given getter operation.
|
static <T> Class<T> |
forName(String name,
ClassLoader... classLoaders)
Find a class for a given class name.
|
static Field |
get(Class type,
String name)
Get a field from a class or superclass using the field name.
|
static Type |
get1ArgParameter(Field field)
Return the type parameter for a 1-arg generic field.
|
static <A extends Annotation> |
getAnnotation(Class target,
Class<A> annotation)
Find the given annotation instance on the given target class or from its class hierarchy.
|
static <T> Class<T> |
getCollectionTarget(Field field)
Find the target class of a field which is a collection.
|
static <T> Constructor<T> |
getConstructor(Class<T> on,
Class<?> withParameter)
Get the constructor for the given class with the given other class as single parameter.
|
static List<Field> |
getFields(Class type)
Get all the non synthetic fields of a class.
|
static List<Field> |
getFields(Class type,
Class<? extends Annotation> withAnnotation)
Get all non synthetic fields of a class, with a given annotation.
|
static <S,T> Class<T> |
getGetterCollectionTarget(Class<S> source,
Function<S,? extends Collection<T>> getter)
Find the target class of a getter when the field is a collection.
|
static <S,T> Class<T> |
getGetterTarget(Class<S> source,
Function<S,T> getter)
Find the target class of a getter.
|
static Method |
getMethod(Class<?> clazz,
String name,
Class<?>... parameterTypes)
Get an existing method on a given class, with the given parameters.
|
static Set<Method> |
getMethods(Class<?> target)
Gets an array of all methods in a class hierarchy walking up recursively to parent classes.
|
static <S,T> Class<T> |
getSetterCollectionTarget(Class<S> source,
BiConsumer<S,? extends Collection<T>> setter)
Find the target class of a setter when the field is a collection.
|
static <S,T> Class<T> |
getSetterTarget(Class<S> source,
BiConsumer<S,T> setter)
Find the target class of a setter.
|
static <T> Class<T> |
getTarget(Field field)
Find the target class of a field be it a collection or not.
|
private static <T> Class<? extends T> |
implementationOf(Class<T> clazz)
Returns the first known implementation of a class.
|
(package private) static <T> boolean |
isConcrete(Class<T> clazz)
Check if a type is concrete (a.k.a can be instantiated)
|
static <T> T |
newInstanceNoArgs(Class<T> clazz)
Instantiate a new object using 0-arg constructor.
|
private static <T> T |
newInstanceUnsafe(Class<T> clazz)
Return a new instance of a given class, for field testing purposes (see
findField(Class, Function). |
static String |
packageName(Class<?> clazz)
Read the package name of a class.
|
private static <T,R> boolean |
primitiveCheck(Field field,
Function<T,R> getter,
T instance,
Object testValue,
R fieldValue)
Check a primitive field value against a setter, twice, using
primitiveTestValue(Class, short)
with a salt of 1. |
private static Object |
primitiveTestValue(Class<?> primitive,
short salt)
Returns a test value (see
TEST_LONG for instance) for the given primitive type. |
static Object |
readField(Field field,
Object onto)
Read the value of a field on a target object.
|
static Object |
readField(String fieldName,
Object onto)
Read the value of a field on a target object.
|
static void |
set(Field field,
Object onto,
Object value)
Set the value value of a field for a given instance.
|
static void |
setFrom(Field field,
Object from,
Object onto)
Set the value value of a field for a given instance, from another instance.
|
private static final org.slf4j.Logger logger
private static final boolean TEST_BOOL
private static final byte TEST_BYTE
private static final char TEST_CHAR
private static final short TEST_SHORT
private static final int TEST_INT
private static final long TEST_LONG
private static final float TEST_FLOAT
private static final double TEST_DOUBLE
public static <T> Class<T> forName(String name, ClassLoader... classLoaders)
T - the target typename - the class nameclassLoaders - the class loaders to use. First match returns. Use Class.forName(String) if no match.ReflectionException - instead of ClassNotFoundExceptionpublic static String packageName(Class<?> clazz)
clazz - the class whose package is to readpublic static Method getMethod(Class<?> clazz, String name, Class<?>... parameterTypes)
Class.getDeclaredMethod(String, Class...).
NoSuchMethodException.clazz - the class of the target methodname - the name of the methodparameterTypes - the method parameter types.public static Set<Method> getMethods(Class<?> target)
target - the classpublic static <A extends Annotation> A getAnnotation(Class target, Class<A> annotation)
A - the annotation typetarget - the target classannotation - the annotation classpublic static List<Field> getFields(Class type)
type - the target classpublic static List<Field> getFields(Class type, Class<? extends Annotation> withAnnotation)
type - the classwithAnnotation - the annotation the field must declare to be eligiblepublic static String fieldToString(Field field)
field - the field to use. Can be null.public static Object readField(Field field, Object onto)
IllegalAccessException !field - the field to readonto - the target object where to read the fieldReflectionException - if the field could not be read for any reason.public static Object readField(String fieldName, Object onto)
IllegalAccessException !fieldName - the name of the field to readonto - the target object where to read the fieldReflectionException - if the field could not be read or does not exist.public static Field get(Class type, String name)
type - the classname - the field namepublic static void set(Field field, Object onto, Object value)
RuntimeException exceptions.field - the field to setonto - the target instancevalue - the field value to setReflectionException - exception with context, if any exception (Illegal Access or Runtime) occurs.public static void setFrom(Field field, Object from, Object onto)
RuntimeException exceptions.field - the field to setfrom - the source instanceonto - the target instanceReflectionException - exception with context, if any exception (Illegal Access or Runtime) occurs.public static Type get1ArgParameter(Field field)
field - the generic field.public static <T> T newInstanceNoArgs(Class<T> clazz)
T - the object typeclazz - the object class to instantiatepublic static <T,R> Field findField(Class<T> clazz, Function<T,R> getter)
T - the class typeR - the field typeclazz - the class holding the field and gettergetter - the getter operationReflectionException - if no field matches the getterprivate static <T,R> boolean primitiveCheck(Field field, Function<T,R> getter, T instance, Object testValue, R fieldValue)
primitiveTestValue(Class, short)
with a salt of 1.T - the type holding the setterR - the target typefield - the field to checkgetter - the getter to useinstance - the instance holding the fieldtestValue - the test value, from the setterfieldValue - the actual field valueReflectionException - could not read the fieldpublic static <T,R> Field findField(Class<T> clazz, BiConsumer<T,R> setter)
T - the class typeR - the field typeclazz - the class holding the field and gettersetter - the setter operationReflectionException - if no field matches the getterpublic static <T> Class<T> getCollectionTarget(Field field)
T - target typefield - the (collection) fieldpublic static <T> Class<T> getTarget(Field field)
T - target typefield - the fieldpublic static <S,T> Class<T> getGetterTarget(Class<S> source, Function<S,T> getter)
S - source typeT - target typesource - the source class, holding the fieldgetter - the getter (lambda function)public static <S,T> Class<T> getSetterTarget(Class<S> source, BiConsumer<S,T> setter)
S - source typeT - target typesource - the source class, holding the fieldsetter - the setter (lambda bi consumer)public static <S,T> Class<T> getGetterCollectionTarget(Class<S> source, Function<S,? extends Collection<T>> getter)
Set<Pojo> getChildren()
this function will return the Pojo class for the lambda ::getChildrenS - source typeT - target typesource - the source class, holding the fieldgetter - the getter (lambda function)public static <S,T> Class<T> getSetterCollectionTarget(Class<S> source, BiConsumer<S,? extends Collection<T>> setter)
void setChildren(Set<Pojo>)
this function will return the Pojo class for the lambda ::setChildrenS - source typeT - target typesource - the source class, holding the fieldsetter - the setter (lambda function)private static <T> T newInstanceUnsafe(Class<T> clazz)
findField(Class, Function).
TEST_LONGT - the type to instantiateclazz - the class of the type to instantiateReflectionException - fake primitive or WrapperReflectionException - Unsafe was not able to instantiate the type T.private static Object primitiveTestValue(Class<?> primitive, short salt)
TEST_LONG for instance) for the given primitive type.primitive - the primitive type or Wrapper.salt - the amount to add to the reference test value (e.g. second test)static <T> boolean isConcrete(Class<T> clazz)
T - the type to checkclazz - the class object of the type to checkprivate static <T> Class<? extends T> implementationOf(Class<T> clazz)
isConcrete(Class).T - the class generic typeclazz - the class whose implementation is soughtpublic static <T> Constructor<T> getConstructor(Class<T> on, Class<?> withParameter)
NoSuchMethodException. It returns null if no match.
T - the constructor target typeon - which class whose constructor we searchwithParameter - the constructor single parameter classCopyright © 2019. All rights reserved.