Class DefaultClassInstantiator

  • All Implemented Interfaces:
    IClassInstantiator

    public class DefaultClassInstantiator
    extends java.lang.Object
    implements IClassInstantiator
    Default class instantiator that is pretty limited. It just hope that the mocked class has a public empty constructor.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.Class<?>[] EMPTY  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.Long callLongMethod​(java.lang.Class<?> clazz, java.lang.String methodName)  
      java.lang.Object[] getArgsForTypes​(java.lang.Class<?>[] methodTypes)
      Get some default instances of provided classes
      java.lang.reflect.Constructor<?> getConstructorToUse​(java.lang.Class<?> clazz)
      Return the constructor considered the best to use with this class.
      private static java.lang.Long getSerializableUID​(java.lang.Class<?> clazz)  
      private static byte[] getSerializedBytes​(java.lang.Class<?> clazz)  
      private boolean isSerializable​(java.lang.Class<?> clazz)
      Tells if the provided class is serializable
      java.lang.Object newInstance​(java.lang.Class<?> c)
      Try to instantiate a class without using a special constructor.
      private static java.lang.Object readObject​(byte[] bytes)  
      • Methods inherited from class java.lang.Object

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

      • EMPTY

        private static final java.lang.Class<?>[] EMPTY
    • Constructor Detail

      • DefaultClassInstantiator

        public DefaultClassInstantiator()
    • Method Detail

      • newInstance

        public java.lang.Object newInstance​(java.lang.Class<?> c)
                                     throws java.lang.InstantiationException
        Try to instantiate a class without using a special constructor. See documentation for the algorithm.
        Specified by:
        newInstance in interface IClassInstantiator
        Parameters:
        c - Class to instantiate
        Returns:
        new instance of clazz
        Throws:
        java.lang.InstantiationException - when an error occured during instantiation
      • isSerializable

        private boolean isSerializable​(java.lang.Class<?> clazz)
        Tells if the provided class is serializable
        Parameters:
        clazz - Class to check
        Returns:
        If the class is serializable
      • getConstructorToUse

        public java.lang.reflect.Constructor<?> getConstructorToUse​(java.lang.Class<?> clazz)
        Return the constructor considered the best to use with this class. Algorithm is: No args constructor and then first constructor defined in the class
        Parameters:
        clazz - Class in which constructor is searched
        Returns:
        Constructor to use
      • getArgsForTypes

        public java.lang.Object[] getArgsForTypes​(java.lang.Class<?>[] methodTypes)
                                           throws java.lang.InstantiationException
        Get some default instances of provided classes
        Parameters:
        methodTypes - Classes to instantiate
        Returns:
        Instances of methodTypes in order
        Throws:
        java.lang.InstantiationException - Thrown if the class instantiation fails
      • getSerializedBytes

        private static byte[] getSerializedBytes​(java.lang.Class<?> clazz)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • getSerializableUID

        private static java.lang.Long getSerializableUID​(java.lang.Class<?> clazz)
      • callLongMethod

        private static java.lang.Long callLongMethod​(java.lang.Class<?> clazz,
                                                     java.lang.String methodName)
      • readObject

        private static java.lang.Object readObject​(byte[] bytes)
                                            throws java.io.IOException,
                                                   java.lang.ClassNotFoundException
        Throws:
        java.io.IOException
        java.lang.ClassNotFoundException