tzh
2024-08-20 ca8393c352368485bcb8b277004fdb0c6cb572c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package jdiff;
 
import java.util.*;
import com.sun.javadoc.*;
 
/**
 * The changes between two classes.
 *
 * See the file LICENSE.txt for copyright details.
 * @author Matthew Doar, mdoar@pobox.com
 */
class ClassDiff {
 
    /** Name of the class. */
    public String name_;
 
    /** Set if this class is an interface in the new API. */
    public boolean isInterface_;
 
    /** 
     * A string describing the changes in inheritance. 
     */
    public String inheritanceChange_ = null;
 
    /** 
     * A string describing the changes in documentation. 
     */
    public String documentationChange_ = null;
 
    /** 
     * A string describing the changes in modifiers. 
     * Changes can be in whether this is a class or interface, whether it is
     * abstract, static, final, and in its visibility.
     */
    public String modifiersChange_ = null;
 
    /** Constructors added in the new API. */
    public List ctorsAdded = null;
    /** Constructors removed in the new API. */
    public List ctorsRemoved = null;
    /** Constructors changed in the new API. */
    public List ctorsChanged = null;
 
    /** Methods added in the new API. */
    public List methodsAdded = null;
    /** Methods removed in the new API. */
    public List methodsRemoved = null;
    /** Methods changed in the new API. */
    public List methodsChanged = null;
 
    /** Fields added in the new API. */
    public List fieldsAdded = null;
    /** Fields removed in the new API. */
    public List fieldsRemoved = null;
    /** Fields changed in the new API. */
    public List fieldsChanged = null;
 
    /* The percentage difference for this class. */
    public double pdiff = 0.0;
 
    /** Default constructor. */
    public ClassDiff(String name) {
        name_ = name;
        isInterface_ = false;
 
        ctorsAdded = new ArrayList(); // ConstructorAPI[]
        ctorsRemoved = new ArrayList(); // ConstructorAPI[]
        ctorsChanged = new ArrayList(); // MemberDiff[]
 
        methodsAdded = new ArrayList(); // MethodAPI[]
        methodsRemoved = new ArrayList(); // MethodAPI[]
        methodsChanged = new ArrayList(); // MemberDiff[]
 
        fieldsAdded = new ArrayList(); // FieldAPI[]
        fieldsRemoved = new ArrayList(); // FieldAPI[]
        fieldsChanged = new ArrayList(); // MemberDiff[]
    }   
 
    /** 
     * Compare the inheritance details of two classes and produce 
     * a String for the inheritanceChanges_ field in this class.
     * If there is no difference, null is returned.
     */
    public static String diff(ClassAPI oldClass, ClassAPI newClass) {
        Collections.sort(oldClass.implements_);
        Collections.sort(newClass.implements_);
        String res = "";
        boolean hasContent = false;
        if (oldClass.extends_ != null && newClass.extends_ != null &&
            oldClass.extends_.compareTo(newClass.extends_) != 0) {
            res += "The superclass changed from <code>" + oldClass.extends_ + "</code> to <code>" + newClass.extends_ + "</code>.<br>";
            hasContent = true;
        }
        // Check for implemented interfaces which were removed
        String removedInterfaces = "";
        int numRemoved = 0;
        Iterator iter = oldClass.implements_.iterator();
        while (iter.hasNext()) {
            String oldInterface = (String)(iter.next());
            int idx = Collections.binarySearch(newClass.implements_, oldInterface);
            if (idx < 0) {
                if (numRemoved != 0)
                    removedInterfaces += ", ";
                removedInterfaces += oldInterface;
                numRemoved++;
            }
        }
        String addedInterfaces = "";
        int numAdded = 0;
        iter = newClass.implements_.iterator();
        while (iter.hasNext()) {
            String newInterface = (String)(iter.next());
            int idx = Collections.binarySearch(oldClass.implements_, newInterface);
            if (idx < 0) {
                if (numAdded != 0)
                    addedInterfaces += ", ";
                addedInterfaces += newInterface;
                numAdded++;
            }
        }
        if (numRemoved != 0) {
            if (hasContent)
                res += " ";
            if (numRemoved == 1)
                res += "Removed interface <code>" + removedInterfaces + "</code>.<br>";
            else
                res += "Removed interfaces <code>" + removedInterfaces + "</code>.<br>";
            hasContent = true;
        }
        if (numAdded != 0) {
            if (hasContent)
                res += " ";
            if (numAdded == 1)
                res += "Added interface <code>" + addedInterfaces + "</code>.<br>";
            else
                res += "Added interfaces <code>" + addedInterfaces + "</code>.<br>";
            hasContent = true;
        }
        if (res.compareTo("") == 0)
            return null;
        return res;
    }
 
    /** Add a change in the modifiers. */
    public void addModifiersChange(String commonModifierChanges) {
        if (commonModifierChanges != null) {
            if (modifiersChange_ == null)
                modifiersChange_ = commonModifierChanges;
            else
                modifiersChange_ += " " + commonModifierChanges;
        }
    }
}