Class ClassGenericsUtil


  • public final class ClassGenericsUtil
    extends java.lang.Object
    Utilities for handling class instantiation, especially with respect to Java generics.

    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>>.

    Since:
    0.2
    Author:
    Erich Schubert
    • Field Summary

      Fields 
      Modifier and Type Field 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.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ClassGenericsUtil()
      Fake Constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static Parameterizer getParameterizer​(java.lang.Class<?> c)
      Get a parameterizer for the given class.
      static <T> T instantiateLowlevel​(java.lang.Class<? extends T> clz)
      Instantiate the first available implementation of an interface.
      static <T> T loadDefault​(java.lang.Class<T> clz, java.lang.String def)
      Try to load the default for a particular interface (must have a public default constructor, used for factories).
      static java.lang.Object make​(Parameterizer par, Parameterization config)
      Method to configure a class, then instantiate when the configuration step was successful.
      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>
      java.lang.Class<T>
      uglyCastIntoSubclass​(java.lang.Class<D> cls)
      Cast the (erased) generics onto a class.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOG

        private static final Logging LOG
        Static logger to use.
      • CLASSLOADER

        static final java.lang.ClassLoader CLASSLOADER
        Class loader.
      • FACTORY_METHOD_NAME

        public static final java.lang.String FACTORY_METHOD_NAME
        Name for a static "parameterize" factory method.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ClassGenericsUtil

        private ClassGenericsUtil()
        Fake Constructor. Use static methods.
    • Method Detail

      • getParameterizer

        public static Parameterizer getParameterizer​(java.lang.Class<?> c)
        Get a parameterizer for the given class.
        Parameters:
        c - Class
        Returns:
        Parameterizer or null.
      • make

        public static java.lang.Object make​(Parameterizer par,
                                            Parameterization config)
        Method to configure a class, then instantiate when the configuration step was successful.

        Don't call this directly use unless you know what you are doing.
        Instead, use Parameterization.tryInstantiate(Class)!

        Otherwise, null will be returned, and the resulting errors can be retrieved from the Parameterization parameter object. In general, you should be checking the Parameterization object for errors before accessing the returned value, since it may be null unexpectedly otherwise.

        Parameters:
        config - Parameterization
        Returns:
        Instance or null
      • parameterizeOrAbort

        public static <C> C parameterizeOrAbort​(java.lang.Class<?> c,
                                                Parameterization config)
        Force parameterization method.

        Please use this only in "runner" classes such as unit tests, since the error handling is not very flexible.

        Type Parameters:
        C - Type
        Parameters:
        c - Class to instantiate
        config - Parameters
        Returns:
        Instance
        Throws:
        AbortException - on errors
      • uglyCastIntoSubclass

        public static <D,​T extends D> java.lang.Class<T> uglyCastIntoSubclass​(java.lang.Class<D> cls)
        Cast the (erased) generics onto a class.

        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.

        Type Parameters:
        D - Base type
        T - Supertype
        Parameters:
        cls - Class type
        Returns:
        cls parameter, but cast to Class<T>
      • instantiateLowlevel

        public static <T> T instantiateLowlevel​(java.lang.Class<? extends T> clz)
        Instantiate the first available implementation of an interface.

        Only supports public and parameterless constructors.

        Parameters:
        clz - Class to instantiate.
        Returns:
        Instance
      • loadDefault

        public static <T> T loadDefault​(java.lang.Class<T> clz,
                                        java.lang.String def)
        Try to load the default for a particular interface (must have a public default constructor, used for factories).
        Type Parameters:
        T - Type
        Parameters:
        clz - Interface to implement
        def - Name of default implementation
        Returns:
        Instance