EnumGen.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25/* 26 * COMPONENT_NAME: idl.toJava 27 * 28 * ORIGINS: 27 29 * 30 * Licensed Materials - Property of IBM 31 * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999 32 * RMI-IIOP v1.0 33 * 34 */ 35 36package com.sun.tools.corba.se.idl.toJavaPortable; 37 38// NOTES: 39// -D61056 <klr> Use Util.helperName 40 41import java.io.File; 42import java.io.PrintWriter; 43import java.util.Enumeration; 44import java.util.Hashtable; 45import java.util.Vector; 46 47import com.sun.tools.corba.se.idl.GenFileStream; 48import com.sun.tools.corba.se.idl.EnumEntry; 49import com.sun.tools.corba.se.idl.SymtabEntry; 50 51/** 52 * 53 **/ 54public class EnumGen implements com.sun.tools.corba.se.idl.EnumGen, JavaGenerator 55{ 56 /** 57 * Public zero-argument constructor. 58 **/ 59 public EnumGen () 60 { 61 } // ctor 62 63 /** 64 * Generate the Java code for an IDL enumeration. 65 **/ 66 public void generate (Hashtable symbolTable, EnumEntry e, PrintWriter s) 67 { 68 this.symbolTable = symbolTable; 69 this.e = e; 70 init (); 71 72 openStream (); 73 if (stream == null) return; 74 generateHolder (); 75 generateHelper (); 76 writeHeading (); 77 writeBody (); 78 writeClosing (); 79 closeStream (); 80 } // generate 81 82 /** 83 * Initialize members unique to this generator. 84 **/ 85 protected void init () 86 { 87 className = e.name (); 88 fullClassName = Util.javaName (e); 89 } 90 91 /** 92 * Open the print stream to which to write the enumeration class. 93 **/ 94 protected void openStream () 95 { 96 stream = Util.stream (e, ".java"); 97 } 98 99 /** 100 * Generate the holder class for this enumeration. 101 **/ 102 protected void generateHolder () 103 { 104 ((Factories)Compile.compiler.factories ()).holder ().generate (symbolTable, e); 105 } 106 107 /** 108 * Generate the helper class for this enumeration. 109 **/ 110 protected void generateHelper () 111 { 112 ((Factories)Compile.compiler.factories ()).helper ().generate (symbolTable, e); 113 } 114 115 /** 116 * Write the heading of the enumeration class, including the package, 117 * imports, class statement, and open curly. 118 **/ 119 protected void writeHeading () 120 { 121 Util.writePackage (stream, e); 122 Util.writeProlog (stream, ((GenFileStream)stream).name ()); 123 if (e.comment () != null) 124 e.comment ().generate ("", stream); 125 stream.println ("public class " + className + " implements org.omg.CORBA.portable.IDLEntity"); 126 stream.println ("{"); 127 } 128 129 /** 130 * Write the members of enumeration class. 131 **/ 132 protected void writeBody () 133 { 134 stream.println (" private int __value;"); 135 stream.println (" private static int __size = " + (e.elements ().size ()) + ';'); 136 stream.println (" private static " + fullClassName + "[] __array = new " + fullClassName + " [__size];"); 137 stream.println (); 138 for (int i = 0; i < e.elements ().size (); ++i) 139 { 140 String label = (String)e.elements ().elementAt (i); 141 stream.println (" public static final int _" + label + " = " + i + ';'); 142 stream.println (" public static final " + fullClassName + ' ' + label + " = new " + fullClassName + "(_" + label + ");"); 143 } 144 stream.println (); 145 writeValue (); 146 writeFromInt (); 147 writeCtors (); 148 } 149 150 /** 151 * Write the value method for the enumeration class. 152 **/ 153 protected void writeValue () 154 { 155 stream.println (" public int value ()"); 156 stream.println (" {"); 157 stream.println (" return __value;"); 158 stream.println (" }"); 159 stream.println (); 160 } // writeValue 161 162 /** 163 * Write the from_int method for the enumeration class. 164 **/ 165 protected void writeFromInt () 166 { 167 stream.println (" public static " + fullClassName + " from_int (int value)"); 168 stream.println (" {"); 169 stream.println (" if (value >= 0 && value < __size)"); 170 stream.println (" return __array[value];"); 171 stream.println (" else"); 172 stream.println (" throw new org.omg.CORBA.BAD_PARAM ();"); 173 stream.println (" }"); 174 stream.println (); 175 } 176 177 /** 178 * Write the protected constructor for the enumeration class. 179 **/ 180 protected void writeCtors () 181 { 182 stream.println (" protected " + className + " (int value)"); 183 stream.println (" {"); 184 stream.println (" __value = value;"); 185 stream.println (" __array[__value] = this;"); 186 stream.println (" }"); 187 } 188 189 /** 190 * Close the enumeration class. 191 **/ 192 protected void writeClosing () 193 { 194 stream.println ("} // class " + className); 195 } 196 197 /** 198 * Close the print stream, which writes the stream to file. 199 **/ 200 protected void closeStream () 201 { 202 stream.close (); 203 } 204 205 /////////////// 206 // From JavaGenerator 207 208 public int helperType (int index, String indent, TCOffsets tcoffsets, String name, SymtabEntry entry, PrintWriter stream) 209 { 210 tcoffsets.set (entry); 211 EnumEntry enumEntry = (EnumEntry)entry; 212 StringBuffer emit = new StringBuffer ("new String[] { "); 213 Enumeration e = enumEntry.elements ().elements (); 214 boolean firstTime = true; 215 while (e.hasMoreElements ()) 216 { 217 if (firstTime) 218 firstTime = false; 219 else 220 emit.append (", "); 221 emit.append ('"' + Util.stripLeadingUnderscores ((String)e.nextElement ()) + '"'); 222 } 223 emit.append ("} "); 224 stream.println (indent + name + " = org.omg.CORBA.ORB.init ().create_enum_tc (" 225 + Util.helperName (enumEntry, true) + ".id (), \"" // <54697> // <d61056> 226// + "_id, \"" <54697> 227 + Util.stripLeadingUnderscores (entry.name ()) + "\", " 228 + new String (emit) + ");"); 229 return index + 1; 230 231 } // helperType 232 233 public int type (int index, String indent, TCOffsets tcoffsets, String name, SymtabEntry entry, PrintWriter stream) { 234 stream.println (indent + name + " = " + Util.helperName (entry, true) + ".type ();"); // <d61056> 235 return index; 236 } // type 237 238 public void helperRead (String entryName, SymtabEntry entry, PrintWriter stream) 239 { 240 stream.println (" return " + Util.javaQualifiedName (entry) + ".from_int (istream.read_long ());"); 241 } // helperRead 242 243 public void helperWrite (SymtabEntry entry, PrintWriter stream) 244 { 245 stream.println (" ostream.write_long (value.value ());"); 246 } // helperWrite 247 248 public int read (int index, String indent, String name, SymtabEntry entry, PrintWriter stream) 249 { 250 stream.println (indent + name + " = " + Util.javaQualifiedName (entry) + ".from_int (istream.read_long ());"); 251 return index; 252 } // read 253 254 public int write (int index, String indent, String name, SymtabEntry entry, PrintWriter stream) 255 { 256 stream.println (indent + "ostream.write_long (" + name + ".value ());"); 257 return index; 258 } // write 259 260 // From JavaGenerator 261 /////////////// 262 263 protected Hashtable symbolTable = null; 264 protected EnumEntry e = null; 265 protected PrintWriter stream = null; 266 267 // Member data unique to this generator 268 String className = null; 269 String fullClassName = null; 270} // class EnumGen 271 272 273/*============================================================================ 274 DATE<AUTHOR> ACTION 275 ---------------------------------------------------------------------------- 276 31jul1997<daz> Modified to write comment immediately preceding class defining 277 enumeration declaration. 278 12dec1998<klr> D55971 - omg 98-11-03 Java 2.4 RTF - make subclassable 279 ===========================================================================*/ 280