Package com.restfb.util
Class ReflectionUtils
java.lang.Object
com.restfb.util.ReflectionUtils
A collection of reflection-related utility methods.
- Since:
- 1.6
- Author:
- Mark Allen, Igor Kabiljo, Scott Hernandez
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA field/annotation pair. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TcreateInstance(Class<T> type) Creates a new instance of the giventype.static booleanReflection-based implementation ofObject.equals(Object).static <T extends Annotation>
List<ReflectionUtils.FieldWithAnnotation<T>>findFieldsWithAnnotation(Class<?> type, Class<T> annotationType) Finds fields on the giventypeand all of its superclasses annotated with annotations of typeannotationType.static <T extends Annotation>
List<Method>findMethodsWithAnnotation(Class<?> type, Class<T> annotationType) Finds methods on the giventypeand all of its superclasses annotated with annotations of typeannotationType.getAccessors(Class<?> clazz) Gets all accessor methods for the givenclazz.static Class<?>For a givenfield, get its first parameterized type argument.static TypegetParameterizedTypeArgument(Type type, int i) Retrieves thei-th parameterized type argument from the giventype.static Class<?>For a givenfield, get its second parameterized type argument.static intReflection-based implementation ofObject.hashCode().static booleanisPrimitive(Object object) Is the givenobjecta primitive type or wrapper for a primitive type?static voidstatic StringReflection-based implementation ofObject.toString().
-
Method Details
-
setJson
-
isPrimitive
Is the givenobjecta primitive type or wrapper for a primitive type?- Parameters:
object- The object to check for primitive-ness.- Returns:
trueifobjectis a primitive type or wrapper for a primitive type,falseotherwise.
-
findFieldsWithAnnotation
public static <T extends Annotation> List<ReflectionUtils.FieldWithAnnotation<T>> findFieldsWithAnnotation(Class<?> type, Class<T> annotationType) Finds fields on the giventypeand all of its superclasses annotated with annotations of typeannotationType.- Type Parameters:
T- The annotation type.- Parameters:
type- The target type token.annotationType- The annotation type token.- Returns:
- A list of field/annotation pairs.
-
findMethodsWithAnnotation
public static <T extends Annotation> List<Method> findMethodsWithAnnotation(Class<?> type, Class<T> annotationType) Finds methods on the giventypeand all of its superclasses annotated with annotations of typeannotationType.These results are cached to mitigate performance overhead.
- Type Parameters:
T- The annotation type.- Parameters:
type- The target type token.annotationType- The annotation type token.- Returns:
- A list of methods with the given annotation.
- Since:
- 1.6.11
-
getFirstParameterizedTypeArgument
For a givenfield, get its first parameterized type argument.For example, a field of type
List<Long>would have a first type argument ofLong.class.If the field has no type arguments,
nullis returned.- Parameters:
field- The field to check.- Returns:
- The field's first parameterized type argument, or
nullif none exists.
-
getSecondParameterizedTypeArgument
For a givenfield, get its second parameterized type argument.If the field has no type arguments,
nullis returned.- Parameters:
field- The field to check.- Returns:
- The field's second parameterized type argument, or
nullif none exists.
-
getParameterizedTypeArgument
Retrieves thei-th parameterized type argument from the giventype.If the type is not parameterized or the index is out of bounds,
nullis returned.- Parameters:
type- The generic type to inspect.i- The index of the parameterized type argument to retrieve (zero-based).- Returns:
- The
i-th parameterized type argument of the given type, ornullif none exists.
-
getAccessors
Gets all accessor methods for the givenclazz.- Parameters:
clazz- The class for which accessors are extracted.- Returns:
- All accessor methods for the given
clazz.
-
toString
Reflection-based implementation ofObject.toString().- Parameters:
object- The object to convert to a string representation.- Returns:
- A string representation of
object. - Throws:
IllegalStateException- If an error occurs while performing reflection operations.
-
hashCode
Reflection-based implementation ofObject.hashCode().- Parameters:
object- The object to hash.- Returns:
- A hashcode for
object. - Throws:
IllegalStateException- If an error occurs while performing reflection operations.
-
equals
Reflection-based implementation ofObject.equals(Object).- Parameters:
object1- One object to compare.object2- Another object to compare.- Returns:
trueif the objects are equal,falseotherwise.- Throws:
IllegalStateException- If an error occurs while performing reflection operations.
-
createInstance
Creates a new instance of the giventype.- Type Parameters:
T- Java type to map to.- Parameters:
type- Type token.- Returns:
- A new instance of
type. - Throws:
FacebookJsonMappingException- If an error occurs when creating a new instance (typeis inaccessible, doesn't have a no-arg constructor, etc.)
-