author | ehelin |
Thu, 29 Oct 2015 14:58:44 +0100 | |
changeset 33623 | 8b6afaf25abd |
parent 29485 | 5a4c74eeaeab |
child 36511 | 9d0388c6b336 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
29485 | 2 |
* Copyright (c) 1996, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.tools.serialver; |
|
27 |
||
28 |
import java.io.*; |
|
29 |
import java.io.ObjectStreamClass; |
|
30 |
import java.util.Properties; |
|
31 |
import java.text.MessageFormat; |
|
32 |
import java.util.ResourceBundle; |
|
33 |
import java.util.MissingResourceException; |
|
34 |
import java.net.URLClassLoader; |
|
35 |
import java.net.URL; |
|
36 |
import java.net.MalformedURLException; |
|
37 |
import java.util.StringTokenizer; |
|
38 |
import sun.net.www.ParseUtil; |
|
39 |
||
29485 | 40 |
/** |
41 |
* Supporting class for the serialver tool. |
|
42 |
*/ |
|
24510
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
43 |
public class SerialVer { |
2 | 44 |
|
45 |
/* |
|
46 |
* A class loader that will load from the CLASSPATH environment |
|
47 |
* variable set by the user. |
|
48 |
*/ |
|
49 |
static URLClassLoader loader = null; |
|
50 |
||
51 |
/* |
|
52 |
* Create a URL class loader that will load classes from the |
|
53 |
* specified classpath. |
|
54 |
*/ |
|
55 |
static void initializeLoader(String cp) |
|
56 |
throws MalformedURLException, IOException { |
|
57 |
URL[] urls; |
|
58 |
StringTokenizer st = new StringTokenizer(cp, File.pathSeparator); |
|
59 |
int count = st.countTokens(); |
|
60 |
urls = new URL[count]; |
|
61 |
for (int i = 0; i < count; i++) { |
|
62 |
urls[i] = ParseUtil.fileToEncodedURL( |
|
63 |
new File(new File(st.nextToken()).getCanonicalPath())); |
|
64 |
} |
|
65 |
loader = new URLClassLoader(urls); |
|
66 |
} |
|
67 |
||
68 |
/* |
|
69 |
* From the classname find the serialVersionUID string formatted |
|
70 |
* for to be copied to a java class. |
|
71 |
*/ |
|
72 |
static String serialSyntax(String classname) throws ClassNotFoundException { |
|
73 |
String ret = null; |
|
74 |
boolean classFound = false; |
|
75 |
||
76 |
// If using old style of qualifyling inner classes with '$'s. |
|
77 |
if (classname.indexOf('$') != -1) { |
|
78 |
ret = resolveClass(classname); |
|
79 |
} else { |
|
80 |
/* Try to resolve the fully qualified name and if that fails, start |
|
81 |
* replacing the '.'s with '$'s starting from the last '.', until |
|
82 |
* the class is resolved. |
|
83 |
*/ |
|
84 |
try { |
|
85 |
ret = resolveClass(classname); |
|
86 |
classFound = true; |
|
87 |
} catch (ClassNotFoundException e) { |
|
88 |
/* Class not found so far */ |
|
89 |
} |
|
90 |
if (!classFound) { |
|
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
24510
diff
changeset
|
91 |
StringBuilder workBuffer = new StringBuilder(classname); |
2 | 92 |
String workName = workBuffer.toString(); |
93 |
int i; |
|
94 |
while ((i = workName.lastIndexOf('.')) != -1 && !classFound) { |
|
95 |
workBuffer.setCharAt(i, '$'); |
|
96 |
try { |
|
97 |
workName = workBuffer.toString(); |
|
98 |
ret = resolveClass(workName); |
|
99 |
classFound = true; |
|
100 |
} catch (ClassNotFoundException e) { |
|
101 |
/* Continue searching */ |
|
102 |
} |
|
103 |
} |
|
104 |
} |
|
105 |
if (!classFound) { |
|
106 |
throw new ClassNotFoundException(); |
|
107 |
} |
|
108 |
} |
|
109 |
return ret; |
|
110 |
} |
|
111 |
||
112 |
static String resolveClass(String classname) throws ClassNotFoundException { |
|
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
113 |
Class<?> cl = Class.forName(classname, false, loader); |
2 | 114 |
ObjectStreamClass desc = ObjectStreamClass.lookup(cl); |
115 |
if (desc != null) { |
|
21651
07756e24916a
8028027: serialver should emit declaration with the 'private' modifier
smarks
parents:
14342
diff
changeset
|
116 |
return " private static final long serialVersionUID = " + |
2 | 117 |
desc.getSerialVersionUID() + "L;"; |
118 |
} else { |
|
119 |
return null; |
|
120 |
} |
|
121 |
} |
|
122 |
||
29485 | 123 |
/** |
124 |
* Entry point for serialver tool. |
|
125 |
* @param args the arguments |
|
126 |
*/ |
|
2 | 127 |
public static void main(String[] args) { |
128 |
String envcp = null; |
|
129 |
int i = 0; |
|
130 |
||
131 |
if (args.length == 0) { |
|
132 |
usage(); |
|
133 |
System.exit(1); |
|
134 |
} |
|
135 |
||
136 |
for (i = 0; i < args.length; i++) { |
|
24510
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
137 |
if (args[i].equals("-classpath")) { |
2 | 138 |
if ((i+1 == args.length) || args[i+1].startsWith("-")) { |
139 |
System.err.println(Res.getText("error.missing.classpath")); |
|
140 |
usage(); |
|
141 |
System.exit(1); |
|
142 |
} |
|
143 |
envcp = new String(args[i+1]); |
|
144 |
i++; |
|
145 |
} else if (args[i].startsWith("-")) { |
|
146 |
System.err.println(Res.getText("invalid.flag", args[i])); |
|
147 |
usage(); |
|
148 |
System.exit(1); |
|
149 |
} else { |
|
150 |
break; // drop into processing class names |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
||
155 |
/* |
|
156 |
* Get user's CLASSPATH environment variable, if the -classpath option |
|
157 |
* is not defined, and make a loader that can read from that path. |
|
158 |
*/ |
|
159 |
if (envcp == null) { |
|
160 |
envcp = System.getProperty("env.class.path"); |
|
161 |
/* |
|
162 |
* If environment variable not set, add current directory to path. |
|
163 |
*/ |
|
164 |
if (envcp == null) { |
|
165 |
envcp = "."; |
|
166 |
} |
|
167 |
} |
|
168 |
||
169 |
try { |
|
170 |
initializeLoader(envcp); |
|
171 |
} catch (MalformedURLException mue) { |
|
172 |
System.err.println(Res.getText("error.parsing.classpath", envcp)); |
|
173 |
System.exit(2); |
|
174 |
} catch (IOException ioe) { |
|
175 |
System.err.println(Res.getText("error.parsing.classpath", envcp)); |
|
176 |
System.exit(3); |
|
177 |
} |
|
178 |
||
24510
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
179 |
/* |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
180 |
* Check if there are any class names specified |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
181 |
*/ |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
182 |
if (i == args.length) { |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
183 |
usage(); |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
184 |
System.exit(1); |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
185 |
} |
2 | 186 |
|
24510
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
187 |
/* |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
188 |
* The rest of the parameters are classnames. |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
189 |
*/ |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
190 |
boolean exitFlag = false; |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
191 |
for (i = i; i < args.length; i++ ) { |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
192 |
try { |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
193 |
String syntax = serialSyntax(args[i]); |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
194 |
if (syntax != null) |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
195 |
System.out.println(args[i] + ":" + syntax); |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
196 |
else { |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
197 |
System.err.println(Res.getText("NotSerializable", |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
198 |
args[i])); |
2 | 199 |
exitFlag = true; |
200 |
} |
|
24510
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
201 |
} catch (ClassNotFoundException cnf) { |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
202 |
System.err.println(Res.getText("ClassNotFound", args[i])); |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
203 |
exitFlag = true; |
2 | 204 |
} |
24510
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
205 |
} |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
206 |
if (exitFlag) { |
a96915fc059d
8042887: Remove serialver -show, this tool does not need a GUI
alanb
parents:
23010
diff
changeset
|
207 |
System.exit(1); |
2 | 208 |
} |
209 |
} |
|
210 |
||
211 |
||
212 |
/** |
|
213 |
* Usage |
|
214 |
*/ |
|
215 |
public static void usage() { |
|
216 |
System.err.println(Res.getText("usage")); |
|
217 |
} |
|
218 |
||
219 |
} |
|
220 |
||
221 |
/** |
|
222 |
* Utility for integrating with serialver and for localization. |
|
223 |
* Handle Resources. Access to error and warning counts. |
|
224 |
* Message formatting. |
|
225 |
* |
|
226 |
* @see java.util.ResourceBundle |
|
227 |
* @see java.text.MessageFormat |
|
228 |
*/ |
|
229 |
class Res { |
|
230 |
||
231 |
private static ResourceBundle messageRB; |
|
232 |
||
233 |
/** |
|
234 |
* Initialize ResourceBundle |
|
235 |
*/ |
|
236 |
static void initResource() { |
|
237 |
try { |
|
238 |
messageRB = |
|
239 |
ResourceBundle.getBundle("sun.tools.serialver.resources.serialver"); |
|
240 |
} catch (MissingResourceException e) { |
|
241 |
throw new Error("Fatal: Resource for serialver is missing"); |
|
242 |
} |
|
243 |
} |
|
244 |
||
245 |
/** |
|
246 |
* get and format message string from resource |
|
247 |
* |
|
248 |
* @param key selects message from resource |
|
249 |
*/ |
|
250 |
static String getText(String key) { |
|
251 |
return getText(key, (String)null); |
|
252 |
} |
|
253 |
||
254 |
/** |
|
255 |
* get and format message string from resource |
|
256 |
* |
|
257 |
* @param key selects message from resource |
|
258 |
* @param a1 first argument |
|
259 |
*/ |
|
260 |
static String getText(String key, String a1) { |
|
261 |
return getText(key, a1, null); |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* get and format message string from resource |
|
266 |
* |
|
267 |
* @param key selects message from resource |
|
268 |
* @param a1 first argument |
|
269 |
* @param a2 second argument |
|
270 |
*/ |
|
271 |
static String getText(String key, String a1, String a2) { |
|
272 |
return getText(key, a1, a2, null); |
|
273 |
} |
|
274 |
||
275 |
/** |
|
276 |
* get and format message string from resource |
|
277 |
* |
|
278 |
* @param key selects message from resource |
|
279 |
* @param a1 first argument |
|
280 |
* @param a2 second argument |
|
281 |
* @param a3 third argument |
|
282 |
*/ |
|
283 |
static String getText(String key, String a1, String a2, String a3) { |
|
284 |
if (messageRB == null) { |
|
285 |
initResource(); |
|
286 |
} |
|
287 |
try { |
|
288 |
String message = messageRB.getString(key); |
|
11125
99b115114fa3
7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents:
5506
diff
changeset
|
289 |
return MessageFormat.format(message, a1, a2, a3); |
2 | 290 |
} catch (MissingResourceException e) { |
291 |
throw new Error("Fatal: Resource for serialver is broken. There is no " + key + " key in resource."); |
|
292 |
} |
|
293 |
} |
|
294 |
} |