Clover icon

sunshower-base

  1. Project Clover database Fri Apr 6 2018 03:41:27 UTC
  2. Package io.sunshower.test.ws

File JAXRSFieldCallback.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
25% of files have more coverage

Code metrics

16
31
8
1
85
75
18
0.58
3.88
8
2.25

Classes

Class Line # Actions
JAXRSFieldCallback 13 31 18
0.709090970.9%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package io.sunshower.test.ws;
2   
3    import io.sunshower.reflect.reflect.Reflect;
4    import java.lang.reflect.Field;
5    import java.lang.reflect.Modifier;
6    import javax.ws.rs.Path;
7    import javax.ws.rs.client.WebTarget;
8    import javax.ws.rs.core.Context;
9    import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
10    import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
11    import org.springframework.util.ReflectionUtils;
12   
 
13    public class JAXRSFieldCallback implements ReflectionUtils.FieldCallback {
14    private final ResteasyWebTarget webTarget;
15    private Object bean;
16    private ConfigurableListableBeanFactory factory;
17   
 
18  11 toggle public JAXRSFieldCallback(ConfigurableListableBeanFactory factory, Object bean) {
19  11 this.bean = bean;
20  11 this.factory = factory;
21  11 this.webTarget = (ResteasyWebTarget) factory.getBean(WebTarget.class);
22    }
23   
 
24  54 toggle @Override
25    public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
26  54 if (!isInjectable(field)) {
27  47 return;
28    }
29  7 ReflectionUtils.makeAccessible(field);
30  7 if (isRemote(field)) {
31  7 try {
32  7 injectRemote(Reflect.resolveProxied(bean), field);
33    } catch (Exception ex) {
34  0 throw new IllegalArgumentException(ex);
35    }
36    }
37  7 if (isContext(field)) {
38  0 injectContext(field);
39    }
40    }
41   
 
42  7 toggle private void injectRemote(Object instance, Field field) {
43  7 Class<?> type = field.getType();
44  7 for (Class<?> current = type; type != null; type = type.getSuperclass()) {
45  7 if (doInject(current, instance, field)) {
46  7 return;
47    }
48  0 for (Class<?> iface : current.getInterfaces()) {
49  0 if (doInject(iface, instance, field)) {
50  0 return;
51    }
52    }
53    }
54    }
55   
 
56  7 toggle private boolean doInject(Class<?> current, Object instance, Field field) {
57  7 if (current.isAnnotationPresent(Path.class)) {
58  7 int modifiers = current.getModifiers();
59  7 if (Modifier.isInterface(modifiers)) {
60  7 Object value = webTarget.proxy(current);
61  7 try {
62  7 field.set(instance, value);
63  7 return true;
64    } catch (ReflectiveOperationException ex) {
65  0 throw new IllegalStateException(ex);
66    }
67    }
68    }
69  0 return false;
70    }
71   
 
72  0 toggle private void injectContext(Field field) {}
73   
 
74  61 toggle private boolean isContext(Field field) {
75  61 return field.isAnnotationPresent(Context.class);
76    }
77   
 
78  61 toggle private boolean isRemote(Field field) {
79  61 return field.isAnnotationPresent(Remote.class);
80    }
81   
 
82  54 toggle private boolean isInjectable(Field field) {
83  54 return isContext(field) || isRemote(field);
84    }
85    }