public final class Classes
extends java.lang.Object
Class utilities.| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
DEFAULT_PACKAGE_NAME
The default package name.
|
static java.lang.String |
PACKAGE_SEPARATOR
The string used to separator packages
|
static char |
PACKAGE_SEPARATOR_CHAR
The characther used to separator packages
|
private static java.util.Map |
PRIMITIVE_NAME_TYPE_MAP
Primitive type name -> class map.
|
private static java.lang.Class[] |
PRIMITIVE_WRAPPER_MAP
Map of primitive types to their wrapper classes
|
| Constructor and Description |
|---|
Classes() |
| Modifier and Type | Method and Description |
|---|---|
private static java.lang.Class |
convertToJavaClass(java.lang.String name,
java.lang.ClassLoader cl)
Convert a given String into the appropriate Class.
|
static java.lang.Class<?>[] |
convertToJavaClasses(java.util.Iterator<java.lang.String> it,
java.lang.ClassLoader cl)
Convert a list of Strings from an Interator into an array of
Classes (the Strings are taken as classnames).
|
static void |
describe(java.lang.StringBuffer buffer,
java.lang.Class clazz)
Describe the class
|
static void |
describe(java.lang.StringBuffer buffer,
java.lang.Object object)
Describe the class of an object
|
static void |
displayClassInfo(java.lang.Class clazz,
java.lang.StringBuffer results)
Format a string buffer containing the Class, Interfaces, CodeSource,
and ClassLoader information for the given object clazz.
|
static void |
forceLoad(java.lang.Class type)
Force the given class to be loaded fully.
|
static void |
getAllInterfaces(java.util.List allIfaces,
java.lang.Class c)
Populates a list with all the interfaces implemented by the argument
class c and all its superclasses.
|
static java.lang.Class[] |
getAllUniqueInterfaces(java.lang.Class c)
Returns an array containing all the unique interfaces implemented
by the argument class c and all its superclasses.
|
static java.lang.reflect.Method |
getAttributeGetter(java.lang.Class cls,
java.lang.String attr)
Returns attribute's getter method.
|
static java.lang.reflect.Method |
getAttributeSetter(java.lang.Class cls,
java.lang.String attr,
java.lang.Class type)
Returns attribute's setter method.
|
static java.net.URL[] |
getClassLoaderURLs(java.lang.ClassLoader cl)
Use reflection to access a URL[] getURLs or URL[] getClasspath method so
that non-URLClassLoader class loaders, or class loaders that override
getURLs to return null or empty, can provide the true classpath info.
|
static java.lang.String |
getDescription(java.lang.Object object)
Describe the class of an object
|
static java.lang.String |
getPackageName(java.lang.Class type)
Get the package name of the specified class.
|
static java.lang.String |
getPackageName(java.lang.String classname)
Get the package name of the specified class.
|
static java.lang.Class |
getPrimitive(java.lang.Class wrapper) |
static java.lang.Class |
getPrimitiveTypeForName(java.lang.String name)
Get the primitive type for the given primitive name.
|
static java.lang.Class |
getPrimitiveWrapper(java.lang.Class type)
Get the wrapper class for the given primitive type.
|
private static java.lang.String |
getProperty(java.lang.String name,
java.lang.String defaultValue)
Get a system property
|
static java.lang.Object |
instantiate(java.lang.Class expected,
java.lang.String property,
java.lang.String defaultClassName)
Instantiate a java class object
|
static boolean |
isPrimitive(java.lang.Class type)
Check if the given class is a primitive class or a primitive
wrapper class.
|
static boolean |
isPrimitive(java.lang.String type)
Check type against boolean, byte, char, short, int, long, float, double.
|
static boolean |
isPrimitiveWrapper(java.lang.Class type)
Check if the given class is a primitive wrapper class.
|
static java.lang.Class |
loadClass(java.lang.String className)
This method acts equivalently to invoking
Thread.currentThread().getContextClassLoader().loadClass(className); but it also
supports primitive types and array classes of object types or primitive types. |
static java.lang.Class |
loadClass(java.lang.String className,
java.lang.ClassLoader classLoader)
This method acts equivalently to invoking classLoader.loadClass(className)
but it also supports primitive types and array classes of object types or
primitive types.
|
static java.lang.String |
stripPackageName(java.lang.Class type)
Get the short name of the specified class by striping off the package
name.
|
static java.lang.String |
stripPackageName(java.lang.String classname)
Get the short name of the specified class by striping off the package
name.
|
public static final java.lang.String PACKAGE_SEPARATOR
public static final char PACKAGE_SEPARATOR_CHAR
public static final java.lang.String DEFAULT_PACKAGE_NAME
private static final java.util.Map PRIMITIVE_NAME_TYPE_MAP
private static final java.lang.Class[] PRIMITIVE_WRAPPER_MAP
public static void displayClassInfo(java.lang.Class clazz,
java.lang.StringBuffer results)
clazz - the Classresults - - the buffer to write the info topublic static java.net.URL[] getClassLoaderURLs(java.lang.ClassLoader cl)
cl - public static java.lang.String getDescription(java.lang.Object object)
object - the objectpublic static void describe(java.lang.StringBuffer buffer,
java.lang.Object object)
buffer - the string bufferobject - the objectpublic static void describe(java.lang.StringBuffer buffer,
java.lang.Class clazz)
buffer - the string bufferclazz - the clazzpublic static java.lang.String stripPackageName(java.lang.String classname)
classname - Class name.public static java.lang.String stripPackageName(java.lang.Class type)
type - Class name.public static java.lang.String getPackageName(java.lang.String classname)
classname - Class name.EmptyStringException - Classname is an empty string.public static java.lang.String getPackageName(java.lang.Class type)
type - Class.public static void forceLoad(java.lang.Class type)
This method attempts to locate a static method on the given class the attempts to invoke it with dummy arguments in the hope that the virtual machine will prepare the class for the method call and call all of the static class initializers.
type - Class to force load.NullArgumentException - Type is null.public static java.lang.Class getPrimitiveTypeForName(java.lang.String name)
For example, "boolean" returns Boolean.TYPE and so on...
name - Primitive type name (boolean, int, byte, ...)java.lang.IllegalArgumentException - Type is not a primitive classpublic static java.lang.Class getPrimitiveWrapper(java.lang.Class type)
type - Primitive class.java.lang.IllegalArgumentException - Type is not a primitive classpublic static void getAllInterfaces(java.util.List allIfaces,
java.lang.Class c)
allIfaces - - the list to populate with the interfacesc - - the class to start scanning for interfacespublic static java.lang.Class[] getAllUniqueInterfaces(java.lang.Class c)
c - - the class to start scanning for interfacespublic static boolean isPrimitiveWrapper(java.lang.Class type)
type - Class to check.public static boolean isPrimitive(java.lang.Class type)
type - Class to check.public static boolean isPrimitive(java.lang.String type)
type - The java type namepublic static java.lang.Class getPrimitive(java.lang.Class wrapper)
wrapper - a primitive wrapper typepublic static java.lang.Object instantiate(java.lang.Class expected,
java.lang.String property,
java.lang.String defaultClassName)
expected - the expected class typeproperty - the system property defining the classdefaultClassName - the default class namepublic static java.lang.Class loadClass(java.lang.String className)
throws java.lang.ClassNotFoundException
Thread.currentThread().getContextClassLoader().loadClass(className); but it also
supports primitive types and array classes of object types or primitive types.className - the qualified name of the class or the name of primitive type or
array in the same format as returned by the
java.lang.Class.getName() method.java.lang.ClassNotFoundException - when the classLoader can not find the requested classpublic static java.lang.Class loadClass(java.lang.String className,
java.lang.ClassLoader classLoader)
throws java.lang.ClassNotFoundException
className - the qualified name of the class or the name of primitive
type or array in the same format as returned by the
java.lang.Class.getName() method.classLoader - the ClassLoader used to load classesjava.lang.ClassNotFoundException - when the classLoader can not
find the requested classpublic static final java.lang.Class<?>[] convertToJavaClasses(java.util.Iterator<java.lang.String> it,
java.lang.ClassLoader cl)
throws java.lang.ClassNotFoundException
it - A java.util.Iterator pointing to a Collection of Stringscl - The ClassLoader to usejava.lang.ClassNotFoundException - When a class could not be loaded from
the specified ClassLoaderpublic static final java.lang.reflect.Method getAttributeGetter(java.lang.Class cls,
java.lang.String attr)
throws java.lang.NoSuchMethodException
cls - the class the attribute belongs tooattr - the attribute's nameNoSuchMethodException - if the getter was not foundjava.lang.NoSuchMethodExceptionpublic static final java.lang.reflect.Method getAttributeSetter(java.lang.Class cls,
java.lang.String attr,
java.lang.Class type)
throws java.lang.NoSuchMethodException
cls - the class the attribute belongs toattr - the attribute's nametype - the attribute's typeNoSuchMethodException - if the setter was not foundjava.lang.NoSuchMethodExceptionprivate static final java.lang.Class convertToJavaClass(java.lang.String name,
java.lang.ClassLoader cl)
throws java.lang.ClassNotFoundException
name - Name of classcl - ClassLoader to usejava.lang.ClassNotFoundException - When the class could not be found by
the specified ClassLoaderprivate static java.lang.String getProperty(java.lang.String name,
java.lang.String defaultValue)
name - the property namedefaultValue - the default value