public final class ClassGenericsUtil
extends java.lang.Object
Due to the way generics are implemented - via erasure - type safety cannot be guaranteed properly at compile time here. These classes collect such cases using helper functions, so that we have to suppress these warnings only in one place.
Note that many of these situations are still type safe, i.e. an empty
array of List<List<?>>
can indeed be cast into a
List<List<Whatever>>
.
The only one potentially unsafe is instantiateGenerics(java.lang.Class<?>, java.lang.String)
, since we
can't verify that the runtime type 'type' adhers to the compile time
restriction T. When T is not generic, such a check is possible, and then the
developer should use instantiate(java.lang.Class<T>, java.lang.String)
instead.
Modifier and Type | Field and Description |
---|---|
(package private) static java.lang.ClassLoader |
CLASSLOADER
Class loader.
|
static java.lang.String |
FACTORY_METHOD_NAME
Name for a static "parameterize" factory method.
|
private static Logging |
LOG
Static logger to use.
|
Modifier | Constructor and Description |
---|---|
private |
ClassGenericsUtil()
Fake Constructor.
|
Modifier and Type | Method and Description |
---|---|
static Parameterizer |
getParameterizer(java.lang.Class<?> c)
Get a parameterizer for the given class.
|
static <T> T |
instantiate(java.lang.Class<T> type,
java.lang.String className)
Returns a new instance of the given type for the specified className.
|
static <T> T |
instantiateGenerics(java.lang.Class<?> type,
java.lang.String className)
Returns a new instance of the given type for the specified className.
|
static <T> T |
instantiateLowlevel(java.lang.Class<? extends T> clz)
Instantiate the first available implementation of an interface.
|
static <C> C |
parameterizeOrAbort(java.lang.Class<?> c,
Parameterization config)
Force parameterization method.
|
static <C> C |
tryInstantiate(java.lang.Class<C> r,
java.lang.Class<?> c,
Parameterization config)
Instantiate a parameterizable class.
|
static <D,T extends D> |
uglyCastIntoSubclass(java.lang.Class<D> cls)
Cast the (erased) generics onto a class.
|
static <BASE,FROM extends BASE,TO extends BASE> |
uglyCrossCast(java.lang.Class<FROM> cls,
java.lang.Class<BASE> base)
This class performs an ugly cast, from
Class<F> to
Class<T> , where both F and T need to extend B. |
private static final Logging LOG
static final java.lang.ClassLoader CLASSLOADER
public static final java.lang.String FACTORY_METHOD_NAME
private ClassGenericsUtil()
public static <T> T instantiate(java.lang.Class<T> type, java.lang.String className) throws ClassInstantiationException
If the Class for className is not found, the instantiation is tried using the package of the given type as package of the given className.
T
- Class type for compile time type checkingtype
- desired Class type of the Object to retrieveclassName
- name of the class to instantiateClassInstantiationException
- When a class cannot be instantiated.public static <T> T instantiateGenerics(java.lang.Class<?> type, java.lang.String className) throws ClassInstantiationException
If the class for className is not found, the instantiation is tried using the package of the given type as package of the given className.
This is a weaker type checked version of "instantiate(java.lang.Class<T>, java.lang.String)
" for use
with generics.
T
- Class type for compile time type checkingtype
- desired Class type of the Object to retrieveclassName
- name of the class to instantiateClassInstantiationException
- When a class cannot be instantiated.public static Parameterizer getParameterizer(java.lang.Class<?> c)
c
- Classpublic static <C> C tryInstantiate(java.lang.Class<C> r, java.lang.Class<?> c, Parameterization config) throws ClassInstantiationException
Parameterization.descend(java.lang.Object)
!C
- base typer
- Base (restriction) classc
- Class to instantiateconfig
- Configuration to use for instantiation.ClassInstantiationException
- When a class cannot be instantiated.public static <C> C parameterizeOrAbort(java.lang.Class<?> c, Parameterization config)
Please use this only in "runner" classes such as unit tests, since the error handling is not very flexible.
C
- Typec
- Class to instantiateconfig
- Parameterspublic static <D,T extends D> java.lang.Class<T> uglyCastIntoSubclass(java.lang.Class<D> cls)
Note: this function is a hack - notice that it would allow you to up-cast any class! Still it is preferable to have this cast in one place than in dozens without any explanation.
The reason this is needed is the following: There is no
Class<Set<String>>.class
.
This method allows you to do
Class<Set<String>> setclass = uglyCastIntoSubclass(Set.class);
We can't type check at runtime, since we don't have T.
D
- Base typeT
- Supertypecls
- Class typecls
parameter, but cast to Class<T>
public static <BASE,FROM extends BASE,TO extends BASE> java.lang.Class<TO> uglyCrossCast(java.lang.Class<FROM> cls, java.lang.Class<BASE> base)
Class<F>
to
Class<T>
, where both F and T need to extend B.
The restrictions are there to avoid misuse of this cast helper.
While this sounds really ugly, the common use case will be something like
BASE = Class<Database> FROM = Class<Database> TO = Class<Database<V>>i.e. the main goal is to add missing Generics to the compile time type.
BASE
- Base typeTO
- Destination typeFROM
- Source typecls
- Class to be castbase
- Base class for type checking.public static <T> T instantiateLowlevel(java.lang.Class<? extends T> clz)
Only supports public and parameterless constructors.
clz
- Class to instantiate.Copyright © 2019 ELKI Development Team. License information.