1 29 30 package com.caucho.amber.gen; 31 32 import com.caucho.amber.field.*; 33 import com.caucho.amber.table.Column; 34 import com.caucho.amber.table.Table; 35 import com.caucho.amber.type.EmbeddableType; 36 import com.caucho.amber.type.Type; 37 import com.caucho.bytecode.*; 38 import com.caucho.java.JavaWriter; 39 import com.caucho.java.gen.ClassComponent; 40 import com.caucho.loader.Environment; 41 import com.caucho.util.L10N; 42 43 import java.io.IOException ; 44 import java.util.ArrayList ; 45 import java.util.HashSet ; 46 47 50 public class EmbeddableComponent extends ClassComponent { 51 private static final L10N L = new L10N(EmbeddableComponent.class); 52 53 private String _baseClassName; 54 private String _extClassName; 55 56 private EmbeddableType _embeddableType; 57 58 public EmbeddableComponent() 59 { 60 } 61 62 65 public void setEmbeddableType(EmbeddableType embeddableType) 66 { 67 _embeddableType = embeddableType; 68 } 69 70 73 public void setBaseClassName(String baseClassName) 74 { 75 _baseClassName = baseClassName; 76 } 77 78 81 public String getBaseClassName() 82 { 83 return _baseClassName; 84 } 85 86 89 public void setExtClassName(String extClassName) 90 { 91 _extClassName = extClassName; 92 } 93 94 97 public String getClassName() 98 { 99 return _extClassName; 100 } 101 102 105 public String getBeanClassName() 106 { 107 return _baseClassName; 108 } 109 110 113 public void generate(JavaWriter out) 114 throws IOException 115 { 116 try { 117 generateHeader(out); 118 119 generateInit(out); 120 121 generateGetField(out); 123 124 generateFields(out); 125 126 generateLoad(out); 127 128 } catch (IOException e) { 129 throw e; 130 } 131 } 132 133 136 private void generateHeader(JavaWriter out) 137 throws IOException 138 { 139 out.println("/*"); 140 out.println(" * Generated by Resin Amber"); 141 out.println(" * " + com.caucho.Version.VERSION); 142 out.println(" */"); 143 out.print("private static final java.util.logging.Logger __caucho_log = "); 144 out.println("java.util.logging.Logger.getLogger(\"" + getBeanClassName() + "\");"); 145 } 146 147 150 private void generateInit(JavaWriter out) 151 throws IOException 152 { 153 String className = getClassName(); 154 int p = className.lastIndexOf('.'); 155 if (p > 0) 156 className = className.substring(p + 1); 157 158 ArrayList <AmberField> fields = _embeddableType.getFields(); 159 160 for (JMethod ctor : _embeddableType.getBeanClass().getConstructors()) { 161 out.println(); 162 out.print("public "); 164 165 out.print(className); 166 out.print("("); 167 168 JClass []args = ctor.getParameterTypes(); 169 for (int i = 0; i < args.length; i++) { 170 if (i != 0) 171 out.print(", "); 172 173 out.print(args[i].getPrintName()); 174 out.print(" a" + i); 175 } 176 out.println(")"); 177 out.println("{"); 178 out.pushDepth(); 179 180 out.print("super("); 181 for (int i = 0; i < args.length; i++) { 182 if (i != 0) 183 out.print(", "); 184 185 out.print("a" + i); 186 } 187 out.println(");"); 188 189 for (AmberField field : fields) { 190 field.generatePostConstructor(out); 191 } 192 193 out.popDepth(); 194 out.println("}"); 195 } 196 } 197 198 201 private void generateFields(JavaWriter out) 202 throws IOException 203 { 204 if (_embeddableType.isIdClass()) 205 return; 206 207 ArrayList <AmberField> fields = _embeddableType.getFields(); 208 209 for (int i = 0; i < fields.size(); i++) { 210 AmberField prop = fields.get(i); 211 212 prop.generatePrologue(out, null); 213 214 prop.generateSuperGetter(out); 215 prop.generateGetProperty(out); 216 217 prop.generateSuperSetter(out); 218 prop.generateSetProperty(out); 219 } 220 } 221 222 225 private void generateGetField(JavaWriter out) 226 throws IOException 227 { 228 out.println(); 229 out.println("public Object __caucho_get_field(int index)"); 230 out.println("{"); 231 out.pushDepth(); 232 233 out.println("switch (index) {"); 234 out.pushDepth(); 235 236 ArrayList <AmberField> fields = _embeddableType.getFields(); 237 238 for (int i = 0; i < fields.size(); i++) { 239 AmberField prop = fields.get(i); 240 241 if (! (prop instanceof PropertyField)) 242 break; 243 244 out.println("case " + i + ":"); 245 246 if (_embeddableType.isIdClass() || _embeddableType.isFieldAccess()) 247 out.println(" return " + prop.getName() + ";"); 248 else out.println(" return " + prop.generateSuperGetter() + ";"); 250 251 out.println(); 252 } 253 254 out.println("default:"); 255 out.println(" return null;"); 256 257 out.popDepth(); 258 out.println("}"); 259 260 out.popDepth(); 261 out.println("}"); 262 } 263 264 267 private void generateLoad(JavaWriter out) 268 throws IOException 269 { 270 out.println(); 271 out.println("public int __caucho_load(com.caucho.amber.manager.AmberConnection aConn,"); 272 out.println(" java.sql.ResultSet rs,"); 273 out.println(" int index)"); 274 out.println(" throws java.sql.SQLException"); 275 out.println("{"); 276 out.pushDepth(); 277 278 if (_embeddableType.isIdClass()) { 279 out.println("return 0;"); 280 } 281 else { 282 _embeddableType.generateLoad(out, "rs", "index", 0, -1); 283 284 out.println("return index;"); 285 } 286 287 out.popDepth(); 288 out.println("}"); 289 } 290 } 291 | Popular Tags |