001: /*
002: * Copyright 1998-1999 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.jdwpgen;
027:
028: import java.util.*;
029: import java.io.*;
030:
031: class SelectNode extends AbstractGroupNode implements TypeNode {
032:
033: AbstractSimpleTypeNode typeNode;
034:
035: void prune() {
036: super .prune();
037: Iterator it = components.iterator();
038:
039: if (it.hasNext()) {
040: Node typeNode = (Node) it.next();
041:
042: if (typeNode instanceof ByteTypeNode
043: || typeNode instanceof IntTypeNode) {
044: this .typeNode = (AbstractSimpleTypeNode) typeNode;
045: it.remove();
046: } else {
047: error("Select must be based on 'int' or 'byte'");
048: }
049: } else {
050: error("empty");
051: }
052: }
053:
054: void constrain(Context ctx) {
055: super .constrain(ctx);
056: if (components.size() < 2) {
057: error("Select must have at least two options");
058: }
059: }
060:
061: void constrainComponent(Context ctx, Node node) {
062: node.constrain(ctx);
063: if (!(node instanceof AltNode)) {
064: error("Select must consist of selector followed by Alt items");
065: }
066: }
067:
068: void document(PrintWriter writer) {
069: typeNode.document(writer);
070: super .document(writer);
071: }
072:
073: String docType() {
074: // should never call this
075: error("Internal - called SelectNode.docType()");
076: return null;
077: }
078:
079: String commonBaseClass() {
080: return name() + "Common";
081: }
082:
083: private String commonVar() {
084: return " a" + commonBaseClass();
085: }
086:
087: void genJavaClassSpecifics(PrintWriter writer, int depth) {
088: indent(writer, depth);
089: writer.println("abstract static class " + commonBaseClass()
090: + " {");
091: if (context.isWritingCommand()) {
092: indent(writer, depth + 1);
093: writer.println("abstract void write(PacketStream ps);");
094: } else {
095: indent(writer, depth + 1);
096: writer.println("abstract " + typeNode.javaParam() + "();");
097: }
098: indent(writer, depth);
099: writer.println("}");
100: typeNode.genJavaDeclaration(writer, depth);
101: indent(writer, depth);
102: writer.println(commonBaseClass() + commonVar() + ";");
103: super .genJavaClassSpecifics(writer, depth);
104: }
105:
106: void genJavaClassBodyComponents(PrintWriter writer, int depth) {
107: // don't naively include alt components
108: }
109:
110: void genJavaWritingClassBody(PrintWriter writer, int depth,
111: String className) {
112: writer.println();
113: indent(writer, depth);
114: writer.print(className + "(" + typeNode.javaParam() + ", ");
115: writer.print(commonBaseClass() + commonVar());
116: writer.println(") {");
117: indent(writer, depth + 1);
118: writer.println("this." + typeNode.name() + " = "
119: + typeNode.name() + ";");
120: indent(writer, depth + 1);
121: writer
122: .println("this." + commonVar() + " =" + commonVar()
123: + ";");
124: indent(writer, depth);
125: writer.println("}");
126: }
127:
128: void genJavaWrites(PrintWriter writer, int depth) {
129: typeNode.genJavaWrite(writer, depth, typeNode.name());
130: indent(writer, depth);
131: writer.println(commonVar() + ".write(ps);");
132: }
133:
134: void genJavaReads(PrintWriter writer, int depth) {
135: typeNode.genJavaRead(writer, depth, typeNode.name());
136: indent(writer, depth);
137: writer.println("switch (" + typeNode.name() + ") {");
138: for (Iterator it = components.iterator(); it.hasNext();) {
139: AltNode alt = (AltNode) it.next();
140: alt.genJavaReadsSelectCase(writer, depth + 1, commonVar());
141: }
142: indent(writer, depth);
143: writer.println("}");
144: }
145:
146: public void genJavaDeclaration(PrintWriter writer, int depth) {
147: typeNode.genJavaDeclaration(writer, depth);
148: super .genJavaDeclaration(writer, depth);
149: }
150:
151: public String javaParam() {
152: return typeNode.javaParam() + ", " + name() + " a" + name();
153: }
154: }
|