Clover icon

sunshower-base

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

File MethodDescriptor.java

 

Coverage histogram

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

Code metrics

30
64
13
1
134
117
34
0.53
4.92
13
2.62

Classes

Class Line # Actions
MethodDescriptor 9 64 34
0.672897267.3%
 

Contributing tests

This file is covered by 11 tests. .

Source view

1    package io.sunshower.reflect.incant;
2   
3    import java.lang.reflect.Method;
4    import java.security.AccessController;
5    import java.security.PrivilegedAction;
6    import java.util.Arrays;
7    import java.util.Map;
8   
 
9    public final class MethodDescriptor<U, T> extends LazyPropertyAware
10    implements PrivilegedAction<Void> {
11   
12    private final Method method;
13    private final Class<U> owner;
14    private Map<String, String> properties;
15    private volatile Class<?>[] parameterTypes;
16   
 
17  14 toggle public MethodDescriptor(final Class<U> owner, final Method method) {
18  14 this.owner = owner;
19  14 this.method = method;
20  14 AccessController.doPrivileged(this);
21    }
22   
 
23  4 toggle public Method getMethod() {
24  4 return method;
25    }
26   
 
27  3 toggle @SuppressWarnings("unchecked")
28    public T invoke(Object o, Object... parameters) {
29  3 try {
30  3 Object result = method.invoke(o, parameters);
31  3 return (T) result;
32    } catch (Exception ex) {
33  0 throw new InvocationFailureException(ex);
34    }
35    }
36   
 
37  4 toggle @SuppressWarnings("unchecked")
38    public T invoke(Object o) {
39  4 try {
40  4 Object result = method.invoke(o);
41  4 return (T) result;
42    } catch (Exception ex) {
43  0 throw new InvocationFailureException(ex);
44    }
45    }
46   
 
47  0 toggle @SuppressWarnings("unchecked")
48    public Class<T> getReturnType() {
49  0 return (Class<T>) method.getReturnType();
50    }
51   
 
52  1 toggle public boolean matches(Method m) {
53  1 if (m == method) return true;
54  0 loadParameterTypes();
55  0 final Class<?>[] theirParameterTypes = m.getParameterTypes();
56  0 final Class<?> theirHolderType = m.getDeclaringClass();
57  0 if (parameterTypes.length == theirParameterTypes.length) {
58  0 return owner == theirHolderType && equals(parameterTypes, theirParameterTypes);
59    }
60  0 return false;
61    }
62   
 
63  3 toggle public boolean matches(Class<?>[] parameterTypes, String name) {
64  3 if (method.getName().matches(name)) {
65  3 loadParameterTypes();
66  3 return equals(this.parameterTypes, parameterTypes);
67    }
68  0 return false;
69    }
70   
 
71  2 toggle public boolean matches(String name) {
72  2 if (method.getName().equals(name)) {
73  2 return method.getParameterCount() == 0;
74    }
75  0 return false;
76    }
77   
 
78  14 toggle @Override
79    public Void run() {
80  14 method.setAccessible(true);
81  14 return null;
82    }
83   
 
84  2 toggle @Override
85    public boolean equals(Object o) {
86  2 if (this == o) return true;
87  2 if (o == null || getClass() != o.getClass()) return false;
88  2 MethodDescriptor<?, ?> that = (MethodDescriptor<?, ?>) o;
89  2 if (!method.equals(that.method)) return false;
90  2 if (!owner.equals(that.owner)) return false;
91  2 return Arrays.equals(parameterTypes, that.parameterTypes);
92    }
93   
 
94  2 toggle @Override
95    public int hashCode() {
96  2 int result = method.hashCode();
97  2 result = 31 * result + owner.hashCode();
98  2 result = 31 * result + Arrays.hashCode(parameterTypes);
99  2 return result;
100    }
101   
 
102  3 toggle private void loadParameterTypes() {
103  3 Class<?>[] r = parameterTypes;
104  3 if (r == null) {
105  3 synchronized (this) {
106  3 r = parameterTypes;
107  3 if (r == null) {
108  3 this.parameterTypes = method.getParameterTypes();
109    }
110    }
111    }
112    }
113   
 
114  3 toggle private boolean equals(Class<?>[] parameterTypes, Class<?>[] theirParameterTypes) {
115  3 if (parameterTypes == null && theirParameterTypes == null) return true;
116  3 if (parameterTypes == null || theirParameterTypes == null) {
117  0 return false;
118    }
119   
120  3 int len = parameterTypes.length;
121  3 int theirLen = theirParameterTypes.length;
122  3 if (len != theirLen) {
123  0 return false;
124    }
125  7 for (int i = 0; i < len; ++i) {
126  4 final Class<?> mine = parameterTypes[i];
127  4 final Class<?> theirs = parameterTypes[i];
128  4 if (!mine.equals(theirs) || !theirs.isAssignableFrom(mine)) {
129  0 return false;
130    }
131    }
132  3 return true;
133    }
134    }