Java EE 5 SDK

com.sun.appserv.management.util.misc
Class ClassUtil

java.lang.Object
  extended by com.sun.appserv.management.util.misc.ClassUtil

public final class ClassUtil
extends Object

Provides a variety of useful utilities having to do with classes, types of objects, etc.


Method Summary
static Class<?> classForName(String name)
           
static boolean classIsAccessible(String name)
           
static boolean classIsArray(Class theClass)
          Test whether a Class is an array
static boolean classnameIsArray(String classname)
          Test whether a classname is an array
static boolean classnameIsPrimitiveArray(String classname)
          Test whether a classname is a primitive array
static String[] classnamesFromSignature(Class[] classes)
          Return the corresponding classes for each classname.
static Class convertArrayClass(Class arrayClass, Class newInnerType)
          Convert inner element to another type.
static String ExpandClassName(String name)
          Expand an abbreviated classname into its true java name.
static Method findMethod(Class theClass, String methodName, Class[] sig)
          Find all methods that match the name.
static Set<Method> findMethods(Method[] candidates, String methodName)
          Find all methods that match the name.
static int getArrayDimensions(Class theClass)
           
static Class getArrayElementClass(Class arrayClass)
           
static String getArrayMemberClassName(String classname)
          Get the classname for an array element.
static Class getClassFromName(String classname)
          Get a Class from a classname.
static Object getFieldValue(Class theInterface, String name)
           
static String getFriendlyClassname(Class theClass)
          Get a "friendly" classname for a Class.
static String getFriendlyClassname(String type)
           
static Class getInnerArrayElementClass(Class arrayClass)
           
static String getPackagePrefix(String classname)
           
static Package[] getPackages()
           
static Package[] getPackages(ClassLoader cl)
           
static char getPrimitiveArrayTypeCode(Class theClass)
          Return the primitive element type code for an array of primitive types.
static char getPrimitiveArrayTypeCode(String classname)
          Return the primitive element type code for an array of primitive types.
static String[] getTypes(Object[] args)
          Return the corresponding classes for each element in an Object[] If an element is null, then its corresponding Class will also be null.
static Object InstantiateDefault(Class inClass)
          Given a Class, create a new instance with an empty constructor.
static Object InstantiateFromString(Class theClass, String theString)
          Given a Class and a String, create a new instance with a constructor that accept a String.
static Object InstantiateObject(Class theClass, Object[] args)
          Create a new object of the specified class using a constructor that accepts the specified arguments.
static Object InstantiateObject(Class theClass, String theString)
          Create a new object of the specified class using a String constructor.
static boolean IsPrimitiveClass(Class theClass)
          Test whether a class is a primitive class.
static Class ObjectClassToPrimitiveClass(Class theClass)
          Map primitive class Classes to Object forms eg int.class to Integer.class
static boolean objectIsArray(Object o)
          Test whether an Object is an array
static boolean objectIsPrimitiveArray(Object o)
          Test whether an Object is an array of primitive types
static Class PrimitiveClassToObjectClass(Class theClass)
          Map primitive class Classes to Object forms eg int.class to Integer.class
static String PrimitiveLetterToClassName(char primitive)
          Convert a primitive class letter to its corresponding class name.
static Class[] signatureFromClassnames(String[] classnames)
          Return the corresponding classes for each classname.
static boolean signaturesAreCompatible(Class<?>[] callee, Class<?>[] argsSignature)
          Return true if caller signature is compatible with callee.
static boolean sigsEqual(Class[] sig1, Class[] sig2)
           
static String stripPackageName(String classname)
          Strip the package name.
static String stripPackagePrefix(String classname)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

classIsAccessible

public static boolean classIsAccessible(String name)

sigsEqual

public static boolean sigsEqual(Class[] sig1,
                                Class[] sig2)

objectIsArray

public static boolean objectIsArray(Object o)
Test whether an Object is an array

Parameters:
o - object to test
Returns:
true if the object is an array, false otherwise.

classIsArray

public static boolean classIsArray(Class theClass)
Test whether a Class is an array

Parameters:
theClass - class to test
Returns:
true if the class is an array, false otherwise.

objectIsPrimitiveArray

public static boolean objectIsPrimitiveArray(Object o)
Test whether an Object is an array of primitive types

Parameters:
o - object to test
Returns:
true if the object is an array, false otherwise.

classnameIsArray

public static boolean classnameIsArray(String classname)
Test whether a classname is an array

Parameters:
classname - classname string
Returns:
true if the object is an array, false otherwise.

stripPackageName

public static String stripPackageName(String classname)
Strip the package name.

Parameters:
classname - classname string
Returns:
the classname, without its package

classnameIsPrimitiveArray

public static boolean classnameIsPrimitiveArray(String classname)
Test whether a classname is a primitive array

Parameters:
classname - classname string
Returns:
true if the object is a primitive array, false otherwise.

getPrimitiveArrayTypeCode

public static char getPrimitiveArrayTypeCode(Class theClass)
Return the primitive element type code for an array of primitive types. Same as getPrimitiveArrayTypeCode( theClass.getName() )

Parameters:
theClass - the Class object
Returns:
the element type code; otherwise (char)0

getPrimitiveArrayTypeCode

public static char getPrimitiveArrayTypeCode(String classname)
Return the primitive element type code for an array of primitive types.

Parameters:
classname - classname string
Returns:
the element type code; otherwise (char)0

getArrayMemberClassName

public static String getArrayMemberClassName(String classname)
Get the classname for an array element.

Parameters:
classname - classname string
Returns:
the classname for the array element

classForName

public static Class<?> classForName(String name)
                             throws ClassNotFoundException
Throws:
ClassNotFoundException

getClassFromName

public static Class getClassFromName(String classname)
                              throws ClassNotFoundException
Get a Class from a classname. Class.forName does not work for primitive types; this methods returns the correct Class for any type.

Parameters:
classname - classname string
Returns:
the classname for the array element
Throws:
ClassNotFoundException

PrimitiveClassToObjectClass

public static Class PrimitiveClassToObjectClass(Class theClass)
Map primitive class Classes to Object forms eg int.class to Integer.class

Parameters:
theClass - the class to map
Returns:
the corresponding Object class or the original Class if not a primitive.

ObjectClassToPrimitiveClass

public static Class ObjectClassToPrimitiveClass(Class theClass)
Map primitive class Classes to Object forms eg int.class to Integer.class

Parameters:
theClass - the class to map
Returns:
the corresponding Object class or the original Class if not a primitive.

IsPrimitiveClass

public static boolean IsPrimitiveClass(Class theClass)
Test whether a class is a primitive class.

Parameters:
theClass - the class to test
Returns:
true if it's a primitive class, false otherwise.

PrimitiveLetterToClassName

public static String PrimitiveLetterToClassName(char primitive)
Convert a primitive class letter to its corresponding class name.

Parameters:
primitive - the primitive character code
Returns:
the corresponding classname

getTypes

public static String[] getTypes(Object[] args)
Return the corresponding classes for each element in an Object[] If an element is null, then its corresponding Class will also be null.

Parameters:
args - an array of objects.
Returns:
an array of classes

signatureFromClassnames

public static Class[] signatureFromClassnames(String[] classnames)
                                       throws ClassNotFoundException
Return the corresponding classes for each classname. If an element is null, then its corresponding Class will also be null.

Parameters:
classnames - an array of classnames.
Returns:
an array of classes
Throws:
ClassNotFoundException

classnamesFromSignature

public static String[] classnamesFromSignature(Class[] classes)
Return the corresponding classes for each classname. If an element is null, then its corresponding Class will also be null.

Parameters:
classes - an array of classnames.
Returns:
an array of classes

getFriendlyClassname

public static String getFriendlyClassname(Class theClass)
Get a "friendly" classname for a Class.

Calls getFriendlyClassname( theClass.getName() )

Parameters:
theClass - the class for which the name should be gotten
Returns:
the "friendly" name

getFriendlyClassname

public static String getFriendlyClassname(String type)

getArrayDimensions

public static int getArrayDimensions(Class theClass)

getArrayElementClass

public static Class getArrayElementClass(Class arrayClass)

getInnerArrayElementClass

public static Class getInnerArrayElementClass(Class arrayClass)
                                       throws ClassNotFoundException
Throws:
ClassNotFoundException

signaturesAreCompatible

public static boolean signaturesAreCompatible(Class<?>[] callee,
                                              Class<?>[] argsSignature)
Return true if caller signature is compatible with callee.

Parameters:
callee - the signature of the method to be called
argsSignature - the signature of the argument list

findMethod

public static final Method findMethod(Class theClass,
                                      String methodName,
                                      Class[] sig)
Find all methods that match the name.


findMethods

public static final Set<Method> findMethods(Method[] candidates,
                                            String methodName)
Find all methods that match the name.


InstantiateObject

public static Object InstantiateObject(Class theClass,
                                       Object[] args)
                                throws Exception
Create a new object of the specified class using a constructor that accepts the specified arguments.

Parameters:
theClass - the Class of the desired Object
args - the argument list for the constructor
Throws:
Exception

InstantiateObject

public static Object InstantiateObject(Class theClass,
                                       String theString)
                                throws Exception
Create a new object of the specified class using a String constructor.

Parameters:
theClass - the Class of the desired Object
theString - the string for a String constructor
Returns:
the resulting Object
Throws:
Exception

InstantiateFromString

public static Object InstantiateFromString(Class theClass,
                                           String theString)
                                    throws Exception
Given a Class and a String, create a new instance with a constructor that accept a String. Primitive types are instantiated as their equivalent Object forms.

Parameters:
theClass - the class from which an instance should be instantiated
theString - the string to be supplied to the constructor
Returns:
the resulting Object
Throws:
Exception

InstantiateDefault

public static Object InstantiateDefault(Class inClass)
                                 throws Exception
Given a Class, create a new instance with an empty constructor. Primitive types are instantiated as their equivalent Object forms. Any value is acceptable in the newly created object.

Parameters:
inClass - the class from which an instance should be instantiated
Returns:
the resulting Object
Throws:
Exception

ExpandClassName

public static String ExpandClassName(String name)
Expand an abbreviated classname into its true java name. Turn "Integer" into "java.lang.Integer", etc.


convertArrayClass

public static Class convertArrayClass(Class arrayClass,
                                      Class newInnerType)
                               throws ClassNotFoundException
Convert inner element to another type. Only works for arrays of Objects. Example: convertArrayClass( "[[[LObject;", "Long" ) =>[[[LLong;

Parameters:
arrayClass -
newInnerType - the desired Class of the innermost element
Throws:
ClassNotFoundException

getPackages

public static Package[] getPackages()

getPackages

public static Package[] getPackages(ClassLoader cl)

getFieldValue

public static Object getFieldValue(Class theInterface,
                                   String name)

stripPackagePrefix

public static String stripPackagePrefix(String classname)

getPackagePrefix

public static String getPackagePrefix(String classname)

Java EE 5 SDK

Submit a bug or feature

Copyright 2006 Sun Microsystems, Inc. All rights reserved.