0001: /*
0002: * Copyright 1999-2002 Sun Microsystems, Inc. All Rights Reserved.
0003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0004: *
0005: * This code is free software; you can redistribute it and/or modify it
0006: * under the terms of the GNU General Public License version 2 only, as
0007: * published by the Free Software Foundation. Sun designates this
0008: * particular file as subject to the "Classpath" exception as provided
0009: * by Sun in the LICENSE file that accompanied this code.
0010: *
0011: * This code is distributed in the hope that it will be useful, but WITHOUT
0012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0014: * version 2 for more details (a copy is included in the LICENSE file that
0015: * accompanied this code).
0016: *
0017: * You should have received a copy of the GNU General Public License version
0018: * 2 along with this work; if not, write to the Free Software Foundation,
0019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0020: *
0021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0022: * CA 95054 USA or visit www.sun.com if you need additional information or
0023: * have any questions.
0024: */
0025: /*
0026: * COMPONENT_NAME: idl.toJava
0027: *
0028: * ORIGINS: 27
0029: *
0030: * Licensed Materials - Property of IBM
0031: * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
0032: * RMI-IIOP v1.0
0033: *
0034: * @(#)MethodGen.java 1.9 99/08/18
0035: */
0036:
0037: package com.sun.tools.corba.se.idl.toJavaPortable;
0038:
0039: // NOTES:
0040: // -After demarshalling an IOR, think about how to deal with the exceptions.
0041: // -catching Exception throws a string which should be in a properties file.
0042: // -30jul1997<daz> Modified to write comment immediately preceding method signature.
0043: // -07May1998<ktp> Modified to support RMI Portable Stub
0044: // -26Aug1998<klr> Modified to pass helper instance to read_Value.
0045: // -F46082.51<daz> Remove -stateful feature; javaStatefulName() obsolete.
0046: // -D56554 <klr> Port bounded string checks from toJava to toJavaPortable
0047: // -D58549 <klr> bounded string checks on in/inout parms throw BAD_PARAM
0048: // -D57112<daz> Valuetype initializers map to ctor, regardless of name, and
0049: // "void _init(...)" methods now mapped correctly.
0050: // -D59297 <klr> pass context parm when Remarshalling
0051: // -D59560 <klr> call read/write_Context
0052: // -D60929 <klr> Update for RTF2.4 changes
0053: // -D61056 <klr> Use Util.helperName
0054: // -D61650<daz> Remove '\n' from generated strings; use println()'s.
0055:
0056: import java.io.PrintWriter;
0057: import java.util.Enumeration;
0058: import java.util.Hashtable;
0059:
0060: import com.sun.tools.corba.se.idl.EnumEntry;
0061: import com.sun.tools.corba.se.idl.ExceptionEntry;
0062: import com.sun.tools.corba.se.idl.InterfaceEntry;
0063: import com.sun.tools.corba.se.idl.MethodEntry;
0064: import com.sun.tools.corba.se.idl.ParameterEntry;
0065: import com.sun.tools.corba.se.idl.PrimitiveEntry;
0066: import com.sun.tools.corba.se.idl.StringEntry;
0067: import com.sun.tools.corba.se.idl.SymtabEntry;
0068: import com.sun.tools.corba.se.idl.SequenceEntry;
0069: import com.sun.tools.corba.se.idl.ValueEntry;
0070: import com.sun.tools.corba.se.idl.ValueBoxEntry;
0071: import com.sun.tools.corba.se.idl.InterfaceState;
0072: import com.sun.tools.corba.se.idl.TypedefEntry;
0073: import com.sun.tools.corba.se.idl.AttributeEntry;
0074:
0075: import com.sun.tools.corba.se.idl.constExpr.Expression;
0076:
0077: /**
0078: *
0079: **/
0080: public class MethodGen implements com.sun.tools.corba.se.idl.MethodGen {
0081: private static final String ONE_INDENT = " ";
0082: private static final String TWO_INDENT = " ";
0083: private static final String THREE_INDENT = " ";
0084: private static final String FOUR_INDENT = " ";
0085: private static final String FIVE_INDENT = " ";
0086: // This is the length of _get_ and _set_
0087: private static final int ATTRIBUTE_METHOD_PREFIX_LENGTH = 5;
0088:
0089: /**
0090: * Public zero-argument constructor.
0091: **/
0092: public MethodGen() {
0093: } // ctor
0094:
0095: /**
0096: * Method generate() is not used in MethodGen. They are replaced by the
0097: * more granular interfaceMethod, stub, skeleton, dispatchSkeleton.
0098: **/
0099: public void generate(Hashtable symbolTable, MethodEntry m,
0100: PrintWriter stream) {
0101: } // generate
0102:
0103: /**
0104: *
0105: **/
0106: protected void interfaceMethod(Hashtable symbolTable,
0107: MethodEntry m, PrintWriter stream) {
0108: this .symbolTable = symbolTable;
0109: this .m = m;
0110: this .stream = stream;
0111: if (m.comment() != null)
0112: m.comment().generate("", stream);
0113: stream.print(" ");
0114: SymtabEntry container = (SymtabEntry) m.container();
0115: boolean isAbstract = false;
0116: boolean valueContainer = false;
0117: if (container instanceof ValueEntry) {
0118: isAbstract = ((ValueEntry) container).isAbstract();
0119: valueContainer = true;
0120: }
0121: if (valueContainer && !isAbstract)
0122: stream.print("public ");
0123: writeMethodSignature();
0124: if (valueContainer && !isAbstract) {
0125: stream.println();
0126: stream.println(" {");
0127: stream.println(" }");
0128: stream.println();
0129: } else
0130: stream.println(";");
0131: } // interfaceMethod
0132:
0133: /**
0134: *
0135: **/
0136: protected void stub(String className, boolean isAbstract,
0137: Hashtable symbolTable, MethodEntry m, PrintWriter stream,
0138: int index) {
0139: localOptimization = ((Arguments) Compile.compiler.arguments).LocalOptimization;
0140: this .isAbstract = isAbstract;
0141: this .symbolTable = symbolTable;
0142: this .m = m;
0143: this .stream = stream;
0144: this .methodIndex = index;
0145: if (m.comment() != null)
0146: m.comment().generate(" ", stream);
0147: stream.print(" public ");
0148: writeMethodSignature();
0149: stream.println();
0150: stream.println(" {");
0151: writeStubBody(className);
0152: stream.println(" } // " + m.name());
0153: stream.println();
0154: } // stub
0155:
0156: /**
0157: *
0158: **/
0159: protected void localstub(Hashtable symbolTable, MethodEntry m,
0160: PrintWriter stream, int index, InterfaceEntry i) {
0161: this .symbolTable = symbolTable;
0162: this .m = m;
0163: this .stream = stream;
0164: this .methodIndex = index;
0165: if (m.comment() != null)
0166: m.comment().generate(" ", stream);
0167: stream.print(" public ");
0168: writeMethodSignature();
0169: stream.println();
0170: stream.println(" {");
0171: writeLocalStubBody(i);
0172: stream.println(" } // " + m.name());
0173: stream.println();
0174: } // stub
0175:
0176: /**
0177: *
0178: **/
0179: protected void skeleton(Hashtable symbolTable, MethodEntry m,
0180: PrintWriter stream, int index) {
0181: this .symbolTable = symbolTable;
0182: this .m = m;
0183: this .stream = stream;
0184: this .methodIndex = index;
0185: if (m.comment() != null)
0186: m.comment().generate(" ", stream);
0187: stream.print(" public ");
0188: writeMethodSignature();
0189: stream.println();
0190: stream.println(" {");
0191: writeSkeletonBody();
0192: stream.println(" } // " + m.name());
0193: } // skeleton
0194:
0195: /**
0196: *
0197: **/
0198: protected void dispatchSkeleton(Hashtable symbolTable,
0199: MethodEntry m, PrintWriter stream, int index) {
0200: this .symbolTable = symbolTable;
0201: this .m = m;
0202: this .stream = stream;
0203: this .methodIndex = index;
0204: if (m.comment() != null)
0205: m.comment().generate(" ", stream);
0206: writeDispatchCall();
0207: } // dispatchSkeleton
0208:
0209: // <d57112>
0210: /**
0211: * Determine whether method entry m is a valuetype initializer.
0212: * @return true if is m is valuetype initializer, false otherwise.
0213: **/
0214: protected boolean isValueInitializer() {
0215: MethodEntry currentInit = null;
0216: if ((m.container() instanceof ValueEntry)) {
0217: Enumeration e = ((ValueEntry) m.container()).initializers()
0218: .elements();
0219: while (currentInit != m && e.hasMoreElements())
0220: currentInit = (MethodEntry) e.nextElement();
0221: }
0222: return (currentInit == m) && (null != m); // True ==> yes, false ==> no.
0223: } // isValueInitializer
0224:
0225: /**
0226: *
0227: **/
0228: protected void writeMethodSignature() {
0229: boolean isValueInitializer = isValueInitializer(); // <d57112>
0230:
0231: // Step 0. Print the return type and name.
0232: // A return type of null indicates the "void" return type. If m is a
0233: // Valuetype intitializer, it has name "init" and a null return type,
0234: // but it maps to a ctor.
0235: // <d57112>
0236: //if (m.type () == null)
0237: //{
0238: // if (m.name ().compareTo ("init") != 0)
0239: // stream.print ("void");
0240: //}
0241: if (m.type() == null) {
0242: if (!isValueInitializer)
0243: stream.print("void");
0244: } else {
0245: // <f46082.51> Remove -stateful feature; javaStatefulName() obsolete.
0246: //stream.print (Util.javaStatefulName (m.type ()));
0247: stream.print(Util.javaName(m.type()));
0248: }
0249: // <d57112> Value initializers map to constructors.
0250: // If the value has an 'init' method with a return type, handle
0251: // the method like other regular methods
0252: //if (m.valueMethod () && m.name ().compareTo ("init") == 0 &&
0253: // m.type () == null)
0254: if (isValueInitializer)
0255: stream.print(' ' + m.container().name() + " (");
0256: else
0257: stream.print(' ' + m.name() + " (");
0258:
0259: // Step 1. Print the parameter list.
0260: boolean firstTime = true;
0261: Enumeration e = m.parameters().elements();
0262: while (e.hasMoreElements()) {
0263: if (firstTime)
0264: firstTime = false;
0265: else
0266: stream.print(", ");
0267: ParameterEntry parm = (ParameterEntry) e.nextElement();
0268:
0269: writeParmType(parm.type(), parm.passType());
0270:
0271: // Print parm name
0272: stream.print(' ' + parm.name());
0273: }
0274:
0275: // Step 2. Add the context parameter if necessary.
0276: if (m.contexts().size() > 0) {
0277: if (!firstTime)
0278: stream.print(", ");
0279: stream.print("org.omg.CORBA.Context $context");
0280: }
0281:
0282: // Step 3. Print the throws clause (if necessary).
0283: if (m.exceptions().size() > 0) {
0284: stream.print(") throws ");
0285: e = m.exceptions().elements();
0286: firstTime = true;
0287: while (e.hasMoreElements()) {
0288: if (firstTime)
0289: firstTime = false;
0290: else
0291: stream.print(", ");
0292: stream.print(Util.javaName((SymtabEntry) e
0293: .nextElement()));
0294: }
0295: } else
0296: stream.print(')');
0297: } // writeMethodSignature
0298:
0299: /**
0300: *
0301: **/
0302: protected void writeParmType(SymtabEntry parm, int passType) {
0303: if (passType != ParameterEntry.In) {
0304: parm = Util.typeOf(parm);
0305: stream.print(Util.holderName(parm));
0306: } else
0307: // passType is `in'
0308: // <f46082.51> Remove -stateful feature; javaStatefulName() obsolete.
0309: //stream.print (Util.javaStatefulName (parm));
0310: stream.print(Util.javaName(parm));
0311: } // writeParmType
0312:
0313: /**
0314: *
0315: **/
0316: protected void writeDispatchCall() {
0317: String indent = " ";
0318: String fullMethodName = m.fullName();
0319: if (m instanceof AttributeEntry) {
0320: // determine the index at which the attribute name starts in the full name
0321: int index = fullMethodName.lastIndexOf('/') + 1;
0322: if (m.type() == null) // if it's a modifier
0323: fullMethodName = fullMethodName.substring(0, index)
0324: + "_set_" + m.name();
0325: else
0326: fullMethodName = fullMethodName.substring(0, index)
0327: + "_get_" + m.name();
0328: }
0329: stream.println(indent + "case " + methodIndex + ": // "
0330: + fullMethodName);
0331: stream.println(indent + "{");
0332: indent = indent + " ";
0333: if (m.exceptions().size() > 0) {
0334: stream.println(indent + "try {");
0335: indent = indent + " ";
0336: }
0337:
0338: // Step 1 Read arguments from the input stream
0339: SymtabEntry mtype = Util.typeOf(m.type());
0340: Enumeration parms = m.parameters().elements();
0341: parms = m.parameters().elements();
0342: while (parms.hasMoreElements()) {
0343: ParameterEntry parm = (ParameterEntry) parms.nextElement();
0344: String name = parm.name();
0345: String anyName = '_' + name;
0346: SymtabEntry type = parm.type();
0347: int passType = parm.passType();
0348:
0349: if (passType == ParameterEntry.In)
0350: Util.writeInitializer(indent, name, "", type,
0351: writeInputStreamRead("in", type), stream);
0352:
0353: else // the parm is a holder
0354: {
0355: String holderName = Util.holderName(type);
0356: stream.println(indent + holderName + ' ' + name
0357: + " = new " + holderName + " ();");
0358: if (passType == ParameterEntry.Inout) {
0359: if (type instanceof ValueBoxEntry) {
0360: ValueBoxEntry v = (ValueBoxEntry) type;
0361: TypedefEntry member = ((InterfaceState) v
0362: .state().elementAt(0)).entry;
0363: SymtabEntry mType = member.type();
0364: if (mType instanceof PrimitiveEntry)
0365: stream.println(indent
0366: + name
0367: + ".value = ("
0368: + writeInputStreamRead("in", parm
0369: .type()) + ").value;");
0370: else
0371: stream.println(indent
0372: + name
0373: + ".value = "
0374: + writeInputStreamRead("in", parm
0375: .type()) + ";");
0376: } else
0377: stream.println(indent
0378: + name
0379: + ".value = "
0380: + writeInputStreamRead("in", parm
0381: .type()) + ";");
0382: }
0383: }
0384: }
0385:
0386: // Step 1a. Read the context parameter if necessary. <d59560>
0387: if (m.contexts().size() > 0) {
0388: stream
0389: .println(indent
0390: + "org.omg.CORBA.Context $context = in.read_Context ();");
0391: }
0392:
0393: // Step 2 Load return if necessary
0394: if (mtype != null)
0395: Util.writeInitializer(indent, "$result", "", mtype, stream);
0396:
0397: // Step 3 Call the method with the list of parameters
0398: writeMethodCall(indent);
0399:
0400: parms = m.parameters().elements();
0401: boolean firstTime = true;
0402: while (parms.hasMoreElements()) {
0403: ParameterEntry parm = (ParameterEntry) parms.nextElement();
0404: if (firstTime)
0405: firstTime = false;
0406: else
0407: stream.print(", ");
0408: stream.print(parm.name());
0409: }
0410:
0411: // Step 3a. Add the context parameter if necessary. <d59560>
0412: if (m.contexts().size() > 0) {
0413: if (!firstTime)
0414: stream.print(", ");
0415: stream.print("$context");
0416: }
0417:
0418: stream.println(");");
0419:
0420: //Step 3b. Create reply;
0421: writeCreateReply(indent);
0422:
0423: // Step 4 Write method's result to the output stream
0424: if (mtype != null) {
0425: writeOutputStreamWrite(indent, "out", "$result", mtype,
0426: stream);
0427: }
0428:
0429: // Step 5 Write inout/out value to the output stream
0430: parms = m.parameters().elements();
0431: while (parms.hasMoreElements()) {
0432: ParameterEntry parm = (ParameterEntry) parms.nextElement();
0433: int passType = parm.passType();
0434: if (passType != ParameterEntry.In) {
0435: writeOutputStreamWrite(indent, "out", parm.name()
0436: + ".value", parm.type(), stream);
0437: }
0438: }
0439:
0440: // Step 6 Handle exception
0441: if (m.exceptions().size() > 0) {
0442: Enumeration exceptions = m.exceptions().elements();
0443: while (exceptions.hasMoreElements()) {
0444: indent = " ";
0445: ExceptionEntry exc = (ExceptionEntry) exceptions
0446: .nextElement();
0447: String fullName = Util.javaQualifiedName(exc);
0448: stream.println(indent + "} catch (" + fullName
0449: + " $ex) {");
0450: indent = indent + " ";
0451: stream.println(indent
0452: + "out = $rh.createExceptionReply ();");
0453: stream.println(indent + Util.helperName(exc, true)
0454: + ".write (out, $ex);"); // <d61056>
0455: }
0456:
0457: indent = " ";
0458: stream.println(indent + "}");
0459: }
0460:
0461: stream.println(" break;");
0462: stream.println(" }");
0463: stream.println();
0464: } // writeDispatchCall
0465:
0466: /**
0467: *
0468: **/
0469: protected void writeStubBody(String className) {
0470: // Step 1 Create a request
0471: String methodName = Util.stripLeadingUnderscores(m.name());
0472: if (m instanceof AttributeEntry) {
0473: if (m.type() == null) // if it's a modifier
0474: methodName = "_set_" + methodName;
0475: else
0476: methodName = "_get_" + methodName;
0477: }
0478: if (localOptimization && !isAbstract) {
0479: stream.println(ONE_INDENT + "while(true) {");
0480: stream.println(TWO_INDENT + "if(!this._is_local()) {");
0481: }
0482: stream.println(THREE_INDENT
0483: + "org.omg.CORBA.portable.InputStream $in = null;");
0484: stream.println(THREE_INDENT + "try {");
0485: stream.println(FOUR_INDENT
0486: + "org.omg.CORBA.portable.OutputStream $out ="
0487: + " _request (\"" + methodName + "\", " + !m.oneway()
0488: + ");");
0489:
0490: // Step 1.b. Check string bounds <d56554 - klr>
0491: // begin <d56554> in/inout string bounds check
0492: Enumeration parms = m.parameters().elements();
0493: while (parms.hasMoreElements()) {
0494: ParameterEntry parm = (ParameterEntry) parms.nextElement();
0495: SymtabEntry parmType = Util.typeOf(parm.type());
0496: if (parmType instanceof StringEntry)
0497: if ((parm.passType() == ParameterEntry.In)
0498: || (parm.passType() == ParameterEntry.Inout)) {
0499: StringEntry string = (StringEntry) parmType;
0500: if (string.maxSize() != null) {
0501: stream.print(THREE_INDENT + "if ("
0502: + parm.name());
0503: if (parm.passType() == ParameterEntry.Inout)
0504: stream.print(".value"); // get from holder
0505: stream.print(" == null || " + parm.name());
0506: if (parm.passType() == ParameterEntry.Inout)
0507: stream.print(".value"); // get from holder
0508: stream.println(".length () > ("
0509: + Util
0510: .parseExpression(string
0511: .maxSize()) + "))");
0512: stream
0513: .println(THREE_INDENT
0514: + "throw new org.omg.CORBA.BAD_PARAM (0,"
0515: + " org.omg.CORBA.CompletionStatus.COMPLETED_NO);");
0516: }
0517: }
0518: }
0519: // end <d56554> in/inout string bounds check
0520:
0521: // Step 2 Load the parameters into the outputStream
0522: parms = m.parameters().elements();
0523: while (parms.hasMoreElements()) {
0524: ParameterEntry parm = (ParameterEntry) parms.nextElement();
0525: if (parm.passType() == ParameterEntry.In)
0526: writeOutputStreamWrite(FOUR_INDENT, "$out",
0527: parm.name(), parm.type(), stream);
0528: else if (parm.passType() == ParameterEntry.Inout)
0529: writeOutputStreamWrite(FOUR_INDENT, "$out", parm.name()
0530: + ".value", parm.type(), stream);
0531: }
0532:
0533: // Step 2a. Write the context parameter if necessary. <d59560>
0534: if (m.contexts().size() > 0) {
0535: stream.println(FOUR_INDENT
0536: + "org.omg.CORBA.ContextList $contextList ="
0537: + "_orb ().create_context_list ();");
0538:
0539: for (int cnt = 0; cnt < m.contexts().size(); cnt++) {
0540: stream.println(FOUR_INDENT + "$contextList.add (\""
0541: + m.contexts().elementAt(cnt) + "\");");
0542: }
0543: stream.println(FOUR_INDENT
0544: + "$out.write_Context ($context, $contextList);");
0545: }
0546:
0547: // Step 3 Invoke the method with the output stream
0548: stream.println(FOUR_INDENT + "$in = _invoke ($out);");
0549:
0550: SymtabEntry mtype = m.type();
0551: if (mtype != null)
0552: Util.writeInitializer(FOUR_INDENT, "$result", "", mtype,
0553: writeInputStreamRead("$in", mtype), stream);
0554:
0555: // Step 4 Read the inout/out values
0556: parms = m.parameters().elements();
0557: while (parms.hasMoreElements()) {
0558: ParameterEntry parm = (ParameterEntry) parms.nextElement();
0559: if (parm.passType() != ParameterEntry.In) {
0560: if (parm.type() instanceof ValueBoxEntry) {
0561: ValueBoxEntry v = (ValueBoxEntry) parm.type();
0562: TypedefEntry member = ((InterfaceState) v.state()
0563: .elementAt(0)).entry;
0564: SymtabEntry mType = member.type();
0565: if (mType instanceof PrimitiveEntry)
0566: stream.println(FOUR_INDENT
0567: + parm.name()
0568: + ".value = ("
0569: + writeInputStreamRead("$in", parm
0570: .type()) + ").value;");
0571: else
0572: stream.println(FOUR_INDENT
0573: + parm.name()
0574: + ".value = "
0575: + writeInputStreamRead("$in", parm
0576: .type()) + ";");
0577: } else
0578: stream.println(FOUR_INDENT + parm.name()
0579: + ".value = "
0580: + writeInputStreamRead("$in", parm.type())
0581: + ";");
0582: }
0583: }
0584: // Step 4.b. Check string bounds <d56554 - klr>
0585: // begin <d56554> out/inout/return string bounds check
0586: parms = m.parameters().elements();
0587: while (parms.hasMoreElements()) {
0588: ParameterEntry parm = (ParameterEntry) parms.nextElement();
0589: SymtabEntry parmType = Util.typeOf(parm.type());
0590: if (parmType instanceof StringEntry)
0591: if ((parm.passType() == ParameterEntry.Out)
0592: || (parm.passType() == ParameterEntry.Inout)) {
0593: StringEntry string = (StringEntry) parmType;
0594: if (string.maxSize() != null) {
0595: stream.print(FOUR_INDENT + "if (" + parm.name()
0596: + ".value.length ()");
0597: stream.println(" > ("
0598: + Util
0599: .parseExpression(string
0600: .maxSize()) + "))");
0601: stream
0602: .println(FIVE_INDENT
0603: + "throw new org.omg.CORBA.MARSHAL(0,"
0604: + "org.omg.CORBA.CompletionStatus.COMPLETED_NO);");
0605: }
0606: }
0607: }
0608: if (mtype instanceof StringEntry) {
0609: StringEntry string = (StringEntry) mtype;
0610: if (string.maxSize() != null) {
0611: stream
0612: .println(FOUR_INDENT
0613: + "if ($result.length () > ("
0614: + Util
0615: .parseExpression(string
0616: .maxSize()) + "))");
0617: stream
0618: .println(FIVE_INDENT
0619: + "throw new org.omg.CORBA.MARSHAL (0,"
0620: + " org.omg.CORBA.CompletionStatus.COMPLETED_NO);");
0621: }
0622: }
0623: // end <d56554> out/inout/return string bounds check
0624:
0625: // Step 5 Handle return if necessary
0626: if (mtype != null) {
0627: stream.println(FOUR_INDENT + "return $result;");
0628: } else {
0629: stream.println(FOUR_INDENT + "return;");
0630: }
0631:
0632: // Step 6 Handle exceptions
0633: stream
0634: .println(THREE_INDENT
0635: + "} catch (org.omg.CORBA.portable.ApplicationException "
0636: + "$ex) {");
0637: stream.println(FOUR_INDENT + "$in = $ex.getInputStream ();");
0638: stream.println(FOUR_INDENT + "String _id = $ex.getId ();");
0639:
0640: if (m.exceptions().size() > 0) {
0641: Enumeration exceptions = m.exceptions().elements();
0642: boolean firstExc = true;
0643: while (exceptions.hasMoreElements()) {
0644: ExceptionEntry exc = (ExceptionEntry) exceptions
0645: .nextElement();
0646: if (firstExc) {
0647: stream.print(FOUR_INDENT + "if ");
0648: firstExc = false;
0649: } else
0650: stream.print(FOUR_INDENT + "else if ");
0651:
0652: stream.println("(_id.equals (\""
0653: + exc.repositoryID().ID() + "\"))");
0654: stream.println(FIVE_INDENT + "throw "
0655: + Util.helperName((SymtabEntry) exc, false)
0656: + ".read ($in);");
0657: }
0658: stream.println(FOUR_INDENT + "else");
0659: stream.println(FIVE_INDENT
0660: + "throw new org.omg.CORBA.MARSHAL (_id);");
0661: } else
0662: stream.println(FOUR_INDENT
0663: + "throw new org.omg.CORBA.MARSHAL (_id);");
0664:
0665: stream
0666: .println(THREE_INDENT
0667: + "} catch (org.omg.CORBA.portable.RemarshalException $rm) {");
0668: stream.print(FOUR_INDENT);
0669: if (m.type() != null) // not a void method
0670: stream.print("return ");
0671: stream.print(m.name() + " (");
0672: {
0673: // write parm names
0674: boolean firstTime = true;
0675: Enumeration e = m.parameters().elements();
0676: while (e.hasMoreElements()) {
0677: if (firstTime)
0678: firstTime = false;
0679: else
0680: stream.print(", ");
0681: ParameterEntry parm = (ParameterEntry) e.nextElement();
0682: stream.print(parm.name());
0683: }
0684: // Step 2. Add the context parameter if necessary. <d59297>
0685: if (m.contexts().size() > 0) {
0686: if (!firstTime)
0687: stream.print(", ");
0688: stream.print("$context");
0689: }
0690: }
0691: stream.println(TWO_INDENT + ");");
0692: stream.println(THREE_INDENT + "} finally {");
0693: stream.println(FOUR_INDENT + "_releaseReply ($in);");
0694: stream.println(THREE_INDENT + "}");
0695: if (localOptimization && !isAbstract) {
0696: stream.println(TWO_INDENT + "}");
0697: writeStubBodyForLocalInvocation(className, methodName);
0698: }
0699:
0700: } // writeStubBody
0701:
0702: /**
0703: * This method writes the else part of the stub method invocation to
0704: * enable local invocation in case of collocation.
0705: * NOTE: This will only be invoked from writeStubBody.
0706: */
0707: private void writeStubBodyForLocalInvocation(String className,
0708: String methodName) {
0709: stream.println(TWO_INDENT + "else {");
0710: stream.println(THREE_INDENT
0711: + "org.omg.CORBA.portable.ServantObject _so =");
0712: stream.println(FOUR_INDENT + "_servant_preinvoke(\""
0713: + methodName + "\", _opsClass);");
0714: stream.println(THREE_INDENT + "if (_so == null ) {");
0715: stream.println(FOUR_INDENT + "continue;");
0716: stream.println(THREE_INDENT + "}");
0717: stream.println(THREE_INDENT + className + "Operations _self =");
0718: stream.println(FOUR_INDENT + "(" + className
0719: + "Operations) _so.servant;");
0720: stream.println(THREE_INDENT + "try {");
0721: Enumeration parms = m.parameters().elements();
0722: if (m instanceof AttributeEntry) {
0723: // Local Method Name should drop _get_ or _set_ prefix for attribute
0724: // entry
0725: methodName = methodName
0726: .substring(ATTRIBUTE_METHOD_PREFIX_LENGTH);
0727: }
0728: boolean voidReturnType = (this .m.type() == null);
0729: if (!voidReturnType) {
0730: stream.println(FOUR_INDENT + Util.javaName(this .m.type())
0731: + " $result;");
0732: }
0733: if (!isValueInitializer()) {
0734: if (voidReturnType) {
0735: stream
0736: .print(FOUR_INDENT + "_self." + methodName
0737: + "( ");
0738: } else {
0739: stream.print(FOUR_INDENT + "$result = _self."
0740: + methodName + "( ");
0741: }
0742: while (parms.hasMoreElements()) {
0743: ParameterEntry param = (ParameterEntry) parms
0744: .nextElement();
0745: if (parms.hasMoreElements()) {
0746: stream.print(" " + param.name() + ",");
0747: } else {
0748: stream.print(" " + param.name());
0749: }
0750: }
0751: stream.print(");");
0752: stream.println(" ");
0753: if (voidReturnType) {
0754: stream.println(FOUR_INDENT + "return;");
0755: } else {
0756: stream.println(FOUR_INDENT + "return $result;");
0757: }
0758: }
0759: stream.println(" ");
0760: stream.println(THREE_INDENT + "}");
0761: stream.println(THREE_INDENT + "finally {");
0762: stream.println(FOUR_INDENT + "_servant_postinvoke(_so);");
0763: stream.println(THREE_INDENT + "}");
0764: stream.println(TWO_INDENT + "}");
0765: stream.println(ONE_INDENT + "}");
0766: }
0767:
0768: protected void writeLocalStubBody(InterfaceEntry i) {
0769: // Step 1 Create a request
0770: String methodName = Util.stripLeadingUnderscores(m.name());
0771: if (m instanceof AttributeEntry) {
0772: if (m.type() == null) // if it's a modifier
0773: methodName = "_set_" + methodName;
0774: else
0775: methodName = "_get_" + methodName;
0776: }
0777: //stream.println (" while(true) {");
0778: stream
0779: .println(" org.omg.CORBA.portable.ServantObject $so = "
0780: + "_servant_preinvoke (\""
0781: + methodName
0782: + "\", " + "_opsClass);");
0783: //stream.println (" if ($so == null) {");
0784: //stream.println (" continue;");
0785: //stream.println (" }");
0786: String opsName = i.name() + "Operations";
0787: stream.println(" " + opsName + " $self = " + "("
0788: + opsName + ") " + "$so.servant;");
0789: stream.println();
0790: stream.println(" try {");
0791: stream.print(" ");
0792: if (m.type() != null) // not a void method
0793: stream.print("return ");
0794: stream.print("$self." + m.name() + " (");
0795: {
0796: // write parm names
0797: boolean firstTime = true;
0798: Enumeration e = m.parameters().elements();
0799: while (e.hasMoreElements()) {
0800: if (firstTime)
0801: firstTime = false;
0802: else
0803: stream.print(", ");
0804: ParameterEntry parm = (ParameterEntry) e.nextElement();
0805: stream.print(parm.name());
0806: }
0807: // Step 2. Add the context parameter if necessary. <d59297>
0808: if (m.contexts().size() > 0) {
0809: if (!firstTime)
0810: stream.print(", ");
0811: stream.print("$context");
0812: }
0813: }
0814: stream.println(");");
0815: //stream.println (" } catch (org.omg.CORBA.portable.RemarshalException $rm) {");
0816: //stream.println (" continue; ");
0817: stream.println(" } finally {");
0818: stream.println(" _servant_postinvoke ($so);");
0819: stream.println(" }");
0820: //stream.println (" }");
0821:
0822: } // writeLocalStubBody
0823:
0824: /**
0825: *
0826: **/
0827: private void writeInsert(String indent, String target,
0828: String source, SymtabEntry type, PrintWriter stream) {
0829: String typeName = type.name();
0830: if (type instanceof PrimitiveEntry) {
0831: // RJB does something have to be done with TC offsets?
0832: if (typeName.equals("long long"))
0833: stream.println(indent + source + ".insert_longlong ("
0834: + target + ");");
0835: else if (typeName.equals("unsigned short"))
0836: stream.println(indent + source + ".insert_ushort ("
0837: + target + ");");
0838: else if (typeName.equals("unsigned long"))
0839: stream.println(indent + source + ".insert_ulong ("
0840: + target + ");");
0841: else if (typeName.equals("unsigned long long"))
0842: stream.println(indent + source + ".insert_ulonglong ("
0843: + target + ");");
0844: else
0845: stream.println(indent + source + ".insert_" + typeName
0846: + " (" + target + ");");
0847: } else if (type instanceof StringEntry)
0848: stream.println(indent + source + ".insert_" + typeName
0849: + " (" + target + ");");
0850: else
0851: stream.println(indent + Util.helperName(type, true)
0852: + ".insert (" + source + ", " + target + ");"); // <d61056>
0853: } // writeInsert
0854:
0855: /**
0856: *
0857: **/
0858: private void writeType(String indent, String name,
0859: SymtabEntry type, PrintWriter stream) {
0860: if (type instanceof PrimitiveEntry) {
0861: // RJB does something have to be done with TC offsets?
0862: if (type.name().equals("long long"))
0863: stream
0864: .println(indent
0865: + name
0866: + " (org.omg.CORBA.ORB.init ().get_primitive_tc (org.omg.CORBA.TCKind.tk_longlong));");
0867: else if (type.name().equals("unsigned short"))
0868: stream
0869: .println(indent
0870: + name
0871: + " (org.omg.CORBA.ORB.init ().get_primitive_tc (org.omg.CORBA.TCKind.tk_ushort));");
0872: else if (type.name().equals("unsigned long"))
0873: stream
0874: .println(indent
0875: + name
0876: + " (org.omg.CORBA.ORB.init ().get_primitive_tc (org.omg.CORBA.TCKind.tk_ulong));");
0877: else if (type.name().equals("unsigned long long"))
0878: stream
0879: .println(indent
0880: + name
0881: + " (org.omg.CORBA.ORB.init ().get_primitive_tc (org.omg.CORBA.TCKind.tk_ulonglong));");
0882: else
0883: stream
0884: .println(indent
0885: + name
0886: + " (org.omg.CORBA.ORB.init ().get_primitive_tc (org.omg.CORBA.TCKind.tk_"
0887: + type.name() + "));");
0888: } else if (type instanceof StringEntry) {
0889: StringEntry s = (StringEntry) type;
0890: Expression e = s.maxSize();
0891: if (e == null)
0892: stream.println(indent + name
0893: + " (org.omg.CORBA.ORB.init ().create_"
0894: + type.name() + "_tc ("
0895: + Util.parseExpression(e) + "));");
0896: else
0897: stream.println(indent + name
0898: + " (org.omg.CORBA.ORB.init ().create_"
0899: + type.name() + "_tc (0));");
0900: } else
0901: stream.println(indent + name + '('
0902: + Util.helperName(type, true) + ".type ());"); // <d61056>
0903: } // writeType
0904:
0905: /**
0906: *
0907: **/
0908: private void writeExtract(String indent, String target,
0909: String source, SymtabEntry type, PrintWriter stream) {
0910: if (type instanceof PrimitiveEntry) {
0911: if (type.name().equals("long long"))
0912: stream.println(indent + target + " = " + source
0913: + ".extract_longlong ();");
0914: else if (type.name().equals("unsigned short"))
0915: stream.println(indent + target + " = " + source
0916: + ".extract_ushort ();");
0917: else if (type.name().equals("unsigned long"))
0918: stream.println(indent + target + " = " + source
0919: + ".extract_ulong ();");
0920: else if (type.name().equals("unsigned long long"))
0921: stream.println(indent + target + " = " + source
0922: + ".extract_ulonglong ();");
0923: else
0924: stream.println(indent + target + " = " + source
0925: + ".extract_" + type.name() + " ();");
0926: } else if (type instanceof StringEntry)
0927: stream.println(indent + target + " = " + source
0928: + ".extract_" + type.name() + " ();");
0929: else
0930: stream.println(indent + target + " = "
0931: + Util.helperName(type, true) + ".extract ("
0932: + source + ");"); // <d61056>
0933: } // writeExtract
0934:
0935: /**
0936: *
0937: **/
0938: private String writeExtract(String source, SymtabEntry type) {
0939: String extract;
0940: if (type instanceof PrimitiveEntry) {
0941: if (type.name().equals("long long"))
0942: extract = source + ".extract_longlong ()";
0943: else if (type.name().equals("unsigned short"))
0944: extract = source + ".extract_ushort ()";
0945: else if (type.name().equals("unsigned long"))
0946: extract = source + ".extract_ulong ()";
0947: else if (type.name().equals("unsigned long long"))
0948: extract = source + ".extract_ulonglong ()";
0949: else
0950: extract = source + ".extract_" + type.name() + " ()";
0951: } else if (type instanceof StringEntry)
0952: extract = source + ".extract_" + type.name() + " ()";
0953: else
0954: extract = Util.helperName(type, true) + ".extract ("
0955: + source + ')'; // <d61056>
0956: return extract;
0957: } // writeExtract
0958:
0959: /**
0960: *
0961: **/
0962: private void writeSkeletonBody() {
0963: SymtabEntry mtype = Util.typeOf(m.type());
0964:
0965: // If there is a return value, increment the appropriate counter
0966: stream.print(" ");
0967: if (mtype != null)
0968: stream.print("return ");
0969: stream.print("_impl." + m.name() + '(');
0970:
0971: // Load the parameters
0972: Enumeration parms = m.parameters().elements();
0973: boolean first = true;
0974: while (parms.hasMoreElements()) {
0975: ParameterEntry parm = (ParameterEntry) parms.nextElement();
0976: if (first)
0977: first = false;
0978: else
0979: stream.print(", ");
0980: stream.print(parm.name());
0981: }
0982: if (m.contexts().size() != 0) {
0983: if (!first)
0984: stream.print(", ");
0985: stream.print("$context");
0986: }
0987:
0988: stream.println(");");
0989: } // writeSkeletonBody
0990:
0991: /**
0992: *
0993: **/
0994: protected String passType(int passType) {
0995: String type;
0996: switch (passType) {
0997: case ParameterEntry.Inout:
0998: type = "org.omg.CORBA.ARG_INOUT.value";
0999: break;
1000: case ParameterEntry.Out:
1001: type = "org.omg.CORBA.ARG_OUT.value";
1002: break;
1003: case ParameterEntry.In:
1004: default:
1005: type = "org.omg.CORBA.ARG_IN.value";
1006: break;
1007: }
1008: return type;
1009: } // passType
1010:
1011: /**
1012: * This is only used by AttributeGen. The java mapping says
1013: * the names should be getXXX and setXXX, but CORBA says they
1014: * should be _get_XXX and _set_XXX. this.name () will be
1015: * getXXX. realName is set by AttributeGen to _get_XXX.
1016: **/
1017: protected void serverMethodName(String name) {
1018: realName = (name == null) ? "" : name;
1019: } // serverMethodName
1020:
1021: /**
1022: *
1023: **/
1024: private void writeOutputStreamWrite(String indent, String oStream,
1025: String name, SymtabEntry type, PrintWriter stream) {
1026: String typeName = type.name();
1027: stream.print(indent);
1028: if (type instanceof PrimitiveEntry) {
1029: if (typeName.equals("long long"))
1030: stream.println(oStream + ".write_longlong (" + name
1031: + ");");
1032: else if (typeName.equals("unsigned short"))
1033: stream.println(oStream + ".write_ushort (" + name
1034: + ");");
1035: else if (typeName.equals("unsigned long"))
1036: stream
1037: .println(oStream + ".write_ulong (" + name
1038: + ");");
1039: else if (typeName.equals("unsigned long long"))
1040: stream.println(oStream + ".write_ulonglong (" + name
1041: + ");");
1042: else
1043: stream.println(oStream + ".write_" + typeName + " ("
1044: + name + ");");
1045: } else if (type instanceof StringEntry)
1046: stream.println(oStream + ".write_" + typeName + " (" + name
1047: + ");");
1048: else if (type instanceof SequenceEntry)
1049: stream.println(oStream + ".write_" + type.type().name()
1050: + " (" + name + ");");
1051: else if (type instanceof ValueBoxEntry) {
1052: ValueBoxEntry v = (ValueBoxEntry) type;
1053: TypedefEntry member = ((InterfaceState) v.state()
1054: .elementAt(0)).entry;
1055: SymtabEntry mType = member.type();
1056:
1057: // if write value to the boxed holder indicated by the name ending with ".value"
1058: if (mType instanceof PrimitiveEntry
1059: && name.endsWith(".value"))
1060: stream.println(Util.helperName(type, true) + ".write ("
1061: + oStream
1062: + ", " // <d61056>
1063: + " new " + Util.javaQualifiedName(type) + " ("
1064: + name + "));"); //<d60929>
1065: else
1066: stream.println(Util.helperName(type, true) + ".write ("
1067: + oStream + ", " + name + ");"); //<d60929> // <d61056>
1068: } else if (type instanceof ValueEntry)
1069: stream.println(Util.helperName(type, true) + ".write ("
1070: + oStream + ", " + name + ");"); //<d60929> // <d61056>
1071: else
1072: stream.println(Util.helperName(type, true) + ".write ("
1073: + oStream + ", " + name + ");"); // <d61056>
1074: } // writeOutputStreamWrite
1075:
1076: /**
1077: *
1078: **/
1079: private String writeInputStreamRead(String source, SymtabEntry type) {
1080: String read = "";
1081: if (type instanceof PrimitiveEntry) {
1082: if (type.name().equals("long long"))
1083: read = source + ".read_longlong ()";
1084: else if (type.name().equals("unsigned short"))
1085: read = source + ".read_ushort ()";
1086: else if (type.name().equals("unsigned long"))
1087: read = source + ".read_ulong ()";
1088: else if (type.name().equals("unsigned long long"))
1089: read = source + ".read_ulonglong ()";
1090: else
1091: read = source + ".read_" + type.name() + " ()";
1092: } else if (type instanceof StringEntry)
1093: read = source + ".read_" + type.name() + " ()";
1094: else
1095: read = Util.helperName(type, true) + ".read (" + source
1096: + ')'; // <d61056>
1097: return read;
1098: } // writeInputStreamRead
1099:
1100: /**
1101: *
1102: **/
1103: protected void writeMethodCall(String indent) {
1104: SymtabEntry mtype = Util.typeOf(m.type());
1105: if (mtype == null)
1106: stream.print(indent + "this." + m.name() + " (");
1107: else
1108: stream.print(indent + "$result = this." + m.name() + " (");
1109: } // writeMethodCall
1110:
1111: /**
1112: *
1113: **/
1114: protected void writeCreateReply(String indent) {
1115: stream.println(indent + "out = $rh.createReply();");
1116: }
1117:
1118: protected int methodIndex = 0;
1119: protected String realName = "";
1120: protected Hashtable symbolTable = null;
1121: protected MethodEntry m = null;
1122: protected PrintWriter stream = null;
1123: protected boolean localOptimization = false;
1124: protected boolean isAbstract = false;
1125: } // class MethodGen
|