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.FileSummary;
056: import org.acm.seguin.summary.ImportSummary;
057: import org.acm.seguin.summary.MethodSummary;
058: import org.acm.seguin.summary.PackageSummary;
059: import org.acm.seguin.summary.Summary;
060: import org.acm.seguin.summary.TypeDeclSummary;
061: import org.acm.seguin.summary.TypeSummary;
062:
063: /**
064: * Gets the type summary associated with a particular type declaration, file,
065: * or package.
066: *
067: *@author Chris Seguin
068: *@created November 30, 1999
069: */
070: public class GetTypeSummary {
071: /**
072: * Get the type summary that this object refers to. If the type summary is
073: * not found or the type is primitive, a null is returned. If the input is
074: * null, the output is also null.
075: *
076: *@param typeDecl the place to start the search
077: *@return the type summary or null
078: */
079: public static TypeSummary query(TypeDeclSummary typeDecl) {
080: if (typeDecl == null) {
081: return null;
082: }
083:
084: // Check if it is primitive
085: if (typeDecl.isPrimitive()) {
086: return null;
087: }
088:
089: String packageName = typeDecl.getPackage();
090: String typeName = typeDecl.getType();
091: if (packageName != null) {
092: // Look up the type in the specified package
093: PackageSummary packageSummary = PackageSummary
094: .getPackageSummary(packageName);
095: return query(packageSummary, typeName);
096: } else {
097: // Find file summary in parent
098: Summary next = typeDecl.getParent();
099: while (!(next instanceof FileSummary)) {
100: next = next.getParent();
101: }
102:
103: FileSummary fileSummary = (FileSummary) next;
104:
105: // Look up the type in the file summary
106: return query(fileSummary, typeName);
107: }
108: }
109:
110: /**
111: * Searches a package for a particular type. This method returns the type,
112: * if it is found. If it is not found or the name is null, this method
113: * returns null.
114: *
115: *@param fileSummary the file summary
116: *@param name the name of the type summary
117: *@return the type summary if it is found and null otherwise
118: */
119: public static TypeSummary query(FileSummary fileSummary, String name) {
120: //System.out.println("Searching " + fileSummary.getName() + "(FileSummary) for " + name);
121: // Check for null pointers
122: if (name == null) {
123: return null;
124: }
125:
126: TypeSummary result = null;
127:
128: // First try with the package
129: result = query((PackageSummary) fileSummary.getParent(), name);
130: if (result != null) {
131: //System.out.println("Found " + name + " in the current package");
132: return result;
133: }
134:
135: // Then try each import statement
136: result = checkImports(fileSummary, name);
137: if (result != null) {
138: //System.out.println("Found " + name + " in an import statement");
139: return result;
140: }
141:
142: // Try in java.lang
143: PackageSummary nextPackage = PackageSummary
144: .getPackageSummary("java.lang");
145: result = query(nextPackage, name);
146:
147: if (result != null) {
148: //System.out.println("Found " + name + " in java.lang");
149: }
150:
151: // Not found
152: return result;
153: }
154:
155: /**
156: * Searches a package for a particular type. This method returns the type,
157: * if it is found. If it is not found or the name is null, this method
158: * returns null.
159: *
160: *@param packageSummary the package summary
161: *@param name the name of the type summary
162: *@return the type summary if it is found and null otherwise
163: */
164: public static TypeSummary query(PackageSummary packageSummary,
165: String name) {
166: //System.out.println("Searching " + packageSummary.getName() + "(PackageSummary) for " + name);
167: // Check for null pointers
168: if (name == null) {
169: return null;
170: }
171:
172: Iterator fileIterator = packageSummary.getFileSummaries();
173: if (fileIterator != null) {
174: while (fileIterator.hasNext()) {
175: FileSummary nextFile = (FileSummary) fileIterator
176: .next();
177:
178: TypeSummary result = checkType(nextFile, name);
179: if (result != null) {
180: //System.out.println("Found " + name + " in " + nextFile.getName());
181: return result;
182: }
183: }
184: }
185:
186: // Not found
187: return null;
188: }
189:
190: /**
191: * Searches a package for a particular type. This method returns the type,
192: * if it is found. If it is not found or the name is null, this method
193: * returns null.
194: *
195: *@param packageName the package name
196: *@param name the name of the type summary
197: *@return the type summary if it is found and null otherwise
198: */
199: public static TypeSummary query(String packageName, String name) {
200: return query(PackageSummary.getPackageSummary(packageName),
201: name);
202: }
203:
204: /**
205: * Finds a nested type based on the name of the object
206: *
207: *@param parent the parent type
208: *@param name the name of the method
209: *@return the type summary if found or null otherwise
210: */
211: public static TypeSummary query(TypeSummary parent, String name) {
212: Iterator iter = parent.getTypes();
213: if (iter == null) {
214: return null;
215: }
216:
217: while (iter.hasNext()) {
218: TypeSummary next = (TypeSummary) iter.next();
219: if (next.getName().equals(name)) {
220: return next;
221: }
222: }
223:
224: return null;
225: }
226:
227: /**
228: * Finds a nested type based on the name of the object
229: *
230: *@param parent the parent type
231: *@param name the name of the method
232: *@return the type summary if found or null otherwise
233: */
234: public static TypeSummary query(MethodSummary parent, String name) {
235: Iterator iter = parent.getDependencies();
236: if (iter == null) {
237: return null;
238: }
239:
240: while (iter.hasNext()) {
241: Summary next = (Summary) iter.next();
242: if (next instanceof TypeSummary) {
243: TypeSummary consider = (TypeSummary) next;
244: if (consider.getName().equals(name)) {
245: return consider;
246: }
247: }
248: }
249:
250: return null;
251: }
252:
253: /**
254: * Searches for the type based on the imports
255: *
256: *@param fileSummary the file summaries
257: *@param name the name we are looking for
258: *@return Description of the Returned Value
259: */
260: private static TypeSummary checkImports(FileSummary fileSummary,
261: String name) {
262: Iterator iter = fileSummary.getImports();
263: if (iter != null) {
264: while (iter.hasNext()) {
265: ImportSummary next = (ImportSummary) iter.next();
266: //System.out.println("Searching " + next.getPackage().getName()
267: // + ((next.getType() == null) ? ".*" : ("." + next.getType()))
268: // + "(ImportSummary) for " + name);
269: String nextTypeName = next.getType();
270: if ((nextTypeName == null)
271: || (nextTypeName.equals(name))) {
272: PackageSummary nextPackage = next.getPackage();
273: TypeSummary result = query(nextPackage, name);
274: if (result != null) {
275: return result;
276: }
277: }
278: }
279: }
280:
281: return null;
282: }
283:
284: /**
285: * Description of the Method
286: *
287: *@param summary Description of Parameter
288: *@param name Description of Parameter
289: *@return Description of the Returned Value
290: */
291: private static TypeSummary checkType(FileSummary summary,
292: String name) {
293: Iterator typeIterator = summary.getTypes();
294: if (typeIterator != null) {
295: while (typeIterator.hasNext()) {
296: TypeSummary nextType = (TypeSummary) typeIterator
297: .next();
298: //System.out.println("Searching " + nextType.getName() + "(TypeSummary) for " + name);
299: if ((nextType != null)
300: && nextType.getName().equals(name)) {
301: return nextType;
302: }
303: }
304: }
305:
306: return null;
307: }
308: }
|