org.jpox.util
Class ClassUtils

java.lang.Object
  extended byorg.jpox.util.ClassUtils

public class ClassUtils
extends java.lang.Object

Utilities for handling classes. These are to supplement the methods provided by the Class object.

Version:
$Revision: 1.27 $

Field Summary
protected static Localiser LOCALISER
          Localisation utility for output messages
 
Constructor Summary
ClassUtils()
           
 
Method Summary
static java.lang.Boolean booleanValueOf(boolean value)
          Utility to perform the same function as the JDK 1.4 method Boolean.valueOf(boolean), so we have no real dependency on JDK 1.4.
static boolean classesAreDescendents(ClassLoaderResolver clr, java.lang.String class_name_1, java.lang.String class_name_2)
          Method to check if 2 classes are direct descendents.
static java.lang.String createFullClassName(java.lang.String pkg_name, java.lang.String cls_name)
          Utility to create the full class name given the package and class name.
static void dumpClassInformation(java.lang.Class cls)
          Utility to use Reflection to dump out the details of a class.
static java.util.Collection getClassFilesForDirectory(java.io.File dir, boolean normal_classes, boolean inner_classes)
          Method to return the class files below the specified directory.
static java.lang.String getClassnameForFilename(java.lang.String filename, java.lang.String rootfilename)
          Utility to get the Class name given the file name and the file name of the root of the package structure.
static java.lang.reflect.Constructor getConstructorWithArguments(java.lang.Class cls, java.lang.Class[] types)
          Convenience method to return the constructor of the passed class that accepts the supplied argument types.
static java.lang.reflect.Field getFieldForClass(java.lang.Class cls, java.lang.String fieldName)
          Obtain a field from a class or superclasses using reflection.
static java.lang.String getFieldNameForJavaBeanGetter(java.lang.String methodName)
          Generate a field name for JavaBeans compatible getter method
static java.lang.String getJavaBeanGetterName(java.lang.String fieldName, boolean isBoolean)
          Generate a JavaBeans compatible getter name
static java.lang.String getJavaBeanSetterName(java.lang.String fieldName)
          Generate a JavaBeans compatible setter name
static java.lang.String getJavaLangClassForType(java.lang.String type)
          Convenience method to return the passed type as a java.lang type wherever possible.
static java.lang.reflect.Method getMethodForClass(java.lang.Class cls, java.lang.String methodName, java.lang.Class[] argtypes)
          Obtain a method from a class or superclasses using reflection
static java.lang.reflect.Method getMethodWithArgument(java.lang.Class cls, java.lang.String methodName, java.lang.Class argType)
          Obtain a method from a class or superclasses using reflection.
static java.lang.Class getPrimitiveTypeForType(java.lang.Class type)
          Method to return the primitive equivalent of the specified type (if any).
static java.util.Collection getSuperclasses(java.lang.Class the_class)
          Method to return the superclasses for a class.
static java.util.Collection getSuperinterfaces(java.lang.Class the_class)
          Method to return the superinterfaces for a class.
static java.lang.Class getWrapperTypeForPrimitiveType(java.lang.Class type)
          Convenience method to return the object wrapper type for a primitive type.
static boolean hasDefaultConstructor(java.lang.Class the_class)
          Method to check for a default constructor on a class.
static boolean isDetachable(java.lang.Object object)
          Utility method to check if the specified object is Detachable.
static boolean isInnerClass(java.lang.String class_name)
          Method to check whether a classname is for an inner class.
static boolean isPersistenceCapable(java.lang.Object object)
          Utility method to check if the specified object is Persistence Capable.
static boolean isPersistenceCapableClass(java.lang.Class cls)
          Utility method to check if the specified class is Persistence Capable.
static boolean isPersistenceCapableClass(java.lang.ClassLoader loader, java.lang.String className)
          Utility method to check if the specified class is PersistenceCapable.
static boolean isPrimitiveWrapperType(java.lang.String typeName)
          Convenience method to return if the passed type (name) is a primitive wrapper type.
static java.lang.Object newInstance(java.lang.Class type, java.lang.Class[] parameterTypes, java.lang.Object[] parameters)
          Accessor for a new instance of an object.
static boolean typesAreCompatible(java.lang.Class cls1, java.lang.Class cls2)
          Convenience method to return if two types are compatible.
static boolean typesAreCompatible(java.lang.Class cls1, java.lang.String clsName2, ClassLoaderResolver clr)
          Convenience method to return if two types are compatible.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOCALISER

protected static final Localiser LOCALISER
Localisation utility for output messages

Constructor Detail

ClassUtils

public ClassUtils()
Method Detail

newInstance

public static java.lang.Object newInstance(java.lang.Class type,
                                           java.lang.Class[] parameterTypes,
                                           java.lang.Object[] parameters)
Accessor for a new instance of an object. Uses reflection to generate the instance using the passed constructor parameter arguments.

Parameters:
type - Type of object (the class).
parameterTypes - Classes of params for the constructor
parameters - The parameters for the constructor
Returns:
The object

getConstructorWithArguments

public static java.lang.reflect.Constructor getConstructorWithArguments(java.lang.Class cls,
                                                                        java.lang.Class[] types)
Convenience method to return the constructor of the passed class that accepts the supplied argument types. Allows for primitive to primitive wrapper conversion. Typically used by the JDOQL ResultClass mapping process.

Parameters:
cls - The class
types - The constructor argument types
Returns:
The constructor

getMethodWithArgument

public static java.lang.reflect.Method getMethodWithArgument(java.lang.Class cls,
                                                             java.lang.String methodName,
                                                             java.lang.Class argType)
Obtain a method from a class or superclasses using reflection. The method will have the specified name and will take a single argument. Allows for the argument type being primitive or its associated wrapper.

Parameters:
cls - the class to find the declared fields and populate the map
methodName - the method name to find
argType - the argument type

getMethodForClass

public static java.lang.reflect.Method getMethodForClass(java.lang.Class cls,
                                                         java.lang.String methodName,
                                                         java.lang.Class[] argtypes)
Obtain a method from a class or superclasses using reflection

Parameters:
cls - the class where to look for the method
methodName - the method name to find
argtypes - the classes argument of the method
Returns:
The Method

isPersistenceCapableClass

public static boolean isPersistenceCapableClass(java.lang.ClassLoader loader,
                                                java.lang.String className)
Utility method to check if the specified class is PersistenceCapable.

Parameters:
loader - The loader to load the class from
className - Name of the class
Returns:
Whether it is PersistenceCapable

isPersistenceCapableClass

public static boolean isPersistenceCapableClass(java.lang.Class cls)
Utility method to check if the specified class is Persistence Capable. Checks that it implements PersistenceCapable.

Parameters:
cls - The class to check
Returns:
Whether the class is Persistence Capable

isPersistenceCapable

public static boolean isPersistenceCapable(java.lang.Object object)
Utility method to check if the specified object is Persistence Capable.

Parameters:
object - The object to check
Returns:
Whether the object is Persistence Capable

isDetachable

public static boolean isDetachable(java.lang.Object object)
Utility method to check if the specified object is Detachable.

Parameters:
object - The object to check
Returns:
Whether the object is Detachable

booleanValueOf

public static java.lang.Boolean booleanValueOf(boolean value)
Utility to perform the same function as the JDK 1.4 method Boolean.valueOf(boolean), so we have no real dependency on JDK 1.4. Please remove when our minimum requirement is JDK1.4 or above.

Parameters:
value - The value
Returns:
The Boolean value

getClassnameForFilename

public static java.lang.String getClassnameForFilename(java.lang.String filename,
                                                       java.lang.String rootfilename)
Utility to get the Class name given the file name and the file name of the root of the package structure.

Parameters:
filename - The filename of the class file
rootfilename - The filename of the root of the package structure
Returns:
The Class name

getClassFilesForDirectory

public static java.util.Collection getClassFilesForDirectory(java.io.File dir,
                                                             boolean normal_classes,
                                                             boolean inner_classes)
Method to return the class files below the specified directory.

Parameters:
dir - The directory
normal_classes - Whether to include normal classes
inner_classes - Whether to include inner classes
Returns:
The class files (Collection of File objects).

isInnerClass

public static boolean isInnerClass(java.lang.String class_name)
Method to check whether a classname is for an inner class. Currently checks for the presence of $ in the name.

Parameters:
class_name - The class name
Returns:
Whether it is an inner class

hasDefaultConstructor

public static boolean hasDefaultConstructor(java.lang.Class the_class)
Method to check for a default constructor on a class. Particular relevance for JDO is the requirement for a default constructor on all Persistence-Capable classes. Doesn't check superclasses for the default constructor.

Parameters:
the_class - The class
Returns:
Whether it has a default constructor

getSuperclasses

public static java.util.Collection getSuperclasses(java.lang.Class the_class)
Method to return the superclasses for a class. The superclasses will be ordered.

Parameters:
the_class - The class
Returns:
The superclass of this class.

getSuperinterfaces

public static java.util.Collection getSuperinterfaces(java.lang.Class the_class)
Method to return the superinterfaces for a class. The superinterfaces will be ordered.

Parameters:
the_class - The class
Returns:
The superinterfaces of this class.

getFieldForClass

public static java.lang.reflect.Field getFieldForClass(java.lang.Class cls,
                                                       java.lang.String fieldName)
Obtain a field from a class or superclasses using reflection.

Parameters:
cls - the class to find the field from
fieldName - the field name to find
Returns:
The Field

getWrapperTypeForPrimitiveType

public static java.lang.Class getWrapperTypeForPrimitiveType(java.lang.Class type)
Convenience method to return the object wrapper type for a primitive type.

Parameters:
type - The primitive type
Returns:
The object wrapper type for this primitive

getPrimitiveTypeForType

public static java.lang.Class getPrimitiveTypeForType(java.lang.Class type)
Method to return the primitive equivalent of the specified type (if any). Returns null if there is no primitive equivalent.

Parameters:
type - The type
Returns:
The primitive equivalent.

isPrimitiveWrapperType

public static boolean isPrimitiveWrapperType(java.lang.String typeName)
Convenience method to return if the passed type (name) is a primitive wrapper type.

Parameters:
typeName - Name of the type
Returns:
Whether it is a primitive wrapper

typesAreCompatible

public static boolean typesAreCompatible(java.lang.Class cls1,
                                         java.lang.String clsName2,
                                         ClassLoaderResolver clr)
Convenience method to return if two types are compatible. Returns true if both types are primitive/wrappers and are of the same type. Returns true if clsName2 is the same or a subclass of cls1. Otherwise returns false;

Parameters:
cls1 - First class
clsName2 - Name of the second class
clr - ClassLoader resolver to use
Returns:
Whether they are compatible

typesAreCompatible

public static boolean typesAreCompatible(java.lang.Class cls1,
                                         java.lang.Class cls2)
Convenience method to return if two types are compatible. Returns true if both types are primitive/wrappers and are of the same type. Returns true if cls2 is the same or a subclass of cls1. Otherwise returns false;

Parameters:
cls1 - First class
cls2 - Second class
Returns:
Whether they are compatible

createFullClassName

public static java.lang.String createFullClassName(java.lang.String pkg_name,
                                                   java.lang.String cls_name)
Utility to create the full class name given the package and class name. Some examples
 packageName=test className=Test, returns result=test.Test
 packageName=test className=test1.Test, returns result=test1.Test
 packageName=<null> className=Test, returns result=Test
 packageName=<null> className=test1.Test, returns result=test1.Test
 

Parameters:
pkg_name - package name.
cls_name - class name.
Returns:
generated full class name.

getJavaLangClassForType

public static java.lang.String getJavaLangClassForType(java.lang.String type)
Convenience method to return the passed type as a java.lang type wherever possible. The passed type will be stripped of any package name and will be checked if it is a known java.lang class. This is used where the user has specified a class name for a collection or map element/key/value type and meant a java.lang class but didn't fully qualify it.

Parameters:
type - The type name
Returns:
The java.lang equivalent (or the input type if not possible)

classesAreDescendents

public static boolean classesAreDescendents(ClassLoaderResolver clr,
                                            java.lang.String class_name_1,
                                            java.lang.String class_name_2)
Method to check if 2 classes are direct descendents. So one of them is a superclass of the other.

Parameters:
clr - ClassLoaderResolver for loading the classes
class_name_1 - Name of first class
class_name_2 - Name of second class
Returns:
Whether they are direct descendents.

dumpClassInformation

public static void dumpClassInformation(java.lang.Class cls)
Utility to use Reflection to dump out the details of a class. Will list all superclasses, interfaces, methods and fields. Can be used, for example, in checking the methods adding by the enhancement process. The information is dumped out the JPOX GENERAL log.

Parameters:
cls - The class to dump out to the log

getJavaBeanGetterName

public static java.lang.String getJavaBeanGetterName(java.lang.String fieldName,
                                                     boolean isBoolean)
Generate a JavaBeans compatible getter name

Parameters:
fieldName - the field name
isBoolean - whether the field is primitive boolean type
Returns:
the getter name

getJavaBeanSetterName

public static java.lang.String getJavaBeanSetterName(java.lang.String fieldName)
Generate a JavaBeans compatible setter name

Parameters:
fieldName - the field name
Returns:
the setter name

getFieldNameForJavaBeanGetter

public static java.lang.String getFieldNameForJavaBeanGetter(java.lang.String methodName)
Generate a field name for JavaBeans compatible getter method

Parameters:
methodName - the method name
Returns:
the field name


Copyright © -2007 . All Rights Reserved.