Update of /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/impl/mock
In directory sc8-pr-cvs7.sourceforge.net:/tmp/cvs-serv16534/input/javasrc/biz/xsoftware/impl/mock
Modified Files:
MethodVerifier.java
Log Message:
fixes for mocklib3
Index: MethodVerifier.java
===================================================================
RCS file: /cvsroot/mocklib/mocklib3/input/javasrc/biz/xsoftware/impl/mock/MethodVerifier.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -C2 -d -r1.2 -r1.3
*** MethodVerifier.java 14 Sep 2006 22:36:53 -0000 1.2
--- MethodVerifier.java 15 Sep 2006 16:01:17 -0000 1.3
***************
*** 2,5 ****
--- 2,8 ----
import java.lang.reflect.Method;
+ import java.util.ArrayList;
+ import java.util.Arrays;
+ import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
***************
*** 47,120 ****
}
! private static Method methodExistInThisClass(Class c, boolean isVerifyArgs, String method, Class... argTypes) {
! Method[] methods = c.getMethods();
! for(int i = 0; i < methods.length; i++) {
! if(log.isLoggable(Level.FINEST))
! log.finest("method in class='"+methods[i].getName()+"' expected='"+method+"'");
! if(method.equals(methods[i].getName())) {
! if(!isVerifyArgs)
! return methods[i];
! else if(noOverloadedVersionsExist(c, methods[i]))
! return methods[i];
! else if(paramsMatch(methods[i], argTypes))
! return methods[i];
! else {
! throwException(c, method);
! }
! }
!
! }
! return null;
}
! /**
! * @param c
* @param method
*/
! private static void throwException(Class c, String method)
{
! String msg = "The following methods match the method expected:\n";
! for(Method m : c.getMethods()) {
! if(m.getName().equals(method))
! msg += MessageHelper.getMethodSignature(m, "")+"\n";
}
! msg += "You need to clarify which method you would like to add the return value to by " +
! "specifying the Class arguments which are the type of overloaded method";
! throw new IllegalArgumentException(msg);
}
! static Method getMethod(Class[] classes, boolean isVerifyArgs, String method, Class ... argTypes) {
! if(method == null)
! throw new IllegalArgumentException("method parameter cannot be null");
! else if(MockObject.ANY.equals(method) || MockObject.NONE.equals(method))
! return null;
! else if(classes == null)
! return null;
!
! String classNames = "";
! for(int i = 0; i < classes.length; i++) {
! if(log.isLoggable(Level.FINEST))
! log.finest("class="+classes[i].getName());
! Method m = methodExistInThisClass(classes[i], isVerifyArgs, method, argTypes);
! if(m != null)
! return m;
! classNames += "\n"+classes[i];
! }
!
! String args = "";
! if(argTypes != null && argTypes.length > 0) {
! int i = 0;
! for(; i < argTypes.length-1; i++) {
! Class c = argTypes[i];
! args += c.getName()+",";
! }
! args+=argTypes[i].getName();
! }
!
! String methodSig = method+"("+args+")";
!
! throw new IllegalArgumentException("method='"+methodSig+"' is not a public method on any of the" +
! "\nfollowing Classes/Interfaces"+classNames);
! }
}
--- 50,190 ----
}
! static Method getMethod(Class[] classes, boolean isVerifyArgs, String method, Class ... argTypes) {
! if(method == null)
! throw new IllegalArgumentException("method parameter cannot be null");
! else if(MockObject.ANY.equals(method) || MockObject.NONE.equals(method))
! return null;
! else if(classes == null)
! return null;
! else if(argTypes != null) {
! for(Class argType : argTypes) {
! if(argType == null)
! throw new IllegalArgumentException("No Class parameters can be null, yet one was null");
! }
! }
!
! List<Method> methods = new ArrayList<Method>();
! for(Class c : classes) {
! if(log.isLoggable(Level.FINEST))
! log.finest("adding methods for class="+c.getName());
! Method[] classMethods = c.getMethods();
! List<Method> listedMethods = Arrays.asList(classMethods);
! methods.addAll(listedMethods);
! }
!
! List<Method> matchingMethods = findMatchingMethods(methods, method, argTypes);
!
! if(matchingMethods == null) {
! String classNames = getClassNamesString(classes);
! String methodSig = getMethodSigString(method, argTypes);
! throw new IllegalArgumentException("method='"+methodSig+"' is not a public method on any of the" +
! "\nfollowing Classes/Interfaces"+classNames);
! } else if(matchingMethods.size() == 1) {
! return matchingMethods.get(0);
! } else if(!isVerifyArgs && matchingMethods.size() > 0) {
! return null; //don't need to return anything for this.
! } else {
! String msg = "Too many methods match your method expected='"+method+"'. Methods found:\n";
! for(Method m : matchingMethods) {
! msg += MessageHelper.getMethodSignature(m, "")+"\n";
! }
! msg += "You need to clarify which method you would like to add the return value to by " +
! "specifying the Class arguments which are the type of overloaded method";
! throw new IllegalArgumentException(msg);
! }
}
! /**
* @param method
+ * @param argTypes
+ * @return
*/
! private static String getMethodSigString(String method, Class... argTypes)
{
! String args = "";
! if(argTypes != null && argTypes.length > 0) {
! int i = 0;
! for(; i < argTypes.length-1; i++) {
! Class c = argTypes[i];
! args += c.getName()+",";
! }
! args+=argTypes[i].getName();
}
!
! String methodSig = method+"("+args+")";
! return methodSig;
}
! /**
! * @param classes
! * @return
! */
! private static String getClassNamesString(Class[] classes)
! {
! String classNames = "";
! for(int i = 0; i < classes.length; i++) {
! classNames += "\n"+classes[i];
! }
! return classNames;
! }
+ /**
+ * @param methods
+ * @param method
+ * @param argTypes
+ * @return
+ */
+ private static List<Method> findMatchingMethods(List<Method> methods, String methodName, Class[] argTypes)
+ {
+ //find all methods with same name first
+ List<Method> matches = new ArrayList<Method>();
+ for(Method method : methods) {
+ if(method.getName().equals(methodName)) {
+ matches.add(method);
+ }
+ }
+ if(matches.size() == 0)
+ return null;
+
+ List<Method> narrowedMatches = new ArrayList<Method>();
+ //find one that takes no args if that is what we are looking for...
+ if(argTypes == null || argTypes.length == 0) {
+ for(Method method : matches) {
+ if(method.getParameterTypes() == null || method.getParameterAnnotations().length == 0) {
+ narrowedMatches.add(method);
+ return narrowedMatches;
+ }
+ }
+ return matches;
+ }
+
+ //we have some argType then....fine the single method....
+ for(Method method : matches) {
+ Class< ? >[] types = method.getParameterTypes();
+ if(isArgTypesMatch(types, argTypes)) {
+ narrowedMatches.add(method);
+ return narrowedMatches;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * @param types
+ * @param argTypes
+ * @return
+ */
+ private static boolean isArgTypesMatch(Class< ? >[] types, Class[] argTypes)
+ {
+ if(types.length != argTypes.length)
+ return false;
+
+ for(int i = 0; i < types.length; i++) {
+ if(!types[i].equals(argTypes[i]))
+ return false;
+ }
+ return true;
+ }
}
+
|