duke@1: /* ohair@554: * Copyright (c) 2002, 2008, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: duke@1: package com.sun.tools.javah; duke@1: jjg@416: import java.io.PrintWriter; jjg@416: import java.text.MessageFormat; jjg@416: import java.util.Locale; duke@1: import java.util.ResourceBundle; duke@1: import java.util.MissingResourceException; jjg@416: import javax.tools.Diagnostic; jjg@416: import javax.tools.Diagnostic.Kind; jjg@416: import javax.tools.DiagnosticListener; jjg@416: import javax.tools.JavaFileObject; duke@1: duke@1: /** duke@1: * Messages, verbose and error handling support. duke@1: * duke@1: * For errors, the failure modes are: duke@1: * error -- User did something wrong duke@1: * bug -- Bug has occurred in javah duke@1: * fatal -- We can't even find resources, so bail fast, don't localize duke@1: * jjg@416: *

This is NOT part of any API supported by Sun Microsystems. jjg@416: * If you write code that depends on this, you do so at your own jjg@416: * risk. This code and its internal interfaces are subject to change jjg@416: * or deletion without notice.

duke@1: */ duke@1: public class Util { jjg@416: /** Exit is used to replace the use of System.exit in the original javah. jjg@416: */ jjg@416: public static class Exit extends Error { jjg@416: private static final long serialVersionUID = 430820978114067221L; jjg@416: Exit(int exitValue) { jjg@416: this(exitValue, null); jjg@416: } jjg@416: jjg@416: Exit(int exitValue, Throwable cause) { jjg@416: super(cause); jjg@416: this.exitValue = exitValue; jjg@416: this.cause = cause; jjg@416: } jjg@416: jjg@416: Exit(Exit e) { jjg@416: this(e.exitValue, e.cause); jjg@416: } jjg@416: jjg@416: public final int exitValue; jjg@416: public final Throwable cause; jjg@416: } duke@1: duke@1: /* duke@1: * Help for verbosity. duke@1: */ jjg@416: public boolean verbose = false; duke@1: jjg@416: public PrintWriter log; jjg@416: public DiagnosticListener dl; jjg@416: jjg@416: Util(PrintWriter log, DiagnosticListener dl) { jjg@416: this.log = log; jjg@416: this.dl = dl; jjg@416: } jjg@416: jjg@416: public void log(String s) { jjg@416: log.println(s); duke@1: } duke@1: duke@1: duke@1: /* duke@1: * Help for loading localized messages. duke@1: */ jjg@416: private ResourceBundle m; duke@1: jjg@416: private void initMessages() throws Exit { duke@1: try { jjg@416: m = ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n"); duke@1: } catch (MissingResourceException mre) { duke@1: fatal("Error loading resources. Please file a bug report.", mre); duke@1: } duke@1: } duke@1: jjg@416: private String getText(String key, Object... args) throws Exit { duke@1: if (m == null) duke@1: initMessages(); duke@1: try { jjg@416: return MessageFormat.format(m.getString(key), args); duke@1: } catch (MissingResourceException e) { duke@1: fatal("Key " + key + " not found in resources.", e); duke@1: } duke@1: return null; /* dead code */ duke@1: } duke@1: duke@1: /* duke@1: * Usage message. duke@1: */ jjg@416: public void usage() throws Exit { jjg@416: log.println(getText("usage")); duke@1: } duke@1: jjg@416: public void version() throws Exit { jjg@416: log.println(getText("javah.version", duke@1: System.getProperty("java.version"), null)); duke@1: } duke@1: duke@1: /* duke@1: * Failure modes. duke@1: */ jjg@416: public void bug(String key) throws Exit { duke@1: bug(key, null); duke@1: } duke@1: jjg@416: public void bug(String key, Exception e) throws Exit { jjg@416: dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key)); jjg@416: dl.report(createDiagnostic(Diagnostic.Kind.NOTE, "bug.report")); jjg@416: throw new Exit(11, e); duke@1: } duke@1: jjg@416: public void error(String key, Object... args) throws Exit { jjg@416: dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key, args)); jjg@416: throw new Exit(15); duke@1: } duke@1: jjg@416: private void fatal(String msg) throws Exit { duke@1: fatal(msg, null); duke@1: } duke@1: jjg@416: private void fatal(String msg, Exception e) throws Exit { jjg@416: dl.report(createDiagnostic(Diagnostic.Kind.ERROR, "", msg)); jjg@416: throw new Exit(10, e); duke@1: } duke@1: jjg@416: private Diagnostic createDiagnostic( jjg@416: final Diagnostic.Kind kind, final String code, final Object... args) { jjg@416: return new Diagnostic() { jjg@416: public String getCode() { jjg@416: return code; jjg@416: } jjg@416: public long getColumnNumber() { jjg@416: return Diagnostic.NOPOS; jjg@416: } jjg@416: public long getEndPosition() { jjg@416: return Diagnostic.NOPOS; jjg@416: } jjg@416: public Kind getKind() { jjg@416: return kind; jjg@416: } jjg@416: public long getLineNumber() { jjg@416: return Diagnostic.NOPOS; jjg@416: } jjg@416: public String getMessage(Locale locale) { jjg@416: if (code.length() == 0) jjg@416: return (String) args[0]; jjg@416: return getText(code, args); // FIXME locale jjg@416: } jjg@416: public long getPosition() { jjg@416: return Diagnostic.NOPOS; jjg@416: } jjg@416: public JavaFileObject getSource() { jjg@416: return null; jjg@416: } jjg@416: public long getStartPosition() { jjg@416: return Diagnostic.NOPOS; jjg@416: } jjg@416: }; duke@1: } duke@1: }