| 1 |
|
package com.atlassian.core.test.util; |
| 2 |
|
|
| 3 |
|
import java.lang.reflect.*; |
| 4 |
|
import java.util.*; |
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
|
| 22 |
|
|
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
@link |
| 42 |
|
@link@link |
| 43 |
|
|
|
|
|
| 71.4% |
Uncovered Elements: 4 (14) |
Complexity: 7 |
Complexity Density: 1 |
|
| 44 |
|
public class DuckTypeProxy |
| 45 |
|
{ |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
|
| 51 |
|
public static UnimplementedMethodHandler RETURN_NULL = new UnimplementedMethodHandler() |
| 52 |
|
{ |
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 53 |
0
|
public Object methodNotImplemented(Method method, Object[] args)... |
| 54 |
|
{ |
| 55 |
0
|
return null; |
| 56 |
|
} |
| 57 |
|
}; |
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
public static UnimplementedMethodHandler THROW = new UnimplementedMethodHandler() |
| 63 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 64 |
1
|
public Object methodNotImplemented(Method method, Object[] args)... |
| 65 |
|
{ |
| 66 |
1
|
throw new UnsupportedOperationException(method.toString()); |
| 67 |
|
} |
| 68 |
|
}; |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
@link |
| 75 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 76 |
1
|
public staticObject getProxy(Class implementingClass, List delegates)... |
| 77 |
|
{ |
| 78 |
1
|
return getProxy(new Class[] { implementingClass }, delegates); |
| 79 |
|
} |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 84 |
5
|
public staticObject getProxy(Class implementingClass, List delegates,... |
| 85 |
|
UnimplementedMethodHandler unimplementedMethodHandler) |
| 86 |
|
{ |
| 87 |
5
|
return getProxy(new Class[] { implementingClass }, delegates, unimplementedMethodHandler); |
| 88 |
|
} |
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
@link@link |
| 93 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 94 |
1
|
public static Object getProxy(Class[] implementingClasses, List delegates)... |
| 95 |
|
{ |
| 96 |
1
|
return getProxy(implementingClasses, delegates, THROW); |
| 97 |
|
} |
| 98 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 99 |
6
|
public static Object getProxy(Class[] implementingClasses, List delegates, UnimplementedMethodHandler unimplementedMethodHandler)... |
| 100 |
|
{ |
| 101 |
6
|
return Proxy.newProxyInstance(DuckTypeProxy.class.getClassLoader(), implementingClasses, new DuckTypeInvocationHandler(delegates, |
| 102 |
|
unimplementedMethodHandler)); |
| 103 |
|
} |
| 104 |
|
|
| 105 |
|
|
| 106 |
|
|
| 107 |
|
@link |
| 108 |
|
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 109 |
0
|
public staticObject getProxy(Class implementingClass, Object delegate)... |
| 110 |
|
{ |
| 111 |
0
|
return getProxy(new Class[] { implementingClass }, Arrays.asList(new Object[] {delegate})); |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 0 |
Complexity Density: - |
|
| 116 |
|
public interface UnimplementedMethodHandler |
| 117 |
|
{ |
| 118 |
|
Object methodNotImplemented(Method method, Object[] args); |
| 119 |
|
} |
| 120 |
|
|
| 121 |
|
|
| 122 |
|
|
| 123 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 7 |
Complexity Density: 0.54 |
|
| 124 |
|
private static class DuckTypeInvocationHandler implements InvocationHandler |
| 125 |
|
{ |
| 126 |
|
private final List delegates; |
| 127 |
|
private final UnimplementedMethodHandler unimplementedMethodHandler; |
| 128 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 129 |
6
|
DuckTypeInvocationHandler(List handlers, UnimplementedMethodHandler unimplementedMethodHandler)... |
| 130 |
|
{ |
| 131 |
6
|
this.delegates = Collections.unmodifiableList(new ArrayList(handlers)); |
| 132 |
6
|
this.unimplementedMethodHandler = unimplementedMethodHandler; |
| 133 |
|
} |
| 134 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 6 |
Complexity Density: 0.55 |
|
| 135 |
6
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable... |
| 136 |
|
{ |
| 137 |
8
|
for (Iterator it = delegates.iterator(); it.hasNext();) |
| 138 |
|
{ |
| 139 |
7
|
Object handler = it.next(); |
| 140 |
7
|
Method duckTypeMethod; |
| 141 |
7
|
try |
| 142 |
|
{ |
| 143 |
7
|
duckTypeMethod = handler.getClass().getMethod(method.getName(), method.getParameterTypes()); |
| 144 |
|
} |
| 145 |
|
catch (NoSuchMethodException ignoreAndTryNext) |
| 146 |
|
{ |
| 147 |
2
|
continue; |
| 148 |
|
} |
| 149 |
5
|
try |
| 150 |
|
{ |
| 151 |
5
|
duckTypeMethod.setAccessible(true); |
| 152 |
5
|
return duckTypeMethod.invoke(handler, args); |
| 153 |
|
} |
| 154 |
|
catch (IllegalArgumentException ignoreAndContinue) |
| 155 |
|
{ |
| 156 |
|
|
| 157 |
|
} |
| 158 |
|
catch (IllegalAccessException ignoreAndContinue) |
| 159 |
|
{ |
| 160 |
|
|
| 161 |
|
} |
| 162 |
|
catch (InvocationTargetException e) |
| 163 |
|
{ |
| 164 |
1
|
throw e.getCause(); |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
1
|
return unimplementedMethodHandler.methodNotImplemented(method, args); |
| 168 |
|
} |
| 169 |
|
} |
| 170 |
|
} |