KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SwingComponentGenerator


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 import java.lang.reflect.*;
25 import java.io.*;
26
27 public class SwingComponentGenerator {
28
29   private static PrintStream out = System.out;
30   private static String JavaDoc packageName;
31
32   public static void main (String JavaDoc[] args) throws Exception JavaDoc {
33     packageName = args[0];
34     for (int i = 1; i < args.length; ++i) {
35       Class JavaDoc c = Class.forName(args[i]);
36       out = new PrintStream(
37         new FileOutputStream("generated/"+getName(c)+"Itf.java"));
38       generateInterface(c);
39       out.close();
40       out = new PrintStream(
41         new FileOutputStream("generated/"+getName(c)+".fractal"));
42       generateTypeDescriptor(c);
43       if (!Modifier.isAbstract(c.getModifiers())) {
44         out.println();
45         generateTemplateDescriptor(c);
46       }
47       out.close();
48       out = new PrintStream(
49         new FileOutputStream("generated/"+getName(c)+"Attributes.java"));
50       generateAttributes(c);
51       out.close();
52       if (!Modifier.isAbstract(c.getModifiers())) {
53         out = new PrintStream(
54           new FileOutputStream("generated/"+getName(c)+"Impl.java"));
55         generateImplementation(c);
56         out.close();
57       }
58     }
59   }
60
61   private static void generateInterface (Class JavaDoc c) {
62     String JavaDoc name = getName(c);
63     out.println("// automatically generated");
64     out.println();
65     out.println("package "+packageName+";\n");
66     out.print("public interface " + name + "Itf ");
67     if (c.getSuperclass().equals(Object JavaDoc.class)) {
68       out.println("{");
69     } else {
70       out.println("extends " + getName(c.getSuperclass()) + "Itf {");
71     }
72     Method[] methods = c.getDeclaredMethods();
73     Class JavaDoc d = c.getSuperclass();
74     for (int i = 0; i < methods.length; ++i) {
75       if (Modifier.isPublic(methods[i].getModifiers())) {
76         if (!Modifier.isFinal(methods[i].getModifiers())) {
77           if (!Modifier.isStatic(methods[i].getModifiers())) {
78             try {
79               d.getMethod(methods[i].getName(),methods[i].getParameterTypes());
80             } catch (NoSuchMethodException JavaDoc e) {
81               generateMethodHeader(methods[i]);
82               out.println(";");
83             }
84           }
85         }
86       }
87     }
88     out.println("}");
89   }
90
91   private static void generateTypeDescriptor (Class JavaDoc c) {
92     String JavaDoc name = getName(c);
93     String JavaDoc fullName = packageName + "." + name;
94     out.println("<!-- skeleton component type automatically generated -->\n");
95     out.print("<definition name=\"" + fullName + "Type\"");
96     if (c.getSuperclass().equals(Object JavaDoc.class)) {
97       out.println(">");
98     } else {
99       String JavaDoc superFullName = packageName + "." + getName(c.getSuperclass());
100       out.println(" extends=\"" + superFullName + "Type\">");
101     }
102     out.println(" <interface name=\"server\" signature=\"" + fullName + "Itf\" role=\"server\"/>");
103     out.println(" <!-- put your own interface types here -->");
104     out.println("</definiton>");
105   }
106
107   private static void generateTemplateDescriptor (Class JavaDoc c) {
108     String JavaDoc name = getName(c);
109     String JavaDoc fullName = packageName + "." + name;
110     out.println("<!-- skeleton template automatically generated -->\n");
111     out.print("<definition name=\"" + fullName + "Impl\"");
112     String JavaDoc extendsName = fullName + "Type";
113     if (!c.getSuperclass().equals(Object JavaDoc.class) && !Modifier.isAbstract(c.getSuperclass().getModifiers())) {
114       String JavaDoc superFullName = packageName + "." + getName(c.getSuperclass());
115       extendsName = superFullName + "Impl," + extendsName;
116     }
117     out.println(" extends=\"" + extendsName + "\">");
118     out.println(" <content class=\"" + fullName + "Impl\"/>");
119     out.println(" <controller>");
120     out.println(" <attributes signature=\"" + fullName + "Attributes\">");
121     out.println(" <!-- put your own attribute definitions here -->");
122     out.println(" </attributes>");
123     out.println(" </controller>");
124     out.println("</definition>");
125   }
126
127   private static void generateAttributes (Class JavaDoc c) {
128     String JavaDoc name = getName(c);
129     out.println("// automatically generated");
130     out.println();
131     out.println("package "+packageName+";\n");
132     out.print("public interface " + name + "Attributes ");
133     if (c.getSuperclass().equals(Object JavaDoc.class)) {
134       out.println("extends org.objectweb.fractal.api.control.AttributeController {");
135     } else {
136       out.println("extends " + getName(c.getSuperclass()) + "Attributes {");
137     }
138     Method[] methods = c.getDeclaredMethods();
139     //Class d = c.getSuperclass();
140
for (int i = 0; i < methods.length; ++i) {
141       String JavaDoc mname = methods[i].getName();
142       Class JavaDoc type;
143       if ((mname.startsWith("get") || mname.startsWith("set"))
144           && Modifier.isPublic(methods[i].getModifiers())
145           && !Modifier.isFinal(methods[i].getModifiers())
146           && !Modifier.isStatic(methods[i].getModifiers()))
147       {
148         type = null;
149         if (mname.startsWith("get")) {
150           if (methods[i].getParameterTypes().length != 0) {
151             continue;
152           }
153           type = methods[i].getReturnType();
154         }
155         if (mname.startsWith("set")) {
156           if (methods[i].getReturnType() != Void.TYPE ||
157               methods[i].getParameterTypes().length != 1)
158           {
159             continue;
160           }
161           type = methods[i].getParameterTypes()[0];
162         }
163         mname = (mname.charAt(0) == 'g' ? 's' : 'g') + mname.substring(1);
164         boolean ok = false;
165         for (int j = 0; j < methods.length; ++j) {
166           if (methods[j].getName().equals(mname)) {
167             if (Modifier.isPublic(methods[j].getModifiers())) {
168               if (!Modifier.isFinal(methods[j].getModifiers())) {
169                 if (!Modifier.isStatic(methods[j].getModifiers())) {
170                   Class JavaDoc otherType = null;
171                   if (mname.charAt(0) == 'g') {
172                     if (methods[j].getParameterTypes().length == 0) {
173                       otherType = methods[j].getReturnType();
174                     }
175                   } else if (methods[j].getParameterTypes().length == 1 && methods[j].getReturnType() == Void.TYPE) {
176                     otherType = methods[j].getParameterTypes()[0];
177                   }
178                   if (type == otherType) {
179                     ok = true;
180                     break;
181                   }
182                 }
183               }
184             }
185           }
186         }
187         if (ok && Modifier.isPublic(methods[i].getModifiers())) {
188           if (!Modifier.isFinal(methods[i].getModifiers())) {
189             if (!Modifier.isStatic(methods[i].getModifiers())) {
190               /*try {
191                 d.getMethod(methods[i].getName(),methods[i].getParameterTypes());
192               } catch (NoSuchMethodException e) {*/

193                 generateMethodHeader(methods[i]);
194                 out.println(";");
195               //}
196
}
197           }
198         }
199       }
200     }
201     out.println("}");
202   }
203
204   private static void generateImplementation (Class JavaDoc c) {
205     String JavaDoc name = getName(c);
206     out.println("// skeleton class automatically generated");
207     out.println();
208     out.println("package "+packageName+";\n");
209     out.println("import org.objectweb.fractal.api.control.BindingController;\n");
210     out.println("public class " + name + "Impl");
211     out.println(" extends " + c.getName());
212     out.print(" implements " + name + "Itf, " + name);
213     out.println("Attributes, BindingController");
214     out.println("{\n");
215     Constructor[] cons = c.getConstructors();
216     for (int i = 0; i < cons.length; ++i) {
217       generateConstructor(cons[i]);
218     }
219     out.println();
220     out.println(" public String listFc () {");
221     out.println(" // put your own code here");
222     out.println(" return new String[0];");
223     out.println(" }\n");
224     out.println(" public Object lookupFc (String clientItfName) {");
225     out.println(" // put your own code here");
226     out.println(" return null;");
227     out.println(" }\n");
228     out.println(" public void bindFc (String clientItfName, Object serverItf) {");
229     out.println(" // put your own code here");
230     out.println(" }\n");
231     out.println(" public void unbindFc (String clientItfName) {");
232     out.println(" // put your own code here");
233     out.println(" }\n");
234     out.println("}");
235   }
236
237   private static void generateMethodHeader (Method m) {
238     out.print(" ");
239     generateType(m.getReturnType());
240     out.print(" ");
241     out.print(m.getName());
242     out.print(" (");
243     generateFormals(m.getParameterTypes());
244     generateExceptions(m.getExceptionTypes());
245   }
246
247   private static void generateConstructor (Constructor c) {
248     out.print(" public ");
249     out.print(getName(c.getDeclaringClass()) + "Impl");
250     out.print(" (");
251     generateFormals(c.getParameterTypes());
252     generateExceptions(c.getExceptionTypes());
253     out.println(" {");
254     out.print(" super(");
255     generateActuals(c.getParameterTypes());
256     out.println(");");
257     out.println(" }");
258   }
259
260   private static void generateFormals (Class JavaDoc[] formals) {
261     for (int i = 0; i < formals.length; ++i) {
262       generateType(formals[i]);
263       out.print(" arg" + i);
264       if (i < formals.length - 1) {
265         out.print(", ");
266       }
267     }
268     out.print(")");
269   }
270
271   private static void generateActuals (Class JavaDoc[] actuals) {
272     for (int i = 0; i < actuals.length; ++i) {
273       out.print("arg" + i);
274       if (i < actuals.length - 1) {
275         out.print(",");
276       }
277     }
278   }
279
280   private static void generateExceptions (Class JavaDoc[] exceptions) {
281     if (exceptions.length > 0) {
282       out.print(" throws ");
283       for (int i = 0; i < exceptions.length; ++i) {
284         generateType(exceptions[i]);
285         if (i < exceptions.length - 1) {
286           out.print(", ");
287         }
288       }
289     }
290   }
291
292   private static void generateType (Class JavaDoc c) {
293     if (c.isArray()) {
294       generateType(c.getComponentType());
295       out.print("[]");
296     } else if (c.isPrimitive()) {
297       if (c.equals(Boolean.TYPE)) {
298         out.print("boolean");
299       } else if (c.equals(Character.TYPE)) {
300         out.print("char");
301       } else if (c.equals(Byte.TYPE)) {
302         out.print("byte");
303       } else if (c.equals(Short.TYPE)) {
304         out.print("short");
305       } else if (c.equals(Integer.TYPE)) {
306         out.print("int");
307       } else if (c.equals(Long.TYPE)) {
308         out.print("long");
309       } else if (c.equals(Float.TYPE)) {
310         out.print("float");
311       } else if (c.equals(Double.TYPE)) {
312         out.print("double");
313       } else if (c.equals(Void.TYPE)) {
314         out.print("void");
315       }
316     } else {
317       out.print(c.getName());
318     }
319   }
320
321   private static String JavaDoc getName (Class JavaDoc c) {
322     String JavaDoc name = c.getName();
323     if (name.lastIndexOf('.') > 0) {
324       name = name.substring(name.lastIndexOf('.') + 1);
325     }
326     return name;
327   }
328 }
329
Popular Tags