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

Wed, 02 Jun 2010 19:08:47 -0700

author
darcy
date
Wed, 02 Jun 2010 19:08:47 -0700
changeset 575
9a7c998bf2fc
parent 554
9d9f26857129
child 581
f2fdd52e4e87
permissions
-rw-r--r--

6933147: Provided new utility visitors supporting SourceVersion.RELEASE_7
Reviewed-by: jjg

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

mercurial