org.mockito.internal.configuration.IndependentAnnotationEngine Maven / Gradle / Ivy
Show all versions of mockito-core Show documentation
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import static org.mockito.internal.exceptions.Reporter.moreThanOneAnnotationNotAllowed;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.ScopedMock;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.plugins.AnnotationEngine;
import org.mockito.plugins.MemberAccessor;
/**
* Initializes fields annotated with @{@link org.mockito.Mock} or @{@link org.mockito.Captor}.
*
*
* The {@link #process(Class, Object)} method implementation does not process super classes!
*
* @see MockitoAnnotations
*/
@SuppressWarnings("unchecked")
public class IndependentAnnotationEngine implements AnnotationEngine {
private final Map, FieldAnnotationProcessor>
annotationProcessorMap = new HashMap<>();
public IndependentAnnotationEngine() {
registerAnnotationProcessor(Mock.class, new MockAnnotationProcessor());
registerAnnotationProcessor(Captor.class, new CaptorAnnotationProcessor());
}
private Object createMockFor(Annotation annotation, Field field) {
return forAnnotation(annotation).process(annotation, field);
}
private FieldAnnotationProcessor forAnnotation(A annotation) {
if (annotationProcessorMap.containsKey(annotation.annotationType())) {
return (FieldAnnotationProcessor)
annotationProcessorMap.get(annotation.annotationType());
}
return new FieldAnnotationProcessor() {
@Override
public Object process(A annotation, Field field) {
return null;
}
};
}
private void registerAnnotationProcessor(
Class annotationClass, FieldAnnotationProcessor fieldAnnotationProcessor) {
annotationProcessorMap.put(annotationClass, fieldAnnotationProcessor);
}
@Override
public AutoCloseable process(Class clazz, Object testInstance) {
List scopedMocks = new ArrayList<>();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
boolean alreadyAssigned = false;
for (Annotation annotation : field.getAnnotations()) {
Object mock = createMockFor(annotation, field);
if (mock instanceof ScopedMock) {
scopedMocks.add((ScopedMock) mock);
}
if (mock != null) {
throwIfAlreadyAssigned(field, alreadyAssigned);
alreadyAssigned = true;
final MemberAccessor accessor = Plugins.getMemberAccessor();
try {
accessor.set(field, testInstance, mock);
} catch (Exception e) {
for (ScopedMock scopedMock : scopedMocks) {
scopedMock.close();
}
throw new MockitoException(
"Problems setting field "
+ field.getName()
+ " annotated with "
+ annotation,
e);
}
}
}
}
return () -> {
for (ScopedMock scopedMock : scopedMocks) {
scopedMock.closeOnDemand();
}
};
}
void throwIfAlreadyAssigned(Field field, boolean alreadyAssigned) {
if (alreadyAssigned) {
throw moreThanOneAnnotationNotAllowed(field.getName());
}
}
}