|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.emarsys.ecommon.util.Classes
public class Classes
Utility for handling Class
es.
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
|
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
|
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
|
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
|
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 |
---|
public Classes()
Method Detail |
---|
public static <C,E extends java.lang.Exception> Classes.Instantiator<C,E> instantiator(java.lang.Class<C> type, java.lang.Class<E> exceptionType)
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.
C
- type
-
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.
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
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)
.
E
- - the concrete type of the Exception
to be thrown
if the instantiation is not possibleS
- - the concrete (super) type of the instance to be createdclassName
- - the name of the class to be createdsuperType
- - the (super) Class
of the instance to be createdexceptionClass
- - the class of the Exception
to be thrown
if the instantiation is not possibleconstructorParamTypes
- - the abstract parameter types of
the Constructor
of the class to be createdconstructorParams
- - the concrete constructor parameters
for the instance to be created
null
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
public static <S,T extends S> T cast(S castee, java.lang.Class<T> concreteClass)
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.
S
- - the declared super type of the casteeT
- - the concrete sub type to be casted tocastee
- - the object to be castedconcreteClass
- - the concrete Class
to be casted to
Class.cast(Object)
public static java.lang.String getSimpleName(java.lang.Class<?> clazz)
null
pointer save delegate to
Class.getSimpleName()
.
clazz
-
String
"null" if the passed class is null,
its simple name otherwise, but never null
.Class.getSimpleName()
public static java.lang.String getSimpleNames(java.lang.Class<?>... classes)
null
pointer save delegate to
Class.getSimpleName()
, intended for arrays.
classes
-
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 ']'#getName()
public static java.lang.String getName(java.lang.Class<?> clazz)
null
pointer save delegate to
Class.getName()
.
clazz
-
String
"null" if the passed class is null,
its name otherwise, but never null
.Class.getName()
public static java.lang.String getNames(java.lang.Class<?>... classes)
null
pointer save delegate to
Class.getName()
, intended for arrays.
classes
-
String
that contains the name of
every class passed ("null" if the class is null,
its name otherwise) which starts with '[' and ends with ']'#getName()
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
.
className
- - the name of the class to be instantiated
Class
instance, never null
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |