/** * Generates a new error message based on the excpetion message. * * @param message the rror message */ public void generateErrorMessage(String message, MethodDescription mDesc) { ObjectDescription oDesc = getObjectDescription(getObject(mDesc.getObjectID())); String methodName = oDesc.getName() + "." + mDesc.getMethodName() + "()"; System.err.println("Method \"" + methodName + "\" can't be invoked:" + message); }
/** * Returns the description of a method. * * @param o object of the method * @param methodName name of the method * @param params method parameters * @return description of the method or <code>null</code> if the requested method could not be * found */ public MethodDescription getMethodDescription(Object o, String methodName, Class... params) { ObjectDescription oDesc = getObjectDescription(o); MethodDescription result = null; ArrayList<Class> paramTypes = new ArrayList<Class>(); for (Class<?> p : params) { // Class c = p.getClass(); paramTypes.add(p); } for (MethodDescription mDesc : oDesc.getMethods()) { if (mDesc.getMethodName().equals(methodName)) { // System.out.println("M: " + mDesc.getMethodName()); boolean isEqual = true; for (int i = 0; i < mDesc.getParameterTypes().length; i++) { // System.out.println(">> P: " + // mDesc.getParameterTypes()[i].getName()); // System.out.println(">> PE: " + paramTypes.get(i).getName()); if (!paramTypes.get(i).equals(mDesc.getParameterTypes()[i])) { isEqual = false; break; } } if (isEqual) { result = mDesc; result.setParameters(params); break; } } } // if (result == null) { // System.err.println(">> MethodDescription not found!" // + " Wrong name or wrong parameters?"); // } return result; }
/** * Returns the description of a method. * * @param o object of the method * @param methodName name of the method * @param params method parameters * @return description of the method */ public MethodDescription getMethodDescriptionFromGUI( Object o, String methodName, Object... params) { ObjectDescription oDesc = getObjectDescription(o); MethodDescription result = null; // ArrayList<Class> paramTypes = new ArrayList<Class>(); // // for (Object p : params) { // Class c = p.getClass(); // paramTypes.add(c); // } for (MethodDescription mDesc : oDesc.getMethods()) { if (mDesc.getMethodName().equals(methodName)) { boolean isEqual = true; for (int i = 0; i < mDesc.getParameterTypes().length; i++) { if (!params[i].equals(mDesc.getParameterTypes()[i])) { isEqual = false; break; } } if (isEqual) { result = mDesc; result.setParameters(params); break; } } } if (result == null) { System.out.println(">> MethodDescription not found!" + " Wrong name or wrong parameters?"); } return result; }
/** * Invokes the method specified by a method description. * * @param methodDescription the method description of the method that is to be invoked. */ protected void invoke(MethodDescription methodDescription) throws InvocationTargetException { VParamUtil.throwIfNull(methodDescription); Object o = getObject(methodDescription.getObjectID()); ObjectDescription oDesc = getObjectDescription(o); if (o == null) { throw new IllegalStateException("Object must not be null!"); } if (methodDescription.getMethodType() == MethodType.REFERENCE) { Object[] parameters = methodDescription.getParameters(); // error handling if (parameters.length > 1) { throw new IllegalArgumentException( "More than one parameter " + "for reference methods is not allowed!"); } if (parameters.length > 0 && parameters[0] != null) { methodDescription.setReturnValue(parameters[0]); } else { methodDescription.setReturnValue(o); } } else if (o instanceof ProxyObject) { ProxyObject proxy = (ProxyObject) o; methodDescription.setReturnValue(proxy.invoke(methodDescription)); if (!methodDescription .getReturnType() .equals(methodDescription.getReturnValue().getClass())) { System.err.println(">> ProxyObject: wrong type of return value!"); generateErrorMessage(">> ProxyObject:" + " wrong type of return value!", methodDescription); } } else if (methodDescription.getMethodType() == MethodType.DEFAULT || methodDescription.getMethodType() == MethodType.CUSTOM_REFERENCE) { Method m = null; try { m = o.getClass() .getMethod( methodDescription.getMethodName(), methodDescription.getParameterTypes()); // access this method even if language visibility forbids // invocation of this method m.setAccessible(true); methodDescription.setReturnValue(m.invoke(o, methodDescription.getParameters())); } catch (NoSuchMethodException ex) { Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex); generateErrorMessage( methodDescription.getMethodName() + "(): " + ex.toString(), methodDescription); } catch (IllegalAccessException ex) { Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex); generateErrorMessage( methodDescription.getMethodName() + "(): " + ex.toString(), methodDescription); } catch (IllegalArgumentException ex) { System.err.println(">> ObjectInspector:" + " invoked method with wrong arguments!"); String yourParamStr = ""; String expectedParamStr = ""; if (methodDescription.getParameters() != null) { for (int i = 0; i < methodDescription.getParameters().length; i++) { Object parameter = methodDescription.getParameters()[i]; if (parameter != null) { yourParamStr += parameter.getClass().getName() + " "; } else { yourParamStr += "null "; } } } else { yourParamStr = "no parameters given!"; } for (int i = 0; i < m.getParameterTypes().length; i++) { expectedParamStr += m.getParameterTypes()[i].getName() + " "; } System.err.println(" your parameters: " + yourParamStr); System.err.println(" expected parameters: " + expectedParamStr); generateErrorMessage( methodDescription.getMethodName() + "(): method invoked with wrong arguments!", methodDescription); } catch (InvocationTargetException ex) { Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex); generateErrorMessage(methodDescription, ex.getCause()); throw ex; } catch (SecurityException ex) { Logger.getLogger(ObjectInspector.class.getName()).log(Level.SEVERE, null, ex); generateErrorMessage( methodDescription.getMethodName() + "(): " + ex.toString(), methodDescription); } } // end else if (o instanceof ProxyObject) }