All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.redhat.ceylon.langtools.tools.javac.main.Main Maven / Gradle / Ivy

There is a newer version: 1.3.3
Show newest version
/*
 * Copyright (c) 1999, 2011, 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.
 */

package com.redhat.ceylon.langtools.tools.javac.main;

import static com.redhat.ceylon.langtools.tools.javac.main.OptionName.*;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.security.DigestInputStream;
import java.security.MessageDigest;
import java.util.MissingResourceException;

import com.redhat.ceylon.javax.annotation.processing.Processor;
import com.redhat.ceylon.javax.tools.JavaFileManager;
import com.redhat.ceylon.javax.tools.JavaFileObject;
import com.redhat.ceylon.langtools.tools.javac.code.Source;
import com.redhat.ceylon.langtools.tools.javac.file.CacheFSInfo;
import com.redhat.ceylon.langtools.tools.javac.file.JavacFileManager;
import com.redhat.ceylon.langtools.tools.javac.jvm.Target;
import com.redhat.ceylon.langtools.tools.javac.main.JavacOption.Option;
import com.redhat.ceylon.langtools.tools.javac.main.RecognizedOptions.OptionHelper;
import com.redhat.ceylon.langtools.tools.javac.processing.AnnotationProcessingError;
import com.redhat.ceylon.langtools.tools.javac.util.*;

/** This class provides a commandline interface to the GJC compiler.
 *
 *  

This is NOT part of any supported API. * If you write code that depends on this, you do so at your own risk. * This code and its internal interfaces are subject to change or * deletion without notice. */ public class Main { /** The name of the compiler, for use in diagnostics. */ String ownName; /** The writer to use for diagnostic output. */ PrintWriter out; /** * If true, certain errors will cause an exception, such as command line * arg errors, or exceptions in user provided code. */ boolean apiMode; /** Result codes. */ static final int EXIT_OK = 0, // Compilation completed with no errors. EXIT_ERROR = 1, // Completed but reported errors. EXIT_CMDERR = 2, // Bad command-line arguments EXIT_SYSERR = 3, // System error or resource exhaustion. EXIT_ABNORMAL = 4; // Compiler terminated abnormally private Option[] recognizedOptions = RecognizedOptions.getJavaCompilerOptions(new OptionHelper() { public void setOut(PrintWriter out) { Main.this.out = out; } public void error(String key, Object... args) { Main.this.error(key, args); } public void printVersion() { Log.printLines(out, getLocalizedString("version", ownName, JavaCompiler.version())); } public void printFullVersion() { Log.printLines(out, getLocalizedString("fullVersion", ownName, JavaCompiler.fullVersion())); } public void printHelp() { help(); } public void printJhelp() { help(); } public void printXhelp() { xhelp(); } public void addFile(File f) { if (!filenames.contains(f)) filenames.append(f); } public void addClassName(String s) { classnames.append(s); } }); /** * Construct a compiler instance. */ public Main(String name) { this(name, new PrintWriter(System.err, true)); } /** * Construct a compiler instance. */ public Main(String name, PrintWriter out) { this.ownName = name; this.out = out; } /** A table of all options that's passed to the JavaCompiler constructor. */ private Options options = null; /** The list of source files to process */ public ListBuffer filenames = null; // XXX sb protected /** List of class files names passed on the command line */ public ListBuffer classnames = null; // XXX sb protected /** Print a string that explains usage. */ protected void help() { Log.printLines(out, getLocalizedString("msg.usage.header", ownName)); for (int i=0; i processArgs(String[] flags) { // XXX sb protected int ac = 0; while (ac < flags.length) { String flag = flags[ac]; ac++; Option option = null; if (flag.length() > 0) { // quick hack to speed up file processing: // if the option does not begin with '-', there is no need to check // most of the compiler options. int firstOptionToCheck = flag.charAt(0) == '-' ? 0 : recognizedOptions.length-1; for (int j=firstOptionToCheck; jnil(), null); } /** Programmatic interface for main function. * @param args The command line parameters. */ public int compile(String[] args, Context context, List fileObjects, Iterable processors) { if (options == null) options = Options.instance(context); // creates a new one filenames = new ListBuffer(); classnames = new ListBuffer(); JavaCompiler comp = null; /* * TODO: Logic below about what is an acceptable command line * should be updated to take annotation processing semantics * into account. */ try { if (args.length == 0 && fileObjects.isEmpty()) { help(); return EXIT_CMDERR; } List files; try { files = processArgs(CommandLine.parse(args)); if (files == null) { // null signals an error in options, abort return EXIT_CMDERR; } else if (files.isEmpty() && fileObjects.isEmpty() && classnames.isEmpty()) { // it is allowed to compile nothing if just asking for help or version info if (options.isSet(HELP) || options.isSet(X) || options.isSet(VERSION) || options.isSet(FULLVERSION)) return EXIT_OK; if (JavaCompiler.explicitAnnotationProcessingRequested(options)) { error("err.no.source.files.classes"); } else { error("err.no.source.files"); } return EXIT_CMDERR; } } catch (java.io.FileNotFoundException e) { Log.printLines(out, ownName + ": " + getLocalizedString("err.file.not.found", e.getMessage())); return EXIT_SYSERR; } boolean forceStdOut = options.isSet("stdout"); if (forceStdOut) { out.flush(); out = new PrintWriter(System.out, true); } context.put(Log.outKey, out); // allow System property in following line as a Mustang legacy boolean batchMode = (options.isUnset("nonBatchMode") && System.getProperty("nonBatchMode") == null); if (batchMode) CacheFSInfo.preRegister(context); fileManager = context.get(JavaFileManager.class); comp = JavaCompiler.instance(context); if (comp == null) return EXIT_SYSERR; Log log = Log.instance(context); if (!files.isEmpty()) { // add filenames to fileObjects comp = JavaCompiler.instance(context); List otherFiles = List.nil(); JavacFileManager dfm = (JavacFileManager)fileManager; for (JavaFileObject fo : dfm.getJavaFileObjectsFromFiles(files)) otherFiles = otherFiles.prepend(fo); for (JavaFileObject fo : otherFiles) fileObjects = fileObjects.prepend(fo); } comp.compile(fileObjects, classnames.toList(), processors); if (log.expectDiagKeys != null) { if (log.expectDiagKeys.isEmpty()) { Log.printLines(log.noticeWriter, "all expected diagnostics found"); return EXIT_OK; } else { Log.printLines(log.noticeWriter, "expected diagnostic keys not found: " + log.expectDiagKeys); return EXIT_ERROR; } } if (comp.errorCount() != 0) return EXIT_ERROR; } catch (IOException ex) { ioMessage(ex); return EXIT_SYSERR; } catch (OutOfMemoryError ex) { resourceMessage(ex); return EXIT_SYSERR; } catch (StackOverflowError ex) { resourceMessage(ex); return EXIT_SYSERR; } catch (FatalError ex) { feMessage(ex); return EXIT_SYSERR; } catch (AnnotationProcessingError ex) { if (apiMode) throw new RuntimeException(ex.getCause()); apMessage(ex); return EXIT_SYSERR; } catch (ClientCodeException ex) { // as specified by javax.tools.JavaCompiler#getTask // and javax.tools.JavaCompiler.CompilationTask#call throw new RuntimeException(ex.getCause()); } catch (PropagatedException ex) { throw ex.getCause(); } catch (Throwable ex) { // Nasty. If we've already reported an error, compensate // for buggy compiler error recovery by swallowing thrown // exceptions. if (comp == null || comp.errorCount() == 0 || options == null || options.isSet("dev")) bugMessage(ex); return EXIT_ABNORMAL; } finally { if (comp != null) { try { comp.close(); } catch (ClientCodeException ex) { throw new RuntimeException(ex.getCause()); } } filenames = null; options = null; } return EXIT_OK; } /** Print a message reporting an internal error. */ void bugMessage(Throwable ex) { Log.printLines(out, getLocalizedString("msg.bug", JavaCompiler.version())); ex.printStackTrace(out); } /** Print a message reporting a fatal error. */ void feMessage(Throwable ex) { Log.printLines(out, ex.getMessage()); if (ex.getCause() != null && options.isSet("dev")) { ex.getCause().printStackTrace(out); } } /** Print a message reporting an input/output error. */ void ioMessage(Throwable ex) { Log.printLines(out, getLocalizedString("msg.io")); ex.printStackTrace(out); } /** Print a message reporting an out-of-resources error. */ void resourceMessage(Throwable ex) { Log.printLines(out, getLocalizedString("msg.resource")); // System.out.println("(name buffer len = " + Name.names.length + " " + Name.nc);//DEBUG ex.printStackTrace(out); } /** Print a message reporting an uncaught exception from an * annotation processor. */ void apMessage(AnnotationProcessingError ex) { Log.printLines(out, getLocalizedString("msg.proc.annotation.uncaught.exception")); ex.getCause().printStackTrace(out); } /** Display the location and checksum of a class. */ void showClass(String className) { out.println("javac: show class: " + className); URL url = getClass().getResource('/' + className.replace('.', '/') + ".class"); if (url == null) out.println(" class not found"); else { out.println(" " + url); try { final String algorithm = "MD5"; byte[] digest; MessageDigest md = MessageDigest.getInstance(algorithm); DigestInputStream in = new DigestInputStream(url.openStream(), md); try { byte[] buf = new byte[8192]; int n; do { n = in.read(buf); } while (n > 0); digest = md.digest(); } finally { in.close(); } StringBuilder sb = new StringBuilder(); for (byte b: digest) sb.append(String.format("%02x", b)); out.println(" " + algorithm + " checksum: " + sb); } catch (Exception e) { out.println(" cannot compute digest: " + e); } } } private JavaFileManager fileManager; /* ************************************************************************ * Internationalization *************************************************************************/ /** Find a localized string in the resource bundle. * @param key The key for the localized string. */ public static String getLocalizedString(String key, Object... args) { // FIXME sb private try { if (messages == null) messages = new JavacMessages(javacBundleName); return messages.getLocalizedString("javac." + key, args); } catch (MissingResourceException e) { throw new Error("Fatal Error: Resource for javac is missing", e); } } public static void useRawMessages(boolean enable) { if (enable) { messages = new JavacMessages(javacBundleName) { @Override public String getLocalizedString(String key, Object... args) { return key; } }; } else { messages = new JavacMessages(javacBundleName); } } private static final String javacBundleName = "com.redhat.ceylon.langtools.tools.javac.resources.ceylonc"; private static JavacMessages messages; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy