src/share/classes/com/sun/tools/javah/Util.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26
aoqi@0 27 package com.sun.tools.javah;
aoqi@0 28
aoqi@0 29 import java.io.PrintWriter;
aoqi@0 30 import java.text.MessageFormat;
aoqi@0 31 import java.util.Locale;
aoqi@0 32 import java.util.ResourceBundle;
aoqi@0 33 import java.util.MissingResourceException;
aoqi@0 34 import javax.tools.Diagnostic;
aoqi@0 35 import javax.tools.Diagnostic.Kind;
aoqi@0 36 import javax.tools.DiagnosticListener;
aoqi@0 37 import javax.tools.JavaFileObject;
aoqi@0 38
aoqi@0 39 /**
aoqi@0 40 * Messages, verbose and error handling support.
aoqi@0 41 *
aoqi@0 42 * For errors, the failure modes are:
aoqi@0 43 * error -- User did something wrong
aoqi@0 44 * bug -- Bug has occurred in javah
aoqi@0 45 * fatal -- We can't even find resources, so bail fast, don't localize
aoqi@0 46 *
aoqi@0 47 * <p><b>This is NOT part of any supported API.
aoqi@0 48 * If you write code that depends on this, you do so at your own
aoqi@0 49 * risk. This code and its internal interfaces are subject to change
aoqi@0 50 * or deletion without notice.</b></p>
aoqi@0 51 */
aoqi@0 52 public class Util {
aoqi@0 53 /** Exit is used to replace the use of System.exit in the original javah.
aoqi@0 54 */
aoqi@0 55 public static class Exit extends Error {
aoqi@0 56 private static final long serialVersionUID = 430820978114067221L;
aoqi@0 57 Exit(int exitValue) {
aoqi@0 58 this(exitValue, null);
aoqi@0 59 }
aoqi@0 60
aoqi@0 61 Exit(int exitValue, Throwable cause) {
aoqi@0 62 super(cause);
aoqi@0 63 this.exitValue = exitValue;
aoqi@0 64 this.cause = cause;
aoqi@0 65 }
aoqi@0 66
aoqi@0 67 Exit(Exit e) {
aoqi@0 68 this(e.exitValue, e.cause);
aoqi@0 69 }
aoqi@0 70
aoqi@0 71 public final int exitValue;
aoqi@0 72 public final Throwable cause;
aoqi@0 73 }
aoqi@0 74
aoqi@0 75 /*
aoqi@0 76 * Help for verbosity.
aoqi@0 77 */
aoqi@0 78 public boolean verbose = false;
aoqi@0 79
aoqi@0 80 public PrintWriter log;
aoqi@0 81 public DiagnosticListener<? super JavaFileObject> dl;
aoqi@0 82
aoqi@0 83 Util(PrintWriter log, DiagnosticListener<? super JavaFileObject> dl) {
aoqi@0 84 this.log = log;
aoqi@0 85 this.dl = dl;
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 public void log(String s) {
aoqi@0 89 log.println(s);
aoqi@0 90 }
aoqi@0 91
aoqi@0 92
aoqi@0 93 /*
aoqi@0 94 * Help for loading localized messages.
aoqi@0 95 */
aoqi@0 96 private ResourceBundle m;
aoqi@0 97
aoqi@0 98 private void initMessages() throws Exit {
aoqi@0 99 try {
aoqi@0 100 m = ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n");
aoqi@0 101 } catch (MissingResourceException mre) {
aoqi@0 102 fatal("Error loading resources. Please file a bug report.", mre);
aoqi@0 103 }
aoqi@0 104 }
aoqi@0 105
aoqi@0 106 private String getText(String key, Object... args) throws Exit {
aoqi@0 107 if (m == null)
aoqi@0 108 initMessages();
aoqi@0 109 try {
aoqi@0 110 return MessageFormat.format(m.getString(key), args);
aoqi@0 111 } catch (MissingResourceException e) {
aoqi@0 112 fatal("Key " + key + " not found in resources.", e);
aoqi@0 113 }
aoqi@0 114 return null; /* dead code */
aoqi@0 115 }
aoqi@0 116
aoqi@0 117 /*
aoqi@0 118 * Usage message.
aoqi@0 119 */
aoqi@0 120 public void usage() throws Exit {
aoqi@0 121 log.println(getText("usage"));
aoqi@0 122 }
aoqi@0 123
aoqi@0 124 public void version() throws Exit {
aoqi@0 125 log.println(getText("javah.version",
aoqi@0 126 System.getProperty("java.version"), null));
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 /*
aoqi@0 130 * Failure modes.
aoqi@0 131 */
aoqi@0 132 public void bug(String key) throws Exit {
aoqi@0 133 bug(key, null);
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 public void bug(String key, Exception e) throws Exit {
aoqi@0 137 dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key));
aoqi@0 138 dl.report(createDiagnostic(Diagnostic.Kind.NOTE, "bug.report"));
aoqi@0 139 throw new Exit(11, e);
aoqi@0 140 }
aoqi@0 141
aoqi@0 142 public void error(String key, Object... args) throws Exit {
aoqi@0 143 dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key, args));
aoqi@0 144 throw new Exit(15);
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 private void fatal(String msg, Exception e) throws Exit {
aoqi@0 148 dl.report(createDiagnostic(Diagnostic.Kind.ERROR, "", msg));
aoqi@0 149 throw new Exit(10, e);
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 private Diagnostic<JavaFileObject> createDiagnostic(
aoqi@0 153 final Diagnostic.Kind kind, final String code, final Object... args) {
aoqi@0 154 return new Diagnostic<JavaFileObject>() {
aoqi@0 155 public String getCode() {
aoqi@0 156 return code;
aoqi@0 157 }
aoqi@0 158 public long getColumnNumber() {
aoqi@0 159 return Diagnostic.NOPOS;
aoqi@0 160 }
aoqi@0 161 public long getEndPosition() {
aoqi@0 162 return Diagnostic.NOPOS;
aoqi@0 163 }
aoqi@0 164 public Kind getKind() {
aoqi@0 165 return kind;
aoqi@0 166 }
aoqi@0 167 public long getLineNumber() {
aoqi@0 168 return Diagnostic.NOPOS;
aoqi@0 169 }
aoqi@0 170 public String getMessage(Locale locale) {
aoqi@0 171 if (code.length() == 0)
aoqi@0 172 return (String) args[0];
aoqi@0 173 return getText(code, args); // FIXME locale
aoqi@0 174 }
aoqi@0 175 public long getPosition() {
aoqi@0 176 return Diagnostic.NOPOS;
aoqi@0 177 }
aoqi@0 178 public JavaFileObject getSource() {
aoqi@0 179 return null;
aoqi@0 180 }
aoqi@0 181 public long getStartPosition() {
aoqi@0 182 return Diagnostic.NOPOS;
aoqi@0 183 }
aoqi@0 184 };
aoqi@0 185 }
aoqi@0 186 }

mercurial