[Clirr-devel] CVS: clirr/src/java/net/sf/clirr/checks MethodSetCheck.java,1.5,1.6
Status: Alpha
Brought to you by:
lkuehne
From: <lk...@us...> - 2004-06-05 16:26:22
|
Update of /cvsroot/clirr/clirr/src/java/net/sf/clirr/checks In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24017/java/net/sf/clirr/checks Modified Files: MethodSetCheck.java Log Message: detect 'pull up method' refactoring Index: MethodSetCheck.java =================================================================== RCS file: /cvsroot/clirr/clirr/src/java/net/sf/clirr/checks/MethodSetCheck.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- MethodSetCheck.java 22 May 2004 13:26:03 -0000 1.5 +++ MethodSetCheck.java 5 Jun 2004 16:26:14 -0000 1.6 @@ -21,6 +21,7 @@ import net.sf.clirr.event.ApiDifference; import net.sf.clirr.event.Severity; +import net.sf.clirr.event.ScopeSelector; import net.sf.clirr.framework.AbstractDiffReporter; import net.sf.clirr.framework.ApiDiffDispatcher; import net.sf.clirr.framework.ClassChangeCheck; @@ -46,10 +47,13 @@ extends AbstractDiffReporter implements ClassChangeCheck { + private ScopeSelector scopeSelector; + /** {@inheritDoc} */ - public MethodSetCheck(ApiDiffDispatcher dispatcher) + public MethodSetCheck(ApiDiffDispatcher dispatcher, ScopeSelector scopeSelector) { super(dispatcher); + this.scopeSelector = scopeSelector; } public final void check(JavaClass compatBaseline, JavaClass currentVersion) @@ -171,7 +175,9 @@ for (Iterator rmIterator = removedMethods.iterator(); rmIterator.hasNext();) { Method method = (Method) rmIterator.next(); - reportMethodRemoved(compatBaseline, method); + String methodSignature = getMethodId(compatBaseline, method); + String superClass = findSuperClassWithSignature(methodSignature, currentVersion); + reportMethodRemoved(compatBaseline, method, superClass); } bNameToMethod.remove(name); } @@ -193,12 +199,55 @@ } } - private void reportMethodRemoved(JavaClass oldClass, Method oldMethod) + /** + * Searches the class hierarchy for a method that has a certtain signature. + * @param methodSignature the sig we're looking for + * @param clazz class where search starts + * @return class name of a superclass of clazz, might be null + */ + private String findSuperClassWithSignature(String methodSignature, JavaClass clazz) { - fireDiff("Method '" - + getMethodId(oldClass, oldMethod) - + "' has been removed", - Severity.ERROR, oldClass, oldMethod); + final JavaClass[] superClasses = clazz.getSuperClasses(); + for (int i = 0; i < superClasses.length; i++) + { + JavaClass superClass = superClasses[i]; + final Method[] superMethods = superClass.getMethods(); + for (int j = 0; j < superMethods.length; j++) + { + Method superMethod = superMethods[j]; + final String superMethodSignature = getMethodId(superClass, superMethod); + if (methodSignature.equals(superMethodSignature)) + { + return superClass.getClassName(); + } + } + + } + return null; + } + + /** + * Report that a method has been removed from a class. + * @param oldClass the class where the method was available + * @param oldMethod the method that has been removed + * @param superClassName the superclass where the method is now available, might be null + */ + private void reportMethodRemoved(JavaClass oldClass, Method oldMethod, String superClassName) + { + if (superClassName == null) + { + fireDiff("Method '" + + getMethodId(oldClass, oldMethod) + + "' has been removed", + Severity.ERROR, oldClass, oldMethod); + } + else + { + fireDiff("Method '" + + getMethodId(oldClass, oldMethod) + + "' is now implemented in superclass " + superClassName, + Severity.INFO, oldClass, oldMethod); + } } private void reportMethodAdded(JavaClass newClass, Method newMethod) @@ -225,7 +274,7 @@ { Method method = methods[i]; - if (!(method.isPublic() || method.isProtected())) + if (!scopeSelector.isSelected(method)) { continue; } @@ -311,15 +360,14 @@ */ 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(" "); + final String scopeDecl = ScopeSelector.getScopeDecl(method); + if (scopeDecl.length() > 0) + { + buf.append(scopeDecl); + buf.append(" "); + } String name = method.getName(); if ("<init>".equals(name)) |