-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
IndependentAnnotationEngine.java
109 lines (99 loc) · 4.13 KB
/
IndependentAnnotationEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* 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}.
*
* <p>
* The {@link #process(Class, Object)} method implementation <strong>does not</strong> process super classes!
*
* @see MockitoAnnotations
*/
@SuppressWarnings("unchecked")
public class IndependentAnnotationEngine
implements AnnotationEngine, org.mockito.configuration.AnnotationEngine {
private final Map<Class<? extends Annotation>, FieldAnnotationProcessor<?>>
annotationProcessorMap =
new HashMap<Class<? extends Annotation>, FieldAnnotationProcessor<?>>();
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 <A extends Annotation> FieldAnnotationProcessor<A> forAnnotation(A annotation) {
if (annotationProcessorMap.containsKey(annotation.annotationType())) {
return (FieldAnnotationProcessor<A>)
annotationProcessorMap.get(annotation.annotationType());
}
return new FieldAnnotationProcessor<A>() {
public Object process(A annotation, Field field) {
return null;
}
};
}
private <A extends Annotation> void registerAnnotationProcessor(
Class<A> annotationClass, FieldAnnotationProcessor<A> fieldAnnotationProcessor) {
annotationProcessorMap.put(annotationClass, fieldAnnotationProcessor);
}
@Override
public AutoCloseable process(Class<?> clazz, Object testInstance) {
List<ScopedMock> 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());
}
}
}