1 |
|
package io.sunshower.service.task.exec; |
2 |
|
|
3 |
|
import io.sunshower.common.Identifier; |
4 |
|
import io.sunshower.service.task.Context; |
5 |
|
import io.sunshower.service.task.ElementDescriptor; |
6 |
|
import io.sunshower.service.task.TaskContext; |
7 |
|
import java.lang.reflect.Field; |
8 |
|
import java.util.*; |
9 |
|
import org.apache.commons.lang.StringUtils; |
10 |
|
import org.apache.tinkerpop.gremlin.structure.Vertex; |
11 |
|
import org.apache.tinkerpop.gremlin.structure.VertexProperty; |
12 |
|
import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph; |
13 |
|
import org.springframework.aop.framework.Advised; |
14 |
|
import org.springframework.aop.support.AopUtils; |
15 |
|
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; |
16 |
|
import org.springframework.context.ApplicationContext; |
17 |
|
|
18 |
|
|
|
|
| 91.4% |
Uncovered Elements: 6 (70) |
Complexity: 17 |
Complexity Density: 0.36 |
|
19 |
|
class InjectionContext { |
20 |
|
|
21 |
|
private final TinkerGraph graph; |
22 |
|
private final TaskContext context; |
23 |
|
private final ElementDescriptor descriptor; |
24 |
|
private final ApplicationContext applicationContext; |
25 |
|
private final Map<String, InjectionSite> namedSites; |
26 |
|
private final Map<Class<?>, InjectionSite> typedSites; |
27 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
28 |
147 |
InjectionContext(... |
29 |
|
TaskContext context, |
30 |
|
ElementDescriptor descriptor, |
31 |
|
TinkerGraph graph, |
32 |
|
ApplicationContext applicationContext) { |
33 |
149 |
this.graph = graph; |
34 |
149 |
this.context = context; |
35 |
152 |
this.descriptor = descriptor; |
36 |
152 |
this.namedSites = new HashMap<>(); |
37 |
152 |
this.typedSites = new HashMap<>(); |
38 |
151 |
this.applicationContext = applicationContext; |
39 |
|
} |
40 |
|
|
|
|
| 90% |
Uncovered Elements: 2 (20) |
Complexity: 4 |
Complexity Density: 0.25 |
|
41 |
147 |
public Object invoke() {... |
42 |
147 |
try { |
43 |
152 |
final Class<?> type = descriptor.getType(); |
44 |
149 |
AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory(); |
45 |
|
|
46 |
151 |
Object instance = |
47 |
|
resolveTarget(beanFactory.createBean(type, AutowireCapableBeanFactory.AUTOWIRE_NO, true)); |
48 |
|
|
49 |
144 |
final Set<InjectionSite> dependencies = new HashSet<>(); |
50 |
|
|
51 |
443 |
for (Class<?> ct = type; ct != null; ct = ct.getSuperclass()) { |
52 |
290 |
for (Field field : ct.getDeclaredFields()) { |
53 |
582 |
if (field.isAnnotationPresent(Context.class)) { |
54 |
593 |
String fieldName = field.getName(); |
55 |
593 |
String name = resolveName(field, field.getAnnotation(Context.class)); |
56 |
|
|
57 |
590 |
Field actualField = ct.getDeclaredField(fieldName); |
58 |
|
|
59 |
578 |
final InjectionSite site = |
60 |
|
new InjectionSite( |
61 |
|
context, fieldName, name, actualField, instance, graph, descriptor, this); |
62 |
586 |
dependencies.add(site); |
63 |
|
} |
64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
148 |
resolve(dependencies); |
68 |
|
|
69 |
148 |
return instance; |
70 |
|
} catch (Exception ex) { |
71 |
0 |
throw new RuntimeException(ex); |
72 |
|
} |
73 |
|
} |
74 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 3 |
Complexity Density: 1 |
|
75 |
148 |
private Object resolveTarget(Object bean) throws Exception {... |
76 |
144 |
if (AopUtils.isAopProxy(bean) && bean instanceof Advised) { |
77 |
5 |
return ((Advised) bean).getTargetSource().getTarget(); |
78 |
|
} |
79 |
145 |
return bean; |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
82 |
139 |
private void resolve(Set<InjectionSite> dependencies) {... |
83 |
143 |
final Iterator<InjectionSite> deps = dependencies.iterator(); |
84 |
740 |
while (deps.hasNext()) { |
85 |
596 |
final InjectionSite next = deps.next(); |
86 |
594 |
if (next.resolveAndInject()) { |
87 |
309 |
deps.remove(); |
88 |
|
} |
89 |
|
} |
90 |
|
|
91 |
153 |
searchAndResolve(dependencies); |
92 |
|
} |
93 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
94 |
149 |
private void searchAndResolve(Set<InjectionSite> dependencies) {... |
95 |
151 |
final Map<Class<?>, Object> resolved = new HashMap<>(); |
96 |
151 |
resolveDependencies(descriptor.getId(), resolved); |
97 |
|
|
98 |
148 |
for (InjectionSite site : dependencies) { |
99 |
291 |
site.inject(resolved); |
100 |
|
} |
101 |
|
} |
102 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.33 |
|
103 |
492 |
private void resolveDependencies(Identifier id, Map<Class<?>, Object> resolved) {... |
104 |
493 |
graph |
105 |
|
.traversal() |
106 |
|
.V(id) |
107 |
|
.inE() |
108 |
|
.forEachRemaining( |
109 |
|
e -> { |
110 |
348 |
Vertex vertex = e.outVertex(); |
111 |
352 |
VertexProperty<Object> result = vertex.property("result"); |
112 |
348 |
if (result.isPresent()) { |
113 |
353 |
Object value = result.value(); |
114 |
353 |
if (value != null) { |
115 |
352 |
Class<?> vtype = value.getClass(); |
116 |
354 |
resolved.put(vtype, result.value()); |
117 |
|
} |
118 |
|
} |
119 |
349 |
resolveDependencies((Identifier) vertex.id(), resolved); |
120 |
|
}); |
121 |
|
} |
122 |
|
|
|
|
| 60% |
Uncovered Elements: 2 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
123 |
581 |
private String resolveName(Field field, Context annotation) {... |
124 |
585 |
if (StringUtils.isEmpty(annotation.name())) { |
125 |
590 |
return field.getName(); |
126 |
|
} |
127 |
0 |
return annotation.name(); |
128 |
|
} |
129 |
|
} |