All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.mockito.internal.stubbing.answers.ClonesArguments Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/*
 * Copyright (c) 2007 Mockito contributors
 * This program is made available under the terms of the MIT License.
 */
package com.fitbur.mockito.internal.stubbing.answers;

import com.fitbur.mockito.internal.configuration.plugins.Plugins;
import com.fitbur.mockito.internal.creation.instance.Instantiator;
import com.fitbur.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues;
import com.fitbur.mockito.internal.util.reflection.LenientCopyTool;
import com.fitbur.mockito.invocation.InvocationOnMock;
import com.fitbur.mockito.stubbing.Answer;

//TODO this needs documentation and further analysis - what if someone changes the answer?
//we might think about implementing it straight on MockSettings
public class ClonesArguments implements Answer {
    public Object answer(InvocationOnMock invocation) throws Throwable {
        Object[] arguments = invocation.getArguments();
        for (int i = 0; i < arguments.length; i++) {
            Object from = arguments[i];
            Instantiator instantiator = Plugins.getInstantiatorProvider().getInstantiator(null);
            Object newInstance = instantiator.newInstance(from.getClass());
            new LenientCopyTool().copyToRealObject(from, newInstance);
            arguments[i] = newInstance;
        }
        return new ReturnsEmptyValues().answer(invocation);
    }
}