public class ClassUtil extends Object
Constructor and Description |
---|
ClassUtil() |
Modifier and Type | Method and Description |
---|---|
static Method[] |
getAllMethods(Class<?> klass)
Deprecated.
- In retrospect, there are few if any good reasons for
calling this method.
getVisibleMethods(java.lang.Class<?>) is the
better choice. |
static Method[] |
getAnnotatedMethods(Class<?> klass,
Class<? extends Annotation> annotationKlass)
Returns the visible methods defined by the class and its superclasses that
have the specified annotation.
|
static Method |
getBestMethod(Class<?> klass,
String methodName,
Object... args)
Returns the most appropriate method to invoke for the specified parameter
values.
|
static Method[] |
getDeclaredMethodsByAccess(Class<?> klass,
boolean isPublic,
boolean isProtected,
boolean isPrivate,
boolean isDefault)
Returns the declared methods of the specified class that have the desired
access modifiers.
|
static <T> T |
getFieldValue(Object obj,
String fieldName,
Class<T> expectedClass)
Looks for the specified field in the class or its superclasses, returning
its value if it exists.
|
static Class<?> |
getPrimitiveType(Object val)
Extracts the
TYPE field value from a primitive wrapper type. |
static HashMultimap<String,Method> |
getVisibleMethodMap(Class<?> klass)
Returns a
multi-map of the
methods visible to the given class. |
static Method[] |
getVisibleMethods(Class<?> klass)
Returns all methods visible to the specified class.
|
static String |
internalNameToExternal(String name)
Converts the "internal" name of a class (eg: "[Ljava/lang/String;") to its
"external" representation (eg: "java.lang.String[]").
|
static boolean |
isOverridden(Method method,
Method[] methods)
Determines whether a given method is overridden by methods in the
passed array.
|
public static String internalNameToExternal(String name)
public static Class<?> getPrimitiveType(Object val)
TYPE
field value from a primitive wrapper type.
Returns null
if the passed value is not a wrapper type.
This method is useful for reflective parameter matching.
@Deprecated public static Method[] getAllMethods(Class<?> klass)
getVisibleMethods(java.lang.Class<?>)
is the
better choice.Class.getDeclaredMethods()
.public static Method[] getDeclaredMethodsByAccess(Class<?> klass, boolean isPublic, boolean isProtected, boolean isPrivate, boolean isDefault)
public static Method[] getVisibleMethods(Class<?> klass)
The order of the returned methods is undefined.
public static HashMultimap<String,Method> getVisibleMethodMap(Class<?> klass)
multi-map
of the
methods visible to the given class. The key of this map is the method name,
the values are the various methods associated with that name.
This method examines all declared methods of the specified class, and the public, protected, and default-access methods declared by the class' ancestors. For overridden methods, the lowest (most subclassed) method is returned.
public static Method[] getAnnotatedMethods(Class<?> klass, Class<? extends Annotation> annotationKlass)
public static Method getBestMethod(Class<?> klass, String methodName, Object... args)
null
.
Notes:
null
argument values are ignored when attempting to
resolve methods. This can result in multiple candidate methods, and
a null
return.
foo(Integer)
and foo(int)
), the variant with an object parameter is
considered a better match than the variant with a primitive parameter.
null
argument will never match a primitive parameter
(the compiler will match as if unboxed, with a runtime NPE).
klass
- The class to examine.methodName
- The name of the method.args
- Actual argument values for the method. See note above
regarding the problems that null
causes.public static boolean isOverridden(Method method, Method[] methods)
public static <T> T getFieldValue(Object obj, String fieldName, Class<T> expectedClass) throws NoSuchFieldException
NoSuchFieldException
if unable
to find the field in the class hierarchy, or if an exception occurred while
retrieving its value.
Note: does not verify that actual field value matches expectedClass
(this gets tricky when dealing with primitive wrapper classes).
NoSuchFieldException