884 lines · 29.4 KB · Java
RawDownload
/* * Copyright (c) 1996, 2007, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * Licensed Materials - Property of IBM * RMI-IIOP v1.0 * Copyright IBM Corp. 1998 1999  All Rights Reserved * */ package sun.rmi.rmic; import java.util.Vector;import java.util.Enumeration;import java.util.ResourceBundle;import java.util.StringTokenizer;import java.util.MissingResourceException; import java.io.OutputStream;import java.io.PrintStream;import java.io.IOException;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.ByteArrayOutputStream; import sun.tools.java.ClassFile;import sun.tools.java.ClassDefinition;import sun.tools.java.ClassDeclaration;import sun.tools.java.ClassNotFound;import sun.tools.java.Identifier;import sun.tools.java.ClassPath; import sun.tools.javac.SourceClass;import sun.tools.util.CommandLine;import java.lang.reflect.Constructor;import java.util.Properties; /** * Main "rmic" program. * * WARNING: The contents of this source file are not part of any * supported API.  Code that depends on them does so at its own risk: * they are subject to change or removal without notice. */public class Main implements sun.rmi.rmic.Constants {    String sourcePathArg;    String sysClassPathArg;    String extDirsArg;    String classPathString;    File destDir;    int flags;    long tm;    Vector classes;    boolean nowrite;    boolean nocompile;    boolean keepGenerated;    boolean status;    String[] generatorArgs;    Vector generators;    Class environmentClass = BatchEnvironment.class;    boolean iiopGeneration = false;     /**     * Name of the program.     */    String program;     /**     * The stream where error message are printed.     */    OutputStream out;     /**     * Constructor.     */    public Main(OutputStream out, String program) {        this.out = out;        this.program = program;    }     /**     * Output a message.     */    public void output(String msg) {        PrintStream out =            this.out instanceof PrintStream ? (PrintStream)this.out            : new PrintStream(this.out, true);        out.println(msg);    }     /**     * Top level error message.  This method is called when the     * environment could not be set up yet.     */    public void error(String msg) {        output(getText(msg));    }     public void error(String msg, String arg1) {        output(getText(msg, arg1));    }     public void error(String msg, String arg1, String arg2) {        output(getText(msg, arg1, arg2));    }     /**     * Usage     */    public void usage() {        error("rmic.usage", program);    }     /**     * Run the compiler     */    public synchronized boolean compile(String argv[]) {         /*         * Handle internal option to use the new (and incomplete) rmic         * implementation.  This option is handled here, rather than         * in parseArgs, so that none of the arguments will be nulled         * before delegating to the new implementation.         */        for (int i = 0; i < argv.length; i++) {            if (argv[i].equals("-Xnew")) {                return (new sun.rmi.rmic.newrmic.Main(out,                                                      program)).compile(argv);            }        }         if (!parseArgs(argv)) {            return false;        }         if (classes.size() == 0) {            usage();            return false;        }         return doCompile();    }     /**     * Get the destination directory.     */    public File getDestinationDir() {        return destDir;    }     /**     * Parse the arguments for compile.     */    public boolean parseArgs(String argv[]) {        sourcePathArg = null;        sysClassPathArg = null;        extDirsArg = null;         classPathString = null;        destDir = null;        flags = F_WARNINGS;        tm = System.currentTimeMillis();        classes = new Vector();        nowrite = false;        nocompile = false;        keepGenerated = false;        generatorArgs = getArray("generator.args",true);        if (generatorArgs == null) {            return false;        }        generators = new Vector();         // Pre-process command line for @file arguments        try {            argv = CommandLine.parse(argv);        } catch (FileNotFoundException e) {            error("rmic.cant.read", e.getMessage());            return false;        } catch (IOException e) {            e.printStackTrace(out instanceof PrintStream ?                              (PrintStream) out :                              new PrintStream(out, true));            return false;        }         // Parse arguments        for (int i = 0 ; i < argv.length ; i++) {            if (argv[i] != null) {                if (argv[i].equals("-g")) {                    flags &= ~F_OPT;                    flags |= F_DEBUG_LINES | F_DEBUG_VARS;                    argv[i] = null;                } else if (argv[i].equals("-O")) {                    flags &= ~F_DEBUG_LINES;                    flags &= ~F_DEBUG_VARS;                    flags |= F_OPT | F_DEPENDENCIES;                    argv[i] = null;                } else if (argv[i].equals("-nowarn")) {                    flags &= ~F_WARNINGS;                    argv[i] = null;                } else if (argv[i].equals("-debug")) {                    flags |= F_DUMP;                    argv[i] = null;                } else if (argv[i].equals("-depend")) {                    flags |= F_DEPENDENCIES;                    argv[i] = null;                } else if (argv[i].equals("-verbose")) {                    flags |= F_VERBOSE;                    argv[i] = null;                } else if (argv[i].equals("-nowrite")) {                    nowrite = true;                    argv[i] = null;                } else if (argv[i].equals("-Xnocompile")) {                    nocompile = true;                    keepGenerated = true;                    argv[i] = null;                } else if (argv[i].equals("-keep") ||                           argv[i].equals("-keepgenerated")) {                    keepGenerated = true;                    argv[i] = null;                } else if (argv[i].equals("-show")) {                    error("rmic.option.unsupported", "-show");                    usage();                    return false;                } else if (argv[i].equals("-classpath")) {                    if ((i + 1) < argv.length) {                        if (classPathString != null) {                            error("rmic.option.already.seen", "-classpath");                            usage();                            return false;                        }                        argv[i] = null;                        classPathString = argv[++i];                        argv[i] = null;                    } else {                        error("rmic.option.requires.argument", "-classpath");                        usage();                        return false;                    }                } else if (argv[i].equals("-sourcepath")) {                    if ((i + 1) < argv.length) {                        if (sourcePathArg != null) {                            error("rmic.option.already.seen", "-sourcepath");                            usage();                            return false;                        }                        argv[i] = null;                        sourcePathArg = argv[++i];                        argv[i] = null;                    } else {                        error("rmic.option.requires.argument", "-sourcepath");                        usage();                        return false;                    }                } else if (argv[i].equals("-bootclasspath")) {                    if ((i + 1) < argv.length) {                        if (sysClassPathArg != null) {                            error("rmic.option.already.seen", "-bootclasspath");                            usage();                            return false;                        }                        argv[i] = null;                        sysClassPathArg = argv[++i];                        argv[i] = null;                    } else {                        error("rmic.option.requires.argument", "-bootclasspath");                        usage();                        return false;                    }                } else if (argv[i].equals("-extdirs")) {                    if ((i + 1) < argv.length) {                        if (extDirsArg != null) {                            error("rmic.option.already.seen", "-extdirs");                            usage();                            return false;                        }                        argv[i] = null;                        extDirsArg = argv[++i];                        argv[i] = null;                    } else {                        error("rmic.option.requires.argument", "-extdirs");                        usage();                        return false;                    }                } else if (argv[i].equals("-d")) {                    if ((i + 1) < argv.length) {                        if (destDir != null) {                            error("rmic.option.already.seen", "-d");                            usage();                            return false;                        }                        argv[i] = null;                        destDir = new File(argv[++i]);                        argv[i] = null;                        if (!destDir.exists()) {                            error("rmic.no.such.directory", destDir.getPath());                            usage();                            return false;                        }                    } else {                        error("rmic.option.requires.argument", "-d");                        usage();                        return false;                    }                } else {                    if (!checkGeneratorArg(argv,i)) {                        usage();                        return false;                    }                }            }        }          // Now that all generators have had a chance at the args,        // scan what's left for classes and illegal args...         for (int i = 0; i < argv.length; i++) {            if (argv[i] != null) {                if (argv[i].startsWith("-")) {                    error("rmic.no.such.option", argv[i]);                    usage();                    return false;                } else {                    classes.addElement(argv[i]);                }            }        }          // If the generators vector is empty, add the default generator...         if (generators.size() == 0) {            addGenerator("default");        }         return true;    }     /**     * If this argument is for a generator, instantiate it, call     * parseArgs(...) and add generator to generators vector.     * Returns false on error.     */    protected boolean checkGeneratorArg(String[] argv, int currentIndex) {        boolean result = true;        if (argv[currentIndex].startsWith("-")) {            String arg = argv[currentIndex].substring(1).toLowerCase(); // Remove '-'            for (int i = 0; i < generatorArgs.length; i++) {                if (arg.equalsIgnoreCase(generatorArgs[i])) {                    // Got a match, add Generator and call parseArgs...                    Generator gen = addGenerator(arg);                    if (gen == null) {                        return false;                    }                    result = gen.parseArgs(argv,this);                    break;                }            }        }        return result;    }     /**     * Instantiate and add a generator to the generators array.     */    protected Generator addGenerator(String arg) {         Generator gen;         // Create an instance of the generator and add it to        // the array...         String className = getString("generator.class." + arg);        if (className == null) {            error("rmic.missing.property",arg);            return null;        }         try {            gen = (Generator) Class.forName(className).newInstance();        } catch (Exception e) {            error("rmic.cannot.instantiate",className);            return null;        }         generators.addElement(gen);         // Get the environment required by this generator...         Class envClass = BatchEnvironment.class;        String env = getString("generator.env." + arg);        if (env != null) {            try {                envClass = Class.forName(env);                 // Is the new class a subclass of the current one?                 if (environmentClass.isAssignableFrom(envClass)) {                     // Yes, so switch to the new one...                     environmentClass = envClass;                 } else {                     // No. Is the current class a subclass of the                    // new one?                     if (!envClass.isAssignableFrom(environmentClass)) {                         // No, so it's a conflict...                         error("rmic.cannot.use.both",environmentClass.getName(),envClass.getName());                        return null;                    }                }            } catch (ClassNotFoundException e) {                error("rmic.class.not.found",env);                return null;            }        }         // If this is the iiop stub generator, cache        // that fact for the jrmp generator...         if (arg.equals("iiop")) {            iiopGeneration = true;        }        return gen;    }     /**     * Grab a resource string and parse it into an array of strings. Assumes     * comma separated list.     * @param name The resource name.     * @param mustExist If true, throws error if resource does not exist. If     * false and resource does not exist, returns zero element array.     */    protected String[] getArray(String name, boolean mustExist) {        String[] result = null;        String value = getString(name);        if (value == null) {            if (mustExist) {                error("rmic.resource.not.found",name);                return null;            } else {                return new String[0];            }        }         StringTokenizer parser = new StringTokenizer(value,", \t\n\r", false);        int count = parser.countTokens();        result = new String[count];        for (int i = 0; i < count; i++) {            result[i] = parser.nextToken();        }         return result;    }     /**     * Get the correct type of BatchEnvironment     */    public BatchEnvironment getEnv() {         ClassPath classPath =            BatchEnvironment.createClassPath(classPathString,                                             sysClassPathArg,                                             extDirsArg);        BatchEnvironment result = null;        try {            Class[] ctorArgTypes = {OutputStream.class,ClassPath.class,Main.class};            Object[] ctorArgs = {out,classPath,this};            Constructor constructor = environmentClass.getConstructor(ctorArgTypes);            result = (BatchEnvironment) constructor.newInstance(ctorArgs);            result.reset();

Lines 1–500 of 884.