View Javadoc

1   //////////////////////////////////////////////////////////////////////////////
2   // Clirr: compares two versions of a java library for binary compatibility
3   // Copyright (C) 2003 - 2005  Lars Kühne
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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                      // report a warning, because a change to the set of types
82                      // implemented by a thrown object can affect the
83                      // exception-catching behaviour of a program.
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 }