1 22 23 24 package com.mchange.v2.codegen.bean; 25 26 import java.lang.reflect.Modifier ; 27 import java.io.IOException ; 28 import com.mchange.v2.codegen.*; 29 30 public class InnerBeanPropertyBeanGenerator extends SimplePropertyBeanGenerator 31 { 32 String 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 innerBeanInitializationExpression = null; 41 public void setInnerBeanClassName(String innerBeanClassName) 42 { this.innerBeanClassName = innerBeanClassName; } 43 44 public String getInnerBeanClassName() 45 { return innerBeanClassName; } 46 47 private String defaultInnerBeanInitializationExpression() 48 { return "new " + innerBeanClassName + "()"; } 49 50 private String findInnerBeanClassName() 51 { return (innerBeanClassName == null ? "InnerBean" : innerBeanClassName); } 52 53 private String 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 return out; 67 } 68 69 70 private void writeSyntheticInnerBeanClass() throws IOException 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 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 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 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 164 { 165 if (innerBeanClassName == null) 166 writeSyntheticInnerBeanClass(); 167 } 168 169 protected void writePropertyVariable( Property prop ) throws IOException 170 { } 171 172 protected void writePropertyGetter( Property prop, Class propType ) throws IOException 173 { 174 String stn = prop.getSimpleTypeName(); 175 String pfx = ("boolean".equals( stn ) ? "is" : "get" ); 176 String 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 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 propType ) throws IOException 190 { 191 String stn = prop.getSimpleTypeName(); 192 String pfx = ("boolean".equals( stn ) ? "is" : "get" ); 193 194 String setVal = this.getSetterDefensiveCopyExpression( prop, propType ); 195 if (setVal == null) setVal = prop.getName(); 196 String getExpression = ("innerBean." + pfx + BeangenUtils.capitalize( prop.getName() ) + "()"); 197 String setStatement = ("innerBean.set" + BeangenUtils.capitalize( prop.getName() ) + "( " + setVal + " );"); 198 BeangenUtils.writePropertySetterWithGetExpressionSetStatement(prop, getExpression, setStatement, iw); 199 } 200 } | Popular Tags |