0N/A/*
553N/A * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage com.sun.tools.javah;
0N/A
415N/Aimport java.io.PrintWriter;
415N/Aimport java.text.MessageFormat;
415N/Aimport java.util.Locale;
0N/Aimport java.util.ResourceBundle;
0N/Aimport java.util.MissingResourceException;
415N/Aimport javax.tools.Diagnostic;
415N/Aimport javax.tools.Diagnostic.Kind;
415N/Aimport javax.tools.DiagnosticListener;
415N/Aimport javax.tools.JavaFileObject;
0N/A
0N/A/**
0N/A * Messages, verbose and error handling support.
0N/A *
0N/A * For errors, the failure modes are:
0N/A * error -- User did something wrong
0N/A * bug -- Bug has occurred in javah
0N/A * fatal -- We can't even find resources, so bail fast, don't localize
0N/A *
580N/A * <p><b>This is NOT part of any supported API.
415N/A * If you write code that depends on this, you do so at your own
415N/A * risk. This code and its internal interfaces are subject to change
415N/A * or deletion without notice.</b></p>
0N/A */
0N/Apublic class Util {
415N/A /** Exit is used to replace the use of System.exit in the original javah.
415N/A */
415N/A public static class Exit extends Error {
415N/A private static final long serialVersionUID = 430820978114067221L;
415N/A Exit(int exitValue) {
415N/A this(exitValue, null);
415N/A }
415N/A
415N/A Exit(int exitValue, Throwable cause) {
415N/A super(cause);
415N/A this.exitValue = exitValue;
415N/A this.cause = cause;
415N/A }
415N/A
415N/A Exit(Exit e) {
415N/A this(e.exitValue, e.cause);
415N/A }
415N/A
415N/A public final int exitValue;
415N/A public final Throwable cause;
415N/A }
0N/A
0N/A /*
0N/A * Help for verbosity.
0N/A */
415N/A public boolean verbose = false;
415N/A
415N/A public PrintWriter log;
415N/A public DiagnosticListener<? super JavaFileObject> dl;
0N/A
415N/A Util(PrintWriter log, DiagnosticListener<? super JavaFileObject> dl) {
415N/A this.log = log;
415N/A this.dl = dl;
415N/A }
415N/A
415N/A public void log(String s) {
415N/A log.println(s);
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Help for loading localized messages.
0N/A */
415N/A private ResourceBundle m;
0N/A
415N/A private void initMessages() throws Exit {
0N/A try {
415N/A m = ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n");
0N/A } catch (MissingResourceException mre) {
0N/A fatal("Error loading resources. Please file a bug report.", mre);
0N/A }
0N/A }
0N/A
415N/A private String getText(String key, Object... args) throws Exit {
0N/A if (m == null)
0N/A initMessages();
0N/A try {
415N/A return MessageFormat.format(m.getString(key), args);
0N/A } catch (MissingResourceException e) {
0N/A fatal("Key " + key + " not found in resources.", e);
0N/A }
0N/A return null; /* dead code */
0N/A }
0N/A
0N/A /*
0N/A * Usage message.
0N/A */
415N/A public void usage() throws Exit {
415N/A log.println(getText("usage"));
0N/A }
0N/A
415N/A public void version() throws Exit {
415N/A log.println(getText("javah.version",
0N/A System.getProperty("java.version"), null));
0N/A }
0N/A
0N/A /*
0N/A * Failure modes.
0N/A */
415N/A public void bug(String key) throws Exit {
0N/A bug(key, null);
0N/A }
0N/A
415N/A public void bug(String key, Exception e) throws Exit {
415N/A dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key));
415N/A dl.report(createDiagnostic(Diagnostic.Kind.NOTE, "bug.report"));
415N/A throw new Exit(11, e);
0N/A }
0N/A
415N/A public void error(String key, Object... args) throws Exit {
415N/A dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key, args));
415N/A throw new Exit(15);
0N/A }
0N/A
415N/A private void fatal(String msg) throws Exit {
0N/A fatal(msg, null);
0N/A }
0N/A
415N/A private void fatal(String msg, Exception e) throws Exit {
415N/A dl.report(createDiagnostic(Diagnostic.Kind.ERROR, "", msg));
415N/A throw new Exit(10, e);
0N/A }
0N/A
415N/A private Diagnostic<JavaFileObject> createDiagnostic(
415N/A final Diagnostic.Kind kind, final String code, final Object... args) {
415N/A return new Diagnostic<JavaFileObject>() {
415N/A public String getCode() {
415N/A return code;
415N/A }
415N/A public long getColumnNumber() {
415N/A return Diagnostic.NOPOS;
415N/A }
415N/A public long getEndPosition() {
415N/A return Diagnostic.NOPOS;
415N/A }
415N/A public Kind getKind() {
415N/A return kind;
415N/A }
415N/A public long getLineNumber() {
415N/A return Diagnostic.NOPOS;
415N/A }
415N/A public String getMessage(Locale locale) {
415N/A if (code.length() == 0)
415N/A return (String) args[0];
415N/A return getText(code, args); // FIXME locale
415N/A }
415N/A public long getPosition() {
415N/A return Diagnostic.NOPOS;
415N/A }
415N/A public JavaFileObject getSource() {
415N/A return null;
415N/A }
415N/A public long getStartPosition() {
415N/A return Diagnostic.NOPOS;
415N/A }
415N/A };
0N/A }
0N/A}