Class TransformerUtils


  • public class TransformerUtils
    extends Object

    Well this class is necessary, because Java generics is the greatest nonsense which was ever developed ...

    This class encapsulates all cast magic and then suppresses the unchecked cast message.

    Since:
    1.0
    Author:
    Andreas Pointner
    • Constructor Detail

      • TransformerUtils

        public TransformerUtils()
    • Method Detail

      • getTypeArgument

        public <T,​S> Class<T> getTypeArgument​(Class<S> clazz,
                                                    int idx)
        This method extracts the generic types from instantiated classes, which directly overwrite the generic parameters of its parent class. e.g. if there is a class ArrayList<T> and the class is then extended by e.g. MyArrayList extends ArrayList<String> then this method can extract the generic argument of this class, which would be in that specific case be string, and the method class would result in something like:
        getTypeArgument(MyArrayList.class, 0) = Class<String>
        Type Parameters:
        T - the type of the result class
        S - the type of tine input class
        Parameters:
        clazz - the class where the generic type should be extracted
        idx - the index of the generic type
        Returns:
        a class which represents the type argument of clazz at idx position. There is also a special case, if the GenericType is a ParameterizedType of the type Graph, then the generic argument from the graph is returned.
        Throws:
        RuntimeException - if the type argument cannot be extracted from the class