corba/src/jdk.rmic/share/classes/sun/rmi/rmic/iiop/Generator.java
author gtriantafill
Wed, 14 Sep 2016 08:17:50 -0400
changeset 41092 c388d897472d
parent 27568 55efe677dc75
permissions -rw-r--r--
8165889: Remove jdk.test.lib.unsafe.UnsafeHelper Summary: Remove use of setAccessible() to get Unsafe. Reviewed-by: shade, lfoltan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
 * Licensed Materials - Property of IBM
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
 * RMI-IIOP v1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
 * Copyright IBM Corp. 1998 1999  All Rights Reserved
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
package sun.rmi.rmic.iiop;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import java.io.File;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import java.io.FileOutputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
import java.io.OutputStreamWriter;
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import java.io.IOException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
import sun.tools.java.Identifier;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
import sun.tools.java.ClassPath;
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
import sun.tools.java.ClassFile;
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
import sun.tools.java.ClassNotFound;
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
import sun.tools.java.ClassDefinition;
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
import sun.tools.java.ClassDeclaration;
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
import sun.rmi.rmic.IndentingWriter;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
import sun.rmi.rmic.Main;
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
import sun.rmi.rmic.iiop.Util;
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
import java.util.HashSet;
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
 * Generator provides a small framework from which IIOP-specific
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
 * generators can inherit.  Common logic is implemented here which uses
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
 * both abstract methods as well as concrete methods which subclasses may
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
 * want to override. The following methods must be present in any subclass:
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
 * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
 *      Default constructor
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
 *              CompoundType getTopType(BatchEnvironment env, ClassDefinition cdef);
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
 *      int parseArgs(String argv[], int currentIndex);
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
 *      boolean requireNewInstance();
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
 *              OutputType[] getOutputTypesFor(CompoundType topType,
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
 *                                     HashSet alreadyChecked);
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
 *              String getFileNameExtensionFor(OutputType outputType);
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
 *              void writeOutputFor (   OutputType outputType,
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
 *                              HashSet alreadyChecked,
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
 *                                                              IndentingWriter writer) throws IOException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
 * </pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
 * @author      Bryan Atsatt
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
public abstract class Generator implements      sun.rmi.rmic.Generator,
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
                                                sun.rmi.rmic.iiop.Constants {
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
    protected boolean alwaysGenerate = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
    protected BatchEnvironment env = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
    protected ContextStack contextStack = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
    private boolean trace = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
    protected boolean idl = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
     * Examine and consume command line arguments.
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
     * @param argv The command line arguments. Ignore null
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
     * and unknown arguments. Set each consumed argument to null.
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
     * @param error Report any errors using the main.error() methods.
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
     * @return true if no errors, false otherwise.
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
    public boolean parseArgs(String argv[], Main main) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
        for (int i = 0; i < argv.length; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
            if (argv[i] != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
                if (argv[i].equalsIgnoreCase("-always") ||
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
                    argv[i].equalsIgnoreCase("-alwaysGenerate")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
                    alwaysGenerate = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
                    argv[i] = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
                } else if (argv[i].equalsIgnoreCase("-xtrace")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
                    trace = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
                    argv[i] = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
        return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
     * Return true if non-conforming types should be parsed.
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
     * @param stack The context stack.
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
    protected abstract boolean parseNonConforming(ContextStack stack);
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
     * Create and return a top-level type.
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
     * @param cdef The top-level class definition.
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
     * @param stack The context stack.
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
     * @return The compound type or null if is non-conforming.
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
    protected abstract CompoundType getTopType(ClassDefinition cdef, ContextStack stack);
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
     * Return an array containing all the file names and types that need to be
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
     * generated for the given top-level type.  The file names must NOT have an
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
     * extension (e.g. ".java").
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
     * @param topType The type returned by getTopType().
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
     * @param alreadyChecked A set of Types which have already been checked.
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
     *  Intended to be passed to Type.collectMatching(filter,alreadyChecked).
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
    protected abstract OutputType[] getOutputTypesFor(CompoundType topType,
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
                                                      HashSet alreadyChecked);
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
     * Return the file name extension for the given file name (e.g. ".java").
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
     * All files generated with the ".java" extension will be compiled. To
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
     * change this behavior for ".java" files, override the compileJavaSourceFile
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
     * method to return false.
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
     * @param outputType One of the items returned by getOutputTypesFor(...)
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
    protected abstract String getFileNameExtensionFor(OutputType outputType);
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
     * Write the output for the given OutputFileName into the output stream.
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
     * @param name One of the items returned by getOutputTypesFor(...)
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
     * @param alreadyChecked A set of Types which have already been checked.
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
     *  Intended to be passed to Type.collectMatching(filter,alreadyChecked).
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
     * @param writer The output stream.
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
    protected abstract void writeOutputFor(OutputType outputType,
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
                                                HashSet alreadyChecked,
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
                                                IndentingWriter writer) throws IOException;
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
     * Return true if a new instance should be created for each
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
     * class on the command line. Subclasses which return true
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
     * should override newInstance() to return an appropriately
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
     * constructed instance.
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
    protected abstract boolean requireNewInstance();
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
     * Return true if the specified file needs generation.
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
    public boolean requiresGeneration (File target, Type theType) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
        boolean result = alwaysGenerate;
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
        if (!result) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
            // Get a ClassFile instance for base source or class
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
            // file.  We use ClassFile so that if the base is in
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
            // a zip file, we can still get at it's mod time...
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
            ClassFile baseFile;
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
            ClassPath path = env.getClassPath();
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
            String className = theType.getQualifiedName().replace('.',File.separatorChar);
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
            // First try the source file...
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
            baseFile = path.getFile(className + ".source");
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
            if (baseFile == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
                // Then try class file...
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
                baseFile = path.getFile(className + ".class");
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
            // Do we have a baseFile?
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
            if (baseFile != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
                // Yes, grab baseFile's mod time...
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
                long baseFileMod = baseFile.lastModified();
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
                // Get a File instance for the target. If it is a source
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
                // file, create a class file instead since the source file
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
                // will frequently be deleted...
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
                String targetName = IDLNames.replace(target.getName(),".java",".class");
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
                String parentPath = target.getParent();
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
                File targetFile = new File(parentPath,targetName);
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
                // Does the target file exist?
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
                if (targetFile.exists()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
                    // Yes, so grab it's mod time...
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
                    long targetFileMod = targetFile.lastModified();
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
                    // Set result...
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
                    result = targetFileMod < baseFileMod;
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
                } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
                    // No, so we must generate...
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
                    result = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
                // No, so we must generate...
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
                result = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
        return result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
     * Create and return a new instance of self. Subclasses
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
     * which need to do something other than default construction
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
     * must override this method.
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
    protected Generator newInstance() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
        Generator result = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
        try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
            result = (Generator) getClass().newInstance();
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
        catch (Exception e){} // Should ALWAYS work!
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
        return result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
     * Default constructor for subclasses to use.
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
    protected Generator() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
     * Generate output. Any source files created which need compilation should
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
     * be added to the compiler environment using the addGeneratedFile(File)
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
     * method.
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
     * @param env       The compiler environment
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
     * @param cdef      The definition for the implementation class or interface from
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
     *              which to generate output
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
     * @param destDir   The directory for the root of the package hierarchy
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
     *                          for generated files. May be null.
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
    public void generate(sun.rmi.rmic.BatchEnvironment env, ClassDefinition cdef, File destDir) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
        this.env = (BatchEnvironment) env;
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
        contextStack = new ContextStack(this.env);
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
        contextStack.setTrace(trace);
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
        // Make sure the environment knows whether or not to parse
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
        // non-conforming types. This will clear out any previously
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
        // parsed types if necessary...
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
        this.env.setParseNonConforming(parseNonConforming(contextStack));
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
        // Get our top level type...
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
        CompoundType topType = getTopType(cdef,contextStack);
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
        if (topType != null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
            Generator generator = this;
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
            // Do we need to make a new instance?
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
            if (requireNewInstance()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
                                // Yes, so make one.  'this' instance is the one instantiated by Main
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
                                // and which knows any needed command line args...
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
                generator = newInstance();
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
            // Now generate all output files...
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
            generator.generateOutputFiles(topType, this.env, destDir);
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
     * Create and return a new instance of self. Subclasses
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
     * which need to do something other than default construction
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
     * must override this method.
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
    protected void generateOutputFiles (CompoundType topType,
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
                                        BatchEnvironment env,
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
                                        File destDir) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
        // Grab the 'alreadyChecked' HashSet from the environment...
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
        HashSet alreadyChecked = env.alreadyChecked;
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
        // Ask subclass for a list of output types...
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
        OutputType[] types = getOutputTypesFor(topType,alreadyChecked);
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
        // Process each file...
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
        for (int i = 0; i < types.length; i++) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
            OutputType current = types[i];
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
            String className = current.getName();
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
            File file = getFileFor(current,destDir);
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
            boolean sourceFile = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
            // Do we need to generate this file?
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
            if (requiresGeneration(file,current.getType())) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
                // Yes. If java source file, add to environment so will be compiled...
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
                if (file.getName().endsWith(".java")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
                    sourceFile = compileJavaSourceFile(current);
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
                                // Are we supposeded to compile this one?
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
                    if (sourceFile) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
                        env.addGeneratedFile(file);
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
                // Now create an output stream and ask subclass to fill it up...
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
                   IndentingWriter out = new IndentingWriter(
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
                                                              new OutputStreamWriter(new FileOutputStream(file)),INDENT_STEP,TAB_SIZE);
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
                    long startTime = 0;
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
                    if (env.verbose()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
                        startTime = System.currentTimeMillis();
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
                    writeOutputFor(types[i],alreadyChecked,out);
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
                    out.close();
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
                    if (env.verbose()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
                        long duration = System.currentTimeMillis() - startTime;
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
                        env.output(Main.getText("rmic.generated", file.getPath(), Long.toString(duration)));
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
                    if (sourceFile) {
27568
55efe677dc75 8049367: Modular Run-Time Images
chegar
parents: 25862
diff changeset
   355
                        env.parseFile(ClassFile.newClassFile(file));
4
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
                    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
                } catch (IOException e) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
                    env.error(0, "cant.write", file.toString());
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
                    return;
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
                // No, say so if we need to...
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
                if (env.verbose()) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
                    env.output(Main.getText("rmic.previously.generated", file.getPath()));
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   373
     * Return the File object that should be used as the output file
02bb8761fcce Initial load
duke
parents:
diff changeset
   374
     * for the given OutputType.
02bb8761fcce Initial load
duke
parents:
diff changeset
   375
     * @param outputType The type to create a file for.
02bb8761fcce Initial load
duke
parents:
diff changeset
   376
     * @param destinationDir The directory to use as the root of the
02bb8761fcce Initial load
duke
parents:
diff changeset
   377
     * package heirarchy.  May be null, in which case the current
02bb8761fcce Initial load
duke
parents:
diff changeset
   378
     * classpath is searched to find the directory in which to create
02bb8761fcce Initial load
duke
parents:
diff changeset
   379
     * the output file.  If that search fails (most likely because the
02bb8761fcce Initial load
duke
parents:
diff changeset
   380
     * package directory lives in a zip or jar file rather than the
02bb8761fcce Initial load
duke
parents:
diff changeset
   381
     * file system), the current user directory is used.
02bb8761fcce Initial load
duke
parents:
diff changeset
   382
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   383
    protected File getFileFor(OutputType outputType, File destinationDir) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   384
        // Calling this method does some crucial initialization
02bb8761fcce Initial load
duke
parents:
diff changeset
   385
        // in a subclass implementation. Don't skip it.
02bb8761fcce Initial load
duke
parents:
diff changeset
   386
        Identifier id = getOutputId(outputType);
02bb8761fcce Initial load
duke
parents:
diff changeset
   387
        File packageDir = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   388
        if(idl){
02bb8761fcce Initial load
duke
parents:
diff changeset
   389
            packageDir = Util.getOutputDirectoryForIDL(id,destinationDir,env);
02bb8761fcce Initial load
duke
parents:
diff changeset
   390
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   391
            packageDir = Util.getOutputDirectoryForStub(id,destinationDir,env);
02bb8761fcce Initial load
duke
parents:
diff changeset
   392
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   393
        String classFileName = outputType.getName() + getFileNameExtensionFor(outputType);
02bb8761fcce Initial load
duke
parents:
diff changeset
   394
        return new File(packageDir, classFileName);
02bb8761fcce Initial load
duke
parents:
diff changeset
   395
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   396
02bb8761fcce Initial load
duke
parents:
diff changeset
   397
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   398
     * Return an identifier to use for output.
02bb8761fcce Initial load
duke
parents:
diff changeset
   399
     * @param outputType the type for which output is to be generated.
02bb8761fcce Initial load
duke
parents:
diff changeset
   400
     * @return the new identifier. This implementation returns the input parameter.
02bb8761fcce Initial load
duke
parents:
diff changeset
   401
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   402
    protected Identifier getOutputId (OutputType outputType) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   403
        return outputType.getType().getIdentifier();
02bb8761fcce Initial load
duke
parents:
diff changeset
   404
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   405
02bb8761fcce Initial load
duke
parents:
diff changeset
   406
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   407
     * Return true if the given file should be compiled.
02bb8761fcce Initial load
duke
parents:
diff changeset
   408
     * @param outputType One of the items returned by getOutputTypesFor(...) for
02bb8761fcce Initial load
duke
parents:
diff changeset
   409
     *   which getFileNameExtensionFor(OutputType) returned ".java".
02bb8761fcce Initial load
duke
parents:
diff changeset
   410
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   411
    protected boolean compileJavaSourceFile (OutputType outputType) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   412
        return true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   413
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   414
02bb8761fcce Initial load
duke
parents:
diff changeset
   415
    //_____________________________________________________________________
02bb8761fcce Initial load
duke
parents:
diff changeset
   416
    // OutputType is a simple wrapper for a name and a Type
02bb8761fcce Initial load
duke
parents:
diff changeset
   417
    //_____________________________________________________________________
02bb8761fcce Initial load
duke
parents:
diff changeset
   418
02bb8761fcce Initial load
duke
parents:
diff changeset
   419
    public class OutputType {
02bb8761fcce Initial load
duke
parents:
diff changeset
   420
        private String name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   421
        private Type type;
02bb8761fcce Initial load
duke
parents:
diff changeset
   422
02bb8761fcce Initial load
duke
parents:
diff changeset
   423
        public OutputType (String name, Type type) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   424
            this.name = name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   425
            this.type = type;
02bb8761fcce Initial load
duke
parents:
diff changeset
   426
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   427
02bb8761fcce Initial load
duke
parents:
diff changeset
   428
        public String getName() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   429
            return name;
02bb8761fcce Initial load
duke
parents:
diff changeset
   430
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   431
02bb8761fcce Initial load
duke
parents:
diff changeset
   432
        public Type getType() {
02bb8761fcce Initial load
duke
parents:
diff changeset
   433
            return type;
02bb8761fcce Initial load
duke
parents:
diff changeset
   434
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   435
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   436
}