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

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

mercurial