author | jlahoda |
Wed, 27 Aug 2014 07:44:00 +0200 | |
changeset 26266 | 2d24bda701dc |
parent 25874 | 83c19f00452c |
child 34752 | 9c262a013456 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
16560 | 2 |
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
||
27 |
package com.sun.tools.javah; |
|
28 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
29 |
import java.io.PrintWriter; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
30 |
import java.text.MessageFormat; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
31 |
import java.util.Locale; |
10 | 32 |
import java.util.ResourceBundle; |
33 |
import java.util.MissingResourceException; |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
34 |
import javax.tools.Diagnostic; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
35 |
import javax.tools.Diagnostic.Kind; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
36 |
import javax.tools.DiagnosticListener; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
37 |
import javax.tools.JavaFileObject; |
10 | 38 |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
39 |
import com.sun.tools.javac.util.DefinedBy; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
40 |
import com.sun.tools.javac.util.DefinedBy.Api; |
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
41 |
|
10 | 42 |
/** |
43 |
* Messages, verbose and error handling support. |
|
44 |
* |
|
45 |
* For errors, the failure modes are: |
|
46 |
* error -- User did something wrong |
|
47 |
* bug -- Bug has occurred in javah |
|
48 |
* fatal -- We can't even find resources, so bail fast, don't localize |
|
49 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
50 |
* <p><b>This is NOT part of any supported API. |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
51 |
* If you write code that depends on this, you do so at your own |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
52 |
* risk. This code and its internal interfaces are subject to change |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
53 |
* or deletion without notice.</b></p> |
10 | 54 |
*/ |
55 |
public class Util { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
56 |
/** Exit is used to replace the use of System.exit in the original javah. |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
57 |
*/ |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
58 |
public static class Exit extends Error { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
59 |
private static final long serialVersionUID = 430820978114067221L; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
60 |
Exit(int exitValue) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
61 |
this(exitValue, null); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
62 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
63 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
64 |
Exit(int exitValue, Throwable cause) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
65 |
super(cause); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
66 |
this.exitValue = exitValue; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
67 |
this.cause = cause; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
68 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
69 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
70 |
Exit(Exit e) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
71 |
this(e.exitValue, e.cause); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
72 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
73 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
74 |
public final int exitValue; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
75 |
public final Throwable cause; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
76 |
} |
10 | 77 |
|
78 |
/* |
|
79 |
* Help for verbosity. |
|
80 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
81 |
public boolean verbose = false; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
82 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
83 |
public PrintWriter log; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
84 |
public DiagnosticListener<? super JavaFileObject> dl; |
10 | 85 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
86 |
Util(PrintWriter log, DiagnosticListener<? super JavaFileObject> dl) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
87 |
this.log = log; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
88 |
this.dl = dl; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
89 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
90 |
|
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
91 |
public void log(String s) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
92 |
log.println(s); |
10 | 93 |
} |
94 |
||
95 |
||
96 |
/* |
|
97 |
* Help for loading localized messages. |
|
98 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
99 |
private ResourceBundle m; |
10 | 100 |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
101 |
private void initMessages() throws Exit { |
10 | 102 |
try { |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
103 |
m = ResourceBundle.getBundle("com.sun.tools.javah.resources.l10n"); |
10 | 104 |
} catch (MissingResourceException mre) { |
105 |
fatal("Error loading resources. Please file a bug report.", mre); |
|
106 |
} |
|
107 |
} |
|
108 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
109 |
private String getText(String key, Object... args) throws Exit { |
10 | 110 |
if (m == null) |
111 |
initMessages(); |
|
112 |
try { |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
113 |
return MessageFormat.format(m.getString(key), args); |
10 | 114 |
} catch (MissingResourceException e) { |
115 |
fatal("Key " + key + " not found in resources.", e); |
|
116 |
} |
|
117 |
return null; /* dead code */ |
|
118 |
} |
|
119 |
||
120 |
/* |
|
121 |
* Usage message. |
|
122 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
123 |
public void usage() throws Exit { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
124 |
log.println(getText("usage")); |
10 | 125 |
} |
126 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
127 |
public void version() throws Exit { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
128 |
log.println(getText("javah.version", |
10 | 129 |
System.getProperty("java.version"), null)); |
130 |
} |
|
131 |
||
132 |
/* |
|
133 |
* Failure modes. |
|
134 |
*/ |
|
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
135 |
public void bug(String key) throws Exit { |
10 | 136 |
bug(key, null); |
137 |
} |
|
138 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
139 |
public void bug(String key, Exception e) throws Exit { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
140 |
dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key)); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
141 |
dl.report(createDiagnostic(Diagnostic.Kind.NOTE, "bug.report")); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
142 |
throw new Exit(11, e); |
10 | 143 |
} |
144 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
145 |
public void error(String key, Object... args) throws Exit { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
146 |
dl.report(createDiagnostic(Diagnostic.Kind.ERROR, key, args)); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
147 |
throw new Exit(15); |
10 | 148 |
} |
149 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
150 |
private void fatal(String msg, Exception e) throws Exit { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
151 |
dl.report(createDiagnostic(Diagnostic.Kind.ERROR, "", msg)); |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
152 |
throw new Exit(10, e); |
10 | 153 |
} |
154 |
||
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
155 |
private Diagnostic<JavaFileObject> createDiagnostic( |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
156 |
final Diagnostic.Kind kind, final String code, final Object... args) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
157 |
return new Diagnostic<JavaFileObject>() { |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
158 |
@DefinedBy(Api.COMPILER) |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
159 |
public String getCode() { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
160 |
return code; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
161 |
} |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
162 |
@DefinedBy(Api.COMPILER) |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
163 |
public long getColumnNumber() { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
164 |
return Diagnostic.NOPOS; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
165 |
} |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
166 |
@DefinedBy(Api.COMPILER) |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
167 |
public long getEndPosition() { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
168 |
return Diagnostic.NOPOS; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
169 |
} |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
170 |
@DefinedBy(Api.COMPILER) |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
171 |
public Kind getKind() { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
172 |
return kind; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
173 |
} |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
174 |
@DefinedBy(Api.COMPILER) |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
175 |
public long getLineNumber() { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
176 |
return Diagnostic.NOPOS; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
177 |
} |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
178 |
@DefinedBy(Api.COMPILER) |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
179 |
public String getMessage(Locale locale) { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
180 |
if (code.length() == 0) |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
181 |
return (String) args[0]; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
182 |
return getText(code, args); // FIXME locale |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
183 |
} |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
184 |
@DefinedBy(Api.COMPILER) |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
185 |
public long getPosition() { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
186 |
return Diagnostic.NOPOS; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
187 |
} |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
188 |
@DefinedBy(Api.COMPILER) |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
189 |
public JavaFileObject getSource() { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
190 |
return null; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
191 |
} |
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
192 |
@DefinedBy(Api.COMPILER) |
3996
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
193 |
public long getStartPosition() { |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
194 |
return Diagnostic.NOPOS; |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
195 |
} |
dc676a9093b3
6572945: javah should be written as an annotation processor, not a doclet
jjg
parents:
10
diff
changeset
|
196 |
}; |
10 | 197 |
} |
198 |
} |