ConstGen.java revision 608:7e06bf1dcb09
1251881Speter/* 2251881Speter * Copyright (c) 1999, 2000, Oracle and/or its affiliates. All rights reserved. 3251881Speter * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4251881Speter * 5251881Speter * This code is free software; you can redistribute it and/or modify it 6251881Speter * under the terms of the GNU General Public License version 2 only, as 7251881Speter * published by the Free Software Foundation. Oracle designates this 8251881Speter * particular file as subject to the "Classpath" exception as provided 9251881Speter * by Oracle in the LICENSE file that accompanied this code. 10251881Speter * 11251881Speter * This code is distributed in the hope that it will be useful, but WITHOUT 12251881Speter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13251881Speter * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14251881Speter * version 2 for more details (a copy is included in the LICENSE file that 15251881Speter * accompanied this code). 16251881Speter * 17251881Speter * You should have received a copy of the GNU General Public License version 18251881Speter * 2 along with this work; if not, write to the Free Software Foundation, 19251881Speter * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20251881Speter * 21251881Speter * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22251881Speter * or visit www.oracle.com if you need additional information or have any 23251881Speter * questions. 24251881Speter */ 25251881Speter/* 26251881Speter * COMPONENT_NAME: idl.toJava 27251881Speter * 28251881Speter * ORIGINS: 27 29251881Speter * 30251881Speter * Licensed Materials - Property of IBM 31251881Speter * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999 32251881Speter * RMI-IIOP v1.0 33251881Speter * 34251881Speter */ 35251881Speter 36251881Speterpackage com.sun.tools.corba.se.idl.toJavaPortable; 37251881Speter 38251881Speter// NOTES: 39251881Speter 40251881Speterimport java.io.File; 41251881Speterimport java.io.PrintWriter; 42251881Speterimport java.util.Hashtable; 43251881Speterimport java.util.Vector; 44251881Speter 45299742Sdimimport com.sun.tools.corba.se.idl.GenFileStream; 46251881Speterimport com.sun.tools.corba.se.idl.ConstEntry; 47251881Speterimport com.sun.tools.corba.se.idl.ModuleEntry; 48251881Speterimport com.sun.tools.corba.se.idl.PrimitiveEntry; 49251881Speterimport com.sun.tools.corba.se.idl.StringEntry; 50251881Speterimport com.sun.tools.corba.se.idl.SymtabEntry; 51251881Speterimport com.sun.tools.corba.se.idl.TypedefEntry; 52251881Speter 53251881Speter/** 54251881Speter * 55251881Speter **/ 56251881Speterpublic class ConstGen implements com.sun.tools.corba.se.idl.ConstGen 57251881Speter{ 58251881Speter /** 59251881Speter * Public zero-argument constructor. 60251881Speter **/ 61251881Speter public ConstGen () 62251881Speter { 63251881Speter } // ctor 64251881Speter 65251881Speter /** 66251881Speter * Generate Java code for an IDL constant. A constant is written to 67299742Sdim * a new class only when it is not a member of an interface; otherwise 68251881Speter * it written to the interface class in which it resides. 69251881Speter **/ 70251881Speter public void generate (Hashtable symbolTable, ConstEntry c, PrintWriter s) 71251881Speter { 72251881Speter this.symbolTable = symbolTable; 73251881Speter this.c = c; 74251881Speter this.stream = s; 75251881Speter init (); 76299742Sdim 77299742Sdim if (c.container () instanceof ModuleEntry) 78251881Speter generateConst (); 79251881Speter else if (stream != null) 80251881Speter writeConstExpr (); 81251881Speter } // generate 82251881Speter 83251881Speter /** 84251881Speter * Initialize members unique to this generator. 85251881Speter **/ 86251881Speter protected void init () 87251881Speter { 88251881Speter } // init 89251881Speter 90251881Speter /** 91251881Speter * Generate the class defining the constant. 92251881Speter **/ 93251881Speter protected void generateConst () 94251881Speter { 95251881Speter openStream (); 96251881Speter if (stream == null) 97251881Speter return; 98251881Speter writeHeading (); 99251881Speter writeBody (); 100251881Speter writeClosing (); 101251881Speter closeStream (); 102251881Speter } // generateConst 103251881Speter 104251881Speter /** 105251881Speter * Open a new print stream only if the constant is not a member 106251881Speter * of an interface. 107251881Speter **/ 108251881Speter protected void openStream () 109251881Speter { 110251881Speter stream = Util.stream (c, ".java"); 111251881Speter } // openStream 112251881Speter 113251881Speter /** 114251881Speter * Write the heading for the class defining the constant. 115251881Speter **/ 116251881Speter protected void writeHeading () 117251881Speter { 118251881Speter Util.writePackage (stream, c); 119251881Speter Util.writeProlog (stream, ((GenFileStream)stream).name ()); 120251881Speter stream.println ("public interface " + c.name ()); 121299742Sdim // should not be done according to the mapping 122299742Sdim // + " extends org.omg.CORBA.portable.IDLEntity"); 123299742Sdim stream.println ("{"); 124299742Sdim } // writeHeading 125251881Speter 126251881Speter /** 127251881Speter * Write the constant expression and any comment, if present. 128299742Sdim **/ 129251881Speter protected void writeBody () 130251881Speter { 131251881Speter writeConstExpr (); 132251881Speter } // writeBody 133251881Speter 134251881Speter /** 135251881Speter * Write the entire constant expression and any comment, if present. 136251881Speter **/ 137251881Speter protected void writeConstExpr () 138251881Speter { 139251881Speter if (c.comment () != null) 140251881Speter c.comment ().generate (" ", stream); 141251881Speter if (c.container () instanceof ModuleEntry) { 142251881Speter 143251881Speter stream.print (" public static final " + Util.javaName (c.type ()) + " value = "); 144251881Speter } else { 145251881Speter stream.print (" public static final " + Util.javaName (c.type ()) + ' ' + c.name () + " = "); 146251881Speter } 147251881Speter writeConstValue (c.type ()); 148251881Speter } // writeConstExpr 149251881Speter 150251881Speter /** 151251881Speter * Write the constant's value according to its type. 152251881Speter **/ 153251881Speter private void writeConstValue (SymtabEntry type) 154251881Speter { 155251881Speter if (type instanceof PrimitiveEntry) 156251881Speter stream.println ('(' + Util.javaName (type) + ")(" + Util.parseExpression (c.value ()) + ");"); 157251881Speter else if (type instanceof StringEntry) 158251881Speter stream.println (Util.parseExpression (c.value ()) + ';'); 159251881Speter else if (type instanceof TypedefEntry) 160299742Sdim { 161251881Speter while (type instanceof TypedefEntry) 162251881Speter type = type.type (); 163251881Speter writeConstValue (type); 164251881Speter } 165299742Sdim else 166251881Speter stream.println (Util.parseExpression (c.value ()) + ';'); 167251881Speter } // writeValue 168251881Speter 169251881Speter /** 170251881Speter * Generate any last words and close the class. 171299742Sdim **/ 172299742Sdim protected void writeClosing () 173299742Sdim { 174299742Sdim stream.println ("}"); 175299742Sdim } // writeClosing 176299742Sdim 177299742Sdim /** 178251881Speter * Close the print stream, causing the file to be written. 179299742Sdim **/ 180299742Sdim protected void closeStream () 181299742Sdim { 182299742Sdim stream.close (); 183299742Sdim } // closeStream 184299742Sdim 185299742Sdim protected java.util.Hashtable symbolTable = null; 186299742Sdim protected ConstEntry c = null; 187299742Sdim protected PrintWriter stream = null; 188299742Sdim} // class ConstGen 189299742Sdim 190299742Sdim 191299742Sdim/*======================================================================================= 192299742Sdim DATE-AUTHOR ACTION 193299742Sdim --------------------------------------------------------------------------------------- 194299742Sdim 11sep1997daz Return when print stream is null and container is NOT a module. Fixes 195251881Speter -keep option, which causes null print stream to be sent to ConstGen. 196251881Speter 31jul1997daz Write source comment immediately preceding constant declaration. 197251881Speter =======================================================================================*/ 198251881Speter