com.emarsys.ecommon.util
Class Classes

java.lang.Object
  extended by com.emarsys.ecommon.util.Classes

public class Classes
extends java.lang.Object

Utility for handling Classes.

Author:
Michael "kULO" Kulovits

Nested Class Summary
static class Classes.Instantiator<C,E extends java.lang.Exception>
          Classes.Instantiator provides a sane interface for dynamic instantiation of classes by implementing the builder pattern.
 
Constructor Summary
Classes()
           
 
Method Summary
static
<S,T extends S>
T
cast(S castee, java.lang.Class<T> concreteClass)
          Provides an asserted (see Assertions) dynamic cast.
static java.lang.Class<?> forName(java.lang.String className)
           If you're totally sure that the Class with the passed name exists then use this method which will "handle" the possibly thrown checked ClassNotFoundException by throwing an unchecked AssertionError.
static java.lang.String getName(java.lang.Class<?> clazz)
          A null pointer save delegate to Class.getName().
static java.lang.String getNames(java.lang.Class<?>... classes)
          A null pointer save delegate to Class.getName(), intended for arrays.
static java.lang.String getSimpleName(java.lang.Class<?> clazz)
          A null pointer save delegate to Class.getSimpleName().
static java.lang.String getSimpleNames(java.lang.Class<?>... classes)
          A null pointer save delegate to Class.getSimpleName(), intended for arrays.
static
<C,E extends java.lang.Exception>
Classes.Instantiator<C,E>
instantiator(java.lang.Class<C> type, java.lang.Class<E> exceptionType)
          Returns a new Classes.Instantiator in order create a new instance of the passed type and in case of an error throw an exception of the passed exceptionType.
static
<E extends java.lang.Exception,S>
S
newInstance(java.lang.String className, java.lang.Class<S> superType, java.lang.Class<E> exceptionClass)
           Creates a new instance of the Class specified by the passed classname and casts it to the passed (super) type.
static
<E extends java.lang.Exception,S>
S
newInstance(java.lang.String className, java.lang.Class<S> superType, java.lang.Class<E> exceptionClass, java.lang.Class<?>[] constructorParamTypes, java.lang.Object[] constructorParams)
           Creates a new instance of the Class specified by the passed classname using the specified constructor and casts it to the passed (super) type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Classes

public Classes()
Method Detail

instantiator

public static <C,E extends java.lang.Exception> Classes.Instantiator<C,E> instantiator(java.lang.Class<C> type,
                                                                                       java.lang.Class<E> exceptionType)
Returns a new Classes.Instantiator in order create a new instance of the passed type and in case of an error throw an exception of the passed exceptionType.

Type Parameters:
C -
Parameters:
type -
Returns:

newInstance

public static <E extends java.lang.Exception,S> S newInstance(java.lang.String className,
                                                              java.lang.Class<S> superType,
                                                              java.lang.Class<E> exceptionClass)
                     throws E extends java.lang.Exception,
                            java.lang.IllegalArgumentException

Creates a new instance of the Class specified by the passed classname and casts it to the passed (super) type.

In case of an error during the instantiation process any Exception (class not found, instantiation not possible, etc.) will be wrapped into an instance of the passed exception class.
Therefore this method is very suitable for creating instances of a dynamically specified (i.e. at runtime) class without having to bother with the horde of exceptions that are thrown by java's reflexion classes.

NOTE that the class to be instanced must provide a public no-args-constructor and be of the passed (super) type; further the passed exception class has to provide a public constructor of the form E( String msg, Throwable th ) otherwise an IllegalArgumentException will be thrown.

Throws:
E - - if the specified class could not be instantiated and/or cast to the passed super type
{@link - IllegalArgumentException} - if it was not possible to create the specified exception instance in case of an error
E extends java.lang.Exception
java.lang.IllegalArgumentException

newInstance

public static <E extends java.lang.Exception,S> S newInstance(java.lang.String className,
                                                              java.lang.Class<S> superType,
                                                              java.lang.Class<E> exceptionClass,
                                                              java.lang.Class<?>[] constructorParamTypes,
                                                              java.lang.Object[] constructorParams)
                     throws E extends java.lang.Exception,
                            java.lang.IllegalArgumentException

Creates a new instance of the Class specified by the passed classname using the specified constructor and casts it to the passed (super) type.

In case of an error during the instantiation process any Exception (class not found, instantiation not possible, etc.) will be wrapped into an instance of the passed exception class.
Therefore this method is very suitable for creating instances of a dynamically specified (i.e. at runtime) class without having to bother with the horde of exceptions that are thrown by java's reflexion classes.

NOTE that the class to be instanced must provide a public constructor with the passed abstract parameter types and be of the passed (super) type; further the passed exception class has to provide a public constructor of the form E( String msg, Throwable th ) otherwise an IllegalArgumentException will be thrown.

If the passed constructor parameter types are null or emtpy, i.e. if the default constructor should be called then the call will be forwarded to newInstance(String, Class, Class).

Type Parameters:
E - - the concrete type of the Exception to be thrown if the instantiation is not possible
S - - the concrete (super) type of the instance to be created
Parameters:
className - - the name of the class to be created
superType - - the (super) Class of the instance to be created
exceptionClass - - the class of the Exception to be thrown if the instantiation is not possible
constructorParamTypes - - the abstract parameter types of the Constructor of the class to be created
constructorParams - - the concrete constructor parameters for the instance to be created
Returns:
always a valid instance of class , never null
Throws:
E - - if the specified class could not be instantiated and/or cast to the passed super type
{@link - IllegalArgumentException} - if it was not possible to create the specified exception instance in case of an error
E extends java.lang.Exception
java.lang.IllegalArgumentException

cast

public static <S,T extends S> T cast(S castee,
                                     java.lang.Class<T> concreteClass)
Provides an asserted (see Assertions) dynamic cast. Use this method only if you are sure(!) that the passed castee object is an instance of the concrete class. If this is not the case an AssertionError will be thrown which will likely kill your application.

Type Parameters:
S - - the declared super type of the castee
T - - the concrete sub type to be casted to
Parameters:
castee - - the object to be casted
concreteClass - - the concrete Class to be casted to
Returns:
the passed castee instance casted to the passed concrete class.
See Also:
Class.cast(Object)

getSimpleName

public static java.lang.String getSimpleName(java.lang.Class<?> clazz)
A null pointer save delegate to Class.getSimpleName().

Parameters:
clazz -
Returns:
the String "null" if the passed class is null, its simple name otherwise, but never null.
See Also:
Class.getSimpleName()

getSimpleNames

public static java.lang.String getSimpleNames(java.lang.Class<?>... classes)
A null pointer save delegate to Class.getSimpleName(), intended for arrays.

Parameters:
classes -
Returns:
a String that contains the simple name of every class passed ("null" if the class is null, its name otherwise) which starts with '[' and ends with ']'
See Also:
#getName()

getName

public static java.lang.String getName(java.lang.Class<?> clazz)
A null pointer save delegate to Class.getName().

Parameters:
clazz -
Returns:
the String "null" if the passed class is null, its name otherwise, but never null.
See Also:
Class.getName()

getNames

public static java.lang.String getNames(java.lang.Class<?>... classes)
A null pointer save delegate to Class.getName(), intended for arrays.

Parameters:
classes -
Returns:
a String that contains the name of every class passed ("null" if the class is null, its name otherwise) which starts with '[' and ends with ']'
See Also:
#getName()

forName

public static java.lang.Class<?> forName(java.lang.String className)

If you're totally sure that the Class with the passed name exists then use this method which will "handle" the possibly thrown checked ClassNotFoundException by throwing an unchecked AssertionError.

Parameters:
className - - the name of the class to be instantiated
Returns:
always a valid Class instance, never null


Copyright © 2010 emarsys AG. All Rights Reserved.