Search in sources :

Example 6 with MemberAccessor

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();
}
Also used : MemberAccessor(org.mockito.plugins.MemberAccessor)

Example 7 with MemberAccessor

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();
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) MemberAccessor(org.mockito.plugins.MemberAccessor) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 8 with MemberAccessor

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();
}
Also used : Field(java.lang.reflect.Field) MemberAccessor(org.mockito.plugins.MemberAccessor) MockitoException(org.mockito.exceptions.base.MockitoException) Spy(org.mockito.Spy) MockitoException(org.mockito.exceptions.base.MockitoException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 9 with MemberAccessor

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;
}
Also used : Field(java.lang.reflect.Field) MemberAccessor(org.mockito.plugins.MemberAccessor)

Aggregations

MemberAccessor (org.mockito.plugins.MemberAccessor)9 MockitoException (org.mockito.exceptions.base.MockitoException)4 Field (java.lang.reflect.Field)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 Instrumentation (java.lang.instrument.Instrumentation)1 Constructor (java.lang.reflect.Constructor)1 Modifier (java.lang.reflect.Modifier)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 BiConsumer (java.util.function.BiConsumer)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 JarEntry (java.util.jar.JarEntry)1