@SuppressWarnings("unchecked")
    public <T> Class<T> createInstanceReplica(T delegator) {
        if (delegator == null) {
            throw new IllegalArgumentException("delegator cannot be null");
        }
        final Class<T> clazz = (Class<T>) delegator.getClass();
        ClassPool classpool = ClassPool.getDefault();
        final String originalClassName = clazz.getName();
        CtClass originalClassAsCtClass;
        final CtClass newClass = classpool.makeClass(generateReplicaClassName(clazz));
        try {
            originalClassAsCtClass = classpool.get(originalClassName);

            copyFields(originalClassAsCtClass, newClass);
            addDelegatorField(delegator, newClass);

            CtMethod[] declaredMethods = originalClassAsCtClass.getDeclaredMethods();
            for (CtMethod ctMethod : declaredMethods) {
                @SuppressWarnings("unused")
                final String code = getReplicaMethodDelegationCode(delegator.getClass(), ctMethod, POWERMOCK_INSTANCE_DELEGATOR_FIELD_NAME);
                CtMethod make2 = CtNewMethod.copy(ctMethod, newClass, null);
                newClass.addMethod(make2);
            }

            CtConstructor[] declaredConstructors = originalClassAsCtClass.getDeclaredConstructors();
            for (CtConstructor ctConstructor : declaredConstructors) {
                CtConstructor copy = CtNewConstructor.copy(ctConstructor, newClass, null);
                newClass.addConstructor(copy);
            }
            return (Class<T>) newClass.toClass(this.getClass().getClassLoader(), this.getClass().getProtectionDomain());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
@ SuppressWarnings ( "unchecked" ) public < T > Class < T > createInstanceReplica ( T delegator ) { if ( delegator == null ) { throw new IllegalArgumentException ( "delegator cannot be null" ) ; } final Class < T > clazz = ( Class < T > ) delegator . getClass ( ) ; ClassPool classpool = ClassPool . getDefault ( ) ; final String originalClassName = clazz . getName ( ) ; CtClass originalClassAsCtClass ; final CtClass newClass = classpool . makeClass ( generateReplicaClassName ( clazz ) ) ; try { originalClassAsCtClass = classpool . get ( originalClassName ) ; copyFields ( originalClassAsCtClass , newClass ) ; addDelegatorField ( delegator , newClass ) ; CtMethod [ ] declaredMethods = originalClassAsCtClass . getDeclaredMethods ( ) ; for ( CtMethod ctMethod : declaredMethods ) { @ SuppressWarnings ( "unused" ) final String code = getReplicaMethodDelegationCode ( delegator . getClass ( ) , ctMethod , POWERMOCK_INSTANCE_DELEGATOR_FIELD_NAME ) ; CtMethod make2 = CtNewMethod . copy ( ctMethod , newClass , null ) ; newClass . addMethod ( make2 ) ; } CtConstructor [ ] declaredConstructors = originalClassAsCtClass . getDeclaredConstructors ( ) ; for ( CtConstructor ctConstructor : declaredConstructors ) { CtConstructor copy = CtNewConstructor . copy ( ctConstructor , newClass , null ) ; newClass . addConstructor ( copy ) ; } return ( Class < T > ) newClass . toClass ( this . getClass ( ) . getClassLoader ( ) , this . getClass ( ) . getProtectionDomain ( ) ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } }
Create a class that is a replica of type {@code T}. To allow for
partial mocking all calls to non-mocked methods will be delegated to the
{@code delegator}.

@param <T>       The type of the replica class to be created.
@param delegator The delegator object that will be invoked to allow for partial
mocking.
@return A replica class that can be used to duck-type an instance.