public final class ClassUtils extends Object
Modifier and Type | Field and Description |
---|---|
static String |
JAVABEAN_GET_PREFIX |
static String |
JAVABEAN_IS_PREFIX |
static String |
JAVABEAN_SET_PREFIX |
Modifier and Type | Method and Description |
---|---|
static boolean |
classExists(String clazz)
Gracefully test whether a class exists.
|
static String |
getGenericTypeAsString(Type type)
Converts a
java.lang.reflect.Type , as returned by getGenericType
into a String representation. |
static Object |
getNumberMaxValue(Class<?> clazz) |
static Object |
getNumberMinValue(Class<?> clazz) |
static <T extends Annotation> |
getOriginalAnnotation(Method method,
Class<T> annotationClass)
Gets the given annotationClass defined on the given method.
|
static Class<?> |
getOriginalDeclaringClass(Method method)
Similar to
Method.getDeclaringClass , but traverses the class heirarchy to find
the class that originally declared this method, in the event the implementation has
been overridden. |
static Type[] |
getOriginalGenericParameterTypes(Method method)
Gets the given genericParameterTypes defined on the given method.
|
static Type |
getOriginalGenericReturnType(Method method)
Gets the given genericReturnType defined on the given method.
|
static String |
getPackagesAsFolderNames(Class<?> clazz)
Converts
com.foo.Bar into /com/foo . |
static <T> T |
getProperty(Object base,
String property)
Get the value of the JavaBean-convention property without using
java.beans , as
that package is not available on all target platforms. |
static Method |
getReadMethod(Class<?> clazz,
String property)
Lookup JavaBean-convention getter without using
java.beans , as that package is
not available on all target platforms. |
static String |
getSimpleName(String qualifiedClassName)
Gets the 'simple' name of the class, given the fully qualified name.
|
static Class<?> |
getWrapperClass(Class<?> clazz)
Returns the wrapper class for a primitive class (eg.
|
static Method |
getWriteMethod(Class<?> clazz,
String property,
Class<?> type)
Lookup JavaBean-convention setter without using
java.beans , as that package is
not available on all target platforms. |
static boolean |
isPrimitive(String className) |
static boolean |
isPrimitiveWrapper(Class<?> clazz)
Returns
true if the given class is the Object-wrapper of a primitive type (eg. |
static Class<?> |
niceForName(String className)
Replacement for
Class.forName() that:
supports primitives (int , long , etc)
returns null if there is no such class (eg. |
static Class<?> |
niceForName(String className,
ClassLoader classLoader)
Replacement for
Class.forName() that:
returns null if there is no such class (eg. |
static Object |
parseNumber(Class<?> clazz,
String value) |
static void |
registerAlienClassLoader(ClassLoader classLoader)
When dealing with multiple isolated ClassLoaders, sometimes the object being inspected may
reference a class that is not available to any of our own ClassLoaders.
|
static void |
setProperty(Object base,
String property,
Object value)
Set the value of the JavaBean-convention property without using
java.beans , as
that package is not available on all target platforms. |
public static final String JAVABEAN_SET_PREFIX
public static final String JAVABEAN_GET_PREFIX
public static final String JAVABEAN_IS_PREFIX
public static Method getReadMethod(Class<?> clazz, String property)
java.beans
, as that package is
not available on all target platforms.public static Method getWriteMethod(Class<?> clazz, String property, Class<?> type)
java.beans
, as that package is
not available on all target platforms.public static <T> T getProperty(Object base, String property)
java.beans
, as
that package is not available on all target platforms.public static void setProperty(Object base, String property, Object value)
java.beans
, as
that package is not available on all target platforms.public static boolean isPrimitiveWrapper(Class<?> clazz)
true
if the given class is the Object-wrapper of a primitive type (eg.
Integer
for int
).
We want to be able to distinguish these, because we can't usefully drill into them and may do
better to 'inspect from parent' (see UIMetawidget
).
public static Class<?> getWrapperClass(Class<?> clazz)
Integer.class
for
int.class
)public static boolean classExists(String clazz)
ClassNotFoundException
.public static void registerAlienClassLoader(ClassLoader classLoader)
Class.forName
will always fail. To cope with this, we record 'alien'
ClassLoaders as and when we encounter thempublic static Class<?> niceForName(String className)
Class.forName()
that:
int
, long
, etc)null
if there is no such class (eg. if the name is a symbolic type,
such as 'Login Screen')public static Class<?> niceForName(String className, ClassLoader classLoader)
Class.forName()
that:
null
if there is no such class (eg. if the name is a symbolic type,
such as 'Login Screen')int
, long
, etc)This method first tries to load the class using the given ClassLoader (if any). If that fails, it then tries the Thread's current ClassLoader (this works best for EJB/WAR splits where, say, metawidget-core and metawidget-annotations are located in the EJB/lib and the other modules are located in the WAR/lib). If that fails, it tries ClassUtils' ClassLoader. If that fails, it tries our alien ClassLoader.
classLoader
- the specific ClassLoader to use to try and load this class. In general clients
should use the other form of this method, which will default to trying the current
Thread's ClassLoader. Can be null.NullPointerException
- if className is nullpublic static boolean isPrimitive(String className)
public static String getSimpleName(String qualifiedClassName)
Named after Class.getSimpleName
.
public static Class<?> getOriginalDeclaringClass(Method method)
Method.getDeclaringClass
, but traverses the class heirarchy to find
the class that originally declared this method, in the event the implementation has
been overridden.public static String getPackagesAsFolderNames(Class<?> clazz)
com.foo.Bar
into /com/foo
. The leading forward slash can
be important in some module loading environments (e.g. for JBoss Forge).public static <T extends Annotation> T getOriginalAnnotation(Method method, Class<T> annotationClass)
We work off no annotations being defined (rather than just annotationClass) because
we still want to support explicitly overriding methods in order to suppress annotations (such
as UiHidden
). This isn't perfect, because the overridden method still has to
define at least one annotation.
This approach is important for proxied classes, which don't always retain annotations.
public static Type getOriginalGenericReturnType(Method method)
This approach is important for proxied classes, which don't always retain genetics.
public static Type[] getOriginalGenericParameterTypes(Method method)
This approach is important for proxied classes, which don't always retain genetics.
Copyright © 2015. All Rights Reserved.