KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xdoclet > retest > util > AbstractClassAndInterfaceComparator


1 package xdoclet.retest.util;
2
3 import xdoclet.XDocletException;
4 import xdoclet.retest.XDocletRetestMessages;
5
6 import java.util.List JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.lang.reflect.Method JavaDoc;
9
10 /**
11  * @author Vincent Harcq (vincent.harcq@hubmethods.com)
12  * @created Mars 5, 2002
13  * @version $Revision: 1.4 $
14  */

15 public abstract class AbstractClassAndInterfaceComparator
16 extends AbstractComparator
17 {
18
19     protected Class JavaDoc one;
20     protected Class JavaDoc two;
21
22     // ------------------------------------------------------------ Constructors
23

24     public AbstractClassAndInterfaceComparator(Class JavaDoc one, Class JavaDoc two)
25     {
26         super();
27         this.one = one;
28         this.two = two;
29         resultSet = new ComparisonResultSet();
30     }
31
32     // ------------------------------------------------------------------ Public
33

34     public ComparisonResultSet compareClassSignature()
35     throws XDocletException
36     {
37         compareInterfaces();
38         compareClass();
39         return resultSet;
40     }
41
42     public ComparisonResultSet compareClassMethodsSignature()
43     throws XDocletException
44     {
45         Method JavaDoc[] oneMs = one.getDeclaredMethods();
46         Method JavaDoc[] twoMs = two.getDeclaredMethods();
47         compareMethodSignatureOneByOne(oneMs,twoMs);
48         compareMethodSignatureOneByOne(twoMs,oneMs);
49         return resultSet;
50     }
51
52     // ----------------------------------------------------------------- Private
53

54     private void compareInterfaces()
55     throws XDocletException
56     {
57         Class JavaDoc[] oneIFs = one.getInterfaces();
58         Class JavaDoc[] twoIFs = two.getInterfaces();
59         if (oneIFs.length != twoIFs.length)
60         {
61             resultSet.addError(XDocletRetestMessages.MISMATCH_NUMBER_INTERFACE,new String JavaDoc[] {one.getName(),two.getName()});
62         }
63         compareInterfacesOneByOne(oneIFs,twoIFs);
64         compareInterfacesOneByOne(twoIFs,oneIFs);
65     }
66
67     private void compareInterfacesOneByOne(Class JavaDoc[] oneIFs,Class JavaDoc[] twoIFs)
68     throws XDocletException
69     {
70         for (int i = 0; i<oneIFs.length; i++){
71             boolean found = false;
72             for (int j = 0; j<twoIFs.length; j++){
73                 if (oneIFs[i].getName().equals(twoIFs[j].getName())){
74                     found = true;
75                     break;
76                 }
77             }
78             if ( ! found ){
79                 resultSet.addError(XDocletRetestMessages.INTERFACE_DEFINED_ONLY_IN,new String JavaDoc[] {oneIFs[i].getName(),two.getName()});
80             }
81         }
82     }
83
84     private void compareClass(){
85         if (one.isInterface() && two.isInterface())
86             return;
87         //TODO : compare "extends" on the two classes
88
}
89
90     private void compareMethodSignatureOneByOne(Method JavaDoc[] oneMs,Method JavaDoc[] twoMs)
91     throws XDocletException
92     {
93         for (int i = 0 ; i < oneMs.length; i++)
94         {
95             boolean found = false;
96             for (int j=0;j<twoMs.length;j++)
97             {
98                 MethodComparator comp = new MethodComparator(oneMs[i],twoMs[j]);
99                 ComparisonResultSet resultSet = comp.compare();
100                 if ( ! resultSet.error())
101                 {
102                     found = true;
103                     break;
104                 }
105             }
106             if ( ! found )
107             {
108                 resultSet.addError(XDocletRetestMessages.METHOD_DEFINED_ONLY_IN,new String JavaDoc[] {shortMethodName(oneMs[i]),two.getName()});
109             }
110         }
111     }
112
113 }
114
Popular Tags