3 import java.io.PrintWriter;
4 import java.lang.reflect.Method;
5 import java.lang.reflect.Modifier;
6 import java.util.Vector;
9 * Defines a factory for creating skeleton implementations of the abstract
10 * methods of Abstract Java classes. The factory can be invoked from the
11 * command line or from another program.
14 * This class extends the interface factory to handle Abstract classes
15 * Copyright (c) Javier Lopez 2001. All Rights Reserved.
17 * AbstractClassFactory is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License as
19 * published by the Free Software Foundation; either version 2, or (at
20 * your option) any later version.
22 * AbstractClassFactory is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * To obtain a copy of the GNU General Public License write to the
28 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
29 * Boston, MA 02111-1307, USA.
31 * @author Javier Lopez
32 * @version $Revision: 1.7 $
34 public class AbstractClassFactory extends InterfaceFactory {
36 /** The interface factory. */
37 static AbstractClassFactory abstractClassFactory;
39 public AbstractClassFactory() {}
42 * Creates an AbstractClassFactory that uses the specified NameFactory for
43 * generating parameter names
45 * @param factory Factory for generating parameter names
47 public AbstractClassFactory(NameFactory factory) {
52 * Generates signatures based on introspection of the specified interface.
53 * Strips package specifiers from generated signatures.
55 * @param argAbstracClassName the abstract class to process for signatures.
56 * @exception NotAnAbstractClassException the requested class isn't an
58 * @exception java.lang.ClassNotFoundException the requested class cannot
61 public void process(String argAbstracClassName)
62 throws ClassNotFoundException, NotAnAbstractClassException {
63 process(argAbstracClassName, true);
67 * Generates signatures based on introspection of the specified class.
69 * @param name the abstract class to process for signatures.
70 * @param truncate toggles truncation of package specifiers in signatures.
72 * @exception NotAnAbstractClassException the requested class isn't an
74 * @exception java.lang.ClassNotFoundException the requested class cannot
77 public void process(String name, boolean truncate)
78 throws ClassNotFoundException, NotAnAbstractClassException {
79 if (null == namefactory)
80 namefactory = new DefaultNameFactory();
82 Class aclass = Class.forName(name);
83 int iModifiers = aclass.getModifiers();
84 if (!Modifier.isAbstract(iModifiers))
85 throw new NotAnAbstractClassException(name);
87 Vector methods = new Vector();
88 getAbstractMethods(aclass, methods);
89 int size = methods.size();
90 for (int i = 0; i < size; i++)
91 sortByDeclaringClass(new Signature((Method)methods.get(i),
97 * Creates a list of the abstract methods in argClass
99 * @param argClass Class to obtained the abstract methods from
100 * @param abstractMethods Contains a list of the abstract methods.
101 * @return a <code>Methods[]</code> containing abstract methods
103 private void getAbstractMethods(Class argClass, Vector abstractMethods) {
104 Method[] methods = argClass.getMethods();
105 Method[] declaredMethods = argClass.getDeclaredMethods();
106 addMethods(methods, abstractMethods);
107 addMethods(declaredMethods, abstractMethods);
111 * Adds the abstract methods in <code>methods</code> into abstractMethods.
113 * @param methods a <code>Method[]</code> to be added
114 * @param abstractMethods a <code>Vector</code> to add abstract methods.
116 private void addMethods(Method[] methods, Vector abstractMethods) {
119 for (int i = 0; i < methods.length; i++) {
121 modifiers = method.getModifiers();
123 if (Modifier.isAbstract(modifiers)
124 && !abstractMethods.contains(method)) {
125 abstractMethods.add(method);
126 } // end of if (method)
127 } // end of for (int i = 0; i < methods.length; i++)
130 public static void getImportedClasses() {
131 println(abstractClassFactory.getImportsAsList());
135 * Makes an implementation of the abstract methods of an abstract class.
136 * This method delegates the creation of the implementation to
137 * makeAbstractClassInternal.
139 * @param name Name of abstract class to be implemented.
140 * @param truncate If <code>true</code>, truncate package specifier
141 * when generating code.
143 public static void makeAbstractClassExpression
144 (String name, boolean truncate) {
146 if (abstractClassFactory == null)
147 abstractClassFactory = new AbstractClassFactory();
149 abstractClassFactory.flush();
150 abstractClassFactory.makeAbstractClassExpressionInternal(name,
155 * Makes an implementation of the abstract methods of an abstract class.
157 * @param name Name of abstract class to be implemented.
158 * @param truncate If <code>true</code>, truncate package specifier
159 * when generating code.
161 private void makeAbstractClassExpressionInternal
162 (String name, boolean truncate) {
164 process(name, truncate);
165 } catch (ClassNotFoundException e) {
166 println("(error \"Error: could not find abstract class named: "
167 + name + ". " + "Note: name must be qualified.\")");
169 } catch (NotAnAbstractClassException e) {
170 println("(error \"Error: " + name
171 + " is not an abstract class.\")");
173 } catch (Exception e) {
175 println("(error \"Error: unknown type.\")");
179 dumpExpression(new PrintWriter(System.out, true), truncate);
182 public static void main(String[] args) {
183 AbstractClassFactory.makeAbstractClassExpression
184 ("javax.swing.AbstractAction", false);
188 class NotAnAbstractClassException extends NotAnInterfaceException {
189 NotAnAbstractClassException (String name) {
192 }// End of AbstractClassFactory
195 * $Log: AbstractClassFactory.java,v $
196 * Revision 1.7 2003/10/19 14:45:30 jslopez
197 * Fixes getAbstractMethods to get the methods included by getDeclaredMethods and
200 * Revision 1.6 2003/10/10 12:51:12 jslopez
201 * Fixes generating skeleton for all methods declared by superclass including
202 * those that the class or superclass provides an implementation.
204 * Revision 1.5 2002/12/04 07:16:38 paulk
207 * Revision 1.4 2002/08/30 12:50:10 jslopez
208 * Fixes regression bug.
209 * Now methods defined in super classes and interfaces are as well
212 * Revision 1.3 2002/08/30 12:29:58 jslopez
213 * Fixes bug generating abstract class skeletons.
215 * Revision 1.2 2002/05/14 06:38:44 paulk
216 * Enhances code generation wizards for implementing interfaces, abstract
217 * classes, etc., to use customizable templates to generate skeleton methods
218 * instead of hard-wired skeletons. Thanks to "Dr. Michael Lipp"
219 * <lipp@danet.de> for proposing and implementing this improvement.
220 * Revision 1.1 2001/08/04 03:14:06 paulk