use of org.mockito.plugins.MemberAccessor in project mockito by mockito.
the class FieldInitializer method acquireFieldInstance.
private FieldInitializationReport acquireFieldInstance() throws IllegalAccessException {
final MemberAccessor accessor = Plugins.getMemberAccessor();
Object fieldInstance = accessor.get(field, fieldOwner);
if (fieldInstance != null) {
return new FieldInitializationReport(fieldInstance, false, false);
}
return instantiator.instantiate();
}
use of org.mockito.plugins.MemberAccessor in project mockito by mockito.
the class ForwardsInvocations method answer.
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Method mockMethod = invocation.getMethod();
try {
Method delegateMethod = getDelegateMethod(mockMethod);
if (!compatibleReturnTypes(mockMethod.getReturnType(), delegateMethod.getReturnType())) {
throw delegatedMethodHasWrongReturnType(mockMethod, delegateMethod, invocation.getMock(), delegatedObject);
}
MemberAccessor accessor = Plugins.getMemberAccessor();
Object[] rawArguments = ((Invocation) invocation).getRawArguments();
return accessor.invoke(delegateMethod, delegatedObject, rawArguments);
} catch (NoSuchMethodException e) {
throw delegatedMethodDoesNotExistOnDelegate(mockMethod, invocation.getMock(), delegatedObject);
} catch (InvocationTargetException e) {
// propagate the original exception from the delegate
throw e.getCause();
}
}
use of org.mockito.plugins.MemberAccessor in project mockito by mockito.
the class SpyAnnotationEngine method process.
@Override
public AutoCloseable process(Class<?> context, Object testInstance) {
Field[] fields = context.getDeclaredFields();
MemberAccessor accessor = Plugins.getMemberAccessor();
for (Field field : fields) {
if (field.isAnnotationPresent(Spy.class) && !field.isAnnotationPresent(InjectMocks.class)) {
assertNoIncompatibleAnnotations(Spy.class, field, Mock.class, Captor.class);
Object instance;
try {
instance = accessor.get(field, testInstance);
if (MockUtil.isMock(instance)) {
// instance has been spied earlier
// for example happens when MockitoAnnotations.openMocks is called two
// times.
Mockito.reset(instance);
} else if (instance != null) {
accessor.set(field, testInstance, spyInstance(field, instance));
} else {
accessor.set(field, testInstance, spyNewInstance(testInstance, field));
}
} catch (Exception e) {
throw new MockitoException("Unable to initialize @Spy annotated field '" + field.getName() + "'.\n" + e.getMessage(), e);
}
}
}
return new NoAction();
}
use of org.mockito.plugins.MemberAccessor in project mockito by mockito.
the class EqualsBuilder method reflectionAppend.
/**
* <p>Appends the fields and values defined by the given object of the
* given Class.</p>
*
* @param lhs the left hand object
* @param rhs the right hand object
* @param clazz the class to append details of
* @param builder the builder to append to
* @param useTransients whether to test transient fields
* @param excludeFields array of field names to exclude from testing
*/
private static boolean reflectionAppend(Object lhs, Object rhs, Class<?> clazz, EqualsBuilder builder, boolean useTransients, String[] excludeFields) {
Field[] fields = clazz.getDeclaredFields();
List<String> excludedFieldList = excludeFields != null ? Arrays.asList(excludeFields) : Collections.<String>emptyList();
MemberAccessor accessor = Plugins.getMemberAccessor();
for (int i = 0; i < fields.length && builder.isEquals; i++) {
Field f = fields[i];
if (!excludedFieldList.contains(f.getName()) && (f.getName().indexOf('$') == -1) && (useTransients || !Modifier.isTransient(f.getModifiers())) && !Modifier.isStatic(f.getModifiers())) {
try {
builder.append(accessor.get(f, lhs), accessor.get(f, rhs));
} catch (RuntimeException | IllegalAccessException ignored) {
// are not equal.
return true;
}
}
}
return false;
}
Aggregations