com.legstar.coxb.util
Class ClassUtil

java.lang.Object
  extended by com.legstar.coxb.util.ClassUtil

public final class ClassUtil
extends Object

A helper class for dynamic class loading.


Nested Class Summary
static class ClassUtil.ClassName
          A simple vehicle for class name and package name.
 
Method Summary
static Object addComplexIndexedProperty(Object objectFactory, Object parentObject, String jaxbTypeName, int index)
          This creates a new complex property item, identified by an index in a parent object array.
static Object addComplexProperty(Object objectFactory, Object parentObject, String jaxbName, String jaxbTypeName)
          This method creates a new instance of a property type in a parent object using a JAXB ObjectFactory.
static void addSimpleIndexedProperty(Object parentObject, int index, Object value)
          This creates a new simple property item, identified by an index in a parent object array.
static Method getCreatorMethod(Object objectFactory, String jaxbTypeName)
          The ObjectFactory exports a create method for each complex type in the object tree.
static Method getGetterMethod(Object parentObject, String getterPrefix, String jaxbName)
          Returns a jaxb property getter method.
static String getGetterName(String getterPrefix, String fieldName)
          Build a conventional bean getter method name from a field name.
static String getGetterPrefix(Class<?> fieldType, int maxOccurs)
          Evaluates the getter method prefix for a field, based on the field type.
static Method getSetterMethod(Object parentObject, String jaxbName, Class<?> jaxbType)
          Returns a jaxb property setter method.
static String getSetterMethodName(String jaxbName)
          Construct a setter method name for a JAXB property.
static Object invokeGetIndexedProperty(Object parentObject, int index)
          This method gets an indexed property object from a parent array object.
static Object invokeGetProperty(Object parentObject, String jaxbName)
          Deprecated. use invokeGetProperty(Object parentObject, String jaxbName, Class < ? > jaxbType)
static Object invokeGetProperty(Object parentObject, String jaxbName, Class<?> jaxbType, int maxOccurs)
          This method gets a property object from a parent object.
static void invokeSetProperty(Object parentObject, String jaxbName, Object value, Class<?> javaType)
          This method sets a property from a parent object to a given value.
static boolean isEnum(String type)
          This will determine if a type name for a simple element is one of java's native types and assume it is an enum otherwise.
static Class<?> loadClass(String qualifiedClassName)
          Rather than using the Class.forName mechanism first, this uses Thread.getContextClassLoader instead.
static Class<?> loadClass(String packageName, String className)
          Rather than using the Class.forName mechanism first, this uses Thread.getContextClassLoader instead.
static Object newObject(String qualifiedClassName)
          Dynamically create an instance of a java class.
static Object newObject(String packageName, String className)
          Dynamically create an instance of a java class.
static ClassUtil.ClassName toClassName(String qualifiedClassName)
          Separate package name and class name from a qualified class name.
static String toPropertyType(String genericType)
          Helper method to extract the type name as used by JAXB create methods.
static String toQualifiedClassName(String packageName, String className)
          Qualifies a class name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

loadClass

public static Class<?> loadClass(String packageName,
                                 String className)
                          throws ClassNotFoundException
Rather than using the Class.forName mechanism first, this uses Thread.getContextClassLoader instead. In a Servlet context such as Tomcat, this allows JAXB classes for instance to be loaded from the web application (webapp) location while this code might have been loaded from shared/lib. If Thread.getContextClassLoader fails to locate the class then we give a last chance to Class.forName.

Parameters:
packageName - the package containing the class
className - the class name
Returns:
the class
Throws:
ClassNotFoundException - if class is not accessible from any class loader

loadClass

public static Class<?> loadClass(String qualifiedClassName)
                          throws ClassNotFoundException
Rather than using the Class.forName mechanism first, this uses Thread.getContextClassLoader instead. In a Servlet context such as Tomcat, this allows JAXB classes for instance to be loaded from the web application (webapp) location while this code might have been loaded from shared/lib. If Thread.getContextClassLoader fails to locate the class then we give a last chance to Class.forName.

Parameters:
qualifiedClassName - the class name to load
Returns:
the class
Throws:
ClassNotFoundException - if class is not accessible from any class loader

newObject

public static Object newObject(String packageName,
                               String className)
                        throws ClassLoadingException
Dynamically create an instance of a java class. This assumes the classes are available on the classpath and returns a new instance.

Parameters:
packageName - the package containing the class
className - the class name
Returns:
a java object
Throws:
ClassLoadingException - if object cannot be instantiated

newObject

public static Object newObject(String qualifiedClassName)
                        throws ClassLoadingException
Dynamically create an instance of a java class. This assumes the classes are available on the classpath and returns a new instance.

Parameters:
qualifiedClassName - the qualified class name
Returns:
a java object
Throws:
ClassLoadingException - if object cannot be instantiated

toQualifiedClassName

public static String toQualifiedClassName(String packageName,
                                          String className)
Qualifies a class name.

Parameters:
packageName - the package name, null if none
className - the class name
Returns:
a qualified class name

toClassName

public static ClassUtil.ClassName toClassName(String qualifiedClassName)
Separate package name and class name from a qualified class name.

Parameters:
qualifiedClassName - qualified class name
Returns:
a structure with both parts

getCreatorMethod

public static Method getCreatorMethod(Object objectFactory,
                                      String jaxbTypeName)
                               throws ClassMethodException
The ObjectFactory exports a create method for each complex type in the object tree. Returns a jaxb object creator method.

Parameters:
objectFactory - JAXB Objectfactory
jaxbTypeName - the jaxb object type name
Returns:
the creator method
Throws:
ClassMethodException - if getter method does not exist

getGetterMethod

public static Method getGetterMethod(Object parentObject,
                                     String getterPrefix,
                                     String jaxbName)
                              throws ClassMethodException
Returns a jaxb property getter method.

Parameters:
parentObject - JAXB Object owning the property
getterPrefix - the getter method prefix (get or is)
jaxbName - the property name to get
Returns:
the getter method
Throws:
ClassMethodException - if getter method does not exist

getGetterName

public static String getGetterName(String getterPrefix,
                                   String fieldName)
Build a conventional bean getter method name from a field name.

Parameters:
getterPrefix - the getter method name prefix (get or is)
fieldName - the field name
Returns:
a getter method name

getSetterMethod

public static Method getSetterMethod(Object parentObject,
                                     String jaxbName,
                                     Class<?> jaxbType)
                              throws ClassMethodException
Returns a jaxb property setter method.

Parameters:
parentObject - JAXB Object owning the property
jaxbName - the property name to get
jaxbType - the property type
Returns:
the getter method
Throws:
ClassMethodException - if getter method does not exist

getSetterMethodName

public static String getSetterMethodName(String jaxbName)
Construct a setter method name for a JAXB property.

Parameters:
jaxbName - the JAXB property name
Returns:
the setter method name

toPropertyType

public static String toPropertyType(String genericType)
Helper method to extract the type name as used by JAXB create methods.

Property type (as returned from Field.getGenericType()) can take 4 forms:

Furthermore, primitive types are returned as their Object equivalent.

Parameters:
genericType - as returned from hostField.getGenericType()
Returns:
a normalized string representation of the type

invokeGetProperty

public static Object invokeGetProperty(Object parentObject,
                                       String jaxbName)
                                throws ClassInvokeException
Deprecated. use invokeGetProperty(Object parentObject, String jaxbName, Class < ? > jaxbType)

This method gets a property object from a parent object.

Parameters:
parentObject - JAXB Object owning the property
jaxbName - the property name to get
Returns:
instance of the property as returned from a getter method
Throws:
ClassInvokeException - if property cannot be accessed

invokeGetProperty

public static Object invokeGetProperty(Object parentObject,
                                       String jaxbName,
                                       Class<?> jaxbType,
                                       int maxOccurs)
                                throws ClassInvokeException
This method gets a property object from a parent object.

Parameters:
parentObject - JAXB Object owning the property
jaxbName - the property name to get
maxOccurs - the maximum number of occurrences
Returns:
instance of the property as returned from a getter method
Throws:
ClassInvokeException - if property cannot be accessed

getGetterPrefix

public static String getGetterPrefix(Class<?> fieldType,
                                     int maxOccurs)
Evaluates the getter method prefix for a field, based on the field type.

Parameters:
fieldType - the field type
maxOccurs - the maximum number of occurrences
Returns:
the prefix to form a getter method

invokeSetProperty

public static void invokeSetProperty(Object parentObject,
                                     String jaxbName,
                                     Object value,
                                     Class<?> javaType)
                              throws ClassInvokeException
This method sets a property from a parent object to a given value.

Parameters:
parentObject - JAXB Object owning the property
jaxbName - the property name to set
value - value to set property to
javaType - the java class the value belongs to
Throws:
ClassInvokeException - if property cannot be set

addComplexProperty

public static Object addComplexProperty(Object objectFactory,
                                        Object parentObject,
                                        String jaxbName,
                                        String jaxbTypeName)
                                 throws ClassInvokeException
This method creates a new instance of a property type in a parent object using a JAXB ObjectFactory. Given the property type name, the method constructs an ObjectFactory call for this property type. This method works for complex types only since the ObjectFactory will have create methods only for those.

Parameters:
objectFactory - a JAXB ObjectFactory
parentObject - instance of the JAXB parent class
jaxbName - complex property name
jaxbTypeName - complex property type name
Returns:
an instance of a JAXB object
Throws:
ClassInvokeException - if JAXB Object cannot be instanciated

invokeGetIndexedProperty

public static Object invokeGetIndexedProperty(Object parentObject,
                                              int index)
                                       throws ClassInvokeException
This method gets an indexed property object from a parent array object. Assumes the parent inherits from java.util.List and implement a get(index) method.

Parameters:
parentObject - instance of the JAXB parent list class
index - item index in list
Returns:
an instance of a JAXB object
Throws:
ClassInvokeException - if JAXB Object cannot be returned

addComplexIndexedProperty

public static Object addComplexIndexedProperty(Object objectFactory,
                                               Object parentObject,
                                               String jaxbTypeName,
                                               int index)
                                        throws ClassInvokeException
This creates a new complex property item, identified by an index in a parent object array. This method assumes the items are complex objects for which the JAXB ObjectFactory implements a create method.

Parameters:
objectFactory - a JAXB ObjectFactory
parentObject - instance of the JAXB parent class
jaxbTypeName - complex property type name
index - item index in list
Returns:
an instance of a JAXB object
Throws:
ClassInvokeException - if JAXB Object cannot be instanciated

addSimpleIndexedProperty

public static void addSimpleIndexedProperty(Object parentObject,
                                            int index,
                                            Object value)
                                     throws ClassInvokeException
This creates a new simple property item, identified by an index in a parent object array.

Parameters:
parentObject - instance of the JAXB parent list class
index - item index in list
value - value to set item to
Throws:
ClassInvokeException - if item cannot be set

isEnum

public static boolean isEnum(String type)
This will determine if a type name for a simple element is one of java's native types and assume it is an enum otherwise.

Parameters:
type - the element java type
Returns:
true if it should be considered an enum


Copyright © 2015 LegSem. All Rights Reserved.