public final class ClassUtils extends Object
Modifier and Type | Class and Description |
---|---|
static class |
ClassUtils.MethodInfo
Simple bean that shows the name, parameter types, and return type of a method.
|
Constructor and Description |
---|
ClassUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
argsMatch(Class<?>[] paramTypes,
Class<?>[] argTypes)
Returns
|
static void |
assertArgsOfType(Method m,
Class<?>... args)
Throws an
IllegalArgumentException if the parameters on the method are not in the specified list provided. |
static <T> Constructor<T> |
findConstructor(Class<T> c,
Visibility vis,
boolean fuzzyArgs,
Class<?>... argTypes)
Finds a constructor with the specified parameters without throwing an exception.
|
static <T> Constructor<T> |
findNoArgConstructor(Class<T> c,
Visibility v)
Locates the no-arg constructor for the specified class.
|
static <T> Constructor<T> |
findPublicConstructor(Class<T> c,
boolean fuzzyArgs,
Class<?>... argTypes)
Finds a public constructor with the specified parameters without throwing an exception.
|
static <T> Constructor<T> |
findPublicConstructor(Class<T> c,
boolean fuzzyArgs,
Object... args)
Finds the public constructor that can take in the specified arguments.
|
static <T> Constructor<T> |
findPublicConstructor(Class<T> c,
Class<?>... args)
Finds the public constructor that can take in the specified arguments.
|
static <T> Constructor<T> |
findPublicConstructor(Class<T> c,
Object... args)
Finds the public constructor that can take in the specified arguments.
|
static Method |
findPublicFromStringMethod(Class<?> c)
Finds the public static "fromString" method on the specified class.
|
static Method |
findPublicMethod(Class<?> c,
String name,
Class<?> returnType,
Class<?>... argTypes)
Finds a public method with the specified parameters.
|
static <T> T |
fromString(Class<T> c,
String s)
Constructs a new instance of the specified class from the specified string.
|
static int |
fuzzyArgsMatch(Class<?>[] paramTypes,
Class<?>[] argTypes)
Returns a number representing the number of arguments that match the specified parameters.
|
static Iterable<Field> |
getAllFields(Class c,
boolean parentFirst)
Returns all the fields in the specified class and all parent classes.
|
static Iterable<Method> |
getAllMethods(Class c,
boolean parentFirst)
Returns all the methods in the specified class and all parent classes.
|
static Class<?>[] |
getClasses(Object... args)
Returns the class types for the specified arguments.
|
static Class<?> |
getClassFromReadableName(ClassLoader cl,
String name)
Converts the string generated by
getReadableClassName(Class) back into a Class . |
static <T extends Annotation> |
getMethodAnnotation(Class<T> a,
Class<?> c,
Method method)
Returns the specified annotation on the specified method.
|
static <T extends Annotation> |
getMethodAnnotation(Class<T> a,
Method m)
Returns the specified annotation on the specified method.
|
static ClassUtils.MethodInfo[] |
getMethodInfo(Collection<Method> m)
Returns
ClassUtils.MethodInfo beans that describe the specified methods. |
static ClassUtils.MethodInfo |
getMethodInfo(Method m)
Returns a
ClassUtils.MethodInfo bean that describes the specified method. |
static String |
getMethodSignature(Method m)
Returns the signature of the specified method.
|
static Iterator<Class<?>> |
getParentClasses(Class<?> c,
boolean parentFirst,
boolean includeInterfaces)
Returns a list of all the parent classes of the specified class including the class itself.
|
static Object |
getPrimitiveDefault(Class<?> primitiveClass)
Returns the default value for the specified primitive class.
|
static Class<?> |
getPrimitiveForWrapper(Class<?> c)
If the specified class is a primitive wrapper (e.g.
|
static Class<?> |
getPrimitiveWrapper(Class<?> c)
If the specified class is a primitive (e.g.
|
static String |
getReadableClassName(Class<?> c)
Shortcut for calling
|
static String |
getReadableClassName(String className)
Converts the specified class name to a readable form when class name is a special construct like
|
static String |
getReadableClassNameForObject(Object o)
Shortcut for calling
|
static ObjectList |
getReadableClassNames(Object[] o)
Given the specified list of objects, return readable names for the class types of the objects.
|
static Class<?> |
getWrapperIfPrimitive(Class<?> c)
If the specified class is a primitive (e.g.
|
static boolean |
isAbstract(Class<?> c)
Returns
|
static boolean |
isNotDeprecated(Class<?> c)
Returns
@Deprecated annotation on it. |
static boolean |
isNotDeprecated(Constructor<?> c)
Returns
@Deprecated annotation on it. |
static boolean |
isNotDeprecated(Method m)
Returns
@Deprecated annotation on it. |
static boolean |
isParentClass(Class<?> parent,
Class<?> child)
Returns
parent is a parent class or the same as child . |
static boolean |
isParentClass(Class<?> parent,
Class<?> child,
boolean strict)
Returns
parent is a parent class of child . |
static boolean |
isParentClass(Class<?> parent,
Type child)
Returns
parent is a parent class or the same as child . |
static boolean |
isPublic(Class<?> c)
Returns
|
static boolean |
isPublic(Constructor<?> c)
Returns
|
static boolean |
isPublic(Method m)
Returns
|
static boolean |
isStatic(Class<?> c)
Returns
|
static boolean |
isStatic(Method m)
Returns
|
static <T> T |
newInstance(Class<T> c,
Object c2)
Creates an instance of the specified class.
|
static <T> T |
newInstance(Class<T> c,
Object c2,
boolean fuzzyArgs,
Object... args)
Creates an instance of the specified class.
|
static <T> T |
newInstanceFromOuter(Object outer,
Class<T> c,
Object c2,
boolean fuzzyArgs,
Object... args)
Creates an instance of the specified class from within the context of another object.
|
static Class<?> |
resolveParameterType(Class<?> c,
int index,
Class<?> oc)
Finds the real parameter type of the specified class.
|
static Field[] |
sort(Field[] m)
Sorts methods in alphabetical order.
|
static Method[] |
sort(Method[] m)
Sorts methods in alphabetical order.
|
static String |
toString(Field f)
Returns a readable representation of the specified field.
|
static String |
toString(Method m)
Returns a readable representation of the specified method.
|
static String |
toString(Object o)
Converts an object to a string.
|
public ClassUtils()
public static ObjectList getReadableClassNames(Object[] o)
o
- The objects.public static String getReadableClassName(Class<?> c)
getReadableClassName (c.getName())
c
- The class.public static String getReadableClassNameForObject(Object o)
getReadableClassName (c.getClass().getName())
o
- The object whose class we want to render.public static String getReadableClassName(String className)
className
- The class name.public static Class<?> getClassFromReadableName(ClassLoader cl, String name) throws ClassNotFoundException
getReadableClassName(Class)
back into a Class
.
Generics are stripped from the string since they cannot be converted to a class.
cl
- The classloader to use to load the class.name
- The readable class name.ClassNotFoundException
public static boolean isParentClass(Class<?> parent, Class<?> child, boolean strict)
parent
is a parent class of child
.parent
- The parent class.child
- The child class.strict
- If parent
is a parent class of child
.public static boolean isParentClass(Class<?> parent, Class<?> child)
parent
is a parent class or the same as child
.parent
- The parent class.child
- The child class.parent
is a parent class or the same as child
.public static boolean isParentClass(Class<?> parent, Type child)
parent
is a parent class or the same as child
.parent
- The parent class.child
- The child class.parent
is a parent class or the same as child
.public static String getMethodSignature(Method m)
For no-arg methods, the signature will be a simple string such as
m
- The methods to get the signature on.public static Class<?> getPrimitiveWrapper(Class<?> c)
int .class
) returns it's wrapper class
(e.g. Integer.class
).c
- The class.public static Class<?> getPrimitiveForWrapper(Class<?> c)
Integer .class
) returns it's
primitive class (e.g. int.class
).c
- The class.public static Class<?> getWrapperIfPrimitive(Class<?> c)
int .class
) returns it's wrapper class
(e.g. Integer.class
).c
- The class.public static boolean isNotDeprecated(Class<?> c)
@Deprecated
annotation on it.c
- The class.@Deprecated
annotation on it.public static boolean isNotDeprecated(Method m)
@Deprecated
annotation on it.m
- The method.@Deprecated
annotation on it.public static boolean isNotDeprecated(Constructor<?> c)
@Deprecated
annotation on it.c
- The constructor.@Deprecated
annotation on it.public static boolean isPublic(Class<?> c)
c
- The class.public static boolean isStatic(Class<?> c)
c
- The class.public static boolean isAbstract(Class<?> c)
c
- The class.public static boolean isPublic(Method m)
m
- The method.public static boolean isStatic(Method m)
m
- The method.public static boolean isPublic(Constructor<?> c)
c
- The constructor.public static <T extends Annotation> T getMethodAnnotation(Class<T> a, Method m)
Similar to Method.getAnnotation(Class)
, but searches up the parent hierarchy for the annotation
defined on parent classes and interfaces.
Normally, annotations defined on methods of parent classes and interfaces are not inherited by the child methods. This utility method gets around that limitation by searching the class hierarchy for the "same" method (i.e. the same name and arguments).
a
- The annotation to search for.m
- The method to search.public static <T extends Annotation> T getMethodAnnotation(Class<T> a, Class<?> c, Method method)
Similar to Method.getAnnotation(Class)
, but searches up the parent hierarchy for the annotation defined
on parent classes and interfaces.
Normally, annotations defined on methods of parent classes and interfaces are not inherited by the child methods. This utility method gets around that limitation by searching the class hierarchy for the "same" method (i.e. the same name and arguments).
a
- The annotation to search for.c
- The child class to start searching from.
Note that it can be a descendant class of the actual declaring class of the method passed in.
This allows you to find annotations on methods overridden by the method passed in.method
- The method to search.public static final <T> Constructor<T> findNoArgConstructor(Class<T> c, Visibility v)
Constructor must match the visibility requirements specified by parameter 'v'.
If class is abstract, always returns
c
- The class from which to locate the no-arg constructor.v
- The minimum visibility.public static Class<?> resolveParameterType(Class<?> c, int index, Class<?> oc)
c
- The class containing the parameters (e.g. PojoSwap<T,S>)index
- The zero-based index of the parameter to resolve.oc
- The class we're trying to resolve the parameter type for.public static Method findPublicMethod(Class<?> c, String name, Class<?> returnType, Class<?>... argTypes)
c
- The class to look for the method.name
- The method name.returnType
- The return type of the method.
Can be a super type of the actual return type.
For example, if the actual return type is CharSequence
, then Object
will match but
String
will not.argTypes
- The argument types of the method.
Can be subtypes of the actual parameter types.
For example, if the parameter type is CharSequence
, then String
will match but
Object
will not.public static <T> Constructor<T> findPublicConstructor(Class<T> c, boolean fuzzyArgs, Class<?>... argTypes)
c
- The class to search for a constructor.fuzzyArgs
- Use fuzzy-arg matching.
Find a constructor that best matches the specified args.argTypes
- The argument types in the constructor.
Can be subtypes of the actual constructor argument types.public static <T> Constructor<T> findConstructor(Class<T> c, Visibility vis, boolean fuzzyArgs, Class<?>... argTypes)
c
- The class to search for a constructor.vis
- The minimum visibility.fuzzyArgs
- Use fuzzy-arg matching.
Find a constructor that best matches the specified args.argTypes
- The argument types in the constructor.
Can be subtypes of the actual constructor argument types.public static boolean argsMatch(Class<?>[] paramTypes, Class<?>[] argTypes)
paramTypes
- The parameters types specified on a method.argTypes
- The class types of the arguments being passed to the method.public static int fuzzyArgsMatch(Class<?>[] paramTypes, Class<?>[] argTypes)
paramTypes
- The parameters types specified on a method.argTypes
- The class types of the arguments being passed to the method.-1
a parameter was found that isn't in the list of args.public static <T> Constructor<T> findPublicConstructor(Class<T> c, Object... args)
c
- The class we're trying to construct.args
- The arguments we want to pass into the constructor.public static <T> Constructor<T> findPublicConstructor(Class<T> c, Class<?>... args)
c
- The class we're trying to construct.args
- The argument types we want to pass into the constructor.public static <T> Constructor<T> findPublicConstructor(Class<T> c, boolean fuzzyArgs, Object... args)
c
- The class we're trying to construct.fuzzyArgs
- Use fuzzy-arg matching.
Find a constructor that best matches the specified args.args
- The arguments we want to pass into the constructor.public static Class<?>[] getClasses(Object... args)
args
- The objects we're getting the classes of.public static ClassUtils.MethodInfo getMethodInfo(Method m)
ClassUtils.MethodInfo
bean that describes the specified method.m
- The method to describe.public static ClassUtils.MethodInfo[] getMethodInfo(Collection<Method> m)
ClassUtils.MethodInfo
beans that describe the specified methods.m
- The methods to describe.public static <T> T newInstance(Class<T> c, Object c2)
c
- The class to cast to.c2
- The class to instantiate.
Can also be an instance of the class.RuntimeException
- if constructor could not be found or called.public static <T> T newInstance(Class<T> c, Object c2, boolean fuzzyArgs, Object... args)
c
- The class to cast to.c2
- The class to instantiate.
Can also be an instance of the class.fuzzyArgs
- Use fuzzy constructor arg matching.
args
- The arguments to pass to the constructor.RuntimeException
- if constructor could not be found or called.public static <T> T newInstanceFromOuter(Object outer, Class<T> c, Object c2, boolean fuzzyArgs, Object... args)
outer
- The outer object.
Can be c
- The class to cast to.c2
- The class to instantiate.
Can also be an instance of the class.fuzzyArgs
- Use fuzzy constructor arg matching.
args
- The arguments to pass to the constructor.RuntimeException
- if constructor could not be found or called.public static Iterable<Field> getAllFields(Class c, boolean parentFirst)
Fields are ordered in either parent-to-child, or child-to-parent order, then alphabetically.
c
- The class to get all fields on.parentFirst
- Order them in parent-class-to-child-class order, otherwise child-class-to-parent-class order.public static Iterable<Method> getAllMethods(Class c, boolean parentFirst)
Methods are ordered in either parent-to-child, or child-to-parent order, then alphabetically.
c
- The class to get all methods on.parentFirst
- Order them in parent-class-to-child-class order, otherwise child-class-to-parent-class order.public static Method[] sort(Method[] m)
m
- The methods to sort.public static Field[] sort(Field[] m)
m
- The methods to sort.public static Iterator<Class<?>> getParentClasses(Class<?> c, boolean parentFirst, boolean includeInterfaces)
c
- The class to retrieve the parent classes.parentFirst
- In parent-to-child order, otherwise child-to-parent.includeInterfaces
- Include interfaces.public static Object getPrimitiveDefault(Class<?> primitiveClass)
primitiveClass
- The primitive class to get the default value for.public static String toString(Method m)
The format of the string is
m
- The method to stringify.public static String toString(Field f)
The format of the string is
f
- The field to stringify.public static void assertArgsOfType(Method m, Class<?>... args) throws FormattedIllegalArgumentException
IllegalArgumentException
if the parameters on the method are not in the specified list provided.m
- The method to test.args
- The valid class types (exact) for the arguments.FormattedIllegalArgumentException
- If any of the parameters on the method weren't in the list.public static Method findPublicFromStringMethod(Class<?> c)
Looks for the following method names:
fromString
fromValue
valueOf
parse
parseString
forName
forString
c
- The class to find the method on.public static <T> T fromString(Class<T> c, String s)
Class must be one of the following:
String
argument.
fromString(String)
(or related) method.
findPublicFromStringMethod(Class)
for the list of possible static method names.
enum
.
c
- The class.s
- The string to create the instance from.public static String toString(Object o)
Normally, this is just going to call toString()
on the object.
However, the Locale
and TimeZone
objects are treated special so that the returned value
works with the fromString(Class, String)
method.
o
- The object to convert to a string.Copyright © 2018 Apache. All rights reserved.