1package jdiff;
2
3import java.io.*;
4import java.util.*;
5
6/**
7 * Class to represent a method, analogous to MethodDoc in the
8 * Javadoc doclet API.
9 *
10 * The method used for Collection comparison (compareTo) must make its
11 * comparison based upon everything that is known about this method.
12 *
13 * See the file LICENSE.txt for copyright details.
14 * @author Matthew Doar, mdoar@pobox.com
15 */
16class MethodAPI implements Comparable {
17
18    /** Name of the method. */
19    public String name_ = null;
20
21    /** Return type of the method. */
22    public String returnType_ = null;
23
24    /**
25     * The fully qualified name of the class or interface this method is
26     * inherited from. If this is null, then the method is defined locally
27     * in this class or interface.
28     */
29    public String inheritedFrom_ = null;
30
31    /**
32     * The exceptions thrown by this method, being all the exception types
33     * separated by commas. "no exceptions" if no exceptions are thrown.
34     */
35    public String exceptions_ = "no exceptions";
36
37    /** Set if this method is abstract. */
38    public boolean isAbstract_ = false;
39
40    /** Set if this method is native. */
41    public boolean isNative_ = false;
42
43    /** Set if this method is synchronized. */
44    public boolean isSynchronized_ = false;
45
46    /** Modifiers for this class. */
47    public Modifiers modifiers_;
48
49    public List params_; // ParamAPI[]
50
51    /** The doc block, default is null. */
52    public String doc_ = null;
53
54    /** Constructor. */
55    public MethodAPI(String name, String returnType, boolean isAbstract,
56                     boolean isNative, boolean isSynchronized,
57                     Modifiers modifiers) {
58        name_ = name;
59        returnType_ = returnType;
60        isAbstract_ = isAbstract;
61        isNative_ = isNative;
62        isSynchronized_ = isSynchronized;
63        modifiers_ = modifiers;
64        params_ = new ArrayList(); // ParamAPI[]
65    }
66
67    /** Copy constructor. */
68    public MethodAPI(MethodAPI m) {
69        name_ = m.name_;
70        returnType_ = m.returnType_;
71        inheritedFrom_ = m.inheritedFrom_;
72        exceptions_ = m.exceptions_;
73        isAbstract_ = m.isAbstract_;
74        isNative_ = m.isNative_;
75        isSynchronized_ = m.isSynchronized_;
76        modifiers_ = m.modifiers_; // Note: shallow copy
77        params_ = m.params_; // Note: shallow copy
78        doc_ = m.doc_;
79        signature_ = m.signature_; // Cached
80    }
81
82    /**
83     * Compare two methods, including the return type, and parameter
84     * names and types, and modifiers.
85     */
86    public int compareTo(Object o) {
87        MethodAPI oMethod = (MethodAPI)o;
88        int comp = name_.compareTo(oMethod.name_);
89        if (comp != 0)
90            return comp;
91        comp = returnType_.compareTo(oMethod.returnType_);
92        if (comp != 0)
93            return comp;
94        if (APIComparator.changedInheritance(inheritedFrom_, oMethod.inheritedFrom_) != 0)
95            return -1;
96        if (isAbstract_ != oMethod.isAbstract_) {
97            return -1;
98        }
99        if (Diff.showAllChanges &&
100	    isNative_ != oMethod.isNative_) {
101            return -1;
102        }
103        if (Diff.showAllChanges &&
104	    isSynchronized_ != oMethod.isSynchronized_) {
105            return -1;
106        }
107        comp = exceptions_.compareTo(oMethod.exceptions_);
108        if (comp != 0)
109            return comp;
110        comp = modifiers_.compareTo(oMethod.modifiers_);
111        if (comp != 0)
112            return comp;
113        comp = getSignature().compareTo(oMethod.getSignature());
114        if (comp != 0)
115            return comp;
116        if (APIComparator.docChanged(doc_, oMethod.doc_))
117            return -1;
118        return 0;
119    }
120
121    /**
122     * Tests two methods, using just the method name, used by indexOf().
123     */
124    public boolean equals(Object o) {
125        if (name_.compareTo(((MethodAPI)o).name_) == 0)
126            return true;
127        return false;
128    }
129
130    /**
131     * Tests two methods for equality, using just the signature.
132     */
133    public boolean equalSignatures(Object o) {
134        if (getSignature().compareTo(((MethodAPI)o).getSignature()) == 0)
135            return true;
136        return false;
137    }
138
139    /** Cached result of getSignature(). */
140    public String signature_ = null;
141
142    /** Return the signature of the method. */
143    public String getSignature() {
144        if (signature_ != null)
145            return signature_;
146        String res = "";
147        boolean first = true;
148        Iterator iter = params_.iterator();
149        while (iter.hasNext()) {
150            if (!first)
151                res += ", ";
152            ParamAPI param = (ParamAPI)(iter.next());
153            res += param.toString();
154            first = false;
155        }
156        signature_ = res;
157        return res;
158    }
159}
160