| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
|
| 21 |
|
package org.perfectjpattern.core.extras.delegate; |
| 22 |
|
|
| 23 |
|
import java.lang.reflect.*; |
| 24 |
|
import java.util.*; |
| 25 |
|
|
| 26 |
|
import org.apache.commons.lang.*; |
| 27 |
|
import org.perfectjpattern.core.api.extras.delegate.*; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
|
| 31 |
|
|
| 32 |
|
|
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
@see |
| 38 |
|
|
| 39 |
|
@param |
| 40 |
|
|
| 41 |
|
@author |
| 42 |
|
@author |
| 43 |
|
|
| 44 |
|
@author |
| 45 |
|
@version |
| 46 |
|
|
| 47 |
|
@SuppressWarnings("unchecked") |
| 48 |
|
public abstract |
|
|
|
| 92.8% |
Uncovered Elements: 5 (69) |
Complexity: 21 |
Complexity Density: 0.48 |
|
| 49 |
|
class AbstractDelegator<I> |
| 50 |
|
implements IDelegator<I> |
| 51 |
|
{ |
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
@param |
| 60 |
|
@param |
| 61 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 62 |
5
|
protected... |
| 63 |
|
AbstractDelegator(Class aReturnClass, Class... aParameters) |
| 64 |
|
{ |
| 65 |
5
|
theReturn = aReturnClass; |
| 66 |
5
|
theArguments = aParameters; |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
@param |
| 75 |
|
@throws |
| 76 |
|
|
| 77 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 78 |
49
|
protected... |
| 79 |
|
AbstractDelegator(Class<I> anInterface) |
| 80 |
|
{ |
| 81 |
49
|
Method myMethod = findMethod(anInterface); |
| 82 |
|
|
| 83 |
47
|
theReturn = myMethod.getReturnType(); |
| 84 |
47
|
theArguments = myMethod.getParameterTypes(); |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
|
| 92 |
|
@param |
| 93 |
|
@param |
| 94 |
|
@param |
| 95 |
|
@return |
| 96 |
|
|
| 97 |
|
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 98 |
1
|
@SuppressWarnings("unchecked")... |
| 99 |
|
protected boolean |
| 100 |
|
isSuitableMethod(Method aTestMethod, Class aReturnClass, |
| 101 |
|
Class... anArguments) |
| 102 |
|
{ |
| 103 |
1
|
Class[] myMethodArgs = aTestMethod.getParameterTypes(); |
| 104 |
|
|
| 105 |
2
|
for (int i = 0; i < myMethodArgs.length; i++) |
| 106 |
|
{ |
| 107 |
1
|
Class myArgument = myMethodArgs[i]; |
| 108 |
|
|
| 109 |
1
|
if (!myArgument.isAssignableFrom(anArguments[i])) |
| 110 |
|
{ |
| 111 |
0
|
return false; |
| 112 |
|
} |
| 113 |
|
} |
| 114 |
|
|
| 115 |
1
|
return isValidReturn(aTestMethod, aReturnClass); |
| 116 |
|
} |
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
|
|
| 121 |
|
|
| 122 |
|
@param |
| 123 |
|
@param |
| 124 |
|
@return |
| 125 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 126 |
14
|
@SuppressWarnings("unchecked")... |
| 127 |
|
protected boolean |
| 128 |
|
isValidReturn(Method aTestMethod, Class aReturnClass) |
| 129 |
|
{ |
| 130 |
14
|
return (aReturnClass == null |
| 131 |
|
|| aTestMethod.getReturnType() == aReturnClass |
| 132 |
|
|| aTestMethod.getReturnType().equals(aReturnClass) |
| 133 |
|
|| aReturnClass.isAssignableFrom(aTestMethod.getReturnType())); |
| 134 |
|
} |
| 135 |
|
|
| 136 |
|
|
| 137 |
|
|
| 138 |
|
|
| 139 |
|
|
| 140 |
|
|
| 141 |
|
@param |
| 142 |
|
@param |
| 143 |
|
@param |
| 144 |
|
@return |
| 145 |
|
|
| 146 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 5 |
Complexity Density: 0.71 |
|
| 147 |
37
|
@SuppressWarnings("unchecked")... |
| 148 |
|
protected static Method[] |
| 149 |
|
findMethod(Class aTargetClass, String aMethodName, |
| 150 |
|
int aNumberOfArguments) |
| 151 |
|
{ |
| 152 |
37
|
Method[] myPossibilities = aTargetClass.getMethods(); |
| 153 |
37
|
List myHolder = new ArrayList(); |
| 154 |
|
|
| 155 |
579
|
for (int i = 0; i < myPossibilities.length; i++) |
| 156 |
|
{ |
| 157 |
542
|
Method myPossibility = myPossibilities[i]; |
| 158 |
|
|
| 159 |
542
|
if (myPossibility.getName().equals(aMethodName) && |
| 160 |
|
myPossibility.getParameterTypes().length == |
| 161 |
|
aNumberOfArguments && Modifier.isPublic(myPossibility. |
| 162 |
|
getModifiers())) |
| 163 |
|
{ |
| 164 |
37
|
myHolder.add(myPossibility); |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
|
|
| 168 |
37
|
return (Method[]) myHolder.toArray(EMPTY_METHOD_ARRAY); |
| 169 |
|
} |
| 170 |
|
|
| 171 |
|
|
| 172 |
|
|
| 173 |
|
|
| 174 |
|
|
| 175 |
|
|
| 176 |
|
@param |
| 177 |
|
@param |
| 178 |
|
@param |
| 179 |
|
@return |
| 180 |
|
|
| 181 |
|
@throws |
| 182 |
|
@throws |
| 183 |
|
@throws |
| 184 |
|
|
|
|
|
| 87.5% |
Uncovered Elements: 2 (16) |
Complexity: 5 |
Complexity Density: 0.42 |
|
| 185 |
48
|
@SuppressWarnings("unchecked")... |
| 186 |
|
protected Method |
| 187 |
|
findMethod(Class aTargetClass, String aMethodName, |
| 188 |
|
AbstractDelegator aDelegator) |
| 189 |
|
throws IllegalArgumentException, UnsupportedOperationException |
| 190 |
|
{ |
| 191 |
48
|
Class[] myArguments = aDelegator.getArguments(); |
| 192 |
48
|
Class myReturnClass = aDelegator.getReturn(); |
| 193 |
|
|
| 194 |
48
|
try |
| 195 |
|
{ |
| 196 |
48
|
Method myReturn = aTargetClass.getMethod(aMethodName, myArguments); |
| 197 |
|
|
| 198 |
13
|
Validate.isTrue(isValidReturn(myReturn, myReturnClass), |
| 199 |
|
"Requested method returns wrong type."); |
| 200 |
|
|
| 201 |
13
|
Validate.isTrue(Modifier.isPublic(myReturn.getModifiers()), |
| 202 |
|
"Requested method is not public."); |
| 203 |
|
|
| 204 |
13
|
return myReturn; |
| 205 |
|
} |
| 206 |
|
catch (SecurityException anException) |
| 207 |
|
{ |
| 208 |
|
|
| 209 |
|
} |
| 210 |
|
catch (NoSuchMethodException anException) |
| 211 |
|
{ |
| 212 |
|
|
| 213 |
|
} |
| 214 |
|
|
| 215 |
35
|
Method[] myPossibilities = findMethod(aTargetClass, aMethodName, |
| 216 |
|
myArguments.length); |
| 217 |
|
|
| 218 |
35
|
for (int i = 0; i < myPossibilities.length; i++) |
| 219 |
|
{ |
| 220 |
35
|
Method myPossibility = myPossibilities[i]; |
| 221 |
|
|
| 222 |
35
|
if (isSuitableMethod(myPossibility, myReturnClass, myArguments)) |
| 223 |
|
{ |
| 224 |
35
|
return myPossibility; |
| 225 |
|
} |
| 226 |
|
} |
| 227 |
|
|
| 228 |
|
throw new UnsupportedOperationException("No suitable method found."); |
| 229 |
|
} |
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
@param |
| 236 |
|
@return |
| 237 |
|
@throws |
| 238 |
|
@throws |
| 239 |
|
|
| 240 |
|
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 3 |
Complexity Density: 0.27 |
|
| 241 |
49
|
protected static Method ... |
| 242 |
|
findMethod(Class anInterface) |
| 243 |
|
{ |
| 244 |
49
|
Validate.notNull(anInterface, "'anInterface' must not be null"); |
| 245 |
49
|
Validate.isTrue(anInterface.isInterface(), |
| 246 |
|
"DelegateTemplate must be constructed with an interface"); |
| 247 |
|
|
| 248 |
49
|
Method[] myMethods = anInterface.getMethods(); |
| 249 |
49
|
Method myReturn = null; |
| 250 |
|
|
| 251 |
97
|
for (int i = 0; i < myMethods.length; i++) |
| 252 |
|
{ |
| 253 |
49
|
Method myTest = myMethods[i]; |
| 254 |
|
|
| 255 |
49
|
if (Modifier.isAbstract(myTest.getModifiers())) |
| 256 |
|
{ |
| 257 |
49
|
Validate.isTrue(myReturn == null, "DelegateTemplate must be " + |
| 258 |
|
"constructed with an interface implementing exactly one " + |
| 259 |
|
"method!"); |
| 260 |
|
|
| 261 |
48
|
myReturn = myTest; |
| 262 |
|
} |
| 263 |
|
} |
| 264 |
|
|
| 265 |
48
|
Validate.notNull(myReturn, "DelegateTemplate must be constructed " + |
| 266 |
|
"with an interface implementing exactly one method!"); |
| 267 |
|
|
| 268 |
47
|
return myReturn; |
| 269 |
|
} |
| 270 |
|
|
| 271 |
|
|
| 272 |
|
|
| 273 |
|
|
| 274 |
|
|
| 275 |
|
@return |
| 276 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 277 |
91
|
protected Class ... |
| 278 |
|
getReturn() |
| 279 |
|
{ |
| 280 |
91
|
return theReturn; |
| 281 |
|
} |
| 282 |
|
|
| 283 |
|
|
| 284 |
|
|
| 285 |
|
|
| 286 |
|
|
| 287 |
|
@return |
| 288 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 289 |
30413
|
protected Class[] ... |
| 290 |
|
getArguments() |
| 291 |
|
{ |
| 292 |
30413
|
return theArguments; |
| 293 |
|
} |
| 294 |
|
|
| 295 |
|
|
| 296 |
|
|
| 297 |
|
|
| 298 |
|
protected |
|
|
|
| 100% |
Uncovered Elements: 0 (25) |
Complexity: 10 |
Complexity Density: 0.67 |
|
| 299 |
|
class DelegateProxy |
| 300 |
|
implements IDelegate, InvocationHandler |
| 301 |
|
{ |
| 302 |
|
|
| 303 |
|
|
| 304 |
|
|
| 305 |
|
|
| 306 |
|
|
| 307 |
|
@param |
| 308 |
|
@param |
| 309 |
|
@param |
| 310 |
|
@param |
| 311 |
|
@throws |
| 312 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 313 |
48
|
public ... |
| 314 |
|
DelegateProxy(Object aTarget, Class aTargetClass, String aMethodName, |
| 315 |
|
AbstractDelegator aTemplate) |
| 316 |
|
throws UnsupportedOperationException |
| 317 |
|
{ |
| 318 |
48
|
theTarget = aTarget; |
| 319 |
|
|
| 320 |
48
|
Method myMethod = |
| 321 |
|
findMethod(aTargetClass, aMethodName, aTemplate); |
| 322 |
48
|
theMethod = myMethod; |
| 323 |
|
} |
| 324 |
|
|
| 325 |
|
|
| 326 |
|
|
| 327 |
|
@inheritDoc |
| 328 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 329 |
30357
|
public Object ... |
| 330 |
|
invoke(Object aProxy, Method aMethod, Object[] anArguments) |
| 331 |
|
{ |
| 332 |
30357
|
return invoke(anArguments); |
| 333 |
|
} |
| 334 |
|
|
| 335 |
|
|
| 336 |
|
|
| 337 |
|
@inheritDoc |
| 338 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 3 |
Complexity Density: 0.75 |
|
| 339 |
30365
|
public Object ... |
| 340 |
|
invoke(Object... anArguments) |
| 341 |
|
throws IllegalArgumentException, UnsupportedOperationException |
| 342 |
|
{ |
| 343 |
30365
|
validateArgs(anArguments, getArguments()); |
| 344 |
|
|
| 345 |
30365
|
try |
| 346 |
|
{ |
| 347 |
30365
|
Object myReturn = getMethod().invoke(getTarget(), anArguments); |
| 348 |
|
|
| 349 |
30365
|
return myReturn; |
| 350 |
|
} |
| 351 |
|
catch (IllegalAccessException anException) |
| 352 |
|
{ |
| 353 |
|
throw new UnsupportedOperationException( |
| 354 |
|
anException.getMessage()); |
| 355 |
|
} |
| 356 |
|
catch (InvocationTargetException anException) |
| 357 |
|
{ |
| 358 |
|
throw new UnsupportedOperationException(anException.getCause()); |
| 359 |
|
} |
| 360 |
|
} |
| 361 |
|
|
| 362 |
|
|
| 363 |
|
|
| 364 |
|
|
| 365 |
|
|
| 366 |
|
|
| 367 |
|
@param |
| 368 |
|
@param |
| 369 |
|
@throws |
| 370 |
|
|
| 371 |
|
@throws |
| 372 |
|
@throws |
| 373 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
| 374 |
30365
|
protected void ... |
| 375 |
|
validateArgs(Object[] anArguments, Class[] anExpectedArguments) |
| 376 |
|
throws IllegalArgumentException |
| 377 |
|
{ |
| 378 |
30365
|
Validate.notNull(anExpectedArguments, |
| 379 |
|
"'anExpectedArguments' must not be null"); |
| 380 |
|
|
| 381 |
30365
|
Validate.isTrue((anArguments == null && anExpectedArguments.length |
| 382 |
|
== 0) || (anArguments != null && anArguments.length == |
| 383 |
|
anExpectedArguments.length), "Delegator required '" + |
| 384 |
|
anExpectedArguments.length + "' arguments"); |
| 385 |
|
|
| 386 |
30365
|
if (anArguments != null) |
| 387 |
|
{ |
| 388 |
60724
|
for (int i = 0; i < anArguments.length; i++) |
| 389 |
|
{ |
| 390 |
30362
|
Validate.isTrue(anExpectedArguments[i].isInstance( |
| 391 |
|
anArguments[i]), "Argument '" + i + "' must be of " + |
| 392 |
|
"type '" + anExpectedArguments[i].getName() + "'"); |
| 393 |
|
} |
| 394 |
|
} |
| 395 |
|
} |
| 396 |
|
|
| 397 |
|
|
| 398 |
|
|
| 399 |
|
|
| 400 |
|
|
| 401 |
|
@return |
| 402 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 403 |
30365
|
protected Method ... |
| 404 |
|
getMethod() |
| 405 |
|
{ |
| 406 |
30365
|
return theMethod; |
| 407 |
|
} |
| 408 |
|
|
| 409 |
|
|
| 410 |
|
|
| 411 |
|
|
| 412 |
|
|
| 413 |
|
@return |
| 414 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 415 |
30365
|
protected Object ... |
| 416 |
|
getTarget() |
| 417 |
|
{ |
| 418 |
30365
|
return theTarget; |
| 419 |
|
} |
| 420 |
|
|
| 421 |
|
|
| 422 |
|
|
| 423 |
|
|
| 424 |
|
private final Method theMethod; |
| 425 |
|
private final Object theTarget; |
| 426 |
|
} |
| 427 |
|
|
| 428 |
|
|
| 429 |
|
|
| 430 |
|
|
| 431 |
|
private static final Method[] EMPTY_METHOD_ARRAY = {}; |
| 432 |
|
|
| 433 |
|
private final Class theReturn; |
| 434 |
|
private final Class[] theArguments; |
| 435 |
|
} |