KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > codegen > intfc > DelegatorGenerator


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.codegen.intfc;
25
26 import java.io.*;
27 import java.util.*;
28 import java.lang.reflect.*;
29 import com.mchange.v2.codegen.*;
30 import com.mchange.v1.lang.ClassUtils;
31
32 public class DelegatorGenerator
33 {
34     int class_modifiers = Modifier.PUBLIC | Modifier.ABSTRACT;
35     int method_modifiers = Modifier.PUBLIC;
36     int wrapping_ctor_modifiers = Modifier.PUBLIC;
37     int default_ctor_modifiers = Modifier.PUBLIC;
38     boolean wrapping_constructor = true;
39     boolean default_constructor = true;
40     boolean inner_getter = true;
41     boolean inner_setter = true;
42
43     Class JavaDoc superclass = null;
44     Class JavaDoc[] extraInterfaces = null;
45
46     final static Comparator classComp = new Comparator()
47     {
48        public int compare(Object JavaDoc a, Object JavaDoc b)
49        { return ((Class JavaDoc) a).getName().compareTo(((Class JavaDoc) b).getName()); }
50     };
51
52     public void setGenerateInnerSetter( boolean b )
53     { this.inner_setter = b; }
54
55     public boolean isGenerateInnerSetter()
56     { return inner_setter; }
57
58     public void setGenerateInnerGetter( boolean b )
59     { this.inner_getter = b; }
60
61     public boolean isGenerateInnerGetter()
62     { return inner_getter; }
63
64     public void setGenerateNoArgConstructor( boolean b )
65     { this.default_constructor = b; }
66
67     public boolean isGenerateNoArgConstructor()
68     { return default_constructor; }
69
70     public void setGenerateWrappingConstructor( boolean b )
71     { this.wrapping_constructor = b; }
72
73     public boolean isGenerateWrappingConstructor()
74     { return wrapping_constructor; }
75
76     public void setWrappingConstructorModifiers( int modifiers )
77     { this.wrapping_ctor_modifiers = modifiers; }
78
79     public int getWrappingConstructorModifiers()
80     { return wrapping_ctor_modifiers; }
81
82     public void setNoArgConstructorModifiers( int modifiers )
83     { this.default_ctor_modifiers = modifiers; }
84
85     public int getNoArgConstructorModifiers()
86     { return default_ctor_modifiers; }
87
88     public void setMethodModifiers( int modifiers )
89     { this.method_modifiers = modifiers; }
90
91     public int getMethodModifiers()
92     { return method_modifiers; }
93
94     public void setClassModifiers( int modifiers )
95     { this.class_modifiers = modifiers; }
96
97     public int getClassModifiers()
98     { return class_modifiers; }
99
100     public void setSuperclass( Class JavaDoc superclass )
101     { this.superclass = superclass; }
102
103     public Class JavaDoc getSuperclass()
104     { return superclass; }
105
106     public void setExtraInterfaces( Class JavaDoc[] extraInterfaces )
107     { this.extraInterfaces = extraInterfaces; }
108
109     public Class JavaDoc[] getExtraInterfaces()
110     { return extraInterfaces; }
111
112     public void writeDelegator(Class JavaDoc intfcl, String JavaDoc genclass, Writer w) throws IOException
113     {
114     IndentedWriter iw = CodegenUtils.toIndentedWriter(w);
115     
116     String JavaDoc pkg = genclass.substring(0, genclass.lastIndexOf('.'));
117     String JavaDoc sgc = CodegenUtils.fqcnLastElement( genclass );
118     String JavaDoc scn = (superclass != null ? ClassUtils.simpleClassName( superclass ) : null);
119     String JavaDoc sin = ClassUtils.simpleClassName( intfcl );
120     String JavaDoc[] eins = null;
121     if (extraInterfaces != null)
122         {
123         eins = new String JavaDoc[ extraInterfaces.length ];
124         for (int i = 0, len = extraInterfaces.length; i < len; ++i)
125             eins[i] = ClassUtils.simpleClassName( extraInterfaces[i] );
126         }
127
128     Set imports = new TreeSet( classComp );
129     
130     Method[] methods = intfcl.getMethods();
131     
132     //TODO: don't add array classes!
133
//build import set
134
if (! CodegenUtils.inSamePackage( intfcl.getName(), genclass ) )
135         imports.add( intfcl );
136     if (superclass != null && ! CodegenUtils.inSamePackage( superclass.getName(), genclass ) )
137         imports.add( superclass );
138     if (extraInterfaces != null)
139         {
140         for (int i = 0, len = extraInterfaces.length; i < len; ++i)
141             {
142             Class JavaDoc checkMe = extraInterfaces[i];
143             if (! CodegenUtils.inSamePackage( checkMe.getName(), genclass ) )
144                 imports.add( checkMe );
145             }
146         }
147     for (int i = 0, len = methods.length; i < len; ++i)
148         {
149         Class JavaDoc[] args = methods[i].getParameterTypes();
150         for (int j = 0, jlen = args.length; j < jlen; ++j)
151             {
152             if (! CodegenUtils.inSamePackage( args[j].getName(), genclass ) )
153                 imports.add( CodegenUtils.unarrayClass( args[j] ) );
154             }
155         Class JavaDoc[] excClasses = methods[i].getExceptionTypes();
156         for (int j = 0, jlen = excClasses.length; j < jlen; ++j)
157             {
158             if (! CodegenUtils.inSamePackage( excClasses[j].getName(), genclass ) )
159                 {
160                 //System.err.println("Adding exception type: " + excClasses[j]);
161
imports.add( CodegenUtils.unarrayClass( excClasses[j] ) );
162                 }
163             }
164         if (! CodegenUtils.inSamePackage( methods[i].getReturnType().getName(), genclass ) )
165             imports.add( CodegenUtils.unarrayClass( methods[i].getReturnType() ) );
166         }
167     generateBannerComment( iw );
168     iw.println("package " + pkg + ';');
169     iw.println();
170     for (Iterator ii = imports.iterator(); ii.hasNext(); )
171         iw.println("import "+ ((Class JavaDoc) ii.next()).getName() + ';');
172     generateExtraImports( iw );
173     iw.println();
174     iw.print(CodegenUtils.getModifierString( class_modifiers ) + " class " + sgc);
175     if (superclass != null)
176         iw.print(" extends " + scn);
177     iw.print(" implements " + sin);
178     if (eins != null)
179         for (int i = 0, len = eins.length; i < len; ++i)
180         iw.print(", " + eins[i]);
181     iw.println();
182     iw.println("{");
183     iw.upIndent();
184
185     iw.println("protected " + sin + " inner;");
186     iw.println();
187
188     if ( wrapping_constructor )
189         {
190         iw.println("public" + ' ' + sgc + '(' + sin + " inner)");
191         iw.println("{ this.inner = inner; }");
192         }
193
194     if (default_constructor)
195         {
196         iw.println();
197         iw.println("public" + ' ' + sgc + "()");
198         iw.println("{}");
199         }
200
201     if (inner_setter)
202         {
203         iw.println();
204         iw.println( CodegenUtils.getModifierString( method_modifiers ) + " void setInner( " + sin + " inner )");
205         iw.println( "{ this.inner = inner; }" );
206         }
207     if (inner_getter)
208         {
209         iw.println();
210         iw.println( CodegenUtils.getModifierString( method_modifiers ) + ' ' + sin + " getInner()");
211         iw.println( "{ return inner; }" );
212         }
213     iw.println();
214     for (int i = 0, len = methods.length; i < len; ++i)
215         {
216         Method method = methods[i];
217         Class JavaDoc retType = method.getReturnType();
218
219         if (i != 0) iw.println();
220         iw.println( CodegenUtils.methodSignature( method_modifiers, method, null ) );
221         iw.println("{");
222         iw.upIndent();
223
224         generatePreDelegateCode( intfcl, genclass, method, iw );
225         generateDelegateCode( intfcl, genclass, method, iw );
226         generatePostDelegateCode( intfcl, genclass, method, iw );
227         
228         iw.downIndent();
229         iw.println("}");
230         }
231
232     iw.println();
233     generateExtraDeclarations( intfcl, genclass, iw );
234
235     iw.downIndent();
236         iw.println("}");
237     }
238
239     protected void generateDelegateCode( Class JavaDoc intfcl, String JavaDoc genclass, Method method, IndentedWriter iw ) throws IOException
240     {
241     Class JavaDoc retType = method.getReturnType();
242     
243     iw.println( (retType == void.class ? "" : "return " ) + "inner." + CodegenUtils.methodCall( method ) + ";" );
244     }
245
246     protected void generateBannerComment( IndentedWriter iw ) throws IOException
247     {
248     iw.println("/*");
249     iw.println(" * This class generated by " + this.getClass().getName());
250     iw.println(" * " + new Date());
251     iw.println(" * DO NOT HAND EDIT!!!!");
252     iw.println(" */");
253     }
254
255     protected void generateExtraImports( IndentedWriter iw ) throws IOException {}
256     protected void generatePreDelegateCode( Class JavaDoc intfcl, String JavaDoc genclass, Method method, IndentedWriter iw ) throws IOException {}
257     protected void generatePostDelegateCode( Class JavaDoc intfcl, String JavaDoc genclass, Method method, IndentedWriter iw ) throws IOException {}
258     protected void generateExtraDeclarations( Class JavaDoc intfcl, String JavaDoc genclass, IndentedWriter iw ) throws IOException {}
259 }
260
Popular Tags