Allows for the calling of methods defined within ResourceHelper instances which aren't part of the core API and so which can only be called via reflection. @param action the name of the method to call (method must take a Resource instance as it's first parameter) @param resource
(String action, Resource resource, Object... params)
| 102 | * method has a void return type |
| 103 | */ |
| 104 | public Object call(String action, Resource resource, Object... params) |
| 105 | throws NoSuchMethodException, IllegalArgumentException, |
| 106 | IllegalAccessException, InvocationTargetException { |
| 107 | |
| 108 | // get all the methods defined for this instance of the helper |
| 109 | Method[] methods = this.getClass().getMethods(); |
| 110 | |
| 111 | outer: for(Method method : methods) { |
| 112 | // for each method.... |
| 113 | |
| 114 | // skip over methods which aren't public |
| 115 | if(!Modifier.isPublic(method.getModifiers())) continue; |
| 116 | |
| 117 | // if the method name doesn't match then skip onto the next method |
| 118 | if(!method.getName().equals(action)) continue; |
| 119 | |
| 120 | // get the types of the methods params |
| 121 | Class<?>[] paramTypes = method.getParameterTypes(); |
| 122 | |
| 123 | // if the method doesn't have the right number of params then skip to the |
| 124 | // next method |
| 125 | if(paramTypes.length != params.length + 1) continue; |
| 126 | |
| 127 | // check the param types and skip to the next method if they aren't |
| 128 | // compatible |
| 129 | if(!paramTypes[0].isAssignableFrom((resource.getClass()))) continue; |
| 130 | for(int i = 0; i < params.length; ++i) { |
| 131 | if(!paramTypes[i + 1].isAssignableFrom(params[i].getClass())) continue outer; |
| 132 | } |
| 133 | |
| 134 | // if we got to here then we have found a method we can call so.... |
| 135 | |
| 136 | // copy the params into a single array |
| 137 | Object[] parameters = new Object[params.length + 1]; |
| 138 | parameters[0] = resource; |
| 139 | System.arraycopy(params, 0, parameters, 1, params.length); |
| 140 | |
| 141 | // and finally call the method |
| 142 | return method.invoke(this, parameters); |
| 143 | |
| 144 | } |
| 145 | |
| 146 | throw new NoSuchMethodException("we can't find what you are looking for"); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public void cleanup() { |
nothing calls this directly
no test coverage detected