Package com.restfb.util
Class ReflectionUtils
- java.lang.Object
-
- com.restfb.util.ReflectionUtils
-
public final class ReflectionUtils extends Object
A collection of reflection-related utility methods.- Since:
- 1.6
- Author:
- Mark Allen, Igor Kabiljo, Scott Hernandez
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ReflectionUtils.FieldWithAnnotation<T extends Annotation>
A field/annotation pair.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> T
createInstance(Class<T> type)
Creates a new instance of the giventype
.static boolean
equals(Object object1, Object object2)
Reflection-based implementation ofObject.equals(Object)
.static <T extends Annotation>
List<ReflectionUtils.FieldWithAnnotation<T>>findFieldsWithAnnotation(Class<?> type, Class<T> annotationType)
Finds fields on the giventype
and 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 giventype
and all of its superclasses annotated with annotations of typeannotationType
.static List<Method>
getAccessors(Class<?> clazz)
Gets all accessor methods for the givenclazz
.static Class<?>
getFirstParameterizedTypeArgument(Field field)
For a givenfield
, get its first parameterized type argument.static Class<?>
getSecondParameterizedTypeArgument(Field field)
For a givenfield
, get its second parameterized type argument.static int
hashCode(Object object)
Reflection-based implementation ofObject.hashCode()
.static boolean
isPrimitive(Object object)
Is the givenobject
a primitive type or wrapper for a primitive type?static String
toString(Object object)
Reflection-based implementation ofObject.toString()
.
-
-
-
Method Detail
-
isPrimitive
public static boolean isPrimitive(Object object)
Is the givenobject
a primitive type or wrapper for a primitive type?- Parameters:
object
- The object to check for primitive-ness.- Returns:
true
ifobject
is a primitive type or wrapper for a primitive type,false
otherwise.
-
findFieldsWithAnnotation
public static <T extends Annotation> List<ReflectionUtils.FieldWithAnnotation<T>> findFieldsWithAnnotation(Class<?> type, Class<T> annotationType)
Finds fields on the giventype
and 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 giventype
and 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
public static Class<?> getFirstParameterizedTypeArgument(Field field)
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,
null
is returned.- Parameters:
field
- The field to check.- Returns:
- The field's first parameterized type argument, or
null
if none exists.
-
getSecondParameterizedTypeArgument
public static Class<?> getSecondParameterizedTypeArgument(Field field)
For a givenfield
, get its second parameterized type argument. If the field has no type arguments,null
is returned.- Parameters:
field
- The field to check.- Returns:
- The field's second parameterized type argument, or
null
if none exists.
-
getAccessors
public static List<Method> getAccessors(Class<?> clazz)
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
public static String toString(Object object)
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
public static int hashCode(Object object)
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
public static boolean equals(Object object1, Object object2)
Reflection-based implementation ofObject.equals(Object)
.- Parameters:
object1
- One object to compare.object2
- Another object to compare.- Returns:
true
if the objects are equal,false
otherwise.- Throws:
IllegalStateException
- If an error occurs while performing reflection operations.
-
createInstance
public static <T> T createInstance(Class<T> type)
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 (type
is inaccessible, doesn't have a no-arg constructor, etc.)
-
-