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 <T extends Annotation> |
appendAnnotations(Class<T> a,
Type t,
List<T> l)
Finds and appends the specified annotation on the specified class and superclasses/interfaces to the specified
list.
|
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 List<Method> |
findMatchingMethods(Method m)
Given a specific method, finds all declared methods with the same name and arguments on all
superclasses and interfaces.
|
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 Method |
findPublicStaticCreateMethod(Class<?> oc,
Class<?> ic,
String name)
Find the public static creator method on the specified class.
|
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 <T extends Annotation> |
getAnnotation(Class<T> a,
Method m)
Shortcut for calling
getAnnotation(a, m, true, true); |
static <T extends Annotation> |
getAnnotation(Class<T> a,
Method m,
boolean searchParentMethods,
boolean searchReturnType)
Finds the annotation of the specified type defined on the specified method.
|
static <T extends Annotation> |
getAnnotation(Class<T> a,
Method m,
int index)
Shortcut for calling
getAnnotation(a, m, index, |
static <T extends Annotation> |
getAnnotation(Class<T> a,
Method m,
int index,
boolean searchParentMethods,
boolean searchArgType)
Finds the annotation of the specified type defined on the specified method argument.
|
static <T extends Annotation> |
getAnnotation(Class<T> a,
Type t)
Similar to
Class.getAnnotation(Class) except also searches annotations on interfaces. |
static <T extends Annotation> |
getAnnotations(Class<T> a,
Method m)
Shortcut for calling
getAnnotations(a, m, |
static <T extends Annotation> |
getAnnotations(Class<T> a,
Method m,
boolean searchParentMethods,
boolean searchReturnType,
boolean parentFirst)
Returns all annotations of the specified type defined on the specified method.
|
static <T extends Annotation> |
getAnnotations(Class<T> a,
Method m,
int index)
Shortcut for calling
getAnnotations(a, m, index, |
static <T extends Annotation> |
getAnnotations(Class<T> a,
Method m,
int index,
boolean searchParentMethods,
boolean searchArgType,
boolean parentFirst)
Returns all annotations of the specified type defined on the specified method argument.
|
static <T extends Annotation> |
getAnnotations(Class<T> a,
Type t)
Returns all instances of the specified annotation on the specified class.
|
static <T extends Annotation> |
getAnnotationsMap(Class<T> a,
Type t)
Same as
getAnnotations(Class, Type) except returns the annotations as a map with the keys being the
class on which the annotation was found. |
static <T extends Annotation> |
getAnnotationsMapParentFirst(Class<T> a,
Type t)
Same as
getAnnotationsMap(Class, Type) except returns results in parent-to-child order. |
static <T extends Annotation> |
getAnnotationsParentFirst(Class<T> a,
Method m)
Shortcut for calling
getAnnotations(a, m, |
static <T extends Annotation> |
getAnnotationsParentFirst(Class<T> a,
Method m,
int index)
Shortcut for calling
getAnnotations(a, m, index, |
static <T extends Annotation> |
getAnnotationsParentFirst(Class<T> a,
Type t)
Same as
getAnnotations(Class, Type) but returns the list in parent-to-child order. |
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> |
getDeclaredAnnotation(Class<T> a,
Type t)
Returns the specified annotation only if it's been declared on the specified class.
|
static Object[] |
getMatchingArgs(Class<?>[] paramTypes,
Object... args)
Matches arguments to a list of parameter types.
|
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 InputStream |
getResource(Class<?> c,
String name)
Similar to
Class.getResourceAsStream(String) except looks up the parent hierarchy for the existence of
the specified resource. |
static String |
getSimpleName(Class<?> c)
Returns the simple name of a class.
|
static String |
getSimpleName(Type t)
Returns the simple name of a class.
|
static Class<?> |
getWrapperIfPrimitive(Class<?> c)
If the specified class is a primitive (e.g.
|
static boolean |
hasAnnotation(Class<? extends Annotation> a,
Method m)
Returns
getAnnotation(Class, Method) returns a value. |
static boolean |
hasAnnotation(Class<? extends Annotation> a,
Method m,
int index)
Returns
getAnnotation(Class, Method, int) returns a value. |
static boolean |
hasAnnotation(Class<? extends Annotation> a,
Type t)
Returns
getAnnotation(Class, Type) returns a value. |
static boolean |
hasArgs(Constructor<?> x)
Returns
|
static boolean |
hasArgs(Constructor<?> x,
Class<?>... args)
Returns
|
static boolean |
hasArgs(Method x)
Returns
|
static boolean |
hasArgs(Method x,
Class<?>... args)
Returns
|
static boolean |
hasFuzzyArgs(Constructor<?> x,
Class<?>... args)
Returns
|
static boolean |
hasFuzzyArgs(Method x,
Class<?>... args)
Returns
|
static boolean |
hasName(Method m,
String name)
Returns
|
static boolean |
hasNoArgs(Constructor<?> x)
Returns
|
static boolean |
hasNoArgs(Method x)
Returns
|
static boolean |
hasNumArgs(Constructor<?> x,
int number)
Returns
|
static boolean |
hasNumArgs(Method x,
int number)
Returns
|
static boolean |
hasPrimitiveWrapper(Class<?> c)
Returns
getPrimitiveWrapper(Class) class returns a value for the specified class. |
static boolean |
hasReturnType(Method m,
Class<?> c)
Returns
|
static boolean |
hasReturnTypeParent(Method m,
Class<?> c)
Returns
|
static <T> T |
invokeConstructorFuzzy(Constructor<T> c,
Object... args)
Invokes the specified constructor using fuzzy-arg matching.
|
static Object |
invokeMethodFuzzy(Method m,
Object pojo,
Object... args)
Invokes the specified method using fuzzy-arg matching.
|
static boolean |
isAbstract(Class<?> c)
Returns
|
static boolean |
isAbstract(Method m)
Returns
|
static boolean |
isAll(Class<?> x,
ClassFlags... flags)
Returns
|
static boolean |
isAll(Constructor<?> x,
ClassFlags... flags)
Returns
|
static boolean |
isAll(Field x,
ClassFlags... flags)
Returns
|
static boolean |
isAll(Method x,
ClassFlags... flags)
Returns
|
static boolean |
isAny(Class<?> x,
ClassFlags... flags)
Returns
|
static boolean |
isAny(Constructor<?> x,
ClassFlags... flags)
Returns
|
static boolean |
isAny(Field x,
ClassFlags... flags)
Returns
|
static boolean |
isAny(Method x,
ClassFlags... flags)
Returns
|
static boolean |
isDeprecated(Class<?> c)
Returns
@Deprecated annotation on it. |
static boolean |
isDeprecated(Constructor<?> c)
Returns
@Deprecated annotation on it. |
static boolean |
isDeprecated(Field f)
Returns
@Deprecated annotation on it. |
static boolean |
isDeprecated(Method m)
Returns
@Deprecated annotation on it. |
static boolean |
isNotAbstract(Class<?> c)
Returns
|
static boolean |
isNotAbstract(Method m)
Returns
|
static boolean |
isNotDeprecated(Class<?> c)
Returns
@Deprecated annotation on it. |
static boolean |
isNotDeprecated(Constructor<?> c)
Returns
@Deprecated annotation on it. |
static boolean |
isNotDeprecated(Field f)
Returns
@Deprecated annotation on it. |
static boolean |
isNotDeprecated(Method m)
Returns
@Deprecated annotation on it. |
static boolean |
isNotPublic(Class<?> c)
Returns
|
static boolean |
isNotPublic(Constructor<?> c)
Returns
|
static boolean |
isNotPublic(Field f)
Returns
|
static boolean |
isNotPublic(Method m)
Returns
|
static boolean |
isNotStatic(Class<?> c)
Returns
|
static boolean |
isNotStatic(Field f)
Returns
|
static boolean |
isNotStatic(Method m)
Returns
|
static boolean |
isNotTransient(Field f)
Returns
|
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(Field f)
Returns
|
static boolean |
isPublic(Method m)
Returns
|
static boolean |
isStatic(Class<?> c)
Returns
|
static boolean |
isStatic(Field f)
Returns
|
static boolean |
isStatic(Method m)
Returns
|
static boolean |
isTransient(Field f)
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 boolean |
setAccessible(Constructor<?> x,
boolean ignoreExceptions)
Attempts to call
x.setAccessible( and quietly ignores security exceptions. |
static boolean |
setAccessible(Field x,
boolean ignoreExceptions)
Attempts to call
x.setAccessible( and quietly ignores security exceptions. |
static boolean |
setAccessible(Method x,
boolean ignoreExceptions)
Attempts to call
x.setAccessible( and quietly ignores security exceptions. |
static Field[] |
sort(Field[] m)
Sorts methods in alphabetical order.
|
static Method[] |
sort(Method[] m)
Sorts methods in alphabetical order.
|
static Class<?> |
toClass(Type t)
Returns the specified type as a
Class . |
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 boolean hasPrimitiveWrapper(Class<?> c)
getPrimitiveWrapper(Class)
class returns a value for the specified class.c
- The class.getPrimitiveWrapper(Class)
class returns a value for the specified class.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 isAll(Class<?> x, ClassFlags... flags)
x
- The class to test.flags
- The flags to test for.public static boolean isAll(Method x, ClassFlags... flags)
x
- The method to test.flags
- The flags to test for.public static boolean isAll(Constructor<?> x, ClassFlags... flags)
x
- The constructor to test.flags
- The flags to test for.public static boolean isAll(Field x, ClassFlags... flags)
x
- The field to test.flags
- The flags to test for.public static boolean isAny(Class<?> x, ClassFlags... flags)
x
- The class to test.flags
- The flags to test for.public static boolean isAny(Method x, ClassFlags... flags)
x
- The method to test.flags
- The flags to test for.public static boolean isAny(Constructor<?> x, ClassFlags... flags)
x
- The constructor to test.flags
- The flags to test for.public static boolean isAny(Field x, ClassFlags... flags)
x
- The field to test.flags
- The flags to test for.public static boolean hasArgs(Method x, Class<?>... args)
x
- The method to test.args
- The arguments to test for.public static boolean hasArgs(Constructor<?> x, Class<?>... args)
x
- The constructor to test.args
- The arguments to test for.public static boolean hasArgs(Constructor<?> x)
x
- The method to test.public static boolean hasNoArgs(Constructor<?> x)
x
- The method to test.public static boolean hasNumArgs(Method x, int number)
x
- The method to test.number
- The number of expected arguments.public static boolean hasNumArgs(Constructor<?> x, int number)
x
- The constructor to test.number
- The number of expected arguments.public static boolean hasFuzzyArgs(Method x, Class<?>... args)
x
- The method to test.args
- The arguments to test for.public static boolean hasFuzzyArgs(Constructor<?> x, Class<?>... args)
x
- The constructor to test.args
- The arguments to test for.public static boolean isDeprecated(Class<?> c)
@Deprecated
annotation on it.c
- The class.@Deprecated
annotation on it.public static boolean isDeprecated(Method m)
@Deprecated
annotation on it.m
- The method.@Deprecated
annotation on it.public static boolean isDeprecated(Constructor<?> c)
@Deprecated
annotation on it.c
- The constructor.@Deprecated
annotation on it.public static boolean isDeprecated(Field f)
@Deprecated
annotation on it.f
- The field.@Deprecated
annotation on it.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 isNotDeprecated(Field f)
@Deprecated
annotation on it.f
- The field.@Deprecated
annotation on it.public static boolean isPublic(Class<?> c)
c
- The class.public static boolean isNotPublic(Class<?> c)
c
- The class.public static boolean isStatic(Class<?> c)
c
- The class.public static boolean isNotStatic(Class<?> c)
c
- The class.public static boolean isAbstract(Class<?> c)
c
- The class.public static boolean isNotAbstract(Class<?> c)
c
- The class.public static boolean isAbstract(Method m)
m
- The method.public static boolean isNotAbstract(Method m)
m
- The method.public static boolean isPublic(Method m)
m
- The method.public static boolean isNotPublic(Method m)
m
- The method.public static boolean isPublic(Field f)
f
- The field.public static boolean isNotPublic(Field f)
f
- The field.public static boolean isStatic(Method m)
m
- The method.public static boolean isNotStatic(Method m)
m
- The method.public static boolean isStatic(Field f)
f
- The field.public static boolean isNotStatic(Field f)
f
- The field.public static boolean isPublic(Constructor<?> c)
c
- The constructor.public static boolean isNotPublic(Constructor<?> c)
c
- The constructor.public static boolean isTransient(Field f)
f
- The field.public static boolean isNotTransient(Field f)
f
- The field.public static boolean hasArgs(Method x)
x
- The method to test.public static boolean hasNoArgs(Method x)
x
- The method to test.public static boolean hasName(Method m, String name)
m
- The method to test.name
- The name to test for.public static boolean hasReturnType(Method m, Class<?> c)
m
- The method to test.c
- The return type to test for.public static boolean hasReturnTypeParent(Method m, Class<?> c)
m
- The method to test.c
- The return type to test for.public static <T extends Annotation> List<T> getAnnotations(Class<T> a, Method m, boolean searchParentMethods, boolean searchReturnType, boolean parentFirst)
a
- The annotation to search for.m
- The method to find the annotation on.searchParentMethods
- If searchReturnType
- If parentFirst
- If public static <T extends Annotation> List<T> getAnnotations(Class<T> a, Method m)
getAnnotations(a, m, true , true , false );
Annotations are ordered method first, then return class, then return superclasses.
a
- The annotation to look for.m
- The method being inspected.public static <T extends Annotation> List<T> getAnnotationsParentFirst(Class<T> a, Method m)
getAnnotations(a, m, true , true , true );
a
- The annotation to look for.m
- The method being inspected.public static <T extends Annotation> T getAnnotation(Class<T> a, Method m, boolean searchParentMethods, boolean searchReturnType)
a
- The annotation to search for.m
- The method to find the annotation on.searchParentMethods
- If searchReturnType
- If public static <T extends Annotation> T getAnnotation(Class<T> a, Method m)
getAnnotation(a, m, true, true);
a
- The annotation to search for.m
- The method to find the annotation on.public static <T extends Annotation> List<T> getAnnotations(Class<T> a, Method m, int index, boolean searchParentMethods, boolean searchArgType, boolean parentFirst)
a
- The annotation to search for.m
- The method containing the argument to find the annotation on.index
- The argument index position.searchParentMethods
- If searchArgType
- If parentFirst
- If public static <T extends Annotation> T getAnnotation(Class<T> a, Method m, int index, boolean searchParentMethods, boolean searchArgType)
a
- The annotation to search for.m
- The method containing the argument to find the annotation on.index
- The argument index position.searchParentMethods
- If searchArgType
- If public static <T extends Annotation> T getAnnotation(Class<T> a, Method m, int index)
getAnnotation(a, m, index, true , true );
a
- The annotation to check for.m
- The method containing the parameter to check.index
- The parameter index.getAnnotation(Class, Method, int)
returns a value.public static <T extends Annotation> List<T> getAnnotations(Class<T> a, Method m, int index)
getAnnotations(a, m, index, true , true , false );
a
- The annotation to look for.m
- The method containing the parameter.index
- The parameter index.public static <T extends Annotation> List<T> getAnnotationsParentFirst(Class<T> a, Method m, int index)
getAnnotations(a, m, index, true , true , true );
a
- The annotation to look for.m
- The method containing the parameter.index
- The parameter index.public static List<Method> findMatchingMethods(Method m)
m
- The method to find matches against.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 Object invokeMethodFuzzy(Method m, Object pojo, Object... args) throws Exception
Arguments will be matched to the parameters based on the parameter types.
Arguments can be in any order.
Extra arguments will be ignored.
Missing arguments will be left
Note that this only works for methods that have distinguishable argument types.
It's not going to work on methods with generic argument types like Object
m
- The method being called.pojo
- The POJO the method is being called on.
args
- The arguments to pass to the method.Exception
public static <T> T invokeConstructorFuzzy(Constructor<T> c, Object... args) throws Exception
Arguments will be matched to the parameters based on the parameter types.
Arguments can be in any order.
Extra arguments will be ignored.
Missing arguments will be left
Note that this only works for constructors that have distinguishable argument types.
It's not going to work on constructors with generic argument types like Object
c
- The constructor being called.args
- The arguments to pass to the constructor.Exception
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 Object[] getMatchingArgs(Class<?>[] paramTypes, Object... args)
Extra parameters are ignored.
Missing parameters are left null.
paramTypes
- The parameter types.args
- The arguments to match to the parameter types.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 Method findPublicStaticCreateMethod(Class<?> oc, Class<?> ic, String name)
oc
- The created type.ic
- The argument type.name
- The method name.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.public static boolean setAccessible(Constructor<?> x, boolean ignoreExceptions)
x.setAccessible(true )
and quietly ignores security exceptions.x
- The constructor.ignoreExceptions
- Ignore SecurityExceptions
and just return public static boolean setAccessible(Method x, boolean ignoreExceptions)
x.setAccessible(true )
and quietly ignores security exceptions.x
- The method.ignoreExceptions
- Ignore SecurityExceptions
and just return public static boolean setAccessible(Field x, boolean ignoreExceptions)
x.setAccessible(true )
and quietly ignores security exceptions.x
- The field.ignoreExceptions
- Ignore SecurityExceptions
and just return public static String getSimpleName(Class<?> c)
Similar to Class.getSimpleName()
, but includes the simple name of an enclosing or declaring class.
c
- The class to get the simple name on.public static String getSimpleName(Type t)
Similar to Class.getSimpleName()
, but includes the simple name of an enclosing or declaring class.
t
- The class to get the simple name on.public static boolean hasAnnotation(Class<? extends Annotation> a, Method m, int index)
getAnnotation(Class, Method, int)
returns a value.a
- The annotation to check for.m
- The method containing the parameter to check.index
- The parameter index.getAnnotation(Class, Method, int)
returns a value.public static boolean hasAnnotation(Class<? extends Annotation> a, Method m)
getAnnotation(Class, Method)
returns a value.a
- The annotation to check for.m
- The method to check.getAnnotation(Class, Method)
returns a value.public static boolean hasAnnotation(Class<? extends Annotation> a, Type t)
getAnnotation(Class, Type)
returns a value.a
- The annotation to check for.t
- The class to check.getAnnotation(Class, Type)
returns a value.public static <T extends Annotation> T getAnnotation(Class<T> a, Type t)
Class.getAnnotation(Class)
except also searches annotations on interfaces.T
- The annotation class type.a
- The annotation class.t
- The annotated class.public static <T extends Annotation> T getDeclaredAnnotation(Class<T> a, Type t)
More efficient than calling Class.getAnnotation(Class)
since it doesn't recursively look for the class
up the parent chain.
T
- The annotation class type.a
- The annotation class.t
- The annotated class.public static <T extends Annotation> List<T> getAnnotations(Class<T> a, Type t)
Searches all superclasses and superinterfaces. Results are ordered child-to-parent.
T
- The annotation class type.a
- The annotation class type.t
- The class being searched.public static <T extends Annotation> List<T> getAnnotationsParentFirst(Class<T> a, Type t)
getAnnotations(Class, Type)
but returns the list in parent-to-child order.a
- The annotation class type.t
- The class being searched.public static <T extends Annotation> LinkedHashMap<Class<?>,T> getAnnotationsMap(Class<T> a, Type t)
getAnnotations(Class, Type)
except returns the annotations as a map with the keys being the
class on which the annotation was found.
Results are ordered child-to-parent.
T
- The annotation class type.a
- The annotation class type.t
- The class being searched.public static <T extends Annotation> LinkedHashMap<Class<?>,T> getAnnotationsMapParentFirst(Class<T> a, Type t)
getAnnotationsMap(Class, Type)
except returns results in parent-to-child order.T
- The annotation class type.a
- The annotation class type.t
- The class being searched.public static <T extends Annotation> void appendAnnotations(Class<T> a, Type t, List<T> l)
a
- The annotation.t
- The class.l
- The list of annotations.public static Class<?> toClass(Type t)
Class
.
If it's already a Class
, it just does a cast.
If it's a ParameterizedType
, it returns the raw type.
t
- The type to convert.Class
, or public static InputStream getResource(Class<?> c, String name)
Class.getResourceAsStream(String)
except looks up the parent hierarchy for the existence of
the specified resource.c
- The class to return the resource on.name
- The resource name.Copyright © 2018 Apache. All rights reserved.