1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.stubbing.answers;
6
7import org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValues;
8import org.mockito.internal.util.reflection.LenientCopyTool;
9import org.mockito.invocation.InvocationOnMock;
10import org.mockito.stubbing.Answer;
11import org.objenesis.ObjenesisHelper;
12
13//TODO this needs documentation and further analysis - what if someone changes the answer?
14//we might think about implementing it straight on MockSettings
15public class ClonesArguments implements Answer<Object> {
16    public Object answer(InvocationOnMock invocation) throws Throwable {
17        Object[] arguments = invocation.getArguments();
18        for (int i = 0; i < arguments.length; i++) {
19            Object from = arguments[i];
20            Object newInstance = ObjenesisHelper.newInstance(from.getClass());
21            new LenientCopyTool().copyToRealObject(from, newInstance);
22            arguments[i] = newInstance;
23        }
24        return new ReturnsEmptyValues().answer(invocation);
25    }
26}