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