[Clirr-devel] CVS: clirr/src/java/net/sf/clirr/checks MethodSetCheck.java,1.1,1.2
Status: Alpha
Brought to you by:
lkuehne
|
From: Lars K?h. <lk...@us...> - 2004-01-18 12:53:32
|
Update of /cvsroot/clirr/clirr/src/java/net/sf/clirr/checks
In directory sc8-pr-cvs1:/tmp/cvs-serv19670/src/java/net/sf/clirr/checks
Modified Files:
MethodSetCheck.java
Log Message:
added helper method to generate method signature in error messages
Index: MethodSetCheck.java
===================================================================
RCS file: /cvsroot/clirr/clirr/src/java/net/sf/clirr/checks/MethodSetCheck.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MethodSetCheck.java 18 Jan 2004 12:04:59 -0000 1.1
+++ MethodSetCheck.java 18 Jan 2004 12:53:27 -0000 1.2
@@ -1,10 +1,14 @@
package net.sf.clirr.checks;
+import java.util.Comparator;
+import java.util.Arrays;
+
import net.sf.clirr.framework.AbstractDiffReporter;
import net.sf.clirr.framework.ClassChangeCheck;
import net.sf.clirr.framework.ApiDiffDispatcher;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
+import org.apache.bcel.generic.Type;
/**
* Checks the methods of a class.
@@ -15,6 +19,22 @@
extends AbstractDiffReporter
implements ClassChangeCheck
{
+ private class MethodComparator implements Comparator
+ {
+ public int compare(Object o1, Object o2)
+ {
+ Method m1 = (Method) o1;
+ Method m2 = (Method) o2;
+
+ String name1 = m1.getName();
+ String name2 = m2.getName();
+
+ final int nameComparison = name1.compareTo(name2);
+ // TODO: compare argcount
+ return nameComparison;
+ }
+ }
+
public MethodSetCheck(ApiDiffDispatcher dispatcher)
{
super(dispatcher);
@@ -22,22 +42,57 @@
public void check(JavaClass compatBaseline, JavaClass currentVersion)
{
- Method[] baselineMethods = compatBaseline.getMethods();
- Method[] currentMethods = currentVersion.getMethods();
+ Method[] baselineMethods = sort(compatBaseline.getMethods());
+ Method[] currentMethods = sort(currentVersion.getMethods());
- for (int i = 0; i < currentMethods.length; i++)
+ for (int i = 0; i < baselineMethods.length; i++)
{
- Method currentMethod = currentMethods[i];
- System.out.println("currentMethod " + i + "= " + currentMethod);
+ Method baselineMethod = baselineMethods[i];
+ System.out.println("baselineMethod " + i + "= " + getMethodId(compatBaseline, baselineMethod));
}
}
- /**
- * Returns an identifier for the method. If the ID has't changed
- * @return
- */
- private String getMethodId()
+ private Method[] sort(Method[] methods)
{
- return null;
+
+ Method[] retval = new Method[methods.length];
+ // TODO: filter public + protected
+ // TODO: remove <clinit>
+ System.arraycopy(methods, 0, retval, 0, methods.length);
+ Arrays.sort(retval, new MethodComparator());
+ return retval;
+ }
+
+ private String getMethodId(JavaClass clazz, Method method)
+ {
+ if (!method.isPublic() && !method.isProtected())
+ {
+ //throw new IllegalArgumentException();
+ }
+
+ StringBuffer buf = new StringBuffer();
+
+ buf.append(method.isPublic() ? "public" : "protected");
+ buf.append(" ");
+
+ String name = method.getName();
+ if ("<init>".equals(name))
+ {
+ final String className = clazz.getClassName();
+ int idx = className.lastIndexOf('.');
+ name = className.substring(idx + 1);
+ }
+ buf.append(name);
+ buf.append('(');
+ Type[] argTypes = method.getArgumentTypes();
+ String argSeparator = "";
+ for (int i = 0; i < argTypes.length; i++)
+ {
+ buf.append(argSeparator);
+ buf.append(argTypes[i].toString());
+ argSeparator = ", ";
+ }
+ buf.append(')');
+ return buf.toString();
}
}
|