jdk/src/jdk.rmic/share/classes/sun/rmi/rmic/RMIGenerator.java
author chegar
Wed, 03 Dec 2014 14:22:58 +0000
changeset 27565 729f9700483a
parent 25859 3317bb8137f4
child 30655 d83f50188ca9
permissions -rw-r--r--
8049367: Modular Run-Time Images Reviewed-by: chegar, dfuchs, ihse, joehw, mullan, psandoz, wetmore Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, bradford.wetmore@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, james.laskey@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com, sundararajan.athijegannathan@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21591
diff changeset
     2
 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*****************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*                    Copyright (c) IBM Corporation 1998                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/*                                                                           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/* (C) Copyright IBM Corp. 1998                                              */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*                                                                           */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*****************************************************************************/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
package sun.rmi.rmic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.OutputStreamWriter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import sun.tools.java.Type;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import sun.tools.java.Identifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import sun.tools.java.ClassDefinition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.tools.java.ClassDeclaration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.tools.java.ClassNotFound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.tools.java.ClassFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import sun.tools.java.MemberDefinition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.corba.se.impl.util.Utility;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * A Generator object will generate the Java source code of the stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * and skeleton classes for an RMI remote implementation class, using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * a particular stub protocol version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * WARNING: The contents of this source file are not part of any
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * supported API.  Code that depends on them does so at its own risk:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * they are subject to change or removal without notice.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author      Peter Jones,  Bryan Atsatt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
public class RMIGenerator implements RMIConstants, Generator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
    64
    private static final Hashtable<String, Integer> versionOptions = new Hashtable<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    static {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
    66
        versionOptions.put("-v1.1", STUB_VERSION_1_1);
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
    67
        versionOptions.put("-vcompat", STUB_VERSION_FAT);
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
    68
        versionOptions.put("-v1.2", STUB_VERSION_1_2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Default constructor for Main to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public RMIGenerator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        version = STUB_VERSION_1_2;     // default is -v1.2 (see 4638155)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Examine and consume command line arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param argv The command line arguments. Ignore null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * and unknown arguments. Set each consumed argument to null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param error Report any errors using the main.error() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return true if no errors, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public boolean parseArgs(String argv[], Main main) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        String explicitVersion = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int i = 0; i < argv.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            if (argv[i] != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                String arg = argv[i].toLowerCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                if (versionOptions.containsKey(arg)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    if (explicitVersion != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        !explicitVersion.equals(arg))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        main.error("rmic.cannot.use.both",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                   explicitVersion, arg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    explicitVersion = arg;
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
    99
                    version = versionOptions.get(arg);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    argv[i] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Generate the source files for the stub and/or skeleton classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * needed by RMI for the given remote implementation class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param env       compiler environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * @param cdef      definition of remote implementation class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *                  to generate stubs and/or skeletons for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param destDir   directory for the root of the package hierarchy
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *                  for generated files
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void generate(BatchEnvironment env, ClassDefinition cdef, File destDir) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        RemoteClass remoteClass = RemoteClass.forClass(env, cdef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (remoteClass == null)        // exit if an error occurred
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        RMIGenerator gen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            gen = new RMIGenerator(env, cdef, destDir, remoteClass, version);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        } catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            env.error(0, "rmic.class.not.found", e.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        gen.generate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private void generate() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        env.addGeneratedFile(stubFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            IndentingWriter out = new IndentingWriter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                new OutputStreamWriter(new FileOutputStream(stubFile)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            writeStub(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            if (env.verbose()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                env.output(Main.getText("rmic.wrote", stubFile.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   143
            env.parseFile(ClassFile.newClassFile(stubFile));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            env.error(0, "cant.write", stubFile.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (version == STUB_VERSION_1_1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            version == STUB_VERSION_FAT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            env.addGeneratedFile(skeletonFile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                IndentingWriter out = new IndentingWriter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                    new OutputStreamWriter(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        new FileOutputStream(skeletonFile)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                writeSkeleton(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (env.verbose()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    env.output(Main.getText("rmic.wrote",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                        skeletonFile.getPath()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                }
27565
729f9700483a 8049367: Modular Run-Time Images
chegar
parents: 25859
diff changeset
   164
                env.parseFile(ClassFile.newClassFile(skeletonFile));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                env.error(0, "cant.write", stubFile.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
             * For bugid 4135136: if skeleton files are not being generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
             * for this compilation run, delete old skeleton source or class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
             * files for this remote implementation class that were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
             * (presumably) left over from previous runs, to avoid user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
             * confusion from extraneous or inconsistent generated files.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            File outputDir = Util.getOutputDirectoryFor(remoteClassName,destDir,env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            File skeletonClassFile = new File(outputDir,skeletonClassName.getName().toString() + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            skeletonFile.delete();      // ignore failures (no big deal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            skeletonClassFile.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * Return the File object that should be used as the source file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * for the given Java class, using the supplied destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * directory for the top of the package hierarchy.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    protected static File sourceFileForClass(Identifier className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                                             Identifier outputClassName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                             File destDir,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                                             BatchEnvironment env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        File packageDir = Util.getOutputDirectoryFor(className,destDir,env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        String outputName = Names.mangleClass(outputClassName).getName().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // Is there any existing _Tie equivalent leftover from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        // previous invocation of rmic -iiop? Only do this once per
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        // class by looking for skeleton generation...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (outputName.endsWith("_Skel")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            String classNameStr = className.getName().toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            File temp = new File(packageDir, Utility.tieName(classNameStr) + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if (temp.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                // Found a tie. Is IIOP generation also being done?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if (!env.getMain().iiopGeneration) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                    // No, so write a warning...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                    env.error(0,"warn.rmic.tie.found",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                              classNameStr,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                              temp.getAbsolutePath());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        String outputFileName = outputName + ".java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        return new File(packageDir, outputFileName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    /** rmic environment for this object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private BatchEnvironment env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /** the remote class that this instance is generating code for */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    private RemoteClass remoteClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    /** version of the stub protocol to use in code generation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    private int version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    /** remote methods for remote class, indexed by operation number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    private RemoteClass.Method[] remoteMethods;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * Names for the remote class and the stub and skeleton classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * to be generated for it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private Identifier remoteClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    private Identifier stubClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    private Identifier skeletonClassName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    private ClassDefinition cdef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    private File destDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private File stubFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    private File skeletonFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * Names to use for the java.lang.reflect.Method static fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * corresponding to each remote method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    private String[] methodFieldNames;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /** cached definition for certain exception classes in this environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private ClassDefinition defException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    private ClassDefinition defRemoteException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    private ClassDefinition defRuntimeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * Create a new stub/skeleton Generator object for the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * remote implementation class to generate code according to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * the given stub protocol version.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private RMIGenerator(BatchEnvironment env, ClassDefinition cdef,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                           File destDir, RemoteClass remoteClass, int version)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        throws ClassNotFound
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        this.destDir     = destDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        this.cdef        = cdef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        this.env         = env;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        this.remoteClass = remoteClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        this.version     = version;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        remoteMethods = remoteClass.getRemoteMethods();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        remoteClassName = remoteClass.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        stubClassName = Names.stubFor(remoteClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        skeletonClassName = Names.skeletonFor(remoteClassName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        methodFieldNames = nameMethodFields(remoteMethods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        stubFile = sourceFileForClass(remoteClassName,stubClassName, destDir , env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        skeletonFile = sourceFileForClass(remoteClassName,skeletonClassName, destDir, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         * Initialize cached definitions for exception classes used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * in the generation process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        defException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            env.getClassDeclaration(idJavaLangException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        defRemoteException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            env.getClassDeclaration(idRemoteException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        defRuntimeException =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            env.getClassDeclaration(idJavaLangRuntimeException).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                getClassDefinition(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Write the stub for the remote class to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    private void writeStub(IndentingWriter p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
         * Write boiler plate comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        p.pln("// Stub class generated by rmic, do not edit.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        p.pln("// Contents subject to change without notice.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * If remote implementation class was in a particular package,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * declare the stub class to be in the same package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (remoteClassName.isQualified()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            p.pln("package " + remoteClassName.getQualifier() + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         * Declare the stub class; implement all remote interfaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        p.plnI("public final class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            Names.mangleClass(stubClassName.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        p.pln("extends " + idRemoteStub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        ClassDefinition[] remoteInterfaces = remoteClass.getRemoteInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        if (remoteInterfaces.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            p.p("implements ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            for (int i = 0; i < remoteInterfaces.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    p.p(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                p.p(remoteInterfaces[i].getName().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        p.pOlnI("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        if (version == STUB_VERSION_1_1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            version == STUB_VERSION_FAT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            writeOperationsArray(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            writeInterfaceHash(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        if (version == STUB_VERSION_FAT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            version == STUB_VERSION_1_2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            p.pln("private static final long serialVersionUID = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                STUB_SERIAL_VERSION_UID + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
             * We only need to declare and initialize the static fields of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
             * Method objects for each remote method if there are any remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
             * methods; otherwise, skip this code entirely, to avoid generating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
             * a try/catch block for a checked exception that cannot occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
             * (see bugid 4125181).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            if (methodFieldNames.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                if (version == STUB_VERSION_FAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    p.pln("private static boolean useNewInvoke;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                writeMethodFieldDeclarations(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                 * Initialize java.lang.reflect.Method fields for each remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                 * method in a static initializer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                p.plnI("static {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                p.plnI("try {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                if (version == STUB_VERSION_FAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                     * Fat stubs must determine whether the API required for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                     * the JDK 1.2 stub protocol is supported in the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                     * runtime, so that it can use it if supported.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                     * determined by using the Reflection API to test if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                     * new invoke method on RemoteRef exists, and setting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                     * static boolean "useNewInvoke" to true if it does, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                     * to false if a NoSuchMethodException is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    p.plnI(idRemoteRef + ".class.getMethod(\"invoke\",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    p.plnI("new java.lang.Class[] {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                    p.pln(idRemote + ".class,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                    p.pln("java.lang.reflect.Method.class,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    p.pln("java.lang.Object[].class,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    p.pln("long.class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    p.pOln("});");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    p.pO();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    p.pln("useNewInvoke = true;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                writeMethodFieldInitializers(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                p.pOlnI("} catch (java.lang.NoSuchMethodException e) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                if (version == STUB_VERSION_FAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                    p.pln("useNewInvoke = false;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                     * REMIND: By throwing an Error here, the application will
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                     * get the NoSuchMethodError directly when the stub class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                     * is initialized.  If we throw a RuntimeException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                     * instead, the application would get an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                     * ExceptionInInitializerError.  Would that be more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                     * appropriate, and if so, which RuntimeException should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                     * be thrown?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                    p.plnI("throw new java.lang.NoSuchMethodError(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    p.pln("\"stub class initialization failed\");");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                    p.pO();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                p.pOln("}");            // end try/catch block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                p.pOln("}");            // end static initializer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        writeStubConstructors(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         * Write each stub method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (remoteMethods.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            p.pln("// methods from remote interfaces");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            for (int i = 0; i < remoteMethods.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                writeStubMethod(p, i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        p.pOln("}");                    // end stub class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Write the constructors for the stub class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    private void writeStubConstructors(IndentingWriter p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        p.pln("// constructors");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
         * Only stubs compatible with the JDK 1.1 stub protocol need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
         * a no-arg constructor; later versions use reflection to find
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
         * the constructor that directly takes a RemoteRef argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (version == STUB_VERSION_1_1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            version == STUB_VERSION_FAT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            p.plnI("public " + Names.mangleClass(stubClassName.getName()) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                "() {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            p.pln("super();");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            p.pOln("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        p.plnI("public " + Names.mangleClass(stubClassName.getName()) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            "(" + idRemoteRef + " ref) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        p.pln("super(ref);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        p.pOln("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Write the stub method for the remote method with the given "opnum".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    private void writeStubMethod(IndentingWriter p, int opnum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        RemoteClass.Method method = remoteMethods[opnum];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        Identifier methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        Type methodType = method.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        Type paramTypes[] = methodType.getArgumentTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        String paramNames[] = nameParameters(paramTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        Type returnType = methodType.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        ClassDeclaration[] exceptions = method.getExceptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
         * Declare stub method; throw exceptions declared in remote
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
         * interface(s).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        p.pln("// implementation of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            methodType.typeString(methodName.toString(), true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        p.p("public " + returnType + " " + methodName + "(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        for (int i = 0; i < paramTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                p.p(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            p.p(paramTypes[i] + " " + paramNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        p.plnI(")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (exceptions.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            p.p("throws ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            for (int i = 0; i < exceptions.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    p.p(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                p.p(exceptions[i].getName().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        p.pOlnI("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
         * The RemoteRef.invoke methods throw Exception, but unless this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
         * stub method throws Exception as well, we must catch Exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
         * thrown from the invocation.  So we must catch Exception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
         * rethrow something we can throw: UnexpectedException, which is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
         * subclass of RemoteException.  But for any subclasses of Exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
         * that we can throw, like RemoteException, RuntimeException, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
         * any of the exceptions declared by this stub method, we want them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
         * to pass through unharmed, so first we must catch any such
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
         * exceptions and rethrow it directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
         * We have to be careful generating the rethrowing catch blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
         * here, because javac will flag an error if there are any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
         * unreachable catch blocks, i.e. if the catch of an exception class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
         * follows a previous catch of it or of one of its superclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
         * The following method invocation takes care of these details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
         */
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
   522
        Vector<ClassDefinition> catchList = computeUniqueCatchList(exceptions);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
         * If we need to catch any particular exceptions (i.e. this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
         * does not declare java.lang.Exception), put the entire stub
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
         * method in a try block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        if (catchList.size() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            p.plnI("try {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (version == STUB_VERSION_FAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            p.plnI("if (useNewInvoke) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        if (version == STUB_VERSION_FAT ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            version == STUB_VERSION_1_2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            if (!returnType.isType(TC_VOID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                p.p("Object $result = ");               // REMIND: why $?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            p.p("ref.invoke(this, " + methodFieldNames[opnum] + ", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (paramTypes.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                p.p("new java.lang.Object[] {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                for (int i = 0; i < paramTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                    if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                        p.p(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    p.p(wrapArgumentCode(paramTypes[i], paramNames[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                p.p("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                p.p("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            p.pln(", " + method.getMethodHash() + "L);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            if (!returnType.isType(TC_VOID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                p.pln("return " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    unwrapArgumentCode(returnType, "$result") + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (version == STUB_VERSION_FAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            p.pOlnI("} else {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        if (version == STUB_VERSION_1_1 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            version == STUB_VERSION_FAT)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            p.pln(idRemoteCall + " call = ref.newCall((" + idRemoteObject +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                ") this, operations, " + opnum + ", interfaceHash);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            if (paramTypes.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                p.plnI("try {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                p.pln("java.io.ObjectOutput out = call.getOutputStream();");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                writeMarshalArguments(p, "out", paramTypes, paramNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                p.pOlnI("} catch (java.io.IOException e) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                p.pln("throw new " + idMarshalException +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    "(\"error marshalling arguments\", e);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                p.pOln("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            p.pln("ref.invoke(call);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (returnType.isType(TC_VOID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                p.pln("ref.done(call);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                p.pln(returnType + " $result;");        // REMIND: why $?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                p.plnI("try {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                p.pln("java.io.ObjectInput in = call.getInputStream();");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                boolean objectRead =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    writeUnmarshalArgument(p, "in", returnType, "$result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                p.pln(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                p.pOlnI("} catch (java.io.IOException e) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                p.pln("throw new " + idUnmarshalException +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    "(\"error unmarshalling return\", e);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                 * If any only if readObject has been invoked, we must catch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                 * ClassNotFoundException as well as IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                if (objectRead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    p.pOlnI("} catch (java.lang.ClassNotFoundException e) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    p.pln("throw new " + idUnmarshalException +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                        "(\"error unmarshalling return\", e);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                p.pOlnI("} finally {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                p.pln("ref.done(call);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                p.pOln("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                p.pln("return $result;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        if (version == STUB_VERSION_FAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            p.pOln("}");                // end if/else (useNewInvoke) block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
         * If we need to catch any particular exceptions, finally write
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
         * the catch blocks for them, rethrow any other Exceptions with an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
         * UnexpectedException, and end the try block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (catchList.size() > 0) {
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
   618
            for (Enumeration<ClassDefinition> enumeration = catchList.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                 enumeration.hasMoreElements();)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            {
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
   621
                ClassDefinition def = enumeration.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                p.pOlnI("} catch (" + def.getName() + " e) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                p.pln("throw e;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            p.pOlnI("} catch (java.lang.Exception e) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            p.pln("throw new " + idUnexpectedException +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                "(\"undeclared checked exception\", e);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            p.pOln("}");                // end try/catch block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        p.pOln("}");                    // end stub method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * Compute the exceptions which need to be caught and rethrown in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * stub method before wrapping Exceptions in UnexpectedExceptions,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * given the exceptions declared in the throws clause of the method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * Returns a Vector containing ClassDefinition objects for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * exception to catch.  Each exception is guaranteed to be unique,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * i.e. not a subclass of any of the other exceptions in the Vector,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * so the catch blocks for these exceptions may be generated in any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * order relative to each other.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * RemoteException and RuntimeException are each automatically placed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * in the returned Vector (if none of their superclasses are already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * present), since those exceptions should always be directly rethrown
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * by a stub method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * The returned Vector will be empty if java.lang.Exception or one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * of its superclasses is in the throws clause of the method, indicating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * that no exceptions need to be caught.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     */
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
   653
    private Vector<ClassDefinition> computeUniqueCatchList(ClassDeclaration[] exceptions) {
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
   654
        Vector<ClassDefinition> uniqueList = new Vector<>();       // unique exceptions to catch
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        uniqueList.addElement(defRuntimeException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
        uniqueList.addElement(defRemoteException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        /* For each exception declared by the stub method's throws clause: */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    nextException:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        for (int i = 0; i < exceptions.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            ClassDeclaration decl = exceptions[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                if (defException.subClassOf(env, decl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                     * (If java.lang.Exception (or a superclass) was declared
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
                     * in the throws clause of this stub method, then we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
                     * have to bother catching anything; clear the list and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                     * return.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                    uniqueList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                } else if (!defException.superClassOf(env, decl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                     * Ignore other Throwables that do not extend Exception,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                     * since they do not need to be caught anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
                 * Compare this exception against the current list of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                 * exceptions that need to be caught:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
                for (int j = 0; j < uniqueList.size();) {
12197
565357741463 7152007: Fix warnings in sun/rmi/rmic
khazra
parents: 12045
diff changeset
   685
                    ClassDefinition def = uniqueList.elementAt(j);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    if (def.superClassOf(env, decl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                         * If a superclass of this exception is already on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                         * the list to catch, then ignore and continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
                        continue nextException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    } else if (def.subClassOf(env, decl)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                         * If a subclass of this exception is on the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                         * to catch, then remove it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                        uniqueList.removeElementAt(j);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                        j++;    // else continue comparing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                /* This exception is unique: add it to the list to catch. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                uniqueList.addElement(decl.getClassDefinition(env));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            } catch (ClassNotFound e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                env.error(0, "class.not.found", e.name, decl.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                 * REMIND: We do not exit from this exceptional condition,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                 * generating questionable code and likely letting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
                 * compiler report a resulting error later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return uniqueList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Write the skeleton for the remote class to a stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    private void writeSkeleton(IndentingWriter p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        if (version == STUB_VERSION_1_2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
            throw new Error("should not generate skeleton for version");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * Write boiler plate comment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        p.pln("// Skeleton class generated by rmic, do not edit.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        p.pln("// Contents subject to change without notice.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
         * If remote implementation class was in a particular package,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
         * declare the skeleton class to be in the same package.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        if (remoteClassName.isQualified()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
            p.pln("package " + remoteClassName.getQualifier() + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         * Declare the skeleton class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        p.plnI("public final class " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            Names.mangleClass(skeletonClassName.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        p.pln("implements " + idSkeleton);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        p.pOlnI("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        writeOperationsArray(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        writeInterfaceHash(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
         * Define the getOperations() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        p.plnI("public " + idOperation + "[] getOperations() {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        p.pln("return (" + idOperation + "[]) operations.clone();");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        p.pOln("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
         * Define the dispatch() method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        p.plnI("public void dispatch(" + idRemote + " obj, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
            idRemoteCall + " call, int opnum, long hash)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        p.pln("throws java.lang.Exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        p.pOlnI("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        if (version == STUB_VERSION_FAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
            p.plnI("if (opnum < 0) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            if (remoteMethods.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                for (int opnum = 0; opnum < remoteMethods.length; opnum++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                    if (opnum > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                        p.pO("} else ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    p.plnI("if (hash == " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                        remoteMethods[opnum].getMethodHash() + "L) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    p.pln("opnum = " + opnum + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                p.pOlnI("} else {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
             * Skeleton throws UnmarshalException if it does not recognize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
             * the method hash; this is what UnicastServerRef.dispatch()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
             * would do.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            p.pln("throw new " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                idUnmarshalException + "(\"invalid method hash\");");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            if (remoteMethods.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                p.pOln("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
             * Ignore the validation of the interface hash if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
             * operation number was negative, since it is really a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
             * method hash instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            p.pOlnI("} else {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        p.plnI("if (hash != interfaceHash)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        p.pln("throw new " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            idSkeletonMismatchException + "(\"interface hash mismatch\");");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        p.pO();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        if (version == STUB_VERSION_FAT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            p.pOln("}");                // end if/else (opnum < 0) block
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
         * Cast remote object instance to our specific implementation class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        p.pln(remoteClassName + " server = (" + remoteClassName + ") obj;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
         * Process call according to the operation number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        p.plnI("switch (opnum) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        for (int opnum = 0; opnum < remoteMethods.length; opnum++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            writeSkeletonDispatchCase(p, opnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        p.pOlnI("default:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
         * Skeleton throws UnmarshalException if it does not recognize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
         * the operation number; this is consistent with the case of an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
         * unrecognized method hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        p.pln("throw new " + idUnmarshalException +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            "(\"invalid method number\");");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        p.pOln("}");                    // end switch statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        p.pOln("}");                    // end dispatch() method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        p.pOln("}");                    // end skeleton class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * Write the case block for the skeleton's dispatch method for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * the remote method with the given "opnum".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    private void writeSkeletonDispatchCase(IndentingWriter p, int opnum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        RemoteClass.Method method = remoteMethods[opnum];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        Identifier methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        Type methodType = method.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        Type paramTypes[] = methodType.getArgumentTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        String paramNames[] = nameParameters(paramTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
        Type returnType = methodType.getReturnType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        p.pOlnI("case " + opnum + ": // " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            methodType.typeString(methodName.toString(), true, false));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
         * Use nested block statement inside case to provide an independent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
         * namespace for local variables used to unmarshal parameters for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
         * this remote method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        p.pOlnI("{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (paramTypes.length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
             * Declare local variables to hold arguments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            for (int i = 0; i < paramTypes.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                p.pln(paramTypes[i] + " " + paramNames[i] + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
             * Unmarshal arguments from call stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            p.plnI("try {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            p.pln("java.io.ObjectInput in = call.getInputStream();");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            boolean objectsRead = writeUnmarshalArguments(p, "in",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                paramTypes, paramNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
            p.pOlnI("} catch (java.io.IOException e) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            p.pln("throw new " + idUnmarshalException +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                "(\"error unmarshalling arguments\", e);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
             * If any only if readObject has been invoked, we must catch
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
             * ClassNotFoundException as well as IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
            if (objectsRead) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                p.pOlnI("} catch (java.lang.ClassNotFoundException e) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                p.pln("throw new " + idUnmarshalException +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                    "(\"error unmarshalling arguments\", e);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            p.pOlnI("} finally {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            p.pln("call.releaseInputStream();");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
            p.pOln("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            p.pln("call.releaseInputStream();");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if (!returnType.isType(TC_VOID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
             * Declare variable to hold return type, if not void.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
            p.p(returnType + " $result = ");            // REMIND: why $?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
         * Invoke the method on the server object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        p.p("server." + methodName + "(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        for (int i = 0; i < paramNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
            if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
                p.p(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            p.p(paramNames[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        p.pln(");");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
         * Always invoke getResultStream(true) on the call object to send
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
         * the indication of a successful invocation to the caller.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
         * the return type is not void, keep the result stream and marshal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
         * the return value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        p.plnI("try {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        if (!returnType.isType(TC_VOID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            p.p("java.io.ObjectOutput out = ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        p.pln("call.getResultStream(true);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        if (!returnType.isType(TC_VOID)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
            writeMarshalArgument(p, "out", returnType, "$result");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
            p.pln(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
        p.pOlnI("} catch (java.io.IOException e) {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        p.pln("throw new " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            idMarshalException + "(\"error marshalling return\", e);");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        p.pOln("}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        p.pln("break;");                // break from switch statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
        p.pOlnI("}");                   // end nested block statement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * Write declaration and initializer for "operations" static array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
    private void writeOperationsArray(IndentingWriter p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        p.plnI("private static final " + idOperation + "[] operations = {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        for (int i = 0; i < remoteMethods.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            if (i > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
                p.pln(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            p.p("new " + idOperation + "(\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                remoteMethods[i].getOperationString() + "\")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        p.pln();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        p.pOln("};");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * Write declaration and initializer for "interfaceHash" static field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
    private void writeInterfaceHash(IndentingWriter p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        p.pln("private static final long interfaceHash = " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
            remoteClass.getInterfaceHash() + "L;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * Write declaration for java.lang.reflect.Method static fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * corresponding to each remote method in a stub.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
    private void writeMethodFieldDeclarations(IndentingWriter p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        for (int i = 0; i < methodFieldNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            p.pln("private static java.lang.reflect.Method " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                methodFieldNames[i] + ";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     * Write code to initialize the static fields for each method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * using the Java Reflection API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    private void writeMethodFieldInitializers(IndentingWriter p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
        for (int i = 0; i < methodFieldNames.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            p.p(methodFieldNames[i] + " = ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
             * Here we look up the Method object in the arbitrary interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
             * that we find in the RemoteClass.Method object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
             * REMIND: Is this arbitrary choice OK?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
             * REMIND: Should this access be part of RemoteClass.Method's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
             * abstraction?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            RemoteClass.Method method = remoteMethods[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
            MemberDefinition def = method.getMemberDefinition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            Identifier methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            Type methodType = method.getType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
            Type paramTypes[] = methodType.getArgumentTypes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
            p.p(def.getClassDefinition().getName() + ".class.getMethod(\"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                methodName + "\", new java.lang.Class[] {");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            for (int j = 0; j < paramTypes.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
                if (j > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                    p.p(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                p.p(paramTypes[j] + ".class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            p.pln("});");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * Following are a series of static utility methods useful during
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * the code generation process:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * Generate an array of names for fields that correspond to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     * array of remote methods.  Each name in the returned array is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     * guaranteed to be unique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * The name of a method is included in its corresponding field name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * to enhance readability of the generated code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
    private static String[] nameMethodFields(RemoteClass.Method[] methods) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
        String[] names = new String[methods.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            names[i] = "$method_" + methods[i].getName() + "_" + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * Generate an array of names for parameters corresponding to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     * given array of types for the parameters.  Each name in the returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * array is guaranteed to be unique.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * A representation of the type of a parameter is included in its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * corresponding field name to enhance the readability of the generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    private static String[] nameParameters(Type[] types) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
        String[] names = new String[types.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
        for (int i = 0; i < names.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
            names[i] = "$param_" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                generateNameFromType(types[i]) + "_" + (i + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
        return names;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     * Generate a readable string representing the given type suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * for embedding within a Java identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
    private static String generateNameFromType(Type type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        int typeCode = type.getTypeCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        switch (typeCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        case TC_BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        case TC_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        case TC_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        case TC_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        case TC_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        case TC_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        case TC_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        case TC_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            return type.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
        case TC_ARRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            return "arrayOf_" + generateNameFromType(type.getElementType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
        case TC_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
            return Names.mangleClass(type.getClassName().getName()).toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
            throw new Error("unexpected type code: " + typeCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * Write a snippet of Java code to marshal a value named "name" of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     * type "type" to the java.io.ObjectOutput stream named "stream".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
     * Primitive types are marshalled with their corresponding methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
     * in the java.io.DataOutput interface, and objects (including arrays)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * are marshalled using the writeObject method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
    private static void writeMarshalArgument(IndentingWriter p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                                             String streamName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                                             Type type, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        int typeCode = type.getTypeCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        switch (typeCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        case TC_BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
            p.p(streamName + ".writeBoolean(" + name + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        case TC_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
            p.p(streamName + ".writeByte(" + name + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
        case TC_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
            p.p(streamName + ".writeChar(" + name + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        case TC_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            p.p(streamName + ".writeShort(" + name + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        case TC_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
            p.p(streamName + ".writeInt(" + name + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        case TC_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
            p.p(streamName + ".writeLong(" + name + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        case TC_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
            p.p(streamName + ".writeFloat(" + name + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
        case TC_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
            p.p(streamName + ".writeDouble(" + name + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        case TC_ARRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        case TC_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            p.p(streamName + ".writeObject(" + name + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            throw new Error("unexpected type code: " + typeCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
     * Write Java statements to marshal a series of values in order as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
     * named in the "names" array, with types as specified in the "types"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
     * array", to the java.io.ObjectOutput stream named "stream".
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    private static void writeMarshalArguments(IndentingWriter p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                                              String streamName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                                              Type[] types, String[] names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
        if (types.length != names.length) {
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 14342
diff changeset
  1135
            throw new Error("parameter type and name arrays different sizes");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
        for (int i = 0; i < types.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            writeMarshalArgument(p, streamName, types[i], names[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
            p.pln(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
     * Write a snippet of Java code to unmarshal a value of type "type"
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
     * from the java.io.ObjectInput stream named "stream" into a variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
     * named "name" (if "name" is null, the value in unmarshalled and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     * discarded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Primitive types are unmarshalled with their corresponding methods
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * in the java.io.DataInput interface, and objects (including arrays)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * are unmarshalled using the readObject method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
    private static boolean writeUnmarshalArgument(IndentingWriter p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
                                                  String streamName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                                                  Type type, String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        boolean readObject = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            p.p(name + " = ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
        int typeCode = type.getTypeCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
        switch (type.getTypeCode()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        case TC_BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
            p.p(streamName + ".readBoolean()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
        case TC_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            p.p(streamName + ".readByte()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
        case TC_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
            p.p(streamName + ".readChar()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        case TC_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            p.p(streamName + ".readShort()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        case TC_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            p.p(streamName + ".readInt()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        case TC_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
            p.p(streamName + ".readLong()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        case TC_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            p.p(streamName + ".readFloat()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        case TC_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
            p.p(streamName + ".readDouble()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        case TC_ARRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        case TC_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            p.p("(" + type + ") " + streamName + ".readObject()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
            readObject = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
            throw new Error("unexpected type code: " + typeCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
        return readObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * Write Java statements to unmarshal a series of values in order of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * types as in the "types" array from the java.io.ObjectInput stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * named "stream" into variables as named in "names" (for any element
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * of "names" that is null, the corresponding value is unmarshalled
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * and discarded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    private static boolean writeUnmarshalArguments(IndentingWriter p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                                                   String streamName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                                                   Type[] types,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                                                   String[] names)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        if (types.length != names.length) {
21591
35320b590d9b 8026491: Typos in string literals
malenkov
parents: 14342
diff changeset
  1216
            throw new Error("parameter type and name arrays different sizes");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        boolean readObject = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        for (int i = 0; i < types.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
            if (writeUnmarshalArgument(p, streamName, types[i], names[i])) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
                readObject = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
            p.pln(";");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        return readObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
     * Return a snippet of Java code to wrap a value named "name" of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
     * type "type" into an object as appropriate for use by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * Java Reflection API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     * For primitive types, an appropriate wrapper class instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
     * with the primitive value.  For object types (including arrays),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
     * no wrapping is necessary, so the value is named directly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
    private static String wrapArgumentCode(Type type, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        int typeCode = type.getTypeCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        switch (typeCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        case TC_BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
            return ("(" + name +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
                    " ? java.lang.Boolean.TRUE : java.lang.Boolean.FALSE)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        case TC_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            return "new java.lang.Byte(" + name + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
        case TC_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            return "new java.lang.Character(" + name + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
        case TC_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            return "new java.lang.Short(" + name + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
        case TC_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            return "new java.lang.Integer(" + name + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        case TC_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            return "new java.lang.Long(" + name + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        case TC_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            return "new java.lang.Float(" + name + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
        case TC_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            return "new java.lang.Double(" + name + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        case TC_ARRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
        case TC_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            throw new Error("unexpected type code: " + typeCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * Return a snippet of Java code to unwrap a value named "name" into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     * a value of type "type", as appropriate for the Java Reflection API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     * For primitive types, the value is assumed to be of the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * wrapper type, and a method is called on the wrapper type to retrieve
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * the primitive value.  For object types (include arrays), no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
     * unwrapping is necessary; the value is simply cast to the expected
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
     * real object type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
    private static String unwrapArgumentCode(Type type, String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        int typeCode = type.getTypeCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        switch (typeCode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        case TC_BOOLEAN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            return "((java.lang.Boolean) " + name + ").booleanValue()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        case TC_BYTE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
            return "((java.lang.Byte) " + name + ").byteValue()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        case TC_CHAR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            return "((java.lang.Character) " + name + ").charValue()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
        case TC_SHORT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            return "((java.lang.Short) " + name + ").shortValue()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        case TC_INT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            return "((java.lang.Integer) " + name + ").intValue()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        case TC_LONG:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            return "((java.lang.Long) " + name + ").longValue()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        case TC_FLOAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            return "((java.lang.Float) " + name + ").floatValue()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        case TC_DOUBLE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
            return "((java.lang.Double) " + name + ").doubleValue()";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        case TC_ARRAY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        case TC_CLASS:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            return "((" + type + ") " + name + ")";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
            throw new Error("unexpected type code: " + typeCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
}