001: /*
002: * Copyright 1999-2004 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: * COMPONENT_NAME: idl.toJava
027: *
028: * ORIGINS: 27
029: *
030: * Licensed Materials - Property of IBM
031: * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
032: * RMI-IIOP v1.0
033: *
034: * @(#)ValueGen24.java 1.21 07/05/05
035: */
036:
037: package com.sun.tools.corba.se.idl.toJavaPortable;
038:
039: // NOTES:
040: // -D62023 <klr> Update for Java 2.4 RTF
041: // -D62794.1 <klr> Don't include operations inherited from abstract valuetypes
042: // -D62794.1 <scn> Don't include operations inherited from supported interfaces
043:
044: import java.io.File;
045: import java.io.PrintWriter;
046: import java.util.Hashtable;
047: import java.util.Enumeration;
048: import java.util.Vector;
049:
050: import com.sun.tools.corba.se.idl.GenFileStream;
051: import com.sun.tools.corba.se.idl.InterfaceEntry;
052: import com.sun.tools.corba.se.idl.SymtabEntry;
053: import com.sun.tools.corba.se.idl.TypedefEntry;
054: import com.sun.tools.corba.se.idl.ValueEntry;
055: import com.sun.tools.corba.se.idl.ValueBoxEntry;
056: import com.sun.tools.corba.se.idl.InterfaceState;
057: import com.sun.tools.corba.se.idl.MethodEntry;
058: import com.sun.tools.corba.se.idl.AttributeEntry;
059: import com.sun.tools.corba.se.idl.PrimitiveEntry;
060: import com.sun.tools.corba.se.idl.SequenceEntry;
061: import com.sun.tools.corba.se.idl.StringEntry;
062: import com.sun.tools.corba.se.idl.StructEntry;
063:
064: /**
065: *
066: **/
067: public class ValueGen24 extends ValueGen {
068: /**
069: * Public zero-argument constructor.
070: **/
071: public ValueGen24() {
072: } // ctor
073:
074: /**
075: * <d62023> - delete constructor; helper is abstract
076: **/
077: protected void writeConstructor() {
078: } // writeConstructor
079:
080: /**
081: * <d62023> - delete write_value from non-boxed helpers
082: * - delete _write from non-boxed helpers
083: **/
084: public void helperWrite(SymtabEntry entry, PrintWriter stream) {
085: // REVISIT: Abstract/Custom??
086: // per Simon mail 5/17/99
087: stream
088: .println(" ((org.omg.CORBA_2_3.portable.OutputStream) ostream).write_value (value, id ());");
089: } // helperWrite
090:
091: /**
092: * <d62023>
093: **/
094: public void helperRead(String entryName, SymtabEntry entry,
095: PrintWriter stream) {
096: // REVISIT: Abstract/Custom??
097: // per Simon mail 5/17/99
098: stream
099: .println(" return ("
100: + entryName
101: + ")((org.omg.CORBA_2_3.portable.InputStream) istream).read_value (id ());");
102: } // helperRead
103:
104: /**
105: * <d62023> - suppress initializers from mapped value; now generated in
106: * the Helper class and Factory class
107: **/
108: protected void writeInitializers() {
109: // override to do nothing
110: } // writeInitializers
111:
112: /**
113: * <d62023> - Goes in mapped class, not Helper
114: **/
115: protected void writeTruncatable() // <d60929>
116: {
117: if (!v.isAbstract()) {
118: stream
119: .println(" private static String[] _truncatable_ids = {");
120: stream.print(" " + Util.helperName(v, true) + ".id ()");
121:
122: // Any safe ValueEntry must have a concete value parent.
123: // The topmost parent cannot be safe since it doesn't have
124: // a concrete parent.
125: ValueEntry child = v;
126: while (child.isSafe()) {
127: stream.println(",");
128: ValueEntry parent = (ValueEntry) child.derivedFrom()
129: .elementAt(0);
130: stream.print(" \""
131: + Util.stripLeadingUnderscoresFromID(parent
132: .repositoryID().ID()) + "\"");
133: child = parent;
134: }
135: stream.println();
136: stream.println(" };");
137: stream.println();
138: stream.println(" public String[] _truncatable_ids() {");
139: stream.println(" return _truncatable_ids;");
140: stream.println(" }");
141: stream.println();
142: }
143: } // writeTruncatable
144:
145: class ImplStreamWriter {
146: private boolean isImplementsWritten = false;
147:
148: public void writeClassName(String name) {
149: if (!isImplementsWritten) {
150: stream.print(" implements ");
151: isImplementsWritten = true;
152: } else
153: stream.print(", ");
154:
155: stream.print(name);
156: }
157: }
158:
159: /**
160: * <d62023> CustomMarshal -> CustomValue for custom valuetypes
161: * mapped class is abstract
162: **/
163: protected void writeHeading() {
164: ImplStreamWriter isw = new ImplStreamWriter();
165:
166: Util.writePackage(stream, v);
167: Util.writeProlog(stream, ((GenFileStream) stream).name());
168:
169: if (v.comment() != null)
170: v.comment().generate("", stream);
171:
172: if (v.isAbstract()) {
173: writeAbstract();
174: return;
175: } else
176: stream.print("public abstract class " + v.name());
177:
178: // There should always be at least one parent: ValueBase
179: SymtabEntry parent = (SymtabEntry) v.derivedFrom().elementAt(0);
180:
181: // If parent is ValueBase, it's mapped to java.io.Serializable
182: String parentName = Util.javaName(parent);
183: boolean cv = false; // true if we've already implemented CustomValue
184:
185: if (parentName.equals("java.io.Serializable")) {
186: if (((ValueEntry) v).isCustom()) {
187: isw
188: .writeClassName("org.omg.CORBA.portable.CustomValue");
189: cv = true;
190: } else
191: isw
192: .writeClassName("org.omg.CORBA.portable.StreamableValue");
193: } else if (!((ValueEntry) parent).isAbstract())
194: stream.print(" extends " + parentName);
195:
196: // if inheriting from abstract values
197: for (int i = 0; i < v.derivedFrom().size(); i++) {
198: parent = (SymtabEntry) v.derivedFrom().elementAt(i);
199: if (((ValueEntry) parent).isAbstract()) {
200: isw.writeClassName(Util.javaName(parent));
201: }
202: }
203:
204: // Write out the supported interfaces
205: Enumeration enumeration = v.supports().elements();
206: while (enumeration.hasMoreElements()) {
207: InterfaceEntry ie = (InterfaceEntry) (enumeration
208: .nextElement());
209: String cname = Util.javaName(ie);
210: if (!ie.isAbstract())
211: cname += "Operations";
212: isw.writeClassName(cname);
213: }
214:
215: // for when a custom valuetype inherits from a non-custom valuetype
216: if (v.isCustom() && !cv)
217: isw.writeClassName("org.omg.CORBA.portable.CustomValue");
218:
219: stream.println();
220: stream.println("{");
221: } // writeHeading
222:
223: /**
224: * <d62023> - private state maps to protected, not default
225: **/
226: protected void writeMembers() {
227: // if the value type contains no data members, a null return is expected
228: if (v.state() == null)
229: return;
230:
231: for (int i = 0; i < v.state().size(); i++) {
232: InterfaceState member = (InterfaceState) v.state()
233: .elementAt(i);
234: SymtabEntry entry = (SymtabEntry) member.entry;
235: Util.fillInfo(entry);
236:
237: if (entry.comment() != null)
238: entry.comment().generate(" ", stream);
239:
240: String modifier = " ";
241: if (member.modifier == InterfaceState.Public)
242: modifier = " public ";
243: else
244: modifier = " protected ";
245: Util.writeInitializer(modifier, entry.name(), "", entry,
246: stream);
247: }
248: stream.println();
249: } // writeMembers
250:
251: /**
252: * <d62023> Methods need to be abstract
253: * writeStreamable
254: **/
255: protected void writeMethods() {
256: // contained vector contains methods, attributes, const, enums, exceptions,
257: // structs, unions, or typedefs that are declared inside the value object.
258: // State members of the nested types are also included in this vector.
259: // Thus, if the declaration of a constructed type is nested in the decl.
260: // of a state member, e.g struct x {boolean b;} memberx;
261: // the generation of the nested type must be handled here.
262: Enumeration e = v.contained().elements();
263: while (e.hasMoreElements()) {
264: SymtabEntry contained = (SymtabEntry) e.nextElement();
265: if (contained instanceof AttributeEntry) {
266: AttributeEntry element = (AttributeEntry) contained;
267: ((AttributeGen24) element.generator()).abstractMethod(
268: symbolTable, element, stream);
269: } else if (contained instanceof MethodEntry) {
270: MethodEntry element = (MethodEntry) contained;
271: ((MethodGen24) element.generator()).abstractMethod(
272: symbolTable, element, stream);
273: } else {
274: // Generate the type referenced by the typedef.
275: if (contained instanceof TypedefEntry)
276: contained.type().generate(symbolTable, stream);
277:
278: // Note that we also need to generate the typedef itself if
279: // contained is a typedef.
280: contained.generate(symbolTable, stream);
281: }
282: }
283:
284: // Abstract values are mapped to interfaces. There is no need to generate
285: // the bindings for inheriting methods in case of inheritance from other
286: // abstract values or supporting interface
287: if (v.isAbstract())
288: return;
289:
290: // Non-abstract, Non-Custom valuetypes support the Streamable interface
291: if (!(v.isCustom() || v.isAbstract()))
292: writeStreamableMethods();
293: } // writeMethods
294:
295: /**
296: * <d62023> Call super._read()
297: **/
298: public int read(int index, String indent, String name,
299: SymtabEntry entry, PrintWriter stream) {
300: // First do the state members from concrete parent hierarchy
301: Vector vParents = ((ValueEntry) entry).derivedFrom();
302: if (vParents != null && vParents.size() != 0) {
303: ValueEntry parent = (ValueEntry) vParents.elementAt(0);
304: if (parent == null)
305: return index;
306:
307: // call super._read if non-abstract value parent
308: if ((!parent.isAbstract())
309: && (!Util.javaQualifiedName(parent).equals(
310: "java.io.Serializable"))) // <d60929>
311: stream.println(indent + "super._read (istream);");
312: }
313:
314: Vector vMembers = ((ValueEntry) entry).state();
315: int noOfMembers = vMembers == null ? 0 : vMembers.size();
316:
317: for (int k = 0; k < noOfMembers; k++) {
318: TypedefEntry member = (TypedefEntry) ((InterfaceState) vMembers
319: .elementAt(k)).entry;
320: String memberName = member.name();
321: SymtabEntry mType = member.type();
322:
323: if (mType instanceof PrimitiveEntry
324: || mType instanceof TypedefEntry
325: || mType instanceof SequenceEntry
326: || mType instanceof StringEntry
327: || !member.arrayInfo().isEmpty())
328: index = ((JavaGenerator) member.generator()).read(
329: index, indent, name + '.' + memberName, member,
330: stream);
331: else
332: stream.println(indent + name + '.' + memberName + " = "
333: + Util.helperName(mType, true)
334: + ".read (istream);"); // <d61056>
335: }
336:
337: return index;
338: } // read
339:
340: /**
341: * <d62023> Call super._write()
342: **/
343: public int write(int index, String indent, String name,
344: SymtabEntry entry, PrintWriter stream) {
345: // First do the state members from concrete parent hierarchy
346: Vector vParents = ((ValueEntry) entry).derivedFrom();
347: if (vParents != null && vParents.size() != 0) {
348: ValueEntry parent = (ValueEntry) vParents.elementAt(0);
349: if (parent == null)
350: return index;
351: // call super._read if non-abstract value parent
352: if ((!parent.isAbstract())
353: && (!Util.javaQualifiedName(parent).equals(
354: "java.io.Serializable"))) // <d60929>
355: stream.println(indent + "super._write (ostream);");
356: }
357:
358: Vector vMembers = ((ValueEntry) entry).state();
359: int noOfMembers = vMembers == null ? 0 : vMembers.size();
360: for (int k = 0; k < noOfMembers; k++) {
361: TypedefEntry member = (TypedefEntry) ((InterfaceState) vMembers
362: .elementAt(k)).entry;
363: String memberName = member.name();
364: SymtabEntry mType = member.type();
365:
366: if (mType instanceof PrimitiveEntry
367: || mType instanceof TypedefEntry
368: || mType instanceof SequenceEntry
369: || mType instanceof StringEntry
370: || !member.arrayInfo().isEmpty())
371: index = ((JavaGenerator) member.generator()).write(
372: index, indent, name + '.' + memberName, member,
373: stream);
374: else
375: stream.println(indent + Util.helperName(mType, true)
376: + // <d61056>
377: ".write (ostream, " + name + '.' + memberName
378: + ");");
379: }
380:
381: return index;
382: } // write
383:
384: /**
385: * <62023> - generate factory interface and default factory
386: **/
387: public void generate(Hashtable symbolTable, ValueEntry v,
388: PrintWriter str) {
389: this .symbolTable = symbolTable;
390: this .v = v;
391: init();
392:
393: openStream();
394: if (stream == null)
395: return;
396: generateTie();
397: generateHelper();
398: generateHolder();
399: if (!v.isAbstract()) {
400: generateValueFactory();
401: generateDefaultFactory();
402: }
403: writeHeading();
404: writeBody();
405: writeClosing();
406: closeStream();
407: } // generate
408:
409: /**
410: *
411: **/
412: protected void generateValueFactory() {
413: ((Factories) Compile.compiler.factories()).valueFactory()
414: .generate(symbolTable, v);
415: } // generateValueFactory
416:
417: /**
418: *
419: **/
420: protected void generateDefaultFactory() {
421: ((Factories) Compile.compiler.factories()).defaultFactory()
422: .generate(symbolTable, v);
423: } // generateDefaultFactory
424: }
|