Clover icon

sunshower-base

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

File ServiceDescriptor.java

 

Coverage histogram

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

Code metrics

16
67
20
2
161
137
30
0.45
3.35
10
1.5

Classes

Class Line # Actions
ServiceDescriptor 7 16 8
0.4166666641.7%
ServiceDescriptor.MethodMap 57 51 22
0.696202569.6%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    package io.sunshower.reflect.incant;
2   
3    import java.lang.reflect.Method;
4    import java.util.Arrays;
5    import java.util.Collection;
6   
 
7    public final class ServiceDescriptor<T> extends LazyPropertyAware {
8   
9    private final Class<T> owner;
10    private final String identifier;
11    private final MethodMap methods;
12   
 
13  3 toggle public ServiceDescriptor(Class<T> owner, String identifier, Method[] methods) {
14  3 this.owner = owner;
15  3 this.identifier = identifier;
16  3 this.methods = new MethodMap(owner, methods);
17    }
18   
 
19  0 toggle public ServiceDescriptor(Class<T> owner, String identifier, Collection<Method> methods) {
20  0 this.owner = owner;
21  0 this.identifier = identifier;
22  0 this.methods = new MethodMap(owner, methods);
23    }
24   
 
25  0 toggle public ServiceDescriptor(
26    String identifier, Class<T> owner, Collection<MethodDescriptor<?, ?>> methods) {
27  0 this.owner = owner;
28  0 this.identifier = identifier;
29  0 this.methods = new MethodMap(methods, owner);
30    }
31   
 
32  0 toggle public ServiceDescriptor(String identifier, Class<T> owner, MethodDescriptor[] methods) {
33  0 this.owner = owner;
34  0 this.identifier = identifier;
35  0 this.methods = new MethodMap(methods, owner);
36    }
37   
 
38  0 toggle public Class<T> getTargetClass() {
39  0 return owner;
40    }
41   
 
42  2 toggle @SuppressWarnings("unchecked")
43    public <U> MethodDescriptor<T, U> resolve(String name) {
44  2 return (MethodDescriptor<T, U>) methods.get(name);
45    }
46   
 
47  2 toggle @SuppressWarnings("unchecked")
48    public <U> MethodDescriptor<T, U> resolve(String name, Class<?>... parameterTypes) {
49  2 return (MethodDescriptor<T, U>) methods.get(parameterTypes, name);
50    }
51   
 
52  4 toggle public String getIdentifier() {
53  4 return identifier;
54    }
55   
56    @SuppressWarnings("unchecked")
 
57    private static final class MethodMap {
58    MethodDescriptor<?, ?>[][] methods;
59   
 
60  0 toggle MethodMap(MethodDescriptor<?, ?>[] methods, Class<?> owner) {
61  0 this.methods = new MethodDescriptor[2][2];
62  0 int i = 0;
63  0 for (MethodDescriptor current : methods) {
64  0 final int argumentCount = current.getMethod().getParameterCount();
65  0 add(current, i++, argumentCount);
66    }
67    }
68   
 
69  0 toggle MethodMap(Collection<MethodDescriptor<?, ?>> methods, Class<?> owner) {
70  0 this.methods = new MethodDescriptor[2][2];
71  0 int i = 0;
72  0 for (MethodDescriptor current : methods) {
73  0 final int argumentCount = current.getMethod().getParameterCount();
74  0 add(current, i++, argumentCount);
75    }
76    }
77   
 
78  0 toggle MethodMap(final Class<?> owner, Collection<Method> methods) {
79  0 this.methods = new MethodDescriptor[2][2];
80  0 int i = 0;
81  0 for (Method method : methods) {
82  0 final MethodDescriptor<?, ?> current = createMethodDescriptor(owner, method);
83  0 final int argumentCount = current.getMethod().getParameterCount();
84  0 add(current, i++, argumentCount);
85    }
86    }
87   
 
88  3 toggle MethodMap(final Class<?> owner, Method[] methods) {
89  3 this.methods = new MethodDescriptor[2][2];
90  7 for (int i = 0; i < methods.length; ++i) {
91  4 final MethodDescriptor<?, ?> current = createMethodDescriptor(owner, methods[i]);
92  4 final int argumentCount = current.getMethod().getParameterCount();
93  4 add(current, i, argumentCount);
94    }
95    }
96   
 
97  2 toggle MethodDescriptor<?, ?> get(Class<?>[] parameterTypes, String name) {
98  2 final MethodDescriptor<?, ?>[] candidates = methods[parameterTypes.length];
99  2 if (candidates != null) {
100  2 for (MethodDescriptor descriptor : candidates) {
101  6 if (descriptor != null && descriptor.matches(parameterTypes, name)) {
102  2 return descriptor;
103    }
104    }
105    }
106  0 return null;
107    }
108   
 
109  2 toggle <U> MethodDescriptor<?, ?> get(String name) {
110  2 final MethodDescriptor<?, ?>[] candidates = methods[0];
111  2 if (candidates != null) {
112  2 for (MethodDescriptor descriptor : candidates) {
113  6 if (descriptor != null && descriptor.matches(name)) {
114  2 return descriptor;
115    }
116    }
117    }
118  0 return null;
119    }
120   
 
121  4 toggle private void add(MethodDescriptor<?, ?> m, int i, int argumentCount) {
122  4 if (argumentCount >= methods.length) {
123  1 resize(argumentCount);
124    }
125  4 final MethodDescriptor<?, ?>[] current = getCurrent(argumentCount);
126  4 methods[argumentCount] = addMethod(current, m);
127    }
128   
 
129  4 toggle private MethodDescriptor<?, ?>[] addMethod(
130    MethodDescriptor<?, ?>[] current, MethodDescriptor m) {
131  4 MethodDescriptor<?, ?>[] methods = current;
132  4 final int lastIndex = current.length - 1;
133  4 final MethodDescriptor<?, ?> last = current[lastIndex];
134  4 if (last == null) {
135  4 methods = resizeArray(methods);
136    }
137  4 methods[lastIndex + 1] = m;
138  4 return methods;
139    }
140   
 
141  4 toggle private MethodDescriptor<?, ?>[] resizeArray(MethodDescriptor<?, ?>[] methods) {
142  4 return Arrays.copyOf(methods, methods.length + 2);
143    }
144   
 
145  4 toggle private MethodDescriptor<?, ?>[] getCurrent(int argumentCount) {
146  4 MethodDescriptor<?, ?>[] methods = this.methods[argumentCount];
147  4 if (methods == null) {
148  1 methods = this.methods[argumentCount] = new MethodDescriptor<?, ?>[2];
149    }
150  4 return methods;
151    }
152   
 
153  1 toggle private void resize(int argumentCount) {
154  1 this.methods = Arrays.copyOf(this.methods, argumentCount + 2);
155    }
156   
 
157  4 toggle private MethodDescriptor<?, ?> createMethodDescriptor(Class<?> owner, Method method) {
158  4 return new MethodDescriptor<>(owner, method);
159    }
160    }
161    }