Path: blob/aarch64-shenandoah-jdk8u272-b10/langtools/src/share/classes/com/sun/tools/javah/Util.java
38899 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/242526package com.sun.tools.javah;2728import java.io.PrintWriter;29import java.text.MessageFormat;30import java.util.Locale;31import java.util.ResourceBundle;32import java.util.MissingResourceException;33import javax.tools.Diagnostic;34import javax.tools.Diagnostic.Kind;35import javax.tools.DiagnosticListener;36import javax.tools.JavaFileObject;3738/**39* Messages, verbose and error handling support.40*41* For errors, the failure modes are:42* error -- User did something wrong43* bug -- Bug has occurred in javah44* fatal -- We can't even find resources, so bail fast, don't localize45*46* <p><b>This is NOT part of any supported API.47* If you write code that depends on this, you do so at your own48* risk. This code and its internal interfaces are subject to change49* or deletion without notice.</b></p>50*/51public class Util {52/** Exit is used to replace the use of System.exit in the original javah.53*/54public static class Exit extends Error {55private static final long serialVersionUID = 430820978114067221L;56Exit(int exitValue) {57this(exitValue, null);58}5960Exit(int exitValue, Throwable cause) {61super(cause);62this.exitValue = exitValue;63this.cause = cause;64}6566Exit(Exit e) {67this(e.exitValue, e.cause);68}6970public final int exitValue;71public final Throwable cause;72}7374/*75* Help for verbosity.76*/77public boolean verbose = false;7879public PrintWriter log;80public DiagnosticListener<? super JavaFileObject> dl;8182Util(PrintWriter log, DiagnosticListener<? super JavaFileObject> dl) {83this.log = log;84this.dl = dl;85}8687public void log(String s) {88log.println(s);89}909192/*93* Help for loading localized messages.94*/95private ResourceBundle m;9697private void initMessages() throws Exit {98try {99m = ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n");100} catch (MissingResourceException mre) {101fatal("Error loading resources. Please file a bug report.", mre);102}103}104105private String getText(String key, Object... args) throws Exit {106if (m == null)107initMessages();108try {109return MessageFormat.format(m.getString(key), args);110} catch (MissingResourceException e) {111fatal("Key " + key + " not found in resources.", e);112}113return null; /* dead code */114}115116/*117* Usage message.118*/119public void usage() throws Exit {120log.println(getText("usage"));121}122123public void version() throws Exit {124log.println(getText("javah.version",125System.getProperty("java.version"), null));126}127128/*129* Failure modes.130*/131public void bug(String key) throws Exit {132bug(key, null);133}134135public void bug(String key, Exception e) throws Exit {136dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key));137dl.report(createDiagnostic(Diagnostic.Kind.NOTE, "bug.report"));138throw new Exit(11, e);139}140141public void error(String key, Object... args) throws Exit {142dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key, args));143throw new Exit(15);144}145146private void fatal(String msg, Exception e) throws Exit {147dl.report(createDiagnostic(Diagnostic.Kind.ERROR, "", msg));148throw new Exit(10, e);149}150151private Diagnostic<JavaFileObject> createDiagnostic(152final Diagnostic.Kind kind, final String code, final Object... args) {153return new Diagnostic<JavaFileObject>() {154public String getCode() {155return code;156}157public long getColumnNumber() {158return Diagnostic.NOPOS;159}160public long getEndPosition() {161return Diagnostic.NOPOS;162}163public Kind getKind() {164return kind;165}166public long getLineNumber() {167return Diagnostic.NOPOS;168}169public String getMessage(Locale locale) {170if (code.length() == 0)171return (String) args[0];172return getText(code, args); // FIXME locale173}174public long getPosition() {175return Diagnostic.NOPOS;176}177public JavaFileObject getSource() {178return null;179}180public long getStartPosition() {181return Diagnostic.NOPOS;182}183};184}185}186187188