001: /*
002: * ====================================================================
003: * The JRefactory License, Version 1.0
004: *
005: * Copyright (c) 2001 JRefactory. All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by the
022: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "JRefactory" must not be used to endorse or promote
027: * products derived from this software without prior written
028: * permission. For written permission, please contact seguin@acm.org.
029: *
030: * 5. Products derived from this software may not be called "JRefactory",
031: * nor may "JRefactory" appear in their name, without prior written
032: * permission of Chris Seguin.
033: *
034: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
035: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
036: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
037: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
038: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
039: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
040: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
041: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
042: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
043: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
044: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
045: * SUCH DAMAGE.
046: * ====================================================================
047: *
048: * This software consists of voluntary contributions made by many
049: * individuals on behalf of JRefactory. For more information on
050: * JRefactory, please see
051: * <http://www.sourceforge.org/projects/jrefactory>.
052: */
053:
054: package org.acm.seguin.summary;
055:
056: import java.io.File;
057: import java.lang.reflect.*;
058: import java.io.IOException;
059: import java.util.LinkedList;
060: import java.util.HashMap;
061: import java.util.Iterator;
062:
063: import net.sourceforge.jrefactory.ast.ASTType;
064: import net.sourceforge.jrefactory.ast.ASTVariableDeclaratorId;
065:
066: /**
067: * Creates a summary of a class using reflection
068: *
069: *@author Mike Atkinson
070: *@created August 28, 2003
071: *@version $Id: ReflectiveSummaryLoader.java,v 1.7 2004/05/04 15:52:26 mikeatkinson Exp $
072: *@since 2.8.00
073: */
074: public class ReflectiveSummaryLoader {
075:
076: /**
077: * Constructor for the package summary
078: *
079: *@since 2.8.00
080: */
081: private ReflectiveSummaryLoader() {
082: }
083:
084: /**
085: * Description of the Method
086: *
087: *@param className Description of the Parameter
088: *@return Description of the Return Value
089: *@exception ClassNotFoundException Description of the Exception
090: */
091: public static TypeSummary loadType(String className,
092: ClassLoader classLoader) throws ClassNotFoundException {
093: //System.out.println("ReflectiveSummaryLoader.loadType(" + className + ","+ classLoader+")");
094: TypeSummary this Type = null;
095: Class clazz = null;
096: PackageSummary parentSummary = null;
097: try {
098: clazz = classLoader.loadClass(className);
099: //Class clazz = Class.forName(className);
100: //System.out.println(" clazz=" + clazz);
101: Class[] subClasees = clazz.getDeclaredClasses();
102: Constructor[] constructors = clazz
103: .getDeclaredConstructors();
104: Field[] fields = clazz.getDeclaredFields();
105: Method[] methods = clazz.getDeclaredMethods();
106: Class[] interfaces = clazz.getInterfaces();
107: Class super class = clazz.getSuperclass();
108: int modifiers = clazz.getModifiers();
109: parentSummary = new PackageSummary(clazz.getPackage()
110: .getName());
111: this Type = new TypeSummary(parentSummary, null);
112: this Type.setName(className);
113: this Type.setInterface(clazz.isInterface());
114: this Type.setModifiers(modifiers);
115: for (int i = 0; i < methods.length; i++) {
116: MethodSummary methodSummary = new MethodSummary(
117: this Type);
118: methodSummary.setModifiers(methods[i].getModifiers());
119: Class rt = methods[i].getReturnType();
120: methodSummary.setReturnType(new TypeDeclSummary(
121: methodSummary, rt));
122: methodSummary.setName(methods[i].getName());
123: Class[] parameters = methods[i].getParameterTypes();
124: for (int j = 0; j < parameters.length; j++) {
125: ParameterSummary parameterSummary = new ParameterSummary(
126: methodSummary, new TypeDeclSummary(
127: methodSummary, parameters[j]), "");
128: methodSummary.add(parameterSummary);
129: }
130: this Type.add(methodSummary);
131: }
132:
133: for (int i = 0; i < fields.length; i++) {
134: FieldSummary fieldSummary = new FieldSummary(this Type,
135: new TypeDeclSummary(this Type, fields[i]
136: .getClass()), fields[i].getName());
137: fieldSummary.setModifiers(fields[i].getModifiers());
138: this Type.add(fieldSummary);
139: }
140: //System.out.println(" => " + thisType);
141: } catch (Exception e) {
142: e.printStackTrace();
143: System.err.println(" className=" + className + ", clazz="
144: + clazz);
145: if (clazz != null) {
146: System.err.println(" clazz.getPackage()="
147: + clazz.getPackage());
148: }
149: if (parentSummary == null) {
150: parentSummary = new PackageSummary("<unknown>");
151: }
152: this Type = new TypeSummary(parentSummary, null);
153: this Type.setName(className);
154: this Type.setInterface(false);
155: }
156:
157: return thisType;
158: }
159:
160: }
|