View Javadoc

1   /*
2    * org.osjava.jardiff.SimpleDiffCriteria
3    *
4    * $Id: IOThread.java 1952 2005-08-28 18:03:41Z cybertiger $
5    * $URL: https://svn.osjava.org/svn/osjava/trunk/osjava-nio/src/java/org/osjava/nio/IOThread.java $
6    * $Rev: 1952 $
7    * $Date: 2005-08-28 18:03:41 +0000 (Sun, 28 Aug 2005) $
8    * $Author: cybertiger $
9    *
10   * Copyright (c) 2005, Antony Riley
11   * All rights reserved.
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted provided that the following conditions are met:
15   *
16   * + Redistributions of source code must retain the above copyright notice,
17   *   this list of conditions and the following disclaimer.
18   *
19   * + Redistributions in binary form must reproduce the above copyright notice,
20   *   this list of conditions and the following disclaimer in the documentation
21   *   and/or other materials provided with the distribution.
22   *
23   * + Neither the name JarDiff nor the names of its contributors may
24   *   be used to endorse or promote products derived from this software without
25   *   specific prior written permission.
26   *
27   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37   * POSSIBILITY OF SUCH DAMAGE.
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          // Yes classes can have a null supername, e.g. java.lang.Object !
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 }