001: /*
002: * sqlc 1
003: * SQL Compiler
004: * Copyright (C) 2003 Hammurapi Group
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * URL: http://www.hammurapi.biz/products/sqlc/index.html
021: * e-Mail: support@hammurapi.biz
022: */
023: package biz.hammurapi.codegen;
024:
025: import java.util.ArrayList;
026: import java.util.Collection;
027: import java.util.HashSet;
028: import java.util.Properties;
029: import java.util.Set;
030:
031: import org.apache.bcel.generic.ClassGen;
032:
033: import biz.hammurapi.codegen.JavaTokenTypes;
034:
035: import antlr.collections.AST;
036:
037: /**
038: * @author Pavel Vlasov
039: * @version $Revision: 1.5 $
040: */
041: public class Class extends ClassGeneratorBase {
042: /**
043: *
044: * @param definition E.g. <CODE>public class C extends a.b.A implements a.b.c.d.B</CODE> Superinterfaces names shall be fully qualified.
045: * @param description
046: * @param listener
047: * @throws GenerationException
048: */
049: public Class(String definition, String description,
050: GenerationListener listener) throws GenerationException {
051: this .listener = listener;
052: AST ast = typeDefinition(definition);
053: if (ast.getType() == JavaTokenTypes.CLASS_DEF) {
054: Set modifiers = new HashSet();
055: String name = null;
056: String super Class = "java.lang.Object";
057: Collection implementedInterfaces = new ArrayList();
058: for (AST node = ast.getFirstChild(); node != null; node = node
059: .getNextSibling()) {
060: switch (node.getType()) {
061: case JavaTokenTypes.MODIFIERS:
062: for (AST child = node.getFirstChild(); child != null; child = child
063: .getNextSibling()) {
064: modifiers.add(child.getText());
065: }
066: break;
067: case JavaTokenTypes.IDENT:
068: name = node.getText();
069: break;
070: case JavaTokenTypes.DOT:
071: name = toString(node);
072: break;
073: case JavaTokenTypes.EXTENDS_CLAUSE:
074: if (node.getFirstChild() != null) {
075: super Class = toString(node.getFirstChild());
076: }
077: break;
078: case JavaTokenTypes.IMPLEMENTS_CLAUSE:
079: for (AST interfaceNode = node.getFirstChild(); interfaceNode != null; interfaceNode = interfaceNode
080: .getNextSibling()) {
081: implementedInterfaces
082: .add(toString(interfaceNode));
083: }
084: break;
085: default:
086: throw new GenerationException("Unexpected node: "
087: + node + " in '" + definition + "'");
088: }
089: }
090:
091: cg = new ClassGen(name, super Class,
092: description == null ? "<generated>" : description,
093: modifiers(modifiers),
094: (String[]) implementedInterfaces
095: .toArray(new String[implementedInterfaces
096: .size()]));
097:
098: if (listener != null
099: && (modifiers.contains("protected") || modifiers
100: .contains("public"))) {
101: listener.onClass(cg, description);
102: }
103: } else {
104: throw new GenerationException("Invalid node type "
105: + ast.getType() + " in definition '" + definition
106: + "'");
107: }
108: }
109:
110: /**
111: * Creates a private field of specified name and type.
112: * @param name
113: * @param type
114: * @param description
115: * @param attributes
116: * @throws GenerationException
117: */
118: public void addField(String name, String type, String description,
119: Properties attributes) throws GenerationException {
120: addField("private " + type + " " + name, description,
121: attributes);
122: }
123: }
|