001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.codegen.bean;
024:
025: import java.lang.reflect.Modifier;
026: import java.io.IOException;
027: import com.mchange.v2.codegen.*;
028:
029: public class InnerBeanPropertyBeanGenerator extends
030: SimplePropertyBeanGenerator {
031: String innerBeanClassName;
032:
033: int inner_bean_member_modifiers = Modifier.PROTECTED;
034:
035: int inner_bean_accessor_modifiers = Modifier.PROTECTED;
036: int inner_bean_replacer_modifiers = Modifier.PROTECTED;
037:
038: String innerBeanInitializationExpression = null; //if you want it to stay null, set to String "null"
039:
040: public void setInnerBeanClassName(String innerBeanClassName) {
041: this .innerBeanClassName = innerBeanClassName;
042: }
043:
044: public String getInnerBeanClassName() {
045: return innerBeanClassName;
046: }
047:
048: private String defaultInnerBeanInitializationExpression() {
049: return "new " + innerBeanClassName + "()";
050: }
051:
052: private String findInnerBeanClassName() {
053: return (innerBeanClassName == null ? "InnerBean"
054: : innerBeanClassName);
055: }
056:
057: private String findInnerBeanInitializationExpression() {
058: return (innerBeanInitializationExpression == null ? defaultInnerBeanInitializationExpression()
059: : innerBeanInitializationExpression);
060: }
061:
062: private int findInnerClassModifiers() {
063: int out = Modifier.STATIC;
064: if (Modifier.isPublic(inner_bean_accessor_modifiers)
065: || Modifier.isPublic(inner_bean_replacer_modifiers))
066: out |= Modifier.PUBLIC;
067: else if (Modifier.isProtected(inner_bean_accessor_modifiers)
068: || Modifier.isProtected(inner_bean_replacer_modifiers))
069: out |= Modifier.PROTECTED;
070: else if (Modifier.isPrivate(inner_bean_accessor_modifiers)
071: && Modifier.isPrivate(inner_bean_replacer_modifiers))
072: out |= Modifier.PRIVATE;
073: //else leave as package accessible
074: return out;
075: }
076:
077: //TODO: add a hook for subclassses to custom define maskedProps
078: private void writeSyntheticInnerBeanClass() throws IOException {
079: int num_props = props.length;
080: Property[] maskedProps = new Property[num_props];
081: for (int i = 0; i < num_props; ++i) {
082: maskedProps[i] = new SimplePropertyMask(props[i]) {
083: public int getVariableModifiers() {
084: return Modifier.PRIVATE | Modifier.TRANSIENT;
085: }
086: };
087: }
088:
089: ClassInfo ci = new WrapperClassInfo(info) {
090: public String getClassName() {
091: return "InnerBean";
092: }
093:
094: public int getModifiers() {
095: return findInnerClassModifiers();
096: }
097: };
098:
099: createInnerGenerator().generate(ci, maskedProps, iw);
100: }
101:
102: protected PropertyBeanGenerator createInnerGenerator() {
103: SimplePropertyBeanGenerator innerGenerator = new SimplePropertyBeanGenerator();
104: innerGenerator.setInner(true);
105: innerGenerator.addExtension(new SerializableExtension());
106: CloneableExtension ce = new CloneableExtension();
107: ce.setExceptionSwallowing(true);
108: innerGenerator.addExtension(ce);
109: return innerGenerator;
110: }
111:
112: protected void writeOtherVariables() throws IOException {
113: iw.println(CodegenUtils
114: .getModifierString(inner_bean_member_modifiers)
115: + ' '
116: + findInnerBeanClassName()
117: + " innerBean = "
118: + findInnerBeanInitializationExpression() + ';');
119: iw.println();
120: iw
121: .println(CodegenUtils
122: .getModifierString(inner_bean_accessor_modifiers)
123: + ' '
124: + findInnerBeanClassName()
125: + " accessInnerBean()");
126: iw.println("{ return innerBean; }");
127: }
128:
129: protected void writeOtherFunctions() throws IOException {
130: iw.print(CodegenUtils
131: .getModifierString(inner_bean_replacer_modifiers)
132: + ' '
133: + findInnerBeanClassName()
134: + " replaceInnerBean( "
135: + findInnerBeanClassName()
136: + " innerBean )");
137: if (constrainedProperties())
138: iw.println(" throws PropertyVetoException");
139: else
140: iw.println();
141: iw.println("{");
142: iw.upIndent();
143: iw.println("beforeReplaceInnerBean();");
144: iw.println("this.innerBean = innerBean;");
145: iw.println("afterReplaceInnerBean();");
146: iw.downIndent();
147: iw.println("}");
148: iw.println();
149:
150: boolean is_abstract = Modifier.isAbstract(info.getModifiers());
151: iw.print("protected ");
152: if (is_abstract)
153: iw.print("abstract ");
154: iw.print("void beforeReplaceInnerBean()");
155: if (constrainedProperties())
156: iw.print(" throws PropertyVetoException");
157: if (is_abstract)
158: iw.println(';');
159: else
160: iw.println(" {} //hook method for subclasses");
161: iw.println();
162:
163: iw.print("protected ");
164: if (is_abstract)
165: iw.print("abstract ");
166: iw.print("void afterReplaceInnerBean()");
167: if (is_abstract)
168: iw.println(';');
169: else
170: iw.println(" {} //hook method for subclasses");
171: iw.println();
172:
173: BeangenUtils.writeExplicitDefaultConstructor(Modifier.PUBLIC,
174: info, iw);
175: iw.println();
176: iw.println("public " + info.getClassName() + "("
177: + findInnerBeanClassName() + " innerBean)");
178: iw.println("{ this.innerBean = innerBean; }");
179: }
180:
181: protected void writeOtherClasses() throws IOException {
182: if (innerBeanClassName == null)
183: writeSyntheticInnerBeanClass();
184: }
185:
186: protected void writePropertyVariable(Property prop)
187: throws IOException { /* do nothing... we have no members, only the inner bean */
188: }
189:
190: protected void writePropertyGetter(Property prop, Class propType)
191: throws IOException {
192: String stn = prop.getSimpleTypeName();
193: String pfx = ("boolean".equals(stn) ? "is" : "get");
194: String methodName = pfx
195: + BeangenUtils.capitalize(prop.getName());
196: iw.print(CodegenUtils.getModifierString(prop
197: .getGetterModifiers()));
198: iw.println(' ' + prop.getSimpleTypeName() + ' ' + methodName
199: + "()");
200: iw.println('{');
201: iw.upIndent();
202: iw.println(stn + ' ' + prop.getName() + " = innerBean."
203: + methodName + "();");
204: String retVal = this .getGetterDefensiveCopyExpression(prop,
205: propType);
206: if (retVal == null)
207: retVal = prop.getName();
208: iw.println("return " + retVal + ';');
209: iw.downIndent();
210: iw.println('}');
211: }
212:
213: protected void writePropertySetter(Property prop, Class propType)
214: throws IOException {
215: String stn = prop.getSimpleTypeName();
216: String pfx = ("boolean".equals(stn) ? "is" : "get");
217:
218: String setVal = this .getSetterDefensiveCopyExpression(prop,
219: propType);
220: if (setVal == null)
221: setVal = prop.getName();
222: String getExpression = ("innerBean." + pfx
223: + BeangenUtils.capitalize(prop.getName()) + "()");
224: String setStatement = ("innerBean.set"
225: + BeangenUtils.capitalize(prop.getName()) + "( "
226: + setVal + " );");
227: BeangenUtils.writePropertySetterWithGetExpressionSetStatement(
228: prop, getExpression, setStatement, iw);
229: }
230: }
|