src/share/classes/com/sun/tools/corba/se/idl/toJavaPortable/ValueGen24.java

Mon, 04 May 2009 18:40:45 -0700

author
tbell
date
Mon, 04 May 2009 18:40:45 -0700
changeset 72
e149090eb21a
parent 1
55540e827aef
child 158
91006f157c46
permissions
-rw-r--r--

6529590: flaw in com.sun.corba.se.impl.presentation.rmi.IDLNameTranslatorImpl
Reviewed-by: darcy

duke@1 1 /*
duke@1 2 * Copyright 1999-2004 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25 /*
duke@1 26 * COMPONENT_NAME: idl.toJava
duke@1 27 *
duke@1 28 * ORIGINS: 27
duke@1 29 *
duke@1 30 * Licensed Materials - Property of IBM
duke@1 31 * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
duke@1 32 * RMI-IIOP v1.0
duke@1 33 *
duke@1 34 */
duke@1 35
duke@1 36 package com.sun.tools.corba.se.idl.toJavaPortable;
duke@1 37
duke@1 38 // NOTES:
duke@1 39 // -D62023 <klr> Update for Java 2.4 RTF
duke@1 40 // -D62794.1 <klr> Don't include operations inherited from abstract valuetypes
duke@1 41 // -D62794.1 <scn> Don't include operations inherited from supported interfaces
duke@1 42
duke@1 43 import java.io.File;
duke@1 44 import java.io.PrintWriter;
duke@1 45 import java.util.Hashtable;
duke@1 46 import java.util.Enumeration;
duke@1 47 import java.util.Vector;
duke@1 48
duke@1 49 import com.sun.tools.corba.se.idl.GenFileStream;
duke@1 50 import com.sun.tools.corba.se.idl.InterfaceEntry;
duke@1 51 import com.sun.tools.corba.se.idl.SymtabEntry;
duke@1 52 import com.sun.tools.corba.se.idl.TypedefEntry;
duke@1 53 import com.sun.tools.corba.se.idl.ValueEntry;
duke@1 54 import com.sun.tools.corba.se.idl.ValueBoxEntry;
duke@1 55 import com.sun.tools.corba.se.idl.InterfaceState;
duke@1 56 import com.sun.tools.corba.se.idl.MethodEntry;
duke@1 57 import com.sun.tools.corba.se.idl.AttributeEntry;
duke@1 58 import com.sun.tools.corba.se.idl.PrimitiveEntry;
duke@1 59 import com.sun.tools.corba.se.idl.SequenceEntry;
duke@1 60 import com.sun.tools.corba.se.idl.StringEntry;
duke@1 61 import com.sun.tools.corba.se.idl.StructEntry;
duke@1 62
duke@1 63 /**
duke@1 64 *
duke@1 65 **/
duke@1 66 public class ValueGen24 extends ValueGen
duke@1 67 {
duke@1 68 /**
duke@1 69 * Public zero-argument constructor.
duke@1 70 **/
duke@1 71 public ValueGen24 ()
duke@1 72 {
duke@1 73 } // ctor
duke@1 74
duke@1 75 /**
duke@1 76 * <d62023> - delete constructor; helper is abstract
duke@1 77 **/
duke@1 78 protected void writeConstructor ()
duke@1 79 {
duke@1 80 } // writeConstructor
duke@1 81
duke@1 82 /**
duke@1 83 * <d62023> - delete write_value from non-boxed helpers
duke@1 84 * - delete _write from non-boxed helpers
duke@1 85 **/
duke@1 86 public void helperWrite (SymtabEntry entry, PrintWriter stream)
duke@1 87 {
duke@1 88 // REVISIT: Abstract/Custom??
duke@1 89 // per Simon mail 5/17/99
duke@1 90 stream.println (" ((org.omg.CORBA_2_3.portable.OutputStream) ostream).write_value (value, id ());");
duke@1 91 } // helperWrite
duke@1 92
duke@1 93 /**
duke@1 94 * <d62023>
duke@1 95 **/
duke@1 96 public void helperRead (String entryName, SymtabEntry entry, PrintWriter stream)
duke@1 97 {
duke@1 98 // REVISIT: Abstract/Custom??
duke@1 99 // per Simon mail 5/17/99
duke@1 100 stream.println (" return (" + entryName + ")((org.omg.CORBA_2_3.portable.InputStream) istream).read_value (id ());");
duke@1 101 } // helperRead
duke@1 102
duke@1 103 /**
duke@1 104 * <d62023> - suppress initializers from mapped value; now generated in
duke@1 105 * the Helper class and Factory class
duke@1 106 **/
duke@1 107 protected void writeInitializers ()
duke@1 108 {
duke@1 109 // override to do nothing
duke@1 110 } // writeInitializers
duke@1 111
duke@1 112 /**
duke@1 113 * <d62023> - Goes in mapped class, not Helper
duke@1 114 **/
duke@1 115 protected void writeTruncatable () // <d60929>
duke@1 116 {
duke@1 117 if (!v.isAbstract ()) {
duke@1 118 stream.println (" private static String[] _truncatable_ids = {");
duke@1 119 stream.print (" " + Util.helperName(v, true) + ".id ()");
duke@1 120
duke@1 121 // Any safe ValueEntry must have a concete value parent.
duke@1 122 // The topmost parent cannot be safe since it doesn't have
duke@1 123 // a concrete parent.
duke@1 124 ValueEntry child = v;
duke@1 125 while (child.isSafe ())
duke@1 126 {
duke@1 127 stream.println(",");
duke@1 128 ValueEntry parent = (ValueEntry)child.derivedFrom ().elementAt (0);
duke@1 129 stream.print(" \"" + Util.stripLeadingUnderscoresFromID (parent.repositoryID ().ID ()) + "\"");
duke@1 130 child = parent;
duke@1 131 }
duke@1 132 stream.println();
duke@1 133 stream.println(" };");
duke@1 134 stream.println();
duke@1 135 stream.println (" public String[] _truncatable_ids() {");
duke@1 136 stream.println (" return _truncatable_ids;");
duke@1 137 stream.println (" }");
duke@1 138 stream.println ();
duke@1 139 }
duke@1 140 } // writeTruncatable
duke@1 141
duke@1 142 class ImplStreamWriter {
duke@1 143 private boolean isImplementsWritten = false ;
duke@1 144
duke@1 145 public void writeClassName( String name )
duke@1 146 {
duke@1 147 if (!isImplementsWritten) {
duke@1 148 stream.print( " implements " ) ;
duke@1 149 isImplementsWritten = true ;
duke@1 150 } else
duke@1 151 stream.print( ", " ) ;
duke@1 152
duke@1 153 stream.print( name ) ;
duke@1 154 }
duke@1 155 }
duke@1 156
duke@1 157 /**
duke@1 158 * <d62023> CustomMarshal -> CustomValue for custom valuetypes
duke@1 159 * mapped class is abstract
duke@1 160 **/
duke@1 161 protected void writeHeading ()
duke@1 162 {
duke@1 163 ImplStreamWriter isw = new ImplStreamWriter() ;
duke@1 164
duke@1 165 Util.writePackage (stream, v);
duke@1 166 Util.writeProlog (stream, ((GenFileStream)stream).name ());
duke@1 167
duke@1 168 if (v.comment () != null)
duke@1 169 v.comment ().generate ("", stream);
duke@1 170
duke@1 171 if (v.isAbstract ()) {
duke@1 172 writeAbstract ();
duke@1 173 return;
duke@1 174 } else
duke@1 175 stream.print ("public abstract class " + v.name ());
duke@1 176
duke@1 177 // There should always be at least one parent: ValueBase
duke@1 178 SymtabEntry parent = (SymtabEntry) v.derivedFrom ().elementAt (0);
duke@1 179
duke@1 180 // If parent is ValueBase, it's mapped to java.io.Serializable
duke@1 181 String parentName = Util.javaName (parent);
duke@1 182 boolean cv = false; // true if we've already implemented CustomValue
duke@1 183
duke@1 184 if (parentName.equals ("java.io.Serializable")) {
duke@1 185 if (((ValueEntry)v).isCustom ()) {
duke@1 186 isw.writeClassName( "org.omg.CORBA.portable.CustomValue" ) ;
duke@1 187 cv = true;
duke@1 188 } else
duke@1 189 isw.writeClassName( "org.omg.CORBA.portable.StreamableValue" ) ;
duke@1 190 } else if ( !((ValueEntry)parent).isAbstract ())
duke@1 191 stream.print (" extends " + parentName);
duke@1 192
duke@1 193 // if inheriting from abstract values
duke@1 194 for (int i = 0; i < v.derivedFrom ().size (); i++) {
duke@1 195 parent = (SymtabEntry) v.derivedFrom ().elementAt (i);
duke@1 196 if ( ((ValueEntry)parent).isAbstract ()) {
duke@1 197 isw.writeClassName( Util.javaName(parent) ) ;
duke@1 198 }
duke@1 199 }
duke@1 200
duke@1 201 // Write out the supported interfaces
duke@1 202 Enumeration enumeration = v.supports().elements();
duke@1 203 while (enumeration.hasMoreElements()) {
duke@1 204 InterfaceEntry ie = (InterfaceEntry)(enumeration.nextElement()) ;
duke@1 205 String cname = Util.javaName(ie) ;
duke@1 206 if (!ie.isAbstract())
duke@1 207 cname += "Operations" ;
duke@1 208 isw.writeClassName( cname ) ;
duke@1 209 }
duke@1 210
duke@1 211 // for when a custom valuetype inherits from a non-custom valuetype
duke@1 212 if ( v.isCustom () && !cv)
duke@1 213 isw.writeClassName( "org.omg.CORBA.portable.CustomValue" ) ;
duke@1 214
duke@1 215 stream.println ();
duke@1 216 stream.println ("{");
duke@1 217 } // writeHeading
duke@1 218
duke@1 219 /**
duke@1 220 * <d62023> - private state maps to protected, not default
duke@1 221 **/
duke@1 222 protected void writeMembers ()
duke@1 223 {
duke@1 224 // if the value type contains no data members, a null return is expected
duke@1 225 if (v.state () == null)
duke@1 226 return;
duke@1 227
duke@1 228 for (int i = 0; i < v.state ().size (); i ++)
duke@1 229 {
duke@1 230 InterfaceState member = (InterfaceState) v.state ().elementAt (i);
duke@1 231 SymtabEntry entry = (SymtabEntry) member.entry;
duke@1 232 Util.fillInfo (entry);
duke@1 233
duke@1 234 if (entry.comment () != null)
duke@1 235 entry.comment ().generate (" ", stream);
duke@1 236
duke@1 237 String modifier = " ";
duke@1 238 if (member.modifier == InterfaceState.Public)
duke@1 239 modifier = " public ";
duke@1 240 else
duke@1 241 modifier = " protected ";
duke@1 242 Util.writeInitializer (modifier, entry.name (), "", entry, stream);
duke@1 243 }
duke@1 244 stream.println();
duke@1 245 } // writeMembers
duke@1 246
duke@1 247 /**
duke@1 248 * <d62023> Methods need to be abstract
duke@1 249 * writeStreamable
duke@1 250 **/
duke@1 251 protected void writeMethods ()
duke@1 252 {
duke@1 253 // contained vector contains methods, attributes, const, enums, exceptions,
duke@1 254 // structs, unions, or typedefs that are declared inside the value object.
duke@1 255 // State members of the nested types are also included in this vector.
duke@1 256 // Thus, if the declaration of a constructed type is nested in the decl.
duke@1 257 // of a state member, e.g struct x {boolean b;} memberx;
duke@1 258 // the generation of the nested type must be handled here.
duke@1 259 Enumeration e = v.contained ().elements ();
duke@1 260 while (e.hasMoreElements ())
duke@1 261 {
duke@1 262 SymtabEntry contained = (SymtabEntry)e.nextElement ();
duke@1 263 if (contained instanceof AttributeEntry)
duke@1 264 {
duke@1 265 AttributeEntry element = (AttributeEntry)contained;
duke@1 266 ((AttributeGen24)element.generator ()).abstractMethod (symbolTable, element, stream);
duke@1 267 }
duke@1 268 else if (contained instanceof MethodEntry)
duke@1 269 {
duke@1 270 MethodEntry element = (MethodEntry)contained;
duke@1 271 ((MethodGen24)element.generator ()).abstractMethod (symbolTable, element, stream);
duke@1 272 }
duke@1 273 else
duke@1 274 {
duke@1 275 // Generate the type referenced by the typedef.
duke@1 276 if (contained instanceof TypedefEntry)
duke@1 277 contained.type ().generate (symbolTable, stream);
duke@1 278
duke@1 279 // Note that we also need to generate the typedef itself if
duke@1 280 // contained is a typedef.
duke@1 281 contained.generate (symbolTable, stream);
duke@1 282 }
duke@1 283 }
duke@1 284
duke@1 285 // Abstract values are mapped to interfaces. There is no need to generate
duke@1 286 // the bindings for inheriting methods in case of inheritance from other
duke@1 287 // abstract values or supporting interface
duke@1 288 if (v.isAbstract ())
duke@1 289 return;
duke@1 290
duke@1 291 // Non-abstract, Non-Custom valuetypes support the Streamable interface
duke@1 292 if (!(v.isCustom () || v.isAbstract ()))
duke@1 293 writeStreamableMethods ();
duke@1 294 } // writeMethods
duke@1 295
duke@1 296 /**
duke@1 297 * <d62023> Call super._read()
duke@1 298 **/
duke@1 299 public int read (int index, String indent, String name, SymtabEntry entry, PrintWriter stream)
duke@1 300 {
duke@1 301 // First do the state members from concrete parent hierarchy
duke@1 302 Vector vParents = ((ValueEntry) entry).derivedFrom ();
duke@1 303 if (vParents != null && vParents.size() != 0)
duke@1 304 {
duke@1 305 ValueEntry parent = (ValueEntry) vParents.elementAt (0);
duke@1 306 if (parent == null)
duke@1 307 return index;
duke@1 308
duke@1 309 // call super._read if non-abstract value parent
duke@1 310 if ((!parent.isAbstract ()) && (! Util.javaQualifiedName(parent).equals ("java.io.Serializable"))) // <d60929>
duke@1 311 stream.println(indent + "super._read (istream);");
duke@1 312 }
duke@1 313
duke@1 314 Vector vMembers = ((ValueEntry) entry).state ();
duke@1 315 int noOfMembers = vMembers == null ? 0 : vMembers.size ();
duke@1 316
duke@1 317 for (int k = 0; k < noOfMembers; k++)
duke@1 318 {
duke@1 319 TypedefEntry member = (TypedefEntry)((InterfaceState)vMembers.elementAt (k)).entry;
duke@1 320 String memberName = member.name ();
duke@1 321 SymtabEntry mType = member.type ();
duke@1 322
duke@1 323 if (mType instanceof PrimitiveEntry ||
duke@1 324 mType instanceof TypedefEntry ||
duke@1 325 mType instanceof SequenceEntry ||
duke@1 326 mType instanceof StringEntry ||
duke@1 327 !member.arrayInfo ().isEmpty ())
duke@1 328 index = ((JavaGenerator)member.generator ()).read (index, indent, name + '.' + memberName, member, stream);
duke@1 329 else
duke@1 330 stream.println (indent + name + '.' + memberName + " = " +
duke@1 331 Util.helperName (mType, true) + ".read (istream);"); // <d61056>
duke@1 332 }
duke@1 333
duke@1 334 return index;
duke@1 335 } // read
duke@1 336
duke@1 337 /**
duke@1 338 * <d62023> Call super._write()
duke@1 339 **/
duke@1 340 public int write (int index, String indent, String name, SymtabEntry entry, PrintWriter stream)
duke@1 341 {
duke@1 342 // First do the state members from concrete parent hierarchy
duke@1 343 Vector vParents = ((ValueEntry)entry).derivedFrom ();
duke@1 344 if (vParents != null && vParents.size () != 0)
duke@1 345 {
duke@1 346 ValueEntry parent = (ValueEntry)vParents.elementAt (0);
duke@1 347 if (parent == null)
duke@1 348 return index;
duke@1 349 // call super._read if non-abstract value parent
duke@1 350 if ((!parent.isAbstract ()) && (! Util.javaQualifiedName(parent).equals ("java.io.Serializable"))) // <d60929>
duke@1 351 stream.println(indent + "super._write (ostream);");
duke@1 352 }
duke@1 353
duke@1 354 Vector vMembers = ((ValueEntry) entry ).state ();
duke@1 355 int noOfMembers = vMembers == null ? 0 : vMembers.size ();
duke@1 356 for (int k = 0; k < noOfMembers; k++)
duke@1 357 {
duke@1 358 TypedefEntry member = (TypedefEntry)((InterfaceState)vMembers.elementAt (k)).entry;
duke@1 359 String memberName = member.name ();
duke@1 360 SymtabEntry mType = member.type ();
duke@1 361
duke@1 362 if (mType instanceof PrimitiveEntry ||
duke@1 363 mType instanceof TypedefEntry ||
duke@1 364 mType instanceof SequenceEntry ||
duke@1 365 mType instanceof StringEntry ||
duke@1 366 !member.arrayInfo ().isEmpty ())
duke@1 367 index = ((JavaGenerator)member.generator ()).write (index, indent, name + '.' + memberName, member, stream);
duke@1 368 else
duke@1 369 stream.println (indent + Util.helperName (mType, true) + // <d61056>
duke@1 370 ".write (ostream, " + name + '.' + memberName + ");");
duke@1 371 }
duke@1 372
duke@1 373 return index;
duke@1 374 } // write
duke@1 375
duke@1 376 /**
duke@1 377 * <62023> - generate factory interface and default factory
duke@1 378 **/
duke@1 379 public void generate (Hashtable symbolTable, ValueEntry v, PrintWriter str)
duke@1 380 {
duke@1 381 this.symbolTable = symbolTable;
duke@1 382 this.v = v;
duke@1 383 init ();
duke@1 384
duke@1 385 openStream ();
duke@1 386 if (stream == null)
duke@1 387 return;
duke@1 388 generateTie ();
duke@1 389 generateHelper ();
duke@1 390 generateHolder ();
duke@1 391 if (!v.isAbstract ()) {
duke@1 392 generateValueFactory ();
duke@1 393 generateDefaultFactory ();
duke@1 394 }
duke@1 395 writeHeading ();
duke@1 396 writeBody ();
duke@1 397 writeClosing ();
duke@1 398 closeStream ();
duke@1 399 } // generate
duke@1 400
duke@1 401 /**
duke@1 402 *
duke@1 403 **/
duke@1 404 protected void generateValueFactory ()
duke@1 405 {
duke@1 406 ((Factories)Compile.compiler.factories ()).valueFactory ().generate (symbolTable, v);
duke@1 407 } // generateValueFactory
duke@1 408
duke@1 409 /**
duke@1 410 *
duke@1 411 **/
duke@1 412 protected void generateDefaultFactory ()
duke@1 413 {
duke@1 414 ((Factories)Compile.compiler.factories ()).defaultFactory ().generate (symbolTable, v);
duke@1 415 } // generateDefaultFactory
duke@1 416 }

mercurial