Class ClassReplicaCreator

java.lang.Object
org.powermock.core.ClassReplicaCreator

public class ClassReplicaCreator extends Object
This class takes care of creating a replica of a class. The class structure is copied to the new class. This is useful in situations where you want to create a mock for a class but it's not possible because of some restrictions (such as the class being loaded by the bootstrap class-loader).
  • Field Details

  • Constructor Details

    • ClassReplicaCreator

      public ClassReplicaCreator()
  • Method Details

    • createClassReplica

      public <T> Class<T> createClassReplica(Class<T> clazz)
    • createInstanceReplica

      public <T> Class<T> createInstanceReplica(T delegator)
      Create a class that is a replica of type T. To allow for partial mocking all calls to non-mocked methods will be delegated to the delegator.
      Type Parameters:
      T - The type of the replica class to be created.
      Parameters:
      delegator - The delegator object that will be invoked to allow for partial mocking.
      Returns:
      A replica class that can be used to duck-type an instance.
    • addDelegatorField

      private <T> void addDelegatorField(T delegator, javassist.CtClass replicaClass) throws javassist.CannotCompileException
      Add a field to the replica class that holds the instance delegator. I.e. if we're creating a instance replica of java.lang.Long this methods adds a new field of type delegator.getClass() to the replica class.
      Throws:
      javassist.CannotCompileException
    • generateReplicaClassName

      private <T> String generateReplicaClassName(Class<T> clazz)
    • copyFields

      private void copyFields(javassist.CtClass originalClassAsCtClass, javassist.CtClass newClass) throws javassist.CannotCompileException, javassist.NotFoundException
      Throws:
      javassist.CannotCompileException
      javassist.NotFoundException
    • getReplicaMethodDelegationCode

      private String getReplicaMethodDelegationCode(Class<?> clazz, javassist.CtMethod ctMethod, String classOrInstanceToDelegateTo) throws javassist.NotFoundException
      Throws:
      javassist.NotFoundException
    • getParameterTypes

      private String[] getParameterTypes(javassist.CtMethod ctMethod) throws javassist.NotFoundException
      Throws:
      javassist.NotFoundException
    • getParametersAsString

      private static String getParametersAsString(String[] types)