use of org.mockito.creation.instance.Instantiator in project mockito by mockito.
the class InlineDelegateByteBuddyMockMaker method doCreateMock.
private <T> T doCreateMock(MockCreationSettings<T> settings, MockHandler handler, boolean nullOnNonInlineConstruction) {
Class<? extends T> type = createMockType(settings);
try {
T instance;
if (settings.isUsingConstructor()) {
instance = new ConstructorInstantiator(settings.getOuterClassInstance() != null, settings.getConstructorArgs()).newInstance(type);
} else {
try {
// We attempt to use the "native" mock maker first that avoids
// Objenesis and Unsafe
instance = newInstance(type);
} catch (InstantiationException ignored) {
if (nullOnNonInlineConstruction) {
return null;
}
Instantiator instantiator = Plugins.getInstantiatorProvider().getInstantiator(settings);
instance = instantiator.newInstance(type);
}
}
MockMethodInterceptor mockMethodInterceptor = new MockMethodInterceptor(handler, settings);
mocks.put(instance, mockMethodInterceptor);
if (instance instanceof MockAccess) {
((MockAccess) instance).setMockitoInterceptor(mockMethodInterceptor);
}
return instance;
} catch (InstantiationException e) {
throw new MockitoException("Unable to create mock instance of type '" + type.getSimpleName() + "'", e);
}
}
use of org.mockito.creation.instance.Instantiator in project mockito by mockito.
the class SubclassByteBuddyMockMaker method createMock.
@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
Class<? extends T> mockedProxyType = createMockType(settings);
Instantiator instantiator = Plugins.getInstantiatorProvider().getInstantiator(settings);
T mockInstance = null;
try {
mockInstance = instantiator.newInstance(mockedProxyType);
MockAccess mockAccess = (MockAccess) mockInstance;
mockAccess.setMockitoInterceptor(new MockMethodInterceptor(handler, settings));
return ensureMockIsAssignableToMockedType(settings, mockInstance);
} catch (ClassCastException cce) {
throw new MockitoException(join("ClassCastException occurred while creating the mockito mock :", " class to mock : " + describeClass(settings.getTypeToMock()), " created class : " + describeClass(mockedProxyType), " proxy instance class : " + describeClass(mockInstance), " instance creation by : " + instantiator.getClass().getSimpleName(), "", "You might experience classloading issues, please ask the mockito mailing-list.", ""), cce);
} catch (org.mockito.creation.instance.InstantiationException e) {
throw new MockitoException("Unable to create mock instance of type '" + mockedProxyType.getSuperclass().getSimpleName() + "'", e);
}
}
use of org.mockito.creation.instance.Instantiator in project mockito by mockito.
the class ClonesArguments method answer.
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] arguments = invocation.getArguments();
for (int i = 0; i < arguments.length; i++) {
Object from = arguments[i];
if (from != null) {
if (from.getClass().isArray()) {
int len = Array.getLength(from);
Object newInstance = Array.newInstance(from.getClass().getComponentType(), len);
for (int j = 0; j < len; ++j) {
Array.set(newInstance, j, Array.get(from, j));
}
arguments[i] = newInstance;
} else {
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);
}
use of org.mockito.creation.instance.Instantiator in project dexmaker by linkedin.
the class InlineDexmakerMockMaker method createMock.
@Override
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) {
Class<T> typeToMock = settings.getTypeToMock();
Set<Class<?>> interfacesSet = settings.getExtraInterfaces();
Class<?>[] extraInterfaces = interfacesSet.toArray(new Class[interfacesSet.size()]);
InvocationHandlerAdapter handlerAdapter = new InvocationHandlerAdapter(handler);
T mock;
if (typeToMock.isInterface()) {
// support interfaces via java.lang.reflect.Proxy
Class[] classesToMock = new Class[extraInterfaces.length + 1];
classesToMock[0] = typeToMock;
System.arraycopy(extraInterfaces, 0, classesToMock, 1, extraInterfaces.length);
// newProxyInstance returns the type of typeToMock
mock = (T) Proxy.newProxyInstance(typeToMock.getClassLoader(), classesToMock, handlerAdapter);
} else {
boolean subclassingRequired = !interfacesSet.isEmpty() || Modifier.isAbstract(typeToMock.getModifiers());
// Add entry hooks to non-abstract methods.
classTransformer.mockClass(MockFeatures.withMockFeatures(typeToMock, interfacesSet));
Class<? extends T> proxyClass;
Instantiator instantiator = Mockito.framework().getPlugins().getDefaultPlugin(InstantiatorProvider2.class).getInstantiator(settings);
if (subclassingRequired) {
try {
// support abstract methods via dexmaker's ProxyBuilder
proxyClass = ProxyBuilder.forClass(typeToMock).implementing(extraInterfaces).onlyMethods(getMethodsToProxy(settings)).withSharedClassLoader().buildProxyClass();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new MockitoException("Failed to mock " + typeToMock, e);
}
try {
mock = instantiator.newInstance(proxyClass);
} catch (org.mockito.creation.instance.InstantiationException e) {
throw new MockitoException("Unable to create mock instance of type '" + proxyClass.getSuperclass().getSimpleName() + "'", e);
}
ProxyBuilder.setInvocationHandler(mock, handlerAdapter);
} else {
try {
mock = instantiator.newInstance(typeToMock);
} catch (org.mockito.creation.instance.InstantiationException e) {
throw new MockitoException("Unable to create mock instance of type '" + typeToMock.getSimpleName() + "'", e);
}
}
}
mocks.put(mock, handlerAdapter);
return mock;
}
Aggregations