001: /*
002: * Copyright (c) 2001-2007, Jean Tessier
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions
007: * are met:
008: *
009: * * Redistributions of source code must retain the above copyright
010: * notice, this list of conditions and the following disclaimer.
011: *
012: * * Redistributions in binary form must reproduce the above copyright
013: * notice, this list of conditions and the following disclaimer in the
014: * documentation and/or other materials provided with the distribution.
015: *
016: * * Neither the name of Jean Tessier nor the names of his contributors
017: * may be used to endorse or promote products derived from this software
018: * without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
021: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
022: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
023: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
024: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
027: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
028: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
029: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
030: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031: */
032:
033: package com.jeantessier.diff;
034:
035: import java.util.*;
036:
037: import com.jeantessier.classreader.*;
038:
039: /**
040: * TODO class comments
041: */
042: public class APIDifferenceStrategy extends DifferenceStrategyDecorator {
043: public APIDifferenceStrategy(DifferenceStrategy delegate) {
044: super (delegate);
045: }
046:
047: public boolean isClassDifferent(Classfile oldClass,
048: Classfile newClass) {
049: return isRemoved(oldClass, newClass)
050: || isNew(oldClass, newClass)
051: || isClassModified(oldClass, newClass);
052: }
053:
054: protected boolean isClassModified(Classfile oldClass,
055: Classfile newClass) {
056: return isDeclarationModified(oldClass, newClass)
057: || isDeprecationModified(oldClass, newClass)
058: || checkForDifferentFeatures(oldClass, newClass);
059: }
060:
061: public boolean isDeclarationModified(Classfile oldClass,
062: Classfile newClass) {
063: return !oldClass.getDeclaration().equals(
064: newClass.getDeclaration());
065: }
066:
067: private boolean checkForDifferentFeatures(Classfile oldClass,
068: Classfile newClass) {
069: return checkForDifferentFields(oldClass, newClass)
070: || checkForDifferentMethods(oldClass, newClass);
071: }
072:
073: private boolean checkForDifferentFields(Classfile oldClass,
074: Classfile newClass) {
075: boolean result = false;
076:
077: Set fieldNameSet = new HashSet();
078:
079: Iterator fields;
080: fields = oldClass.getAllFields().iterator();
081: while (fields.hasNext()) {
082: fieldNameSet.add(((Field_info) fields.next()).getName());
083: }
084: fields = newClass.getAllFields().iterator();
085: while (fields.hasNext()) {
086: fieldNameSet.add(((Field_info) fields.next()).getName());
087: }
088:
089: Iterator fieldNames = fieldNameSet.iterator();
090: while (!result && fieldNames.hasNext()) {
091: String fieldName = (String) fieldNames.next();
092: Field_info oldField = oldClass.getField(fieldName);
093: Field_info newField = newClass.getField(fieldName);
094:
095: result = isFieldDifferent(oldField, newField);
096: }
097:
098: return result;
099: }
100:
101: private boolean checkForDifferentMethods(Classfile oldClass,
102: Classfile newClass) {
103: boolean result = false;
104:
105: Set methodSignatureSet = new HashSet();
106:
107: Iterator methods;
108: methods = oldClass.getAllMethods().iterator();
109: while (methods.hasNext()) {
110: methodSignatureSet.add(((Method_info) methods.next())
111: .getSignature());
112: }
113: methods = newClass.getAllMethods().iterator();
114: while (methods.hasNext()) {
115: methodSignatureSet.add(((Method_info) methods.next())
116: .getSignature());
117: }
118:
119: Iterator methodSignatures = methodSignatureSet.iterator();
120: while (!result && methodSignatures.hasNext()) {
121: String methodSignature = (String) methodSignatures.next();
122: Method_info oldMethod = oldClass.getMethod(methodSignature);
123: Method_info newMethod = newClass.getMethod(methodSignature);
124:
125: result = isMethodDifferent(oldMethod, newMethod);
126: }
127:
128: return result;
129: }
130:
131: public boolean isFieldDifferent(Field_info oldField,
132: Field_info newField) {
133: return isRemoved(oldField, newField)
134: || isNew(oldField, newField)
135: || isDeprecationModified(oldField, newField)
136: || isDeclarationModified(oldField, newField)
137: || isConstantValueDifferent(
138: oldField.getConstantValue(), newField
139: .getConstantValue());
140: }
141:
142: public boolean isMethodDifferent(Method_info oldMethod,
143: Method_info newMethod) {
144: return isRemoved(oldMethod, newMethod)
145: || isNew(oldMethod, newMethod)
146: || isDeprecationModified(oldMethod, newMethod)
147: || isDeclarationModified(oldMethod, newMethod)
148: || isCodeDifferent(oldMethod.getCode(), newMethod
149: .getCode());
150: }
151:
152: protected boolean isRemoved(Object oldElement, Object newElement) {
153: return oldElement != null && newElement == null;
154: }
155:
156: protected boolean isDeprecationModified(Deprecatable oldItem,
157: Deprecatable newItem) {
158: return oldItem.isDeprecated() != newItem.isDeprecated();
159: }
160:
161: private boolean isDeclarationModified(Feature_info oldFeature,
162: Feature_info newFeature) {
163: return !oldFeature.getDeclaration().equals(
164: newFeature.getDeclaration());
165: }
166:
167: protected boolean isNew(Object oldElement, Object newElement) {
168: return oldElement == null && newElement != null;
169: }
170:
171: public boolean isPackageDifferent(Map oldPackage, Map newPackage) {
172: return isPackageRemoved(oldPackage, newPackage)
173: || isPackageNew(oldPackage, newPackage)
174: || isPackageModified(oldPackage, newPackage);
175: }
176:
177: protected boolean isPackageRemoved(Map oldPackage, Map newPackage) {
178: return !oldPackage.isEmpty() && newPackage.isEmpty();
179: }
180:
181: protected boolean isPackageNew(Map oldPackage, Map newPackage) {
182: return oldPackage.isEmpty() && !newPackage.isEmpty();
183: }
184:
185: protected boolean isPackageModified(Map oldPackage, Map newPackage) {
186: return oldPackage.size() != newPackage.size()
187: || checkForDifferentClasses(oldPackage, newPackage);
188: }
189:
190: private boolean checkForDifferentClasses(Map oldPackage,
191: Map newPackage) {
192: boolean result = false;
193:
194: Set classNames = new HashSet();
195: classNames.addAll(oldPackage.keySet());
196: classNames.addAll(newPackage.keySet());
197:
198: Iterator i = classNames.iterator();
199: while (!result && i.hasNext()) {
200: String className = (String) i.next();
201: Classfile oldClass = (Classfile) oldPackage.get(className);
202: Classfile newClass = (Classfile) newPackage.get(className);
203:
204: result = isClassDifferent(oldClass, newClass);
205: }
206:
207: return result;
208: }
209: }
|