Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.service.task.exec

File InjectionSite.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
10% of files have more coverage

Code metrics

14
38
3
1
90
81
14
0.37
12.67
3
4.67

Classes

Class Line # Actions
InjectionSite 14 38 14
0.836363683.6%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    package io.sunshower.service.task.exec;
2   
3    import io.sunshower.common.Identifier;
4    import io.sunshower.service.task.ElementDescriptor;
5    import io.sunshower.service.task.TaskContext;
6    import java.lang.reflect.Field;
7    import java.util.Map;
8    import java.util.logging.Level;
9    import org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerGraph;
10    import org.springframework.aop.support.AopUtils;
11    import org.springframework.util.ReflectionUtils;
12   
13    /** Created by haswell on 3/27/17. */
 
14    class InjectionSite {
15   
16    private final String name;
17    private final Field field;
18    private final Object instance;
19    private final String fieldName;
20    private final TaskContext context;
21    private final ElementDescriptor descriptor;
22   
 
23  576 toggle public InjectionSite(
24    TaskContext context,
25    String fieldName,
26    String name,
27    Field actualField,
28    Object instance,
29    TinkerGraph graph,
30    ElementDescriptor descriptor,
31    InjectionContext injectionContext) {
32  587 this.name = name;
33  588 this.context = context;
34  593 this.field = actualField;
35  591 this.instance = instance;
36  589 this.fieldName = fieldName;
37  591 this.descriptor = descriptor;
38    }
39   
 
40  577 toggle public boolean resolveAndInject() {
41  581 try {
42  595 final Class<?> type = field.getType();
43  588 field.setAccessible(true);
44  595 if (name.equals("name") && String.class.equals(type)) {
45  153 field.set(instance, descriptor.getName());
46  152 return true;
47  439 } else if (name.equals("id") && Identifier.class.equals(type)) {
48  0 field.set(instance, descriptor.getId());
49  0 return true;
50  440 } else if (context.containsBinding(type)) {
51  10 field.set(instance, context.getBinding(type));
52  10 return true;
53    } else {
54  434 Object element = descriptor.getElement();
55  435 if (element != null) {
56  433 final Class<?> etype = AopUtils.getTargetClass(element);
57  437 if (type.isAssignableFrom(etype)) {
58  146 field.set(instance, element);
59  147 return true;
60    }
61    }
62    }
63   
64    } catch (IllegalAccessException e) {
65  0 ParallelTaskExecutor.log.log(Level.WARNING, "Failed to inject site. Reason: ", e);
66    }
67  287 return false;
68    }
69   
 
70  277 toggle public void inject(Map<Class<?>, Object> resolved) {
71  283 final Class<?> type = field.getType();
72  291 try {
73  287 if (resolved.containsKey(type)) {
74  6 final Object value = resolved.get(type);
75  6 ReflectionUtils.setField(field, instance, value);
76  6 return;
77    }
78    } catch (Exception ex) {
79  0 throw new RuntimeException(ex);
80    }
81   
82  281 for (Map.Entry<Class<?>, Object> entries : resolved.entrySet()) {
83  188 final Class<?> currentType = entries.getKey();
84  189 if (currentType.isAssignableFrom(type)) {
85  0 ReflectionUtils.setField(field, instance, entries.getValue());
86  0 break;
87    }
88    }
89    }
90    }