1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package net.sf.clirr.core.internal.checks;
21
22 import net.sf.clirr.core.Message;
23 import net.sf.clirr.core.Severity;
24 import net.sf.clirr.core.internal.AbstractDiffReporter;
25 import net.sf.clirr.core.internal.ApiDiffDispatcher;
26 import net.sf.clirr.core.internal.ClassChangeCheck;
27 import net.sf.clirr.core.internal.CoIterator;
28 import net.sf.clirr.core.internal.NameComparator;
29 import net.sf.clirr.core.spi.JavaType;
30
31 /***
32 * Detects changes in the set of superclasses.
33 *
34 * @author lkuehne
35 */
36 public final class ClassHierarchyCheck extends AbstractDiffReporter implements ClassChangeCheck
37 {
38 private static final Message MSG_ADDED_CLASS_TO_SUPERCLASSES = new Message(5000);
39 private static final Message MSG_REMOVED_CLASS_FROM_SUPERCLASSES = new Message(5001);
40
41 /***
42 * Create a new instance of this check.
43 * @param dispatcher the diff dispatcher that distributes the detected changes to the listeners.
44 */
45 public ClassHierarchyCheck(ApiDiffDispatcher dispatcher)
46 {
47 super(dispatcher);
48 }
49
50 /*** {@inheritDoc} */
51 public boolean check(JavaType compatBaseline, JavaType currentVersion)
52 {
53 JavaType[] compatSupers = compatBaseline.getSuperClasses();
54 JavaType[] currentSupers = currentVersion.getSuperClasses();
55
56 boolean isThrowable = false;
57 for (int i = 0; i < compatSupers.length; i++)
58 {
59 JavaType javaClass = compatSupers[i];
60 if ("java.lang.Throwable".equals(javaClass.getName()))
61 {
62 isThrowable = true;
63 }
64 }
65
66 final String className = compatBaseline.getName();
67
68 CoIterator iter = new CoIterator(new NameComparator(), compatSupers, currentSupers);
69
70 while (iter.hasNext())
71 {
72 iter.next();
73 JavaType baselineSuper = (JavaType) iter.getLeft();
74 JavaType currentSuper = (JavaType) iter.getRight();
75
76 if (baselineSuper == null)
77 {
78 Severity severity;
79 if (isThrowable)
80 {
81
82
83
84 severity = Severity.WARNING;
85 }
86 else
87 {
88 severity = Severity.INFO;
89 }
90
91 log(MSG_ADDED_CLASS_TO_SUPERCLASSES,
92 getSeverity(compatBaseline, severity), className, null, null,
93 new String[] {currentSuper.getName()});
94 }
95 else if (currentSuper == null)
96 {
97 log(MSG_REMOVED_CLASS_FROM_SUPERCLASSES,
98 getSeverity(compatBaseline, Severity.ERROR), className, null, null,
99 new String[] {baselineSuper.getName()});
100 }
101 }
102
103 return true;
104 }
105 }