User: vharcq
Date: 02/02/25 09:39:06
Added: core/test/src/xdoclet/retest/util
AbstractClassAndInterfaceComparator.java
AbstractComparator.java ClassComparator.java
ComparisonResultSet.java InterfaceComparator.java
MethodComparator.java
Log:
The Comparator classes: Compare 2 classes, 2 interfaces, 2 methods (and in the
future 2 field)
Comparison done by Reflection level so the "signature" of those are compared
Revision Changes Path
1.1
xdoclet/core/test/src/xdoclet/retest/util/AbstractClassAndInterfaceComparator.java
Index: AbstractClassAndInterfaceComparator.java
===================================================================
/*
* $Id: AbstractClassAndInterfaceComparator.java,v 1.1 2002/02/25 17:39:05 vharcq
Exp $
*/
package xdoclet.retest.util;
import xdoclet.XDocletException;
import java.util.List;
import java.util.ArrayList;
import java.lang.reflect.Method;
public abstract class AbstractClassAndInterfaceComparator
extends AbstractComparator
{
protected Class one;
protected Class two;
protected ComparisonResultSet resultSet;
// ------------------------------------------------------------ Constructors
public AbstractClassAndInterfaceComparator(Class one, Class two){
super();
this.one = one;
this.two = two;
resultSet = new ComparisonResultSet();
}
// ------------------------------------------------------------------ Public
public ComparisonResultSet compareClassSignature()
throws XDocletException{
compareInterfaces();
compareClass();
return resultSet;
}
public ComparisonResultSet compareClassMethodsSignature()
throws XDocletException{
Method[] oneMs = one.getDeclaredMethods();
Method[] twoMs = two.getDeclaredMethods();
compareMethodSignatureOneByOne(oneMs,twoMs);
compareMethodSignatureOneByOne(twoMs,oneMs);
return resultSet;
}
// ----------------------------------------------------------------- Private
private void compareInterfaces()
throws XDocletException{
Class[] oneIFs = one.getInterfaces();
Class[] twoIFs = two.getInterfaces();
if (oneIFs.length != twoIFs.length){
resultSet.addError("mismatch_number_interface",new String[]
{one.getName(),two.getName()});
}
compareInterfacesOneByOne(oneIFs,twoIFs);
compareInterfacesOneByOne(twoIFs,oneIFs);
}
private void compareInterfacesOneByOne(Class[] oneIFs,Class[] twoIFs)
throws XDocletException{
for (int i = 0; i<oneIFs.length; i++){
boolean found = false;
for (int j = 0; j<twoIFs.length; j++){
if (oneIFs[i].getName().equals(twoIFs[j].getName())){
found = true;
break;
}
}
if ( ! found ){
resultSet.addError("interface_defined_only_in",new String[]
{oneIFs[i].getName(),one.getName()});
}
}
}
private void compareClass(){
if (one.isInterface() && two.isInterface())
return;
//TODO : compare "extends" on the two classes
}
private void compareMethodSignatureOneByOne(Method[] oneMs,Method[] twoMs)
throws XDocletException{
for (int i = 0 ; i < oneMs.length; i++){
boolean found = false;
for (int j=0;j<twoMs.length;j++){
MethodComparator comp = new MethodComparator(oneMs[i],twoMs[j]);
ComparisonResultSet resultSet = comp.compare();
if ( ! resultSet.error()){
found = true;
break;
}
}
if ( ! found ){
resultSet.addError("method_defined_only_in",new String[]
{shortMethodName(oneMs[i]),one.getName()});
}
}
}
}
1.1
xdoclet/core/test/src/xdoclet/retest/util/AbstractComparator.java
Index: AbstractComparator.java
===================================================================
/*
* $Id: AbstractComparator.java,v 1.1 2002/02/25 17:39:06 vharcq Exp $
*/
package xdoclet.retest.util;
import xdoclet.XDocletException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
public abstract class AbstractComparator
{
public abstract ComparisonResultSet compare()
throws XDocletException;
public String shortClassName(Class clazz){
String name = clazz.getName();
StringTokenizer st = new StringTokenizer(name,".");
String ret = name;
while (st.hasMoreTokens()) ret = st.nextToken();
return ret.trim();
}
public String shortMethodName(Method method){
String name = "[" + shortClassName(method.getReturnType()) + "] " +
method.getName() + "(" ;
Class[] params = method.getParameterTypes();
for (int i = 0; i< params.length;i++){
name += shortClassName(params[i]);
if ( i < params.length - 1) name += ",";
}
name += ") E{";
Class[] es = method.getExceptionTypes();
for (int i = 0; i< es.length;i++){
name += shortClassName(es[i]);
if ( i < es.length - 1) name += ",";
}
name += "}";
return name.trim();
}
}
1.1 xdoclet/core/test/src/xdoclet/retest/util/ClassComparator.java
Index: ClassComparator.java
===================================================================
/*
* $Id: ClassComparator.java,v 1.1 2002/02/25 17:39:06 vharcq Exp $
*/
package xdoclet.retest.util;
import xdoclet.XDocletException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
public class ClassComparator
extends AbstractClassAndInterfaceComparator
{
public ClassComparator(Class one,Class two){
super(one,two);
}
public ComparisonResultSet compare()
throws XDocletException{
if ( one.isInterface() ){
resultSet.addError("is_not_a_class",new String[] {one.getName()} );
return resultSet;
}
if ( two.isInterface() ){
resultSet.addError("is_not_a_class",new String[] {two.getName()} );
return resultSet;
}
compareClassSignature();
compareClassMethodsSignature();
//TODO compareClassFieldsSignature();
//TODO compareClassContent();
return resultSet;
}
}
1.1
xdoclet/core/test/src/xdoclet/retest/util/ComparisonResultSet.java
Index: ComparisonResultSet.java
===================================================================
/*
* $Id: ComparisonResultSet.java,v 1.1 2002/02/25 17:39:06 vharcq Exp $
*/
package xdoclet.retest.util;
import xdoclet.util.Translator;
import xdoclet.XDocletException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class ComparisonResultSet
{
public static final String TEST_BUNDLE = "xdoclet.retest.Messages";
protected List messages;
public ComparisonResultSet(){
messages = new ArrayList();
}
public String[] getMessages(){
return (String[])messages.toArray(new String[messages.size()]);
}
public void addError(String message, String[] params)
throws XDocletException{
messages.add(Translator.getString(TEST_BUNDLE,message,params));
}
public boolean error(){
return messages.size() > 0;
}
public String toString(){
String ret="";
if (messages.size() > 0){
ret = ret + "==== " + messages.size() + " error(s) ====\n";
Iterator it = messages.iterator();
while (it.hasNext()){
ret = ret + "========== " + (String)it.next() + "\n";
}
}
return ret;
}
}
1.1
xdoclet/core/test/src/xdoclet/retest/util/InterfaceComparator.java
Index: InterfaceComparator.java
===================================================================
/*
* $Id: InterfaceComparator.java,v 1.1 2002/02/25 17:39:06 vharcq Exp $
*/
package xdoclet.retest.util;
import xdoclet.XDocletException;
public class InterfaceComparator
extends AbstractClassAndInterfaceComparator
{
public InterfaceComparator(Class one,Class two){
super(one,two);
}
public ComparisonResultSet compare() throws XDocletException{
if ( ! one.isInterface() ){
resultSet.addError("is_not_an_interface",new String[] {one.getName()} );
return resultSet;
}
if ( ! two.isInterface() ){
resultSet.addError("is_not_an_interface",new String[] {two.getName()} );
return resultSet;
}
compareClassSignature();
compareClassMethodsSignature();
//TODO compareClassFieldsSignature();
return resultSet;
}
}
1.1 xdoclet/core/test/src/xdoclet/retest/util/MethodComparator.java
Index: MethodComparator.java
===================================================================
/*
* $Id: MethodComparator.java,v 1.1 2002/02/25 17:39:06 vharcq Exp $
*/
package xdoclet.retest.util;
import xdoclet.XDocletException;
import java.lang.reflect.Method;
public class MethodComparator
extends AbstractComparator
{
protected Method one;
protected Method two;
protected ComparisonResultSet resultSet;
public MethodComparator(Method one,Method two){
super();
this.one = one;
this.two = two;
resultSet = new ComparisonResultSet();
}
public ComparisonResultSet compare()
throws XDocletException{
if ( ! one.getName().equals(two.getName())){
resultSet.addError("method_does_not_have_same_name",new String[]
{one.getName(),two.getName()});
return resultSet;
}
if ( !
shortClassName(one.getReturnType()).equals(shortClassName(two.getReturnType()))){
resultSet.addError("method_does_not_have_same_return_type",new String[]
{one.getName(),shortClassName(one.getDeclaringClass()),shortClassName(two.getDeclaringClass())});
return resultSet;
}
Class[] onePs = one.getParameterTypes();
Class[] twoPs = two.getParameterTypes();
if (onePs.length != twoPs.length){
resultSet.addError("method_does_not_have_same_number_of_param",new
String[]
{one.getName(),shortClassName(one.getDeclaringClass()),shortClassName(two.getDeclaringClass())});
return resultSet;
}
for (int i=0; i<onePs.length; i++){
if ( ! shortClassName(onePs[i]).equals(shortClassName(twoPs[i]))){
resultSet.addError("method_does_not_have_same_type_of_param",new
String[]
{one.getName(),shortClassName(one.getDeclaringClass()),shortClassName(two.getDeclaringClass())});
return resultSet;
}
}
Class[] oneEs = one.getExceptionTypes();
Class[] twoEs = two.getExceptionTypes();
if (oneEs.length != twoEs.length){
resultSet.addError("method_does_not_have_same_number_of_exceptions",new
String[]
{one.getName(),shortClassName(one.getDeclaringClass()),shortClassName(two.getDeclaringClass())});
return resultSet;
}
for (int i=0; i<oneEs.length; i++){
if ( ! shortClassName(oneEs[i]).equals(shortClassName(twoEs[i]))){
resultSet.addError("method_does_not_have_same_type_of_exceptions",new String[]
{one.getName(),shortClassName(one.getDeclaringClass()),shortClassName(two.getDeclaringClass())});
return resultSet;
}
}
return resultSet;
}
}
_______________________________________________
Xdoclet-devel mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/xdoclet-devel