Clover coverage report - CRISPY - 1.1.1
Coverage timestamp: Mi Nov 15 2006 13:09:46 CET
file stats: LOC: 203   Methods: 11
NCLOC: 159   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Invoker.java 100% 99% 90,9% 98,8%
coverage coverage
 1    package net.sf.crispy.util;
 2   
 3    import java.lang.reflect.Method;
 4    import java.security.AccessController;
 5    import java.util.ArrayList;
 6    import java.util.HashMap;
 7    import java.util.List;
 8    import java.util.Map;
 9    import java.util.Vector;
 10   
 11   
 12    /**
 13    * Work with reflection API.
 14    *
 15    * @author Linke
 16    *
 17    */
 18    public final class Invoker {
 19   
 20  0 private Invoker() { super(); }
 21   
 22    private static Map compatibleTypes = new HashMap();
 23   
 24    /**
 25    * Invoke method from pvObject with the method-name (pvMethodName) and parameter (pvPrams) via reflection.
 26    * @param pvObject object for the method call
 27    * @param pvMethodName name of method
 28    * @param pvParams parameter for method
 29    * @return the return value from the method call
 30    * @throws Exception
 31    */
 32  4 public static Object invokeWithConverter (Object pvObject, String pvMethodName, Vector pvParams, Converter pvConverter) throws Exception {
 33  4 return invoke (pvObject, pvMethodName, pvParams, true, pvConverter);
 34    }
 35   
 36  4 public static Object invoke (String pvClassName, String pvMethodName, Vector pvParams, boolean pvWithConverter, Converter pvConverter) throws Exception {
 37  4 Class lvClass = Class.forName(pvClassName);
 38  4 Object lvObject = lvClass.newInstance();
 39  4 return invoke (lvObject, pvMethodName, pvParams, pvWithConverter, pvConverter);
 40    }
 41   
 42  66 public static Object invoke (Object pvObject, String pvMethodName, Vector pvParams, boolean pvWithConverter, Converter pvConverter) throws Exception {
 43  66 Class clazz = pvObject.getClass();
 44  66 Method lvMethod = findMethod(clazz, pvMethodName, pvParams);
 45  65 Converter lvConverter = (pvWithConverter == true ? pvConverter : null);
 46  65 Object[] lvArgs = vector2Array(pvParams, lvMethod, lvConverter);
 47  65 AccessController.doPrivileged(new MethodAccessiblePrivilegedAction(lvMethod));
 48  65 Object lvMethodResult = null;
 49  65 try {
 50  65 lvMethodResult = lvMethod.invoke(pvObject, lvArgs);
 51    } catch (IllegalArgumentException e) {
 52  1 throw new IllegalArgumentException("Illegal argument by method: " + lvMethod + " and args: " + pvParams + " with object: " + pvObject);
 53    }
 54  62 if (pvWithConverter == true) {
 55  6 lvMethodResult = lvConverter.makeSimple (lvMethodResult);
 56    }
 57  62 return lvMethodResult;
 58    }
 59   
 60  213 public static Object[] vector2Array(Vector pvVector, Method pvMethod, Converter pvConverter) throws Exception {
 61  213 Object[] lvArgs = new Object[pvVector.size()];
 62  213 int lvMethodParamsLength = pvMethod.getParameterTypes().length;
 63  213 int lvVectorSize = pvVector.size();
 64  213 for (int j = 0; j < lvArgs.length; j++) {
 65  285 if (pvConverter != null) {
 66  208 if (lvMethodParamsLength == lvVectorSize) {
 67    // if (pvMethod.getParameterTypes().length > j) {
 68  182 Class lvParamType = pvMethod.getParameterTypes()[j];
 69  182 if (lvParamType.isArray()) {
 70  9 Class lvType = lvParamType.getComponentType();
 71  9 lvArgs[j] = pvConverter.makeComplex (pvVector.get(j), lvParamType, lvType);
 72    } else {
 73  173 lvArgs[j] = pvConverter.makeComplex (pvVector.get(j), lvParamType);
 74    }
 75    }
 76    // pvVector.size > pvMethod.getParameterTypes().length
 77    // ??? Critical Point ???
 78    else {
 79  26 lvArgs[j] = pvConverter.makeComplex (pvVector.get(j));
 80    }
 81    }
 82    // Converter == null
 83    else {
 84  77 lvArgs[j] = pvVector.get(j);
 85    }
 86    }
 87  213 return lvArgs;
 88    }
 89   
 90  263 public static boolean isCompatibleType (Class pvType1, Class pvType2) {
 91  263 if (compatibleTypes.size() == 0) {
 92  11 compatibleTypes.put(byte.class, Byte.class);
 93  11 compatibleTypes.put(short.class, Short.class);
 94  11 compatibleTypes.put(int.class, Integer.class);
 95  11 compatibleTypes.put(long.class, Long.class);
 96  11 compatibleTypes.put(float.class, Float.class);
 97  11 compatibleTypes.put(double.class, Double.class);
 98  11 compatibleTypes.put(char.class, Character.class);
 99  11 compatibleTypes.put(boolean.class, Boolean.class);
 100   
 101  11 compatibleTypes.put(Long.class, long.class);
 102    }
 103  263 Class lvType = (Class) compatibleTypes.get(pvType1);
 104  263 boolean lvReturn = false;
 105  263 if (lvType != null) {
 106  225 lvReturn = (compatibleTypes.get(pvType1).equals(pvType2));
 107    }
 108  263 return lvReturn;
 109    }
 110   
 111  485 public static Method findMethod(Class pvClass, String pvMethodName, List pvParams) throws Exception {
 112  485 List lvParams = pvParams;
 113  5 if (lvParams == null) { lvParams = new ArrayList(0); }
 114  485 Method[] lvMethods = findAllMethods(pvClass, pvMethodName);
 115  485 Method lvMerkMethod = null;
 116  485 for (int i = 0; i < lvMethods.length; i++) {
 117  601 Class lvParamTypes[] = lvMethods[i].getParameterTypes();
 118   
 119    // Methoden-Parameter und pvParams muessen gleiche Anzahl haben
 120  601 if (lvParamTypes.length == lvParams.size()) {
 121    // wenn Name und Anzahl Parameter uebereinstimmen, dann Methode merken
 122  589 lvMerkMethod = lvMethods[i];
 123   
 124  589 boolean lvParamOk = true;
 125  589 for (int j = 0; j < lvParamTypes.length; j++) {
 126  666 if (lvParamTypes[j].isArray()) {
 127  52 Class lvParamArrayType = Converter.getArrayType(lvParams.get(j));
 128    // Typen im Array stimmen NICHT ueberein
 129  52 if (!lvParamTypes[j].getComponentType().equals(lvParamArrayType)) {
 130  31 lvParamOk = false;
 131  31 break;
 132    }
 133    } else {
 134  614 Class lvParamClass = (lvParams.get(j) == null ? null : lvParams.get(j).getClass());
 135  614 if ((!lvParamTypes[j].equals(lvParamClass)) && (!isCompatibleType(lvParamTypes[j], lvParamClass))) {
 136  161 lvParamOk = false;
 137  161 break;
 138    }
 139    }
 140    }
 141    // Methodenname und Parameter stimmen ueberein
 142  589 if (lvParamOk) {
 143  397 return lvMethods[i];
 144    }
 145    }
 146    }
 147   
 148  88 if (lvMerkMethod != null) {
 149  74 return lvMerkMethod;
 150    }
 151    // ??? if the method has the right name, but the size of parameters is not correct ???
 152    // ??? by overloading can the chois from this method false ???
 153  14 else if (lvMethods.length > 0) {
 154  10 return lvMethods[0];
 155    }
 156  4 throw new NoSuchMethodException("For class: " + pvClass
 157    + " with method: " + pvMethodName + " with parameter: " + lvParams);
 158    }
 159   
 160  499 public static Method[] findAllMethods(Class pvClass, String pvMethodName) throws Exception {
 161  499 Method[] lvMethods = pvClass.getMethods();
 162  499 ArrayList lvFindedMethods = new ArrayList();
 163  499 for (int i = 0; i < lvMethods.length; i++) {
 164  11157 if (lvMethods[i].getName().equals(pvMethodName)){
 165  657 lvFindedMethods.add(lvMethods[i]);
 166    }
 167    }
 168  499 return (Method[]) lvFindedMethods.toArray(new Method [lvFindedMethods.size()]);
 169    }
 170   
 171  136 public static Vector array2Vector (Object [] pvArray) throws Exception {
 172  136 Vector param = new Vector();
 173  136 if (pvArray != null){
 174  129 for (int i = 0; i < pvArray.length; i++) {
 175  176 param.add(pvArray[i]);
 176    }
 177    }
 178  136 return param;
 179    }
 180   
 181  2 public static Vector array2SimpleVector (Object [] pvArray) throws Exception {
 182  2 Vector param = new Vector();
 183  2 if (pvArray != null){
 184  1 Converter lvConverter = new Converter();
 185  1 for (int i = 0; i < pvArray.length; i++) {
 186  2 param.add(lvConverter.makeSimple(pvArray[i]));
 187    }
 188    }
 189  2 return param;
 190    }
 191   
 192  136 public static Object[] array2SimpleArray (Object [] pvArray, Converter pvConverter) throws Exception {
 193  136 Object ret[] = null;
 194  136 if (pvArray != null){
 195  129 ret = new Object[pvArray.length];
 196  129 for (int i = 0; i < pvArray.length; i++) {
 197  176 ret[i] = pvConverter.makeSimple(pvArray[i]);
 198    }
 199    }
 200  136 return ret;
 201    }
 202   
 203    }