Source Code Cross Referenced for NamedInterfaceGeneratingStatement.java in  » Database-ORM » sqlc » biz » hammurapi » sqlc » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » sqlc » biz.hammurapi.sqlc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        /*
0002:         * sqlc 1
0003:         * SQL Compiler 
0004:         * Copyright (C) 2003  Hammurapi Group
0005:         *
0006:         * This program is free software; you can redistribute it and/or
0007:         * modify it under the terms of the GNU Lesser General Public
0008:         * License as published by the Free Software Foundation; either
0009:         * version 2 of the License, or (at your option) any later version.
0010:         *
0011:         * This program is distributed in the hope that it will be useful,
0012:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014:         * Lesser General Public License for more details.
0015:         *
0016:         * You should have received a copy of the GNU Lesser General Public
0017:         * License along with this library; if not, write to the Free Software
0018:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0019:         *
0020:         * URL: http://www.hammurapi.biz/products/sqlc/index.html
0021:         * e-Mail: support@hammurapi.biz
0022:         */
0023:        package biz.hammurapi.sqlc;
0024:
0025:        import java.sql.ResultSet;
0026:        import java.sql.SQLException;
0027:        import java.util.ArrayList;
0028:        import java.util.Collection;
0029:        import java.util.HashMap;
0030:        import java.util.Iterator;
0031:        import java.util.Map;
0032:        import java.util.Properties;
0033:        import java.util.Set;
0034:
0035:        import org.apache.bcel.Constants;
0036:        import org.apache.bcel.classfile.Utility;
0037:        import org.apache.bcel.generic.ALOAD;
0038:        import org.apache.bcel.generic.ARETURN;
0039:        import org.apache.bcel.generic.ASTORE;
0040:        import org.apache.bcel.generic.ATHROW;
0041:        import org.apache.bcel.generic.DUP;
0042:        import org.apache.bcel.generic.GOTO;
0043:        import org.apache.bcel.generic.ICONST;
0044:        import org.apache.bcel.generic.IFEQ;
0045:        import org.apache.bcel.generic.ILOAD;
0046:        import org.apache.bcel.generic.InstructionFactory;
0047:        import org.apache.bcel.generic.InstructionHandle;
0048:        import org.apache.bcel.generic.InstructionList;
0049:        import org.apache.bcel.generic.LDC;
0050:        import org.apache.bcel.generic.RETURN;
0051:        import org.apache.bcel.generic.Type;
0052:        import org.w3c.dom.Element;
0053:
0054:        import biz.hammurapi.codegen.Class;
0055:        import biz.hammurapi.codegen.ClassGeneratorBase;
0056:        import biz.hammurapi.codegen.Consumer;
0057:        import biz.hammurapi.codegen.ExceptionHandler;
0058:        import biz.hammurapi.codegen.GenerationException;
0059:        import biz.hammurapi.codegen.Interface;
0060:        import biz.hammurapi.codegen.InterfacePool;
0061:        import biz.hammurapi.codegen.MethodPrototype;
0062:        import biz.hammurapi.codegen.InterfacePool.InterfaceDescriptor;
0063:        import biz.hammurapi.sql.DatabaseObject;
0064:        import biz.hammurapi.sql.Parameterizer;
0065:        import biz.hammurapi.sql.Projector;
0066:        import biz.hammurapi.sql.SQLProcessor;
0067:        import biz.hammurapi.sql.columns.Column;
0068:        import biz.hammurapi.sql.columns.ObjectColumn;
0069:        import biz.hammurapi.sql.metadata.ColumnDescriptor;
0070:        import biz.hammurapi.sql.metadata.GenerationPolicy;
0071:        import biz.hammurapi.sql.metadata.ParameterDescriptor;
0072:        import biz.hammurapi.xml.dom.XmlBeanBase;
0073:
0074:        /**
0075:         * @author Pavel Vlasov
0076:         * @version $Revision: 1.15 $
0077:         */
0078:        public abstract class NamedInterfaceGeneratingStatement extends
0079:                NamedStatement {
0080:
0081:            private String interfaceImplVisibility = "public";
0082:
0083:            /**
0084:             * @param name
0085:             * @param description
0086:             * @param sql
0087:             * @param parameterDescriptors
0088:             * @param parameterDescriptors2
0089:             */
0090:            public NamedInterfaceGeneratingStatement(String name,
0091:                    String description, String sql,
0092:                    Collection parameterDescriptors,
0093:                    Collection parameterDescriptors2, boolean generateMutators) {
0094:                super (name, description, sql, parameterDescriptors,
0095:                        parameterDescriptors2);
0096:                this .generateMutators = generateMutators;
0097:            }
0098:
0099:            /**
0100:             * @param name
0101:             * @param description
0102:             * @param sql
0103:             * @param processor
0104:             * @param policy
0105:             * @param hasNullableParameters
0106:             * @throws SQLException
0107:             */
0108:            public NamedInterfaceGeneratingStatement(String name,
0109:                    String description, String sql, SQLProcessor processor,
0110:                    GenerationPolicy policy, boolean hasNullableParameters,
0111:                    boolean generateMutators) throws SQLException {
0112:                super (name, description, sql, processor, policy,
0113:                        hasNullableParameters);
0114:                this .generateMutators = generateMutators;
0115:            }
0116:
0117:            private boolean generateMutators;
0118:
0119:            private String smartBaseName = DatabaseObject.class.getName();
0120:
0121:            /**
0122:             * @param packageName
0123:             * @param consumer
0124:             * @throws GenerationException
0125:             */
0126:            protected void generateInterface(String packageName,
0127:                    Consumer consumer) throws GenerationException {
0128:                InterfaceDescriptor master = interfaceDescriptor.getMaster();
0129:                if (!master.isExternal() && master == interfaceDescriptor) {
0130:                    String iName = getInterfaceName(packageName);
0131:
0132:                    Collection super Interfaces = interfaceDescriptor
0133:                            .getSuperInterfaces(packageName);
0134:                    Iterator sit = super Interfaces.iterator();
0135:                    StringBuffer extendedInterfaces = new StringBuffer();
0136:                    if (sit.hasNext()) {
0137:                        extendedInterfaces.append(" extends ");
0138:                    }
0139:                    while (sit.hasNext()) {
0140:                        extendedInterfaces.append(sit.next().toString());
0141:                        if (sit.hasNext()) {
0142:                            extendedInterfaces.append(", ");
0143:                        }
0144:                    }
0145:
0146:                    Set ownMethods = interfaceDescriptor.getOwnMethods();
0147:                    Interface i = new Interface("public interface " + iName
0148:                            + extendedInterfaces.toString(),
0149:                            "Query result interface. "
0150:                                    + extendedInterfaces.toString(), consumer
0151:                                    .getListener());
0152:                    Iterator it = getInterfaceProperties().iterator();
0153:                    while (it.hasNext()) {
0154:                        final ColumnDescriptor cd = (ColumnDescriptor) it
0155:                                .next();
0156:                        final String javaType = javaType(cd);
0157:                        if (javaType != null) {
0158:                            String getterName = javaType + " get"
0159:                                    + cd.getName() + "()";
0160:                            if (ownMethods.contains(getterName)) {
0161:                                i.addMethod(getterName, null, "Accessor for "
0162:                                        + cd.getName(), null);
0163:                            }
0164:
0165:                            if (generateMutators
0166:                                    && ownMethods.contains("void set"
0167:                                            + cd.getName() + "(" + javaType
0168:                                            + ")")) {
0169:                                i.addMethod("void set" + cd.getName() + "("
0170:                                        + javaType + " newValue)", null,
0171:                                        "Mutator for " + cd.getName(), null);
0172:                            }
0173:                        }
0174:                    }
0175:
0176:                    if (ownMethods.contains("void toDom("
0177:                            + Element.class.getName() + ")")) {
0178:                        i.addMethod("void toDom(" + Element.class.getName()
0179:                                + " holder)", null, "Serializes to DOM", null);
0180:                    }
0181:
0182:                    consumer.consume(i.getJavaClass());
0183:                }
0184:            }
0185:
0186:            /**
0187:             * @param packageName
0188:             * @param consumer
0189:             * @throws GenerationException
0190:             */
0191:            protected void generateInterfaceImpl(String packageName,
0192:                    Consumer consumer) throws GenerationException {
0193:                String interfaceImplName = getInterfaceImplName(packageName,
0194:                        true);
0195:
0196:                if (interfaceImplName != null) {
0197:                    String iName = getInterfaceName(packageName);
0198:                    Class c = new Class(interfaceImplVisibility + " class "
0199:                            + interfaceImplName + " extends "
0200:                            + XmlBeanBase.class.getName() + " implements "
0201:                            + iName,
0202:                            "Default implementation of query interface",
0203:                            consumer.getListener());
0204:
0205:                    Iterator it = getInterfaceProperties().iterator();
0206:                    while (it.hasNext()) {
0207:                        final ColumnDescriptor cd = (ColumnDescriptor) it
0208:                                .next();
0209:                        String javaType = javaType(cd);
0210:                        if (javaType != null) {
0211:                            Properties fieldAttributes = new Properties();
0212:                            if (cd.isPrimaryKey()) {
0213:                                fieldAttributes.setProperty("primary-key",
0214:                                        "yes");
0215:
0216:                            }
0217:                            fieldAttributes.setProperty("column", "yes");
0218:                            fieldAttributes.setProperty("database-name", cd
0219:                                    .getDbName());
0220:                            fieldAttributes.setProperty("java-type", javaType);
0221:                            fieldAttributes.setProperty("label", cd.getLabel());
0222:
0223:                            if (cd.isNullable()) {
0224:                                fieldAttributes.setProperty("nullable", "yes");
0225:                            }
0226:                            c.addField("private " + javaType + " "
0227:                                    + cd.getName(), null, fieldAttributes);
0228:
0229:                            MethodPrototype accessor = new MethodPrototype(c,
0230:                                    "public " + javaType + " get"
0231:                                            + cd.getName() + "()", null);
0232:                            InstructionList il = new InstructionList();
0233:                            //    0    0:aload_0
0234:                            il.append(new ALOAD(0));
0235:                            //    1    1:getfield        #23  <Field String name>
0236:                            il.append(c.createGetField(cd.getName()));
0237:                            //    2    4:areturn         
0238:                            il.append(accessor.createReturn());
0239:                            accessor.addMethod(il, null, "Accessor for "
0240:                                    + cd.getDbName());
0241:
0242:                            if (generateMutators) {
0243:                                MethodPrototype mutator = new MethodPrototype(
0244:                                        c, "public void set" + cd.getName()
0245:                                                + "(" + javaType + " "
0246:                                                + cd.getName() + ")", null);
0247:                                InstructionList mil = new InstructionList();
0248:                                //    0    0:aload_0
0249:                                mil.append(new ALOAD(0));
0250:                                //    1    1:iload_1
0251:                                mil.append(mutator.createVariableLoad(cd
0252:                                        .getName()));
0253:                                //    2    2:putfield        #13  <Field int age>
0254:                                mil.append(c.createPutField(cd.getName()));
0255:                                //    3    5:return          
0256:                                mil.append(new RETURN());
0257:                                mutator.addMethod(mil, null, "Mutator for "
0258:                                        + cd.getDbName());
0259:                            }
0260:                        }
0261:                    }
0262:
0263:                    MethodPrototype dc = new MethodPrototype(c,
0264:                            "public void <init>()", null);
0265:                    InstructionList il = new InstructionList();
0266:                    //    0    0:aload_0     
0267:                    il.append(new ALOAD(0));
0268:                    //    1    1:invokespecial   #33  <Method void XmlBeanBase()>
0269:                    il.append(c.createInvoke(XmlBeanBase.class.getName(),
0270:                            "void <init>()", null, Constants.INVOKESPECIAL));
0271:                    //    2    4:return
0272:                    il.append(new RETURN());
0273:                    dc.addMethod(il, null, "Default constructor");
0274:
0275:                    MethodPrototype rsc = new MethodPrototype(c,
0276:                            "public void <init>(" + ResultSet.class.getName()
0277:                                    + " rs) throws "
0278:                                    + SQLException.class.getName(), null);
0279:                    il = new InstructionList();
0280:                    //    0    0:aload_0     
0281:                    il.append(new ALOAD(0));
0282:                    //    1    1:invokespecial   #33  <Method void XmlBeanBase()>
0283:                    il.append(c.createInvoke(XmlBeanBase.class.getName(),
0284:                            "void <init>()", null, Constants.INVOKESPECIAL));
0285:
0286:                    it = getInterfaceProperties().iterator();
0287:                    while (it.hasNext()) {
0288:                        final ColumnDescriptor cd = (ColumnDescriptor) it
0289:                                .next();
0290:                        String javaType = javaType(cd);
0291:                        if (javaType != null) {
0292:                            //    2    4:aload_0
0293:                            il.append(new ALOAD(0));
0294:                            //    3    5:aload_1    
0295:                            il.append(new ALOAD(1));
0296:                            //    4    6:ldc1            #35  <String "NAME">
0297:                            il.append(new LDC(c.getClassGen().getConstantPool()
0298:                                    .addString(cd.getDbName())));
0299:                            //    5    8:invokeinterface #41  <Method String ResultSet.getString(String)>
0300:                            String getterSignature = ParameterDescriptor
0301:                                    .findGetterSignature(javaType);
0302:                            il.append(c.createInvoke(ResultSet.class.getName(),
0303:                                    getterSignature, null,
0304:                                    Constants.INVOKEINTERFACE));
0305:
0306:                            int idx = getterSignature.indexOf(' ');
0307:                            String rType = getterSignature.substring(0, idx);
0308:                            if (!javaType.equals(rType)) {
0309:                                if (!ParameterDescriptor.isPrimitive(javaType)) {
0310:                                    // Convert
0311:                                    il.append(new LDC(c.getClassGen()
0312:                                            .getConstantPool().addString(
0313:                                                    javaType)));
0314:                                    il
0315:                                            .append(c
0316:                                                    .createInvoke(
0317:                                                            XmlBeanBase.class
0318:                                                                    .getName(),
0319:                                                            "java.lang.Object convert(java.lang.Object,java.lang.String)",
0320:                                                            null,
0321:                                                            Constants.INVOKESTATIC));
0322:                                }
0323:
0324:                                il.append(c.getInstructionFactory().createCast(
0325:                                        Type.OBJECT,
0326:                                        ClassGeneratorBase
0327:                                                .java2BcelType(javaType)));
0328:                            }
0329:
0330:                            //    6   13:putfield        #23  <Field String name>
0331:                            il.append(c.createPutField(cd.getName()));
0332:                        }
0333:                    }
0334:                    //   12   28:return          
0335:                    il.append(new RETURN());
0336:                    rsc.addMethod(il, null, "Constructor from row");
0337:
0338:                    consumer.consume(c.getJavaClass());
0339:                }
0340:            }
0341:
0342:            /**
0343:             * @param packageName
0344:             * @param consumer
0345:             * @throws GenerationException
0346:             */
0347:            protected void generateSmartInterfaceImpl(String packageName,
0348:                    Consumer consumer) throws GenerationException {
0349:                String interfaceImplName = getInterfaceImplName(packageName,
0350:                        true);
0351:
0352:                if (interfaceImplName != null) {
0353:                    String iName = getInterfaceName(packageName);
0354:
0355:                    Class c = new Class(interfaceImplVisibility + " class "
0356:                            + interfaceImplName + " extends "
0357:                            + getSmartBaseName() + " implements " + iName,
0358:                            "Smart implementation of query interface", consumer
0359:                                    .getListener());
0360:
0361:                    Iterator it = getInterfaceProperties().iterator();
0362:                    InstructionFactory instructionFactory = c
0363:                            .getInstructionFactory();
0364:                    while (it.hasNext()) {
0365:                        final ColumnDescriptor cd = (ColumnDescriptor) it
0366:                                .next();
0367:                        String javaType = javaType(cd);
0368:                        if (javaType != null) {
0369:                            String colType = Column.columnType(javaType)
0370:                                    .getName();
0371:                            Properties fieldAttributes = new Properties();
0372:                            if (cd.isPrimaryKey()) {
0373:                                fieldAttributes.setProperty("primary-key",
0374:                                        "yes");
0375:                            }
0376:                            fieldAttributes.setProperty("column", "yes");
0377:                            fieldAttributes.setProperty("database-name", cd
0378:                                    .getDbName());
0379:                            fieldAttributes.setProperty("java-type", javaType);
0380:                            fieldAttributes.setProperty("label", cd.getLabel());
0381:
0382:                            if (cd.isNullable()) {
0383:                                fieldAttributes.setProperty("nullable", "yes");
0384:                            }
0385:                            c.addField("protected " + colType + " "
0386:                                    + cd.getName(), null, fieldAttributes);
0387:
0388:                            MethodPrototype accessor = new MethodPrototype(c,
0389:                                    "public " + javaType + " get"
0390:                                            + cd.getName() + "()", null);
0391:                            InstructionList il = new InstructionList();
0392:                            //    0    0:aload_0
0393:                            il.append(new ALOAD(0));
0394:                            //    1    1:getfield        #23  <Field String name>
0395:                            il.append(c.createGetField(cd.getName()));
0396:                            //    2    4:invokevirtual   #121 <Method int IntColumn.getValue()>
0397:                            String columnParameter = columnParameter(cd);
0398:
0399:                            il.append(c.createInvoke(colType, columnParameter
0400:                                    + " getValue()", null,
0401:                                    Constants.INVOKEVIRTUAL));
0402:                            //    3    7:checkcast       #134 <Class String>
0403:
0404:                            if (!"java.lang.Object".equals(javaType)
0405:                                    && ObjectColumn.class.equals(Column
0406:                                            .columnType(javaType))) {
0407:                                il
0408:                                        .append(instructionFactory
0409:                                                .createCast(
0410:                                                        ClassGeneratorBase
0411:                                                                .java2BcelType("java.lang.Object"),
0412:                                                        ClassGeneratorBase
0413:                                                                .java2BcelType(javaType)));
0414:                            }
0415:                            //    2    4:areturn         
0416:                            il.append(accessor.createReturn());
0417:                            accessor.addMethod(il, null, "Accessor for "
0418:                                    + cd.getDbName());
0419:
0420:                            if (generateMutators) {
0421:                                MethodPrototype mutator = new MethodPrototype(
0422:                                        c, "public void set" + cd.getName()
0423:                                                + "(" + javaType + " "
0424:                                                + cd.getName() + ")", null);
0425:                                InstructionList mil = new InstructionList();
0426:                                //    0    0:aload_0
0427:                                mil.append(new ALOAD(0));
0428:                                //    1    1:getfield        #26  <Field IntColumn id>
0429:                                mil.append(c.createGetField(cd.getName()));
0430:                                //    2    4:iload_1
0431:                                mil.append(mutator.createVariableLoad(cd
0432:                                        .getName()));
0433:                                //    3    5:invokevirtual   #123 <Method void IntColumn.setValue(int)>
0434:                                mil.append(c.createInvoke(colType,
0435:                                        "void setValue(" + columnParameter
0436:                                                + ")", null,
0437:                                        Constants.INVOKEVIRTUAL));
0438:                                //    4    8:return
0439:                                mil.append(new RETURN());
0440:                                mutator.addMethod(mil, null, "Mutator for "
0441:                                        + cd.getDbName());
0442:                            }
0443:                        }
0444:                    }
0445:
0446:                    generateSmartImplStaticStuff(c, consumer);
0447:
0448:                    generateSmartImplDefaultConstructor(c);
0449:                    generateSmartImplConstructorWithForceParameter(c);
0450:
0451:                    generateSmartImplConstructorFromDomElement(c);
0452:                    generateSmartImplConstructorFromDomElementEx(c);
0453:                    generateSmartImplConstructorFromResultSet(c);
0454:                    generateSmartImplClone(c);
0455:
0456:                    consumer.consume(c.getJavaClass());
0457:                }
0458:            }
0459:
0460:            /**
0461:             * @return
0462:             */
0463:            private String getSmartBaseName() {
0464:                return smartBaseName;
0465:            }
0466:
0467:            /**
0468:             * @param c
0469:             * @throws GenerationException
0470:             */
0471:            private void generateSmartImplClone(Class c)
0472:                    throws GenerationException {
0473:                InstructionFactory instructionFactory = c
0474:                        .getInstructionFactory();
0475:                MethodPrototype clone = new MethodPrototype(c,
0476:                        "public java.lang.Object clone() throws "
0477:                                + CloneNotSupportedException.class.getName(),
0478:                        null);
0479:                InstructionList il = new InstructionList();
0480:
0481:                //DatabaseObjectTemplate ret = (DatabaseObjectTemplate)super.clone();
0482:                //    0    0:aload_0 
0483:                il.append(new ALOAD(0));
0484:                //    1    1:invokespecial   #64  <Method Object DatabaseObject.clone()>
0485:                il.append(c.createInvoke(getSmartBaseName(),
0486:                        "java.lang.Object clone()", null,
0487:                        Constants.INVOKESPECIAL));
0488:                //    2    4:checkcast       #2   <Class DatabaseObjectTemplate>
0489:                String className = c.getClassGen().getClassName();
0490:                il.append(instructionFactory.createCast(Type.OBJECT,
0491:                        ClassGeneratorBase.java2BcelType(className)));
0492:                //    3    7:astore_1
0493:                clone.declareVariable("ret", className);
0494:                il.append(clone.createVariableStore("ret"));
0495:
0496:                Iterator it = getInterfaceProperties().iterator();
0497:                while (it.hasNext()) {
0498:                    final ColumnDescriptor cd = (ColumnDescriptor) it.next();
0499:                    String javaType = javaType(cd);
0500:                    if (javaType != null) {
0501:                        String colType = Column.columnType(javaType).getName();
0502:
0503:                        //ret.id = (IntColumn)id.clone();
0504:                        //    4    8:aload_1
0505:                        il.append(clone.createVariableLoad("ret"));
0506:                        //    5    9:aload_0         
0507:                        il.append(new ALOAD(0));
0508:                        //    6   10:getfield        #35  <Field IntColumn id>
0509:                        il.append(c.createGetField(cd.getName()));
0510:                        //    7   13:invokevirtual   #65  <Method Object IntColumn.clone()>
0511:                        il.append(c.createInvoke(colType,
0512:                                "java.lang.Object clone()", null,
0513:                                Constants.INVOKEVIRTUAL));
0514:                        //    8   16:checkcast       #28  <Class IntColumn>
0515:                        il.append(instructionFactory.createCast(Type.OBJECT,
0516:                                ClassGeneratorBase.java2BcelType(colType)));
0517:                        //    9   19:putfield        #35  <Field IntColumn id>
0518:                        il.append(c.createPutField(cd.getName()));
0519:
0520:                        //   ret.addColumn(ret.id);
0521:                        //   10   22:aload_1
0522:                        il.append(clone.createVariableLoad("ret"));
0523:                        //   11   23:aload_1         
0524:                        il.append(clone.createVariableLoad("ret"));
0525:                        //   12   24:getfield        #35  <Field IntColumn id>
0526:                        il.append(c.createGetField(cd.getName()));
0527:                        //   13   27:invokevirtual   #39  <Method void addColumn(Column)>
0528:                        il.append(c.createInvoke("void addColumn("
0529:                                + Column.class.getName() + ")", null,
0530:                                Constants.INVOKEVIRTUAL));
0531:                    }
0532:                }
0533:
0534:                il.append(clone.createVariableLoad("ret"));
0535:                il.append(clone.createReturn());
0536:                clone.addMethod(il, null, "Deep clone - clones columns.");
0537:            }
0538:
0539:            /**
0540:             * @param c
0541:             * @param instructionFactory
0542:             * @throws GenerationException
0543:             */
0544:            private void generateSmartImplConstructorFromResultSet(Class c)
0545:                    throws GenerationException {
0546:                InstructionFactory instructionFactory = c
0547:                        .getInstructionFactory();
0548:                MethodPrototype rsc = new MethodPrototype(
0549:                        c,
0550:                        "public void <init>(" + ResultSet.class.getName()
0551:                                + " rs) throws " + SQLException.class.getName(),
0552:                        null);
0553:                InstructionList il = new InstructionList();
0554:                //    0    0:aload_0     
0555:                il.append(new ALOAD(0));
0556:                //    1    1:invokespecial   #33  <Method void XmlBeanBase()>
0557:                il.append(c.createInvoke(getSmartBaseName(), "void <init>()",
0558:                        null, Constants.INVOKESPECIAL));
0559:
0560:                Iterator it = getInterfaceProperties().iterator();
0561:                while (it.hasNext()) {
0562:                    final ColumnDescriptor cd = (ColumnDescriptor) it.next();
0563:                    String javaType = javaType(cd);
0564:                    if (javaType != null) {
0565:                        String colType = Column.columnType(javaType).getName();
0566:                        //			        if(Column.hasColumn(rs, "LAST_NAME"))
0567:                        //*  28   57:aload_1         
0568:                        il.append(new ALOAD(1));
0569:                        //*  29   58:ldc1            #38  <String "LAST_NAME">
0570:                        il.append(new LDC(c.getClassGen().getConstantPool()
0571:                                .addString(cd.getDbName())));
0572:                        //*  30   60:invokestatic    #71  <Method boolean Column.hasColumn(ResultSet, String)>
0573:                        il
0574:                                .append(c
0575:                                        .createInvoke(
0576:                                                Column.class.getName(),
0577:                                                "boolean hasColumn(java.sql.ResultSet,java.lang.String)",
0578:                                                null, Constants.INVOKESTATIC));
0579:                        //*  31   63:ifeq  91
0580:                        IFEQ ifInstr = new IFEQ(null);
0581:                        il.append(ifInstr);
0582:
0583:                        // id = new IntColumn("ID", true, rs.getInt("ID"));
0584:                        //    2    4:aload_0
0585:                        il.append(new ALOAD(0));
0586:                        //    3    5:new             #16  <Class IntColumn>
0587:                        il.append(instructionFactory.createNew(colType));
0588:                        //    4    8:dup
0589:                        il.append(new DUP());
0590:                        //    5    9:ldc1            #18  <String "ID">
0591:                        il.append(new LDC(c.getClassGen().getConstantPool()
0592:                                .addString(cd.getDbName())));
0593:
0594:                        String cp = columnParameter(cd);
0595:                        if ("java.lang.Object".equals(cp)) {
0596:                            il
0597:                                    .append(c.createGetField("class$"
0598:                                            + cd.getName()));
0599:                        }
0600:
0601:                        //    6   11:iconst_1
0602:                        il.append(cd.isPrimaryKey() ? new ICONST(1)
0603:                                : new ICONST(0));
0604:                        //    7   12:aload_1
0605:                        il.append(new ALOAD(1));
0606:                        //    8   13:ldc1            #18  <String "ID">
0607:                        il.append(new LDC(c.getClassGen().getConstantPool()
0608:                                .addString(cd.getDbName())));
0609:                        //    9   15:invokeinterface #52  <Method int ResultSet.getInt(String)>
0610:                        String getterSignature = ParameterDescriptor
0611:                                .findGetterSignature(javaType);
0612:                        il.append(c.createInvoke(ResultSet.class.getName(),
0613:                                getterSignature, null,
0614:                                Constants.INVOKEINTERFACE));
0615:                        int idx = getterSignature.indexOf(' ');
0616:                        String rType = getterSignature.substring(0, idx);
0617:                        if (!javaType.equals(rType)) {
0618:                            if (!ParameterDescriptor.isPrimitive(javaType)) {
0619:                                // Convert
0620:                                il
0621:                                        .append(new LDC(c.getClassGen()
0622:                                                .getConstantPool().addString(
0623:                                                        javaType)));
0624:                                il
0625:                                        .append(c
0626:                                                .createInvoke(
0627:                                                        XmlBeanBase.class
0628:                                                                .getName(),
0629:                                                        "java.lang.Object convert(java.lang.Object,java.lang.String)",
0630:                                                        null,
0631:                                                        Constants.INVOKESTATIC));
0632:                            }
0633:
0634:                            il.append(instructionFactory.createCast(
0635:                                    Type.OBJECT, ClassGeneratorBase
0636:                                            .java2BcelType(javaType)));
0637:                        }
0638:
0639:                        Collection paramTypes = new ArrayList();
0640:                        paramTypes.add("java.lang.String");
0641:                        if ("java.lang.Object".equals(cp)) {
0642:                            paramTypes.add("java.lang.Class");
0643:                        }
0644:                        paramTypes.add("boolean");
0645:                        paramTypes.add(cp);
0646:
0647:                        //   10   20:invokespecial   #55  <Method void IntColumn(String, boolean, int)>
0648:                        il.append(c.createInvoke(colType, "void <init>()",
0649:                                paramTypes, Constants.INVOKESPECIAL));
0650:                        //   11   23:putfield        #23  <Field IntColumn id>
0651:                        il.append(c.createPutField(cd.getName()));
0652:
0653:                        GOTO gotoInstr = new GOTO(null);
0654:                        il.append(gotoInstr);
0655:
0656:                        //					System.out.println(name+"."+cd.getName()+" -> "+colType);
0657:                        //id = new IntColumn("ID", true);
0658:                        //    3    6:aload_0
0659:                        ifInstr.setTarget(il.append(new ALOAD(0)));
0660:                        //    4    7:new             #19  <Class IntColumn>
0661:                        il.append(instructionFactory.createNew(colType));
0662:                        //    5   10:dup
0663:                        il.append(new DUP());
0664:                        //    6   11:ldc1            #21  <String "ID">
0665:                        il.append(new LDC(c.getClassGen().getConstantPool()
0666:                                .addString(cd.getDbName())));
0667:
0668:                        if ("java.lang.Object".equals(cp)) {
0669:                            il
0670:                                    .append(c.createGetField("class$"
0671:                                            + cd.getName()));
0672:                        }
0673:                        //    7   13:iconst_1
0674:                        il.append(cd.isPrimaryKey() ? new ICONST(1)
0675:                                : new ICONST(0));
0676:                        //    8   14:invokespecial   #24  <Method void IntColumn(String, boolean)>
0677:
0678:                        paramTypes = new ArrayList();
0679:                        paramTypes.add("java.lang.String");
0680:                        if ("java.lang.Object".equals(cp)) {
0681:                            paramTypes.add("java.lang.Class");
0682:                        }
0683:                        paramTypes.add("boolean");
0684:
0685:                        il.append(c.createInvoke(colType, "void <init>()",
0686:                                paramTypes, Constants.INVOKESPECIAL));
0687:                        //    9   17:putfield        #26  <Field IntColumn id>
0688:                        il.append(c.createPutField(cd.getName()));
0689:
0690:                        //addColumn(id);
0691:                        //   12   26:aload_0
0692:                        gotoInstr.setTarget(il.append(new ALOAD(0)));
0693:                        //   13   27:aload_0
0694:                        il.append(new ALOAD(0));
0695:
0696:                        //   14   28:getfield        #23  <Field IntColumn id>
0697:                        il.append(c.createGetField(cd.getName()));
0698:
0699:                        //   15   31:invokevirtual   #27  <Method void addColumn(biz.hammurapi.sql.columns.Column)>
0700:                        il
0701:                                .append(c
0702:                                        .createInvoke(
0703:                                                "void addColumn(biz.hammurapi.sql.columns.Column)",
0704:                                                null, Constants.INVOKEVIRTUAL));
0705:
0706:                        //		        firstName.setLabel("First name");
0707:                        //   21   43:aload_0
0708:                        il.append(new ALOAD(0));
0709:                        //   22   44:getfield        #105 <Field ObjectColumn firstName>
0710:                        il.append(c.createGetField(cd.getName()));
0711:                        //   23   47:ldc1            #107 <String "First name">
0712:                        il.append(new LDC(c.getClassGen().getConstantPool()
0713:                                .addString(cd.getLabel())));
0714:                        //   24   49:invokevirtual   #110 <Method void ObjectColumn.setLabel(String)>
0715:                        il.append(c.createInvoke(
0716:                                "biz.hammurapi.sql.columns.Column",
0717:                                "void setLabel(java.lang.String)", null,
0718:                                Constants.INVOKEVIRTUAL));
0719:
0720:                        if (useSqlTypes
0721:                                && colType.equals(ObjectColumn.class.getName())) {
0722:                            // setSqlType()
0723:                            il.append(new ALOAD(0));
0724:                            il.append(c.createGetField(cd.getName()));
0725:                            il.append(new LDC(c.getClassGen().getConstantPool()
0726:                                    .addInteger(cd.getDbType())));
0727:                            il.append(c.createInvoke(colType,
0728:                                    "void setSqlType(int)", null,
0729:                                    Constants.INVOKEVIRTUAL));
0730:                        }
0731:                    }
0732:                }
0733:
0734:                //   41   85:aload_0         
0735:                //   42   86:invokevirtual   #117 <Method void setOriginal()>
0736:                il.append(new ALOAD(0));
0737:                il.append(c.createInvoke("void setOriginal()", null,
0738:                        Constants.INVOKEVIRTUAL));
0739:
0740:                //   12   28:return          
0741:                il.append(new RETURN());
0742:                rsc.addMethod(il, null, "Constructor from row");
0743:            }
0744:
0745:            /**
0746:             * @param c
0747:             * @throws GenerationException
0748:             */
0749:            private void generateSmartImplConstructorWithForceParameter(Class c)
0750:                    throws GenerationException {
0751:                MethodPrototype fc = new MethodPrototype(c,
0752:                        "public void <init>(boolean force)", null);
0753:                InstructionList il = new InstructionList();
0754:                //    0    0:aload_0     
0755:                il.append(new ALOAD(0));
0756:                il.append(new ILOAD(1));
0757:                //    1    1:invokespecial   #33  <Method void XmlBeanBase()>
0758:                il.append(c.createInvoke(getSmartBaseName(),
0759:                        "void <init>(boolean)", null, Constants.INVOKESPECIAL));
0760:
0761:                generateSmartImplConstructor(c, il);
0762:
0763:                //    2    4:return
0764:                il.append(new RETURN());
0765:                fc.addMethod(il, null, "Constructor with force parameter");
0766:            }
0767:
0768:            /**
0769:             * @param c
0770:             * @throws GenerationException
0771:             */
0772:            private void generateSmartImplDefaultConstructor(Class c)
0773:                    throws GenerationException {
0774:                MethodPrototype dc = new MethodPrototype(c,
0775:                        "public void <init>()", null);
0776:                InstructionList il = new InstructionList();
0777:                //    0    0:aload_0     
0778:                il.append(new ALOAD(0));
0779:                //    1    1:invokespecial   #33  <Method void XmlBeanBase()>
0780:                il.append(c.createInvoke(getSmartBaseName(), "void <init>()",
0781:                        null, Constants.INVOKESPECIAL));
0782:
0783:                generateSmartImplConstructor(c, il);
0784:
0785:                //    2    4:return
0786:                il.append(new RETURN());
0787:                dc.addMethod(il, null, "Default constructor");
0788:            }
0789:
0790:            /**
0791:             * @param c
0792:             * @throws GenerationException
0793:             */
0794:            private void generateSmartImplConstructorFromDomElement(Class c)
0795:                    throws GenerationException {
0796:                InstructionList il;
0797:                MethodPrototype xmlc = new MethodPrototype(
0798:                        c,
0799:                        "public void <init>(org.w3c.dom.Element holder, boolean force) throws biz.hammurapi.config.ConfigurationException",
0800:                        null);
0801:                il = new InstructionList();
0802:                //    0    0:aload_0     
0803:                il.append(new ALOAD(0));
0804:
0805:                il.append(new ILOAD(2));
0806:                //    1    1:invokespecial   #33  <Method void XmlBeanBase()>
0807:                il.append(c.createInvoke("void <init>(boolean)", null,
0808:                        Constants.INVOKESPECIAL));
0809:
0810:                //    2    4:aload_0         
0811:                il.append(new ALOAD(0));
0812:                //    3    5:aload_1         
0813:                il.append(new ALOAD(1));
0814:                //    4    6:invokevirtual   #95  <Method void fromDom(Element)>
0815:                il.append(c.createInvoke("void fromDom(org.w3c.dom.Element)",
0816:                        null, Constants.INVOKEVIRTUAL));
0817:                //    5    9:return  
0818:                il.append(new RETURN());
0819:
0820:                xmlc.addMethod(il, null, "Constructor from DOM element");
0821:            }
0822:
0823:            /**
0824:             * @param c
0825:             * @throws GenerationException
0826:             */
0827:            private void generateSmartImplConstructorFromDomElementEx(Class c)
0828:                    throws GenerationException {
0829:                InstructionList il;
0830:                MethodPrototype xmlc = new MethodPrototype(
0831:                        c,
0832:                        "public void <init>(org.w3c.dom.Element holder, java.util.Properties pathMap, org.apache.xpath.CachedXPathAPI cxpa, boolean force) throws biz.hammurapi.config.ConfigurationException",
0833:                        null);
0834:                il = new InstructionList();
0835:                //    0    0:aload_0     
0836:                il.append(new ALOAD(0));
0837:
0838:                il.append(new ILOAD(4));
0839:                //    1    1:invokespecial   #33  <Method void XmlBeanBase()>
0840:                il.append(c.createInvoke("void <init>(boolean)", null,
0841:                        Constants.INVOKESPECIAL));
0842:
0843:                //    2    4:aload_0         
0844:                il.append(new ALOAD(0));
0845:                //    3    5:aload_1         
0846:                il.append(new ALOAD(1));
0847:                //    4    6:aload_2         
0848:                il.append(new ALOAD(2));
0849:                //    5    7:aload_3         
0850:                il.append(new ALOAD(3));
0851:                //    4    6:invokevirtual   #95  <Method void fromDom(Element)>
0852:                il
0853:                        .append(c
0854:                                .createInvoke(
0855:                                        "void fromDom(org.w3c.dom.Element,java.util.Properties,org.apache.xpath.CachedXPathAPI)",
0856:                                        null, Constants.INVOKEVIRTUAL));
0857:                //    5    9:return  
0858:                il.append(new RETURN());
0859:
0860:                xmlc.addMethod(il, null, "Constructor from DOM element");
0861:            }
0862:
0863:            /**
0864:             * Generates projector and project methods
0865:             * @param c
0866:             * @param consumer
0867:             * @throws GenerationException
0868:             */
0869:            private void generateSmartImplStaticStuff(Class parentClass,
0870:                    Consumer consumer) throws GenerationException {
0871:                String parentClassName = parentClass.getJavaClass()
0872:                        .getClassName();
0873:                String className = parentClassName + "$Projector";
0874:                Class pc = new Class("static class " + className
0875:                        + " extends java.lang.Object implements "
0876:                        + Projector.class.getName(), "Projector class",
0877:                        consumer.getListener());
0878:                pc.getJavaClass().setModifiers(Constants.ACC_STATIC);
0879:
0880:                MethodPrototype dc = new MethodPrototype(pc, "void <init>()",
0881:                        null);
0882:                InstructionList il = new InstructionList();
0883:                //    0    0:aload_0     
0884:                il.append(new ALOAD(0));
0885:                //    1    1:invokespecial   #33  <Method void XmlBeanBase()>
0886:                il.append(pc.createInvoke("java.lang.Object", "void <init>()",
0887:                        null, Constants.INVOKESPECIAL));
0888:                //    2    4:return
0889:                il.append(new RETURN());
0890:                dc.addMethod(il, null, "Default constructor");
0891:
0892:                MethodPrototype project = new MethodPrototype(pc,
0893:                        "public java.lang.Object project("
0894:                                + ResultSet.class.getName()
0895:                                + " rs) throws java.sql.SQLException", null);
0896:                il = new InstructionList();
0897:                //    0    0:new             #22  <Class DatabaseObjectTemplate>
0898:                il
0899:                        .append(pc.getInstructionFactory().createNew(
0900:                                parentClassName));
0901:                //    1    3:dup  
0902:                il.append(new DUP());
0903:                //    2    4:aload_1 
0904:                il.append(new ALOAD(1));
0905:                //    3    5:invokespecial   #25  <Method void DatabaseObjectTemplate(ResultSet)>
0906:                il.append(pc.createInvoke(parentClassName, "void <init>("
0907:                        + ResultSet.class.getName() + ")", null,
0908:                        Constants.INVOKESPECIAL));
0909:                //    4    8:areturn
0910:                il.append(new ARETURN());
0911:
0912:                project.addMethod(il, null, "Projects result set to "
0913:                        + parentClassName);
0914:
0915:                consumer.consume(pc.getJavaClass());
0916:
0917:                parentClass.addField("private static "
0918:                        + Projector.class.getName() + " _projector",
0919:                        "Projector", null);
0920:
0921:                /**
0922:                 * Static initializer
0923:                 */
0924:                il = new InstructionList();
0925:                //    0    0:new             #18  <Class DatabaseObjectTemplate$TemplateProjector>
0926:                il.append(parentClass.getInstructionFactory().createNew(
0927:                        className));
0928:                //    1    3:dup             
0929:                il.append(new DUP());
0930:                //    2    4:invokespecial   #21  <Method void DatabaseObjectTemplate$TemplateProjector()>
0931:                il.append(parentClass.createInvoke(className, "void <init>()",
0932:                        null, Constants.INVOKESPECIAL));
0933:                //    3    7:putstatic       #23  <Field Projector projector>
0934:                il.append(parentClass.createPutField("_projector"));
0935:
0936:                Iterator it = getInterfaceProperties().iterator();
0937:                ExceptionHandler eh = new ExceptionHandler(
0938:                        ClassNotFoundException.class.getName());
0939:                InstructionFactory instructionFactory = parentClass
0940:                        .getInstructionFactory();
0941:                while (it.hasNext()) {
0942:                    final ColumnDescriptor cd = (ColumnDescriptor) it.next();
0943:                    String javaType = javaType(cd);
0944:                    if (javaType != null) {
0945:                        String cp = columnParameter(cd);
0946:                        if ("java.lang.Object".equals(cp)) {
0947:                            parentClass.addField(
0948:                                    "private static java.lang.Class class$"
0949:                                            + cd.getName(), "Column class",
0950:                                    null);
0951:
0952:                            //    0    0:ldc1            #20  <String "java.lang.String">
0953:                            InstructionHandle ih = il
0954:                                    .append(new LDC(
0955:                                            parentClass
0956:                                                    .getClassGen()
0957:                                                    .getConstantPool()
0958:                                                    .addString(
0959:                                                            javaType
0960:                                                                    .indexOf("[]") == -1 ? javaType
0961:                                                                    : Utility
0962:                                                                            .getSignature(javaType))));
0963:
0964:                            if (eh.getFrom() == null) {
0965:                                eh.setFrom(ih);
0966:                            }
0967:
0968:                            //    5   10:invokestatic    #30  <Method Class Class.forName(String)>
0969:                            il
0970:                                    .append(parentClass
0971:                                            .createInvoke(
0972:                                                    "java.lang.Class",
0973:                                                    "java.lang.Class forName(java.lang.String)",
0974:                                                    null,
0975:                                                    Constants.INVOKESTATIC));
0976:                            //    7   14:putstatic       #22  <Field Class class$0>
0977:                            eh.setTo(il.append(parentClass
0978:                                    .createPutField("class$" + cd.getName())));
0979:                        }
0980:                    }
0981:                }
0982:
0983:                GOTO gt = new GOTO(null);
0984:                il.append(gt);
0985:
0986:                Collection ehl = new ArrayList();
0987:
0988:                if (eh.getFrom() != null) {
0989:                    //*   4   11:astore_0
0990:                    eh.setHandler(il.append(new ASTORE(0)));
0991:                    //    9   20:new             #32  <Class NoClassDefFoundError>
0992:                    il.append(instructionFactory
0993:                            .createNew(NoClassDefFoundError.class.getName()));
0994:                    //    6   15:dup
0995:                    il.append(new DUP());
0996:                    //    7   16:aload_0     
0997:                    il.append(new ALOAD(0));
0998:                    //    8   17:invokevirtual   #36  <Method String ClassNotFoundException.getMessage()>
0999:                    il.append(parentClass.createInvoke("java.lang.Throwable",
1000:                            "java.lang.String getMessage()", null,
1001:                            Constants.INVOKEVIRTUAL));
1002:                    //    9   20:invokespecial   #40  <Method void NoClassDefFoundError(String)>
1003:                    il.append(parentClass.createInvoke(
1004:                            NoClassDefFoundError.class.getName(),
1005:                            "void <init>(java.lang.String)", null,
1006:                            Constants.INVOKESPECIAL));
1007:                    //   10   23:athrow          
1008:                    il.append(new ATHROW());
1009:
1010:                    ehl.add(eh);
1011:                }
1012:
1013:                gt.setTarget(il.append(new RETURN()));
1014:
1015:                parentClass.addStaticInitializer(il, ehl, "Static initializer");
1016:
1017:                /**
1018:                 * Project.
1019:                 */
1020:                project = new MethodPrototype(parentClass,
1021:                        "public static java.util.Collection project("
1022:                                + SQLProcessor.class.getName()
1023:                                + " processor, java.lang.String sql, "
1024:                                + Parameterizer.class.getName()
1025:                                + " parameterizer)", null);
1026:
1027:                il = new InstructionList();
1028:                //    0    0:aload_0     
1029:                il.append(new ALOAD(0));
1030:                //    1    1:aload_1         
1031:                il.append(new ALOAD(1));
1032:                //    2    2:aload_2         
1033:                il.append(new ALOAD(2));
1034:                //    3    3:getstatic       #23  <Field Projector projector>
1035:                il.append(parentClass.createGetField("_projector"));
1036:                //    4    6:invokevirtual   #174 <Method Collection SQLProcessor.project(String, Parameterizer, Projector)>
1037:                il.append(parentClass.createInvoke(
1038:                        SQLProcessor.class.getName(),
1039:                        "java.util.Collection project(java.lang.String, "
1040:                                + Parameterizer.class.getName() + ", "
1041:                                + Projector.class.getName() + ")", null,
1042:                        Constants.INVOKEVIRTUAL));
1043:                //    5    9:areturn
1044:                il.append(new ARETURN());
1045:                project.addMethod(il, null,
1046:                        "Returns database backed collection with elements of type "
1047:                                + parentClassName);
1048:
1049:                project = new MethodPrototype(
1050:                        parentClass,
1051:                        "public static java.util.Collection project("
1052:                                + SQLProcessor.class.getName()
1053:                                + " processor, java.lang.String sql, "
1054:                                + Parameterizer.class.getName()
1055:                                + " parameterizer, java.util.Collection receiver) throws java.sql.SQLException",
1056:                        null);
1057:
1058:                il = new InstructionList();
1059:                //    0    0:aload_0     
1060:                il.append(new ALOAD(0));
1061:                //    1    1:aload_1         
1062:                il.append(new ALOAD(1));
1063:                //    2    2:aload_2         
1064:                il.append(new ALOAD(2));
1065:                //    3    3:getstatic       #23  <Field Projector projector>
1066:                il.append(parentClass.createGetField("_projector"));
1067:                //    4    6:aload_3
1068:                il.append(new ALOAD(3));
1069:                //    5    7:invokevirtual   #181 <Method Collection SQLProcessor.project(String, Parameterizer, Projector, Collection)>
1070:                il.append(parentClass.createInvoke(
1071:                        SQLProcessor.class.getName(),
1072:                        "java.util.Collection project(java.lang.String, "
1073:                                + Parameterizer.class.getName() + ", "
1074:                                + Projector.class.getName()
1075:                                + ",java.util.Collection)", null,
1076:                        Constants.INVOKEVIRTUAL));
1077:                //    6   10:areturn         
1078:                il.append(new ARETURN());
1079:                project.addMethod(il, null,
1080:                        "Populates receiver collection with elements of type "
1081:                                + parentClassName + " and returns it");
1082:            }
1083:
1084:            /**
1085:             * @param cd
1086:             * @return Java type of column
1087:             */
1088:            private String javaType(final ColumnDescriptor cd) {
1089:                return getColumnType(cd, cd.isNullable() ? cd.getType()
1090:                        : ParameterDescriptor.toPrimitive(cd.getType()));
1091:            }
1092:
1093:            /**
1094:             * @param c
1095:             * @param il
1096:             * @throws GenerationException
1097:             */
1098:            private void generateSmartImplConstructor(Class c,
1099:                    InstructionList il) throws GenerationException {
1100:                Iterator it = getInterfaceProperties().iterator();
1101:                InstructionFactory instructionFactory = c
1102:                        .getInstructionFactory();
1103:                while (it.hasNext()) {
1104:                    final ColumnDescriptor cd = (ColumnDescriptor) it.next();
1105:                    String javaType = javaType(cd);
1106:                    if (javaType != null) {
1107:                        String colType = Column.columnType(javaType).getName();
1108:                        //				System.out.println(name+"."+cd.getName()+" -> "+colType);
1109:                        //id = new IntColumn("ID", true);
1110:                        //    3    6:aload_0
1111:                        il.append(new ALOAD(0));
1112:                        //    4    7:new             #19  <Class IntColumn>
1113:                        il.append(instructionFactory.createNew(colType));
1114:                        //    5   10:dup
1115:                        il.append(new DUP());
1116:                        //    6   11:ldc1            #21  <String "ID">
1117:                        il.append(new LDC(c.getClassGen().getConstantPool()
1118:                                .addString(cd.getDbName())));
1119:
1120:                        String cp = columnParameter(cd);
1121:                        if ("java.lang.Object".equals(cp)) {
1122:                            il
1123:                                    .append(c.createGetField("class$"
1124:                                            + cd.getName()));
1125:                        }
1126:
1127:                        //    7   13:iconst_1
1128:                        il.append(cd.isPrimaryKey() ? new ICONST(1)
1129:                                : new ICONST(0));
1130:
1131:                        Collection paramTypes = new ArrayList();
1132:                        paramTypes.add("java.lang.String");
1133:                        if ("java.lang.Object".equals(cp)) {
1134:                            paramTypes.add("java.lang.Class");
1135:                        }
1136:                        paramTypes.add("boolean");
1137:
1138:                        //    8   14:invokespecial   #24  <Method void IntColumn(String, boolean)>
1139:                        il.append(c.createInvoke(colType, "void <init>()",
1140:                                paramTypes, Constants.INVOKESPECIAL));
1141:                        //    9   17:putfield        #26  <Field IntColumn id>
1142:                        il.append(c.createPutField(cd.getName()));
1143:
1144:                        //addColumn(id);
1145:                        il.append(new ALOAD(0));
1146:                        il.append(new ALOAD(0));
1147:                        il.append(c.createGetField(cd.getName()));
1148:                        //   13   25:invokevirtual   #30  <Method void addColumn(biz.hammurapi.sql.columns.Column)>
1149:                        il
1150:                                .append(c
1151:                                        .createInvoke(
1152:                                                "void addColumn(biz.hammurapi.sql.columns.Column)",
1153:                                                null, Constants.INVOKEVIRTUAL));
1154:
1155:                        //		        firstName.setLabel("First name");
1156:                        //   21   43:aload_0
1157:                        il.append(new ALOAD(0));
1158:                        //   22   44:getfield        #105 <Field ObjectColumn firstName>
1159:                        il.append(c.createGetField(cd.getName()));
1160:                        //   23   47:ldc1            #107 <String "First name">
1161:                        il.append(new LDC(c.getClassGen().getConstantPool()
1162:                                .addString(cd.getLabel())));
1163:                        //   24   49:invokevirtual   #110 <Method void ObjectColumn.setLabel(String)>
1164:                        il.append(c.createInvoke(
1165:                                "biz.hammurapi.sql.columns.Column",
1166:                                "void setLabel(java.lang.String)", null,
1167:                                Constants.INVOKEVIRTUAL));
1168:
1169:                        if (useSqlTypes
1170:                                && colType.equals(ObjectColumn.class.getName())) {
1171:                            // setSqlType()
1172:                            il.append(new ALOAD(0));
1173:                            il.append(c.createGetField(cd.getName()));
1174:                            il.append(new LDC(c.getClassGen().getConstantPool()
1175:                                    .addInteger(cd.getDbType())));
1176:                            il.append(c.createInvoke(colType,
1177:                                    "void setSqlType(int)", null,
1178:                                    Constants.INVOKEVIRTUAL));
1179:                        }
1180:                    }
1181:                }
1182:            }
1183:
1184:            /**
1185:             * @param cd
1186:             * @return column type
1187:             */
1188:            private String columnParameter(final ColumnDescriptor cd) {
1189:                String javaType = javaType(cd);
1190:                return ParameterDescriptor.isPrimitive(javaType) ? javaType
1191:                        : "java.lang.Object";
1192:            }
1193:
1194:            /**
1195:             * @param packageName
1196:             * @return
1197:             */
1198:            protected String getInterfaceName(String packageName) {
1199:                if (interfaceDescriptor == null) {
1200:                    return ((packageName == null || packageName.length() == 0) ? ""
1201:                            : packageName + ".")
1202:                            + name;
1203:                }
1204:
1205:                InterfaceDescriptor master = interfaceDescriptor.getMaster();
1206:                if (master.isExternal()) {
1207:                    return master.getName();
1208:                }
1209:
1210:                return ((packageName == null || packageName.length() == 0) ? ""
1211:                        : packageName + ".")
1212:                        + master.getName();
1213:            }
1214:
1215:            //private InterfacePool pool;
1216:            private InterfaceDescriptor interfaceDescriptor;
1217:
1218:            protected void setInterfacePool(InterfacePool pool) {
1219:                if (isToBeGenerated()) {
1220:                    //this.pool=pool;
1221:                    Map attributes = null;
1222:                    if (generateMutators && getMode() != MODE_SIMPLE) {
1223:                        attributes = new HashMap();
1224:                        attributes.put("smart", "yes");
1225:                    }
1226:                    interfaceDescriptor = pool.addInterface(name, attributes);
1227:
1228:                    Iterator it = getInterfaceProperties().iterator();
1229:                    while (it.hasNext()) {
1230:                        final ColumnDescriptor cd = (ColumnDescriptor) it
1231:                                .next();
1232:                        String javaType = javaType(cd);
1233:                        if (javaType != null) {
1234:                            interfaceDescriptor.addMethod(javaType + " get"
1235:                                    + cd.getName() + "()");
1236:
1237:                            if (generateMutators) {
1238:                                interfaceDescriptor.addMethod("void set"
1239:                                        + cd.getName() + "(" + javaType + ")");
1240:                            }
1241:                        }
1242:                    }
1243:
1244:                    interfaceDescriptor.addMethod("void toDom("
1245:                            + Element.class.getName() + ")");
1246:                }
1247:            }
1248:
1249:            protected abstract Collection getInterfaceProperties();
1250:
1251:            private Map columnTypes = new HashMap();
1252:
1253:            public static final byte MODE_SIMPLE = 0;
1254:            public static final byte MODE_INSERT = 1;
1255:            public static final byte MODE_UPDATE = 2;
1256:            public static final byte MODE_SMART = 3;
1257:
1258:            private byte mode;
1259:
1260:            protected String getColumnType(ColumnDescriptor cd,
1261:                    String defaultType) {
1262:                return columnTypes.containsKey(cd.getDbName().toUpperCase()) ? (String) columnTypes
1263:                        .get(cd.getDbName().toUpperCase())
1264:                        : defaultType;
1265:            }
1266:
1267:            protected boolean isSkipColumn(ColumnDescriptor cd) {
1268:                return columnTypes.containsKey(cd.getDbName().toUpperCase()) ? columnTypes
1269:                        .get(cd.getDbName().toUpperCase()) == null
1270:                        : false;
1271:            }
1272:
1273:            public void setColumnType(String name, String type) {
1274:                columnTypes.put(name.toUpperCase(), type);
1275:            }
1276:
1277:            public void setSkipColumn(String name) {
1278:                columnTypes.put(name.toUpperCase(), null);
1279:            }
1280:
1281:            private boolean generateImplementationClassFieldInit;
1282:
1283:            /**
1284:             * @param c
1285:             * @throws GenerationException
1286:             */
1287:            protected void generateImplementationClassField(Class c,
1288:                    String packageName) throws GenerationException {
1289:                String implClassFieldName = getImplClassFieldName(packageName);
1290:                if (c.hasField(implClassFieldName)) {
1291:                    generateImplementationClassFieldInit = false;
1292:                } else {
1293:                    c.addField("private static final java.lang.Class "
1294:                            + implClassFieldName, "Implementation class field",
1295:                            null);
1296:                    generateImplementationClassFieldInit = true;
1297:                }
1298:            }
1299:
1300:            /**
1301:             * @param packageName
1302:             * @return
1303:             */
1304:            protected String getImplClassFieldName(String packageName) {
1305:                return "IMPL_CLASS_"
1306:                        + getInterfaceName(packageName).replace('.', '_');
1307:            }
1308:
1309:            protected void appendImplementationClassFieldInit(Class c,
1310:                    String packageName, InstructionList target)
1311:                    throws GenerationException {
1312:                if (generateImplementationClassFieldInit) {
1313:                    //    0    0:ldc1            #16  <String "CacheEntryImpl">
1314:                    target.append(new LDC(
1315:                            c.getClassGen().getConstantPool().addString(
1316:                                    getInterfaceImplName(packageName, false))));
1317:                    //    1    2:invokestatic    #22  <Method Class Class.forName(String)>
1318:                    target.append(c.createInvoke("java.lang.Class",
1319:                            "java.lang.Class forName(java.lang.String)", null,
1320:                            Constants.INVOKESTATIC));
1321:                    //    2    5:putstatic       #24  <Field Class IMPL_CLASS>
1322:                    target
1323:                            .append(c
1324:                                    .createPutField(getImplClassFieldName(packageName)));
1325:                }
1326:            }
1327:
1328:            /**
1329:             * @param packageName
1330:             * @return Interface name or null if returnNullIfExists is true and implementation is found
1331:             * in the classloader.
1332:             * @throws GenerationException
1333:             */
1334:            private String getInterfaceImplName(String packageName,
1335:                    boolean returnNullIfExists) throws GenerationException {
1336:                if (interfaceDescriptor == null) {
1337:                    return getInterfaceName(packageName) + "Impl";
1338:                }
1339:
1340:                InterfaceDescriptor master = interfaceDescriptor.getMaster();
1341:                if (master.isExternal()) {
1342:                    String externalImplName = master.getName() + "Impl";
1343:                    return returnNullIfExists ? null : externalImplName;
1344:                } else if (master == interfaceDescriptor || !returnNullIfExists) {
1345:                    return getInterfaceName(packageName) + "Impl";
1346:                } else {
1347:                    return null;
1348:                }
1349:            }
1350:
1351:            public void generate(String packageName, Consumer consumer,
1352:                    Class masterEngine) throws GenerationException {
1353:                if (isToBeGenerated()) {
1354:                    generateInterface(packageName, consumer);
1355:                    //			System.out.print(this+" "+getInterfaceName(packageName)+" -> ");
1356:                    if ((mode != MODE_SIMPLE && generateMutators)
1357:                            || (interfaceDescriptor != null && "yes"
1358:                                    .equals(interfaceDescriptor.getAttributes()
1359:                                            .get("smart")))) {
1360:                        //			    System.out.println("Smart");
1361:                        generateSmartInterfaceImpl(packageName, consumer);
1362:                    } else {
1363:                        //			    System.out.println("Plain");
1364:                        generateInterfaceImpl(packageName, consumer);
1365:                    }
1366:                }
1367:
1368:                generateParameterizer(packageName, consumer, masterEngine);
1369:
1370:                if (masterEngine == null) {
1371:                    generateEngine(packageName, consumer);
1372:                } else {
1373:                    generateEngineMethods(packageName, masterEngine);
1374:                }
1375:            }
1376:
1377:            /**
1378:             * @return Returns the isSmart.
1379:             */
1380:            public byte getMode() {
1381:                return mode;
1382:            }
1383:
1384:            /**
1385:             * @param mode 
1386:             */
1387:            public void setMode(byte mode) {
1388:                this .mode = mode;
1389:            }
1390:
1391:            public String getInterfaceImplVisibility() {
1392:                return interfaceImplVisibility;
1393:            }
1394:
1395:            public void setInterfaceImplVisibility(
1396:                    String interfaceImplVisibility) {
1397:                this .interfaceImplVisibility = interfaceImplVisibility;
1398:            }
1399:
1400:            /**
1401:             * Class name for smart implementation base class. Defaults to biz.hammurapi.sql.DatabaseObject
1402:             * @param smartBaseName The smartBaseName to set.
1403:             */
1404:            public void setSmartBaseName(String smartBaseName) {
1405:                this.smartBaseName = smartBaseName;
1406:            }
1407:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.