001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.summary.query;
053:
054: import java.util.Iterator;
055: import org.acm.seguin.summary.MethodSummary;
056: import org.acm.seguin.summary.ParameterSummary;
057: import org.acm.seguin.summary.TypeDeclSummary;
058: import org.acm.seguin.summary.TypeSummary;
059:
060: /**
061: * Checks that two methods are the same. Also provides a search feature to find
062: * a method with a specific signature in a type
063: *
064: *@author Chris Seguin
065: *@created September 12, 2001
066: */
067: public class SameMethod {
068: private final static int SAME = 0;
069: private final static int ONE_ANCESTOR = 1;
070: private final static int TWO_ANCESTOR = 2;
071: private final static int ERROR = 3;
072: private final static int ANCESTOR = 4;
073:
074: /**
075: * Checks if two methods are the same
076: *
077: *@param one Description of Parameter
078: *@param two Description of Parameter
079: *@return Description of the Returned Value
080: */
081: public static boolean query(MethodSummary one, MethodSummary two) {
082: return check(one, two, SAME);
083: }
084:
085: /**
086: * Description of the Method
087: *
088: *@param one Description of Parameter
089: *@param two Description of Parameter
090: *@return Description of the Returned Value
091: */
092: public static boolean conflict(MethodSummary one, MethodSummary two) {
093: return check(one, two, ANCESTOR);
094: }
095:
096: /**
097: * Finds the method with the same signature in the other type
098: *
099: *@param type Description of Parameter
100: *@param method Description of Parameter
101: *@return Description of the Returned Value
102: */
103: public static MethodSummary find(TypeSummary type,
104: MethodSummary method) {
105: Iterator iter = type.getMethods();
106: if (iter != null) {
107: while (iter.hasNext()) {
108: MethodSummary next = (MethodSummary) iter.next();
109: if (query(next, method)) {
110: return next;
111: }
112: }
113: }
114: return null;
115: }
116:
117: /**
118: * Finds the method with a conflicting in the other type
119: *
120: *@param type Description of Parameter
121: *@param method Description of Parameter
122: *@return Description of the Returned Value
123: */
124: public static MethodSummary findConflict(TypeSummary type,
125: MethodSummary method) {
126: Iterator iter = type.getMethods();
127: if (iter != null) {
128: while (iter.hasNext()) {
129: MethodSummary next = (MethodSummary) iter.next();
130: if (conflict(next, method)) {
131: return next;
132: }
133: }
134: }
135: return null;
136: }
137:
138: /**
139: * Checks the types
140: *
141: *@param one Description of Parameter
142: *@param two Description of Parameter
143: *@param way Description of Parameter
144: *@return Description of the Returned Value
145: */
146: private static int compareTypes(TypeSummary one, TypeSummary two,
147: int way) {
148: if (one == two) {
149: return way;
150: }
151:
152: if (way == ANCESTOR) {
153: if (Ancestor.query(one, two)) {
154: return ONE_ANCESTOR;
155: } else if (Ancestor.query(two, one)) {
156: return TWO_ANCESTOR;
157: }
158: }
159:
160: if ((ONE_ANCESTOR == way) && (Ancestor.query(two, one))) {
161: return way;
162: }
163:
164: if ((TWO_ANCESTOR == way) && (Ancestor.query(one, two))) {
165: return way;
166: }
167:
168: return ERROR;
169: }
170:
171: /**
172: * Work horse that actually checks the methods
173: *
174: *@param one Description of Parameter
175: *@param two Description of Parameter
176: *@param test Description of Parameter
177: *@return Description of the Returned Value
178: */
179: private static boolean check(MethodSummary one, MethodSummary two,
180: int test) {
181: if (!one.getName().equals(two.getName())) {
182: return false;
183: }
184:
185: Iterator oneIter = one.getParameters();
186: Iterator twoIter = two.getParameters();
187:
188: if (oneIter == null) {
189: return twoIter == null;
190: }
191:
192: if (twoIter == null) {
193: return false;
194: }
195:
196: while (oneIter.hasNext() && twoIter.hasNext()) {
197: ParameterSummary oneParam = (ParameterSummary) oneIter
198: .next();
199: ParameterSummary twoParam = (ParameterSummary) twoIter
200: .next();
201:
202: TypeDeclSummary oneDecl = oneParam.getTypeDecl();
203: TypeDeclSummary twoDecl = twoParam.getTypeDecl();
204:
205: TypeSummary typeOne = GetTypeSummary.query(oneDecl);
206: TypeSummary typeTwo = GetTypeSummary.query(twoDecl);
207:
208: if (compareTypes(typeOne, typeTwo, test) == ERROR) {
209: return false;
210: }
211: }
212:
213: return !(oneIter.hasNext() || twoIter.hasNext());
214: }
215: }
|