| 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.structural.adapter; |
| 22 |
|
|
| 23 |
|
import java.lang.reflect.*; |
| 24 |
|
import java.util.*; |
| 25 |
|
|
| 26 |
|
import org.apache.commons.lang.*; |
| 27 |
|
import org.perfectjpattern.core.api.structural.adapter.*; |
| 28 |
|
|
| 29 |
|
|
| 30 |
|
@link |
| 31 |
|
|
| 32 |
|
|
| 33 |
|
@link |
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
@author |
| 39 |
|
@version |
| 40 |
|
|
| 41 |
|
public final |
|
|
|
| 98% |
Uncovered Elements: 1 (49) |
Complexity: 12 |
Complexity Density: 0.33 |
|
| 42 |
|
class NameMatchAdaptingStrategy |
| 43 |
|
extends ExactMatchAdaptingStrategy |
| 44 |
|
implements IAdaptingStrategy |
| 45 |
|
{ |
| 46 |
|
|
| 47 |
|
|
| 48 |
|
|
| 49 |
|
|
| 50 |
|
@link |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
@param |
| 55 |
|
|
| 56 |
|
@throws |
| 57 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 58 |
5
|
public ... |
| 59 |
|
NameMatchAdaptingStrategy(Map<String, String> aMethodsMapping) |
| 60 |
|
{ |
| 61 |
5
|
Validate.notNull(aMethodsMapping, "'aMethodsMap' must not be null"); |
| 62 |
|
|
| 63 |
5
|
theMethodsMapping = aMethodsMapping; |
| 64 |
|
} |
| 65 |
|
|
| 66 |
|
|
| 67 |
|
|
| 68 |
|
@inheritDoc |
| 69 |
|
|
|
|
|
| 96.9% |
Uncovered Elements: 1 (32) |
Complexity: 9 |
Complexity Density: 0.38 |
|
| 70 |
5
|
@Override... |
| 71 |
|
public void |
| 72 |
|
validate(Class<?> aTargetClass, Object anAdaptee, Object anAdapter) |
| 73 |
|
throws NoSuchMethodError |
| 74 |
|
{ |
| 75 |
5
|
Validate.notNull(aTargetClass, "'aTargetClass' must not be null"); |
| 76 |
5
|
Validate.notNull(anAdaptee, "'anAdaptee' must not be null"); |
| 77 |
5
|
Validate.notNull(anAdapter, "'anAdapter' must not be null"); |
| 78 |
|
|
| 79 |
5
|
Class<?> myAdapteeClass = anAdaptee.getClass(); |
| 80 |
|
|
| 81 |
|
|
| 82 |
|
|
| 83 |
|
|
| 84 |
5
|
Set<String> myTargetMethods = new HashSet<String>(); |
| 85 |
5
|
for (Method myTargetMethod : aTargetClass.getMethods()) |
| 86 |
|
{ |
| 87 |
28
|
myTargetMethods.add(myTargetMethod.getName()); |
| 88 |
|
} |
| 89 |
|
|
| 90 |
5
|
for (Map.Entry<String, String> myEntry : theMethodsMapping.entrySet()) |
| 91 |
|
{ |
| 92 |
8
|
String myTargetMethodName = myEntry.getKey(); |
| 93 |
8
|
String myAdapteeMethodName = myEntry.getValue(); |
| 94 |
|
|
| 95 |
8
|
boolean myFound = false; |
| 96 |
8
|
for (Method myAdapteeMethod : myAdapteeClass.getMethods()) |
| 97 |
|
{ |
| 98 |
30
|
if (myAdapteeMethodName.equals(myAdapteeMethod.getName())) |
| 99 |
|
{ |
| 100 |
8
|
Class<?>[] myParameterTypes = myAdapteeMethod. |
| 101 |
|
getParameterTypes(); |
| 102 |
|
|
| 103 |
8
|
try |
| 104 |
|
{ |
| 105 |
8
|
aTargetClass.getMethod(myTargetMethodName, |
| 106 |
|
myParameterTypes); |
| 107 |
|
|
| 108 |
6
|
myTargetMethods.remove(myTargetMethodName); |
| 109 |
|
|
| 110 |
6
|
myFound = true; |
| 111 |
|
|
| 112 |
6
|
break; |
| 113 |
|
} |
| 114 |
|
|
| 115 |
|
catch (Exception anException) |
| 116 |
|
|
| 117 |
|
{ |
| 118 |
|
|
| 119 |
|
} |
| 120 |
|
} |
| 121 |
|
} |
| 122 |
|
|
| 123 |
8
|
if (!myFound) |
| 124 |
|
{ |
| 125 |
|
throw new NoSuchMethodError("Method mismatch between Target " + |
| 126 |
|
"and Adaptee '" + myTargetMethodName + "'"); |
| 127 |
|
} |
| 128 |
|
} |
| 129 |
|
|
| 130 |
|
|
| 131 |
|
|
| 132 |
3
|
if (myTargetMethods.size() > 0) |
| 133 |
|
{ |
| 134 |
2
|
validate(aTargetClass, anAdaptee, myTargetMethods); |
| 135 |
2
|
validate(aTargetClass, anAdapter, myTargetMethods); |
| 136 |
|
|
| 137 |
|
|
| 138 |
2
|
if (myTargetMethods.size() > 0) |
| 139 |
|
{ |
| 140 |
|
throw new NoSuchMethodError("Not all target methods " + |
| 141 |
|
"are implemented: '" + Arrays.toString(myTargetMethods. |
| 142 |
|
toArray()) + "'"); |
| 143 |
|
} |
| 144 |
|
} |
| 145 |
|
} |
| 146 |
|
|
| 147 |
|
|
| 148 |
|
|
| 149 |
|
@inheritDoc |
| 150 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
| 151 |
5
|
@Override... |
| 152 |
|
public Method |
| 153 |
|
resolve(Class<?> aTargetClass, Object anAdaptee, Object anAdapter, |
| 154 |
|
Method aTargetMethod) |
| 155 |
|
throws IllegalArgumentException, NoSuchMethodError |
| 156 |
|
{ |
| 157 |
5
|
Validate.notNull(aTargetClass, "'aTargetClass' must not be null"); |
| 158 |
5
|
Validate.notNull(anAdaptee, "'anAdaptee' must not be null"); |
| 159 |
5
|
Validate.notNull(anAdapter, "'anAdapter' must not be null"); |
| 160 |
5
|
Validate.notNull(aTargetMethod, "'aTargetMethod' must not be null"); |
| 161 |
|
|
| 162 |
5
|
String myAdapteeMethodName = theMethodsMapping.get(aTargetMethod. |
| 163 |
|
getName()); |
| 164 |
|
|
| 165 |
5
|
Method myResultMethod = null; |
| 166 |
5
|
if (myAdapteeMethodName != null) |
| 167 |
|
{ |
| 168 |
4
|
myResultMethod = super.resolve(anAdaptee, aTargetMethod, |
| 169 |
|
myAdapteeMethodName); |
| 170 |
|
} |
| 171 |
|
else |
| 172 |
|
{ |
| 173 |
1
|
myResultMethod = super.resolve(aTargetClass, anAdaptee, anAdapter, |
| 174 |
|
aTargetMethod); |
| 175 |
|
} |
| 176 |
|
|
| 177 |
5
|
return myResultMethod; |
| 178 |
|
} |
| 179 |
|
|
| 180 |
|
|
| 181 |
|
|
| 182 |
|
|
| 183 |
|
private final Map<String, String> theMethodsMapping; |
| 184 |
|
} |