ariba.util.core
Class ClassUtil

java.lang.Object
  extended by ariba.util.core.ClassUtil

public final class ClassUtil
extends java.lang.Object

ClassUtil. A set of helpers for dealing with java classes.


Field Summary
static java.lang.String NativeBoolean
           
static java.lang.String NativeByte
           
static java.lang.String NativeChar
           
static java.lang.String NativeDouble
           
static java.lang.String NativeFloat
           
static java.lang.String NativeInteger
           
static java.lang.String NativeLong
           
static java.lang.String NativeShort
           
 
Method Summary
static java.lang.Class classForName(java.lang.String className)
          Find a Class for the specified class name.
static java.lang.Class classForName(java.lang.String className, boolean warning)
          Find a Class for the specified class name.
static java.lang.Class classForName(java.lang.String className, java.lang.Class supposedSuperclass)
          Find a Class for the specified class name.
static java.lang.Class classForName(java.lang.String className, java.lang.Class supposedSuperclass, boolean warning)
          Find a Class for the specified class name.
static java.lang.Class classForNameNonCaching(java.lang.String className, java.lang.String pattern, boolean warning)
          Find a Class for the specified class name.
static java.lang.Class classForNameWithException(java.lang.String className)
          Find a Class for the specified class name.
static java.lang.Class classForNativeType(java.lang.String typeName)
           
static void classTouch(java.lang.String name)
          Make sure a class' static inits have run.
static ClassFactory getClassFactory()
           
static java.lang.String getClassNameOfObject(java.lang.Object o)
          Returns the name of the class of the specified object.
static java.lang.reflect.Field[] getDeclaredFields(java.lang.Class clazz)
          ClassUtil.getDeclaredFields returns all the declared fields for the class and it's superclasses.
static boolean instanceOf(java.lang.Class instance, java.lang.Class target)
          Check if one class inherits from another class.
static boolean instanceOf(java.lang.Object object, java.lang.String className)
          Check if an object is an instance of a class identified by it's name.
static java.lang.Object invokeStaticMethod(java.lang.String className, java.lang.String methodName)
          Invokes the specified static method of the specified class.
static java.lang.Object invokeStaticMethod(java.lang.String className, java.lang.String methodName, java.lang.Class[] paramTypes, java.lang.Object[] args)
          Invokes the specified static method of the specified class.
static java.lang.Object newInstance(java.lang.Class theClass)
          Creates a new instance of the specified class.
static java.lang.Object newInstance(java.lang.Class classObj, java.lang.Class supposedSuperclass, boolean warning)
          Creates a new Instance of the specified class with error checking.
static java.lang.Object newInstance(java.lang.String className)
          Creates a new instance of the specified class.
static java.lang.Object newInstance(java.lang.String className, boolean warning)
          Creates a new instance of the specified class.
static java.lang.Object newInstance(java.lang.String className, java.lang.Class supposedSuperclass, boolean warning)
          Creates a new Instance of the specified class with error checking.
static java.lang.Object newInstance(java.lang.String className, java.lang.String supposedSuperclassName)
          Creates a new Instance of the specified class with error checking.
static java.lang.Object newInstance(java.lang.String className, java.lang.String supposedSuperclassName, boolean warning)
          Creates a new Instance of the specified class with error checking.
static ClassFactory setClassFactory(ClassFactory cf)
           
static java.lang.String stripClassFromClassName(java.lang.String className)
          Find the package specifier for a given class name.
static java.lang.String stripPackageFromClassName(java.lang.String className)
          Strips any package specifiers from the given class name.
static java.lang.String typeToVMType(java.lang.String type)
          Convert from a class name into an internal java representation of that class String int -> I java.lang.String -> Ljava.lang.String;
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NativeInteger

public static java.lang.String NativeInteger

NativeBoolean

public static java.lang.String NativeBoolean

NativeDouble

public static java.lang.String NativeDouble

NativeFloat

public static java.lang.String NativeFloat

NativeLong

public static java.lang.String NativeLong

NativeByte

public static java.lang.String NativeByte

NativeShort

public static java.lang.String NativeShort

NativeChar

public static java.lang.String NativeChar
Method Detail

classTouch

public static void classTouch(java.lang.String name)
Make sure a class' static inits have run. This method loads the specified class to make sure that it's static methods have run. If the class is not found, no warning will be printed.

Parameters:
name - the name of the class to load.

classForNativeType

public static java.lang.Class classForNativeType(java.lang.String typeName)

classForName

public static java.lang.Class classForName(java.lang.String className)
Find a Class for the specified class name. A warning will be printed if the class was not found.

Parameters:
className - the name of the class to find
Returns:
the Class for the given className, or null if the Class doesn't exist.
See Also:
newInstance(java.lang.String)

classForName

public static java.lang.Class classForName(java.lang.String className,
                                           java.lang.Class supposedSuperclass)
Find a Class for the specified class name. A warning will be printed if the class was not found.

Parameters:
className - the name of the class to find
supposedSuperclass - The class of the required superclass for the class specified by className
Returns:
the Class for the given className, or null if the Class doesn't exist.
See Also:
newInstance(java.lang.String)

setClassFactory

public static ClassFactory setClassFactory(ClassFactory cf)

getClassFactory

public static ClassFactory getClassFactory()

classForName

public static java.lang.Class classForName(java.lang.String className,
                                           boolean warning)
Find a Class for the specified class name. Prints a warning message if the class can't be found and warning is true.

Parameters:
className - the name of the class to find
warning - if true and the class can not be found, a warning will be printed
Returns:
the Class for the given className, or null if the Class doesn't exist.
See Also:
newInstance(java.lang.String)

classForName

public static java.lang.Class classForName(java.lang.String className,
                                           java.lang.Class supposedSuperclass,
                                           boolean warning)
Find a Class for the specified class name. Prints a warning message if the class can't be found and warning is true.

Parameters:
className - the name of the class to find
supposedSuperclass - The required superclass for the class
warning - if true and the class can not be found, or it is not assignable to the supposedSuperclass, a warning will be printed
Returns:
the Class for the given className, or null if the Class doesn't exist.
See Also:
newInstance(java.lang.String)

classForNameWithException

public static java.lang.Class classForNameWithException(java.lang.String className)
                                                 throws java.lang.ClassNotFoundException
Find a Class for the specified class name. Throws an exception if the class is not found.

The java spec does not define behavior with a null className. If a null is passed in a ClassNotFoundException will be thrown.

Parameters:
className - the name of the class to find
Returns:
the Class for the given className
Throws:
java.lang.ClassNotFoundException - if the class can not be found.
See Also:
newInstance(java.lang.String)

classForNameNonCaching

public static java.lang.Class classForNameNonCaching(java.lang.String className,
                                                     java.lang.String pattern,
                                                     boolean warning)
Find a Class for the specified class name. Throws an exception if the class is not found.

Each call to classForNameNonCaching reloads the byte codes into the VM. This is very useful debugging programs that have long start-up times because classes loaded with classForNameNonCaching can be recompiled and reloaded into the VM without restarting.

The pattern parameter allows you to extend the dynamic type of class loading to other classes that are instantiated by the class identified in className. If you provide null for this parameter, then the only class that gets dynamically reloaded is the className class. Any classes it instantiates will be based on byte codes cached in the VM. Note the className needs to conform to the pattern.

The java spec does not define behavior with a null className. If a null is passed in a ClassNotFoundException will be thrown. This will dynamically load RequisitionTester plus any classes that RequisitionTester instantiates that also belong to packages that begin with the string "test".

Parameters:
className - the name of the class to find - null not allowed
pattern - the classname pattern for other classes to reload
warning - if true and the class can not be found, a warning will be printed
Returns:
the Class for the given className
See Also:
newInstance(java.lang.String), StringUtil.stringMatchesPattern(java.lang.String, java.lang.String)

instanceOf

public static boolean instanceOf(java.lang.Object object,
                                 java.lang.String className)
Check if an object is an instance of a class identified by it's name.

Parameters:
object - the object to test the instance of
className - the name of the class being tested for
Returns:
true if object is an instance the class specified by className, including subclasses; false otherwise

instanceOf

public static boolean instanceOf(java.lang.Class instance,
                                 java.lang.Class target)
Check if one class inherits from another class.

Parameters:
instance - the class to check the inheritance tree of, must not be null.
target - the class to check against. If null, will return false.
Returns:
true if the class instance is, or inherits from, the class target; false otherwise

getClassNameOfObject

public static java.lang.String getClassNameOfObject(java.lang.Object o)
Returns the name of the class of the specified object. It is shorthand for o.getClass().getName()

Parameters:
o - the object to find the class name of
Returns:
the name as returned by o.getClass().getName()

newInstance

public static java.lang.Object newInstance(java.lang.String className)
Creates a new instance of the specified class. If the class className is derived from BaseObject and you want the new instance to be properly initialized, you should call BaseObject.New() instead. If the class can not be found a warning will be printed.

Parameters:
className - the name of class to create a new instance of
Returns:
a new instance of the class specified by className. If the class can not be found, null will be returned.
See Also:
classForName(java.lang.String)

newInstance

public static java.lang.Object newInstance(java.lang.String className,
                                           boolean warning)
Creates a new instance of the specified class. If the class className is derived from BaseObject and you want the new instance to be properly initialized, you should call BaseObject.New() instead. If the class can not be found an optional warning may be printed.

Parameters:
className - the name of class to create a new instance of
warning - if true and the class can not be found, a warning will be printed
Returns:
a new instance of the class specified by className. If the class can not be found, null will be returned.
See Also:
classForName(java.lang.String)

newInstance

public static java.lang.Object newInstance(java.lang.String className,
                                           java.lang.String supposedSuperclassName)
Creates a new Instance of the specified class with error checking. Use this when the new object created should be a subclass of some known class. Prints an error message if the class cannot be created.

Parameters:
className - the class to create a new instance of
supposedSuperclassName - The name of the required superclass for the new class
Returns:
a new instance of the class theClass. If there is an error, null will be returned.

newInstance

public static java.lang.Object newInstance(java.lang.String className,
                                           java.lang.String supposedSuperclassName,
                                           boolean warning)
Creates a new Instance of the specified class with error checking. Use this when the new object created should be a subclass of some known class.

Parameters:
className - the class to create a new instance of
supposedSuperclassName - The name of the required superclass for the new class
warning - if true and the class can not be found, a warning will be printed
Returns:
a new instance of the class theClass. If there is an error, null will be returned.

newInstance

public static java.lang.Object newInstance(java.lang.String className,
                                           java.lang.Class supposedSuperclass,
                                           boolean warning)
Creates a new Instance of the specified class with error checking. Use this when the new object created should be a subclass of some known class.

Parameters:
className - the class to create a new instance of
supposedSuperclass - The class of the required superclass for the new class
warning - if true and the class can not be found, a warning will be printed
Returns:
a new instance of the class theClass. If there is an error, null will be returned.

newInstance

public static java.lang.Object newInstance(java.lang.Class classObj,
                                           java.lang.Class supposedSuperclass,
                                           boolean warning)
Creates a new Instance of the specified class with error checking. Use this when the new object created should be a subclass of some known class.

Parameters:
classObj - the class to create a new instance of
supposedSuperclass - The class of the required superclass for the new class
warning - if true and the class can not be found, a warning will be printed
Returns:
a new instance of the class theClass. If there is an error, null will be returned.

newInstance

public static java.lang.Object newInstance(java.lang.Class theClass)
Creates a new instance of the specified class. If the class theClass is derived from BaseObject and you want the new instance to be properly initialized, you should call BaseObject.New() instead. If the instance can not be created, a warning will be printed.

Parameters:
theClass - the class to create a new instance of
Returns:
a new instance of the class theClass. If there is an error, null will be returned.
See Also:
classForName(java.lang.String)

stripPackageFromClassName

public static java.lang.String stripPackageFromClassName(java.lang.String className)
Strips any package specifiers from the given class name. For instance, "java.util.List" will become "List".

Parameters:
className - class name to strip
Returns:
the class name without the package prefix

stripClassFromClassName

public static java.lang.String stripClassFromClassName(java.lang.String className)
Find the package specifier for a given class name. For instance, "java.util.List" will become "java.util".

Parameters:
className - class name to strip
Returns:
the package specifier for the given className

invokeStaticMethod

public static java.lang.Object invokeStaticMethod(java.lang.String className,
                                                  java.lang.String methodName)
Invokes the specified static method of the specified class. If thrown, NoSuchMethodException, ClassNotFoundException, InvocationTargetException, and IllegalAccessException are silently caught and null is returned.

Parameters:
className - the name of the class to invoke the static method on
methodName - the name of the method to call
Returns:
the result of the method invocation, or if there was an exception while trying to find or invoke the method, null is returned.
See Also:
Method.invoke(java.lang.Object, java.lang.Object...)

invokeStaticMethod

public static java.lang.Object invokeStaticMethod(java.lang.String className,
                                                  java.lang.String methodName,
                                                  java.lang.Class[] paramTypes,
                                                  java.lang.Object[] args)
Invokes the specified static method of the specified class.

Parameters:
className - the name of the class to invoke the static method on
methodName - the name of the method to call
paramTypes - an array of Class types that *exactly* match the signature of the method being invoked.
args - an array of Object arguments to the method
Returns:
the result of the method invocation, or if there was an exception while trying to find or invoke the method, null is returned.
See Also:
Method.invoke(java.lang.Object, java.lang.Object...)

getDeclaredFields

public static java.lang.reflect.Field[] getDeclaredFields(java.lang.Class clazz)
ClassUtil.getDeclaredFields returns all the declared fields for the class and it's superclasses. The standard Class.getFields only returns public fields for the class and it's superclases, and the Class.getDeclaredFields returns all fields, public and private, but doesn't return superclass fields. This method does both private fields and superclass fields.

Parameters:
clazz - the Class to discover the fields of
Returns:
an array of all public and private fields accessable from this class

typeToVMType

public static java.lang.String typeToVMType(java.lang.String type)
Convert from a class name into an internal java representation of that class String int -> I java.lang.String -> Ljava.lang.String;



AribaWeb User Interface Development Framework
Copyright © 2000-2014 Ariba, Inc. All Rights Reserved.