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 package org.osjava.jardiff;
40 import java.util.Arrays;
41 import java.util.HashSet;
42
43 /***
44 * A specific type of DiffCriteria which is only true for classes, methods
45 * and fields which are not synthetic, and are public or protected.
46 *
47 * @author <a href="mailto:antony@cyberiantiger.org">Antony Riley</a>
48 */
49 public class SimpleDiffCriteria implements DiffCriteria
50 {
51 /***
52 * Check if a class is valid.
53 * If the class is not synthetic and is public or protected, return true.
54 *
55 * @param info Info describing the class.
56 * @return True if the class meets the criteria, false otherwise.
57 */
58 public boolean validClass(ClassInfo info) {
59 return !info.isSynthetic() && (info.isPublic() || info.isProtected());
60 }
61
62 /***
63 * Check if a method is valid.
64 * If the method is not synthetic and is public or protected, return true.
65 *
66 * @param info Info describing the method.
67 * @return True if the method meets the criteria, false otherwise.
68 */
69 public boolean validMethod(MethodInfo info) {
70 return !info.isSynthetic() && (info.isPublic() || info.isProtected());
71 }
72
73 /***
74 * Check if a field is valid.
75 * If the method is not synthetic and is public or protected, return true.
76 *
77 * @param info Info describing the field.
78 * @return True if the field meets the criteria, false otherwise.
79 */
80 public boolean validField(FieldInfo info) {
81 return !info.isSynthetic() && (info.isPublic() || info.isProtected());
82 }
83
84 /***
85 * Check if there is a change between two versions of a class.
86 * Returns true if the access flags differ, or if the superclass differs
87 * or if the implemented interfaces differ.
88 *
89 * @param oldInfo Info about the old version of the class.
90 * @param newInfo Info about the new version of the class.
91 * @return True if the classes differ, false otherwise.
92 */
93 public boolean differs(ClassInfo oldInfo, ClassInfo newInfo) {
94 if (oldInfo.getAccess() != newInfo.getAccess())
95 return true;
96
97 if(oldInfo.getSupername() == null) {
98 if(newInfo.getSupername() != null) {
99 return true;
100 }
101 } else if (!oldInfo.getSupername().equals(newInfo.getSupername())) {
102 return true;
103 }
104 java.util.Set oldInterfaces
105 = new HashSet(Arrays.asList(oldInfo.getInterfaces()));
106 java.util.Set newInterfaces
107 = new HashSet(Arrays.asList(newInfo.getInterfaces()));
108 if (!oldInterfaces.equals(newInterfaces))
109 return true;
110 return false;
111 }
112
113 /***
114 * Check if there is a change between two versions of a method.
115 * Returns true if the access flags differ, or if the thrown
116 * exceptions differ.
117 *
118 * @param oldInfo Info about the old version of the method.
119 * @param newInfo Info about the new version of the method.
120 * @return True if the methods differ, false otherwise.
121 */
122 public boolean differs(MethodInfo oldInfo, MethodInfo newInfo) {
123 if (oldInfo.getAccess() != newInfo.getAccess())
124 return true;
125 if (oldInfo.getExceptions() == null
126 || newInfo.getExceptions() == null) {
127 if (oldInfo.getExceptions() != newInfo.getExceptions())
128 return true;
129 } else {
130 java.util.Set oldExceptions
131 = new HashSet(Arrays.asList(oldInfo.getExceptions()));
132 java.util.Set newExceptions
133 = new HashSet(Arrays.asList(newInfo.getExceptions()));
134 if (!oldExceptions.equals(newExceptions))
135 return true;
136 }
137 return false;
138 }
139
140 /***
141 * Check if there is a change between two versions of a field.
142 * Returns true if the access flags differ, or if the inital value
143 * of the field differs.
144 *
145 * @param oldInfo Info about the old version of the field.
146 * @param newInfo Info about the new version of the field.
147 * @return True if the fields differ, false otherwise.
148 */
149 public boolean differs(FieldInfo oldInfo, FieldInfo newInfo) {
150 if (oldInfo.getAccess() != newInfo.getAccess())
151 return true;
152 if (oldInfo.getValue() == null || newInfo.getValue() == null) {
153 if (oldInfo.getValue() != newInfo.getValue())
154 return true;
155 } else if (!oldInfo.getValue().equals(newInfo.getValue()))
156 return true;
157 return false;
158 }
159 }