Class GenericConstructor<T>


  • public class GenericConstructor<T>
    extends java.lang.Object
    Class that allows any class to be instantiated via any accessible constructor. Really a short hand to avoid writing a bunch of reflective code.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.lang.reflect.Constructor constructor  
      private java.lang.Object lock  
      private java.lang.Class<T> resultType  
      private java.lang.Class<?>[] signature  
      private java.lang.Class<?> type  
      private java.lang.String typeName  
    • Constructor Summary

      Constructors 
      Constructor Description
      GenericConstructor​(java.lang.Class<T> type, java.lang.String className, java.lang.Class<?>... signature)
      Create a generic of type T for the untyped class cls.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      T create​(java.lang.Object... args)
      Create an instance of type T using the constructor that matches the given arguments if possible.
      private void getConstructor()  
      • Methods inherited from class java.lang.Object

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

      • lock

        private final java.lang.Object lock
      • typeName

        private java.lang.String typeName
      • resultType

        private java.lang.Class<T> resultType
      • type

        private java.lang.Class<?> type
      • signature

        private java.lang.Class<?>[] signature
      • constructor

        private java.lang.reflect.Constructor constructor
    • Constructor Detail

      • GenericConstructor

        public GenericConstructor​(java.lang.Class<T> type,
                                  java.lang.String className,
                                  java.lang.Class<?>... signature)
        Create a generic of type T for the untyped class cls. Generally cls is a class that has been generated and loaded, so no compiled code can depend on the class directly. However, the generated class probably implements some interface T, represented here by Class<T>.
        Parameters:
        type - The expected type of a create call.
        className - The name of the class to use for a constructor.
        signature - The signature of the desired constructor.
        Throws:
        java.lang.IllegalArgumentException - if cls is not a subclass of type.
    • Method Detail

      • getConstructor

        private void getConstructor()
      • create

        public T create​(java.lang.Object... args)
        Create an instance of type T using the constructor that matches the given arguments if possible. The constructor is cached, so an instance of GenericClass should always be used for the same types of arguments. If a call fails, a check is made to see if a different constructor could be used.
        Parameters:
        args - The constructor arguments.
        Returns:
        A new instance of the object.