use of org.mockito.creation.instance.InstantiationException in project mockito by mockito.
the class InlineDelegateByteBuddyMockMaker method newInstance.
@Override
@SuppressWarnings("unchecked")
public <T> T newInstance(Class<T> cls) throws InstantiationException {
Constructor<?>[] constructors = cls.getDeclaredConstructors();
if (constructors.length == 0) {
throw new InstantiationException(cls.getName() + " does not define a constructor");
}
Constructor<?> selected = constructors[0];
for (Constructor<?> constructor : constructors) {
if (Modifier.isPublic(constructor.getModifiers())) {
selected = constructor;
break;
}
}
Class<?>[] types = selected.getParameterTypes();
Object[] arguments = new Object[types.length];
int index = 0;
for (Class<?> type : types) {
arguments[index++] = makeStandardArgument(type);
}
MemberAccessor accessor = Plugins.getMemberAccessor();
try {
return (T) accessor.newInstance(selected, callback -> {
mockitoConstruction.set(true);
try {
return callback.newInstance();
} finally {
mockitoConstruction.set(false);
}
}, arguments);
} catch (Exception e) {
throw new InstantiationException("Could not instantiate " + cls.getName(), e);
}
}
use of org.mockito.creation.instance.InstantiationException 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.InstantiationException in project mockito by mockito.
the class JavaEightUtil method getStaticFieldValue.
/**
* Gets a value of the classes' field using reflection to stay backwards-compatible with older JDKs.
*
* @param fqcn The fully qualified class name of the type to be produced.
* @param fieldName The name of th classes' field which value is going to be returned.
* @return the restored value.
*/
private static Object getStaticFieldValue(final String fqcn, final String fieldName) {
try {
final Class<?> type = getClass(fqcn);
final Field field = type.getField(fieldName);
return field.get(null);
// any exception is really unexpected since the type name has
// already been verified
} catch (Exception e) {
throw new InstantiationException(String.format("Could not get %s#%s(): %s", fqcn, fieldName, e), e);
}
}
Aggregations