KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > codegen > bean > InnerBeanPropertyBeanGenerator


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.bean;
25
26 import java.lang.reflect.Modifier JavaDoc;
27 import java.io.IOException JavaDoc;
28 import com.mchange.v2.codegen.*;
29
30 public class InnerBeanPropertyBeanGenerator extends SimplePropertyBeanGenerator
31 {
32     String JavaDoc innerBeanClassName;
33
34     int inner_bean_member_modifiers = Modifier.PROTECTED;
35
36     int inner_bean_accessor_modifiers = Modifier.PROTECTED;
37     int inner_bean_replacer_modifiers = Modifier.PROTECTED;
38
39     String JavaDoc innerBeanInitializationExpression = null; //if you want it to stay null, set to String "null"
40

41     public void setInnerBeanClassName(String JavaDoc innerBeanClassName)
42     { this.innerBeanClassName = innerBeanClassName; }
43
44     public String JavaDoc getInnerBeanClassName()
45     { return innerBeanClassName; }
46
47     private String JavaDoc defaultInnerBeanInitializationExpression()
48     { return "new " + innerBeanClassName + "()"; }
49
50     private String JavaDoc findInnerBeanClassName()
51     { return (innerBeanClassName == null ? "InnerBean" : innerBeanClassName); }
52
53     private String JavaDoc findInnerBeanInitializationExpression()
54     { return (innerBeanInitializationExpression == null ? defaultInnerBeanInitializationExpression() : innerBeanInitializationExpression); }
55
56     private int findInnerClassModifiers()
57     {
58     int out = Modifier.STATIC;
59     if (Modifier.isPublic( inner_bean_accessor_modifiers ) || Modifier.isPublic( inner_bean_replacer_modifiers ))
60         out |= Modifier.PUBLIC;
61     else if (Modifier.isProtected( inner_bean_accessor_modifiers ) || Modifier.isProtected( inner_bean_replacer_modifiers ))
62         out |= Modifier.PROTECTED;
63     else if (Modifier.isPrivate( inner_bean_accessor_modifiers ) && Modifier.isPrivate( inner_bean_replacer_modifiers ))
64         out |= Modifier.PRIVATE;
65     //else leave as package accessible
66
return out;
67     }
68
69
70     //TODO: add a hook for subclassses to custom define maskedProps
71
private void writeSyntheticInnerBeanClass() throws IOException JavaDoc
72     {
73     int num_props = props.length;
74     Property[] maskedProps = new Property[ num_props ];
75     for (int i = 0; i < num_props; ++i)
76         {
77         maskedProps[i] = new SimplePropertyMask( props[i] )
78             {
79             public int getVariableModifiers()
80             { return Modifier.PRIVATE | Modifier.TRANSIENT; }
81             };
82         }
83
84     ClassInfo ci = new WrapperClassInfo( info )
85         {
86         public String JavaDoc getClassName()
87         { return "InnerBean"; }
88         
89         public int getModifiers()
90         { return findInnerClassModifiers(); }
91         };
92
93     createInnerGenerator().generate( ci, maskedProps, iw );
94     }
95
96     protected PropertyBeanGenerator createInnerGenerator()
97     {
98     SimplePropertyBeanGenerator innerGenerator = new SimplePropertyBeanGenerator();
99     innerGenerator.setInner( true );
100     innerGenerator.addExtension( new SerializableExtension() );
101     CloneableExtension ce = new CloneableExtension();
102     ce.setExceptionSwallowing( true );
103     innerGenerator.addExtension( ce );
104     return innerGenerator;
105     }
106
107     protected void writeOtherVariables() throws IOException JavaDoc
108     {
109     iw.println( CodegenUtils.getModifierString( inner_bean_member_modifiers ) + ' ' +
110              findInnerBeanClassName() + " innerBean = " + findInnerBeanInitializationExpression() + ';');
111     iw.println();
112     iw.println( CodegenUtils.getModifierString( inner_bean_accessor_modifiers ) + ' ' +
113             findInnerBeanClassName() + " accessInnerBean()");
114     iw.println("{ return innerBean; }");
115     }
116
117     protected void writeOtherFunctions() throws IOException JavaDoc
118     {
119     iw.print( CodegenUtils.getModifierString( inner_bean_replacer_modifiers ) + ' ' +
120           findInnerBeanClassName() + " replaceInnerBean( " + findInnerBeanClassName() + " innerBean )");
121     if (constrainedProperties())
122         iw.println(" throws PropertyVetoException");
123     else
124         iw.println();
125     iw.println("{");
126     iw.upIndent();
127     iw.println("beforeReplaceInnerBean();");
128     iw.println("this.innerBean = innerBean;");
129     iw.println("afterReplaceInnerBean();");
130     iw.downIndent();
131     iw.println("}");
132     iw.println();
133
134     boolean is_abstract = Modifier.isAbstract( info.getModifiers() );
135     iw.print("protected ");
136     if (is_abstract)
137         iw.print("abstract ");
138     iw.print("void beforeReplaceInnerBean()");
139     if (constrainedProperties())
140         iw.print(" throws PropertyVetoException");
141     if (is_abstract)
142         iw.println(';');
143     else
144         iw.println(" {} //hook method for subclasses");
145     iw.println();
146
147     iw.print("protected ");
148     if (is_abstract)
149         iw.print("abstract ");
150     iw.print("void afterReplaceInnerBean()");
151     if (is_abstract)
152         iw.println(';');
153     else
154         iw.println(" {} //hook method for subclasses");
155     iw.println();
156
157     BeangenUtils.writeExplicitDefaultConstructor( Modifier.PUBLIC, info, iw );
158     iw.println();
159     iw.println("public " + info.getClassName() + "(" + findInnerBeanClassName() + " innerBean)");
160     iw.println("{ this.innerBean = innerBean; }");
161     }
162
163     protected void writeOtherClasses() throws IOException JavaDoc
164     {
165     if (innerBeanClassName == null)
166         writeSyntheticInnerBeanClass();
167     }
168
169     protected void writePropertyVariable( Property prop ) throws IOException JavaDoc
170     { /* do nothing... we have no members, only the inner bean */ }
171
172     protected void writePropertyGetter( Property prop, Class JavaDoc propType ) throws IOException JavaDoc
173     {
174     String JavaDoc stn = prop.getSimpleTypeName();
175     String JavaDoc pfx = ("boolean".equals( stn ) ? "is" : "get" );
176     String JavaDoc methodName = pfx + BeangenUtils.capitalize( prop.getName() );
177     iw.print( CodegenUtils.getModifierString( prop.getGetterModifiers() ) );
178     iw.println(' ' + prop.getSimpleTypeName() + ' ' + methodName + "()");
179     iw.println('{');
180     iw.upIndent();
181     iw.println( stn + ' ' + prop.getName() + " = innerBean." + methodName + "();");
182     String JavaDoc retVal = this.getGetterDefensiveCopyExpression( prop, propType );
183     if (retVal == null) retVal = prop.getName();
184     iw.println("return " + retVal + ';');
185     iw.downIndent();
186     iw.println('}');
187     }
188
189     protected void writePropertySetter( Property prop, Class JavaDoc propType ) throws IOException JavaDoc
190     {
191     String JavaDoc stn = prop.getSimpleTypeName();
192     String JavaDoc pfx = ("boolean".equals( stn ) ? "is" : "get" );
193
194     String JavaDoc setVal = this.getSetterDefensiveCopyExpression( prop, propType );
195     if (setVal == null) setVal = prop.getName();
196     String JavaDoc getExpression = ("innerBean." + pfx + BeangenUtils.capitalize( prop.getName() ) + "()");
197     String JavaDoc setStatement = ("innerBean.set" + BeangenUtils.capitalize( prop.getName() ) + "( " + setVal + " );");
198     BeangenUtils.writePropertySetterWithGetExpressionSetStatement(prop, getExpression, setStatement, iw);
199     }
200 }
Popular Tags