1 |
779 |
jeremybenn |
/* gnu.classpath.tools.SerialVer
|
2 |
|
|
Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
19 |
|
|
02111-1307 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package gnu.classpath.tools.serialver;
|
39 |
|
|
|
40 |
|
|
import gnu.classpath.tools.common.ClasspathToolParser;
|
41 |
|
|
import gnu.classpath.tools.getopt.FileArgumentCallback;
|
42 |
|
|
import gnu.classpath.tools.getopt.Option;
|
43 |
|
|
import gnu.classpath.tools.getopt.OptionException;
|
44 |
|
|
import gnu.classpath.tools.getopt.Parser;
|
45 |
|
|
|
46 |
|
|
import java.io.File;
|
47 |
|
|
import java.io.ObjectStreamClass;
|
48 |
|
|
import java.net.URL;
|
49 |
|
|
import java.net.URLClassLoader;
|
50 |
|
|
import java.text.MessageFormat;
|
51 |
|
|
import java.util.ArrayList;
|
52 |
|
|
import java.util.Iterator;
|
53 |
|
|
import java.util.StringTokenizer;
|
54 |
|
|
|
55 |
|
|
/**
|
56 |
|
|
* This class is an implementation of the `serialver' program. Any number of
|
57 |
|
|
* class names can be passed as arguments, and the serial version unique
|
58 |
|
|
* identitfier for each class will be printed in a manner suitable for cuting
|
59 |
|
|
* and pasting into a Java source file.
|
60 |
|
|
*/
|
61 |
|
|
public class SerialVer
|
62 |
|
|
{
|
63 |
|
|
// List of classes to load.
|
64 |
|
|
ArrayList<String> classes = new ArrayList<String>();
|
65 |
|
|
// The class path to use.
|
66 |
|
|
String classpath;
|
67 |
|
|
|
68 |
|
|
// FIXME: taken from ClassLoader, should share it.
|
69 |
|
|
private static void addFileURL(ArrayList<URL> list, String file)
|
70 |
|
|
{
|
71 |
|
|
try
|
72 |
|
|
{
|
73 |
|
|
list.add(new File(file).toURL());
|
74 |
|
|
}
|
75 |
|
|
catch(java.net.MalformedURLException x)
|
76 |
|
|
{
|
77 |
|
|
}
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
private ClassLoader getClassLoader()
|
81 |
|
|
{
|
82 |
|
|
// FIXME: this code is taken from ClassLoader.
|
83 |
|
|
// We should share it somewhere.
|
84 |
|
|
URL[] urls;
|
85 |
|
|
if (classpath == null)
|
86 |
|
|
urls = new URL[0];
|
87 |
|
|
else
|
88 |
|
|
{
|
89 |
|
|
StringTokenizer tok = new StringTokenizer(classpath,
|
90 |
|
|
File.pathSeparator, true);
|
91 |
|
|
ArrayList<URL> list = new ArrayList<URL>();
|
92 |
|
|
while (tok.hasMoreTokens())
|
93 |
|
|
{
|
94 |
|
|
String s = tok.nextToken();
|
95 |
|
|
if (s.equals(File.pathSeparator))
|
96 |
|
|
addFileURL(list, "."); //$NON-NLS-1$
|
97 |
|
|
else
|
98 |
|
|
{
|
99 |
|
|
addFileURL(list, s);
|
100 |
|
|
if (tok.hasMoreTokens())
|
101 |
|
|
{
|
102 |
|
|
// Skip the separator.
|
103 |
|
|
tok.nextToken();
|
104 |
|
|
// If the classpath ended with a separator,
|
105 |
|
|
// append the current directory.
|
106 |
|
|
if (!tok.hasMoreTokens())
|
107 |
|
|
addFileURL(list, "."); //$NON-NLS-1$
|
108 |
|
|
}
|
109 |
|
|
}
|
110 |
|
|
}
|
111 |
|
|
urls = new URL[list.size()];
|
112 |
|
|
urls = (URL[]) list.toArray(urls);
|
113 |
|
|
}
|
114 |
|
|
return new URLClassLoader(urls);
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
private void printMessage(String format, String klass)
|
118 |
|
|
{
|
119 |
|
|
System.err.println(MessageFormat.format(format, new Object[] { klass }));
|
120 |
|
|
}
|
121 |
|
|
|
122 |
|
|
public void run(String[] args)
|
123 |
|
|
{
|
124 |
|
|
Parser p = new ClasspathToolParser("serialver", true) //$NON-NLS-1$
|
125 |
|
|
{
|
126 |
|
|
protected void validate() throws OptionException
|
127 |
|
|
{
|
128 |
|
|
if (classes.isEmpty())
|
129 |
|
|
throw new OptionException(Messages.getString("SerialVer.NoClassesSpecd")); //$NON-NLS-1$
|
130 |
|
|
}
|
131 |
|
|
};
|
132 |
|
|
p.setHeader(Messages.getString("SerialVer.HelpHeader")); //$NON-NLS-1$
|
133 |
|
|
|
134 |
|
|
p.add(new Option(Messages.getString("SerialVer.5"), Messages.getString("SerialVer.ClasspathHelp"), "PATH") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
|
135 |
|
|
{
|
136 |
|
|
public void parsed(String argument) throws OptionException
|
137 |
|
|
{
|
138 |
|
|
if (classpath != null)
|
139 |
|
|
throw new OptionException(Messages.getString("SerialVer.DupClasspath")); //$NON-NLS-1$
|
140 |
|
|
classpath = argument;
|
141 |
|
|
}
|
142 |
|
|
});
|
143 |
|
|
|
144 |
|
|
p.parse(args, new FileArgumentCallback()
|
145 |
|
|
{
|
146 |
|
|
public void notifyFile(String fileArgument) throws OptionException
|
147 |
|
|
{
|
148 |
|
|
classes.add(fileArgument);
|
149 |
|
|
}
|
150 |
|
|
});
|
151 |
|
|
|
152 |
|
|
ClassLoader loader = getClassLoader();
|
153 |
|
|
Iterator it = classes.iterator();
|
154 |
|
|
while (it.hasNext())
|
155 |
|
|
{
|
156 |
|
|
String name = (String) it.next();
|
157 |
|
|
try
|
158 |
|
|
{
|
159 |
|
|
Class clazz = loader.loadClass(name);
|
160 |
|
|
ObjectStreamClass osc = ObjectStreamClass.lookup(clazz);
|
161 |
|
|
if (osc != null)
|
162 |
|
|
System.out.println(clazz.getName() + ": " //$NON-NLS-1$
|
163 |
|
|
+ "static final long serialVersionUID = " //$NON-NLS-1$
|
164 |
|
|
+ osc.getSerialVersionUID() + "L;"); //$NON-NLS-1$
|
165 |
|
|
else
|
166 |
|
|
printMessage(Messages.getString("SerialVer.ClassNotSerial"), name); //$NON-NLS-1$
|
167 |
|
|
}
|
168 |
|
|
catch (ClassNotFoundException e)
|
169 |
|
|
{
|
170 |
|
|
printMessage(Messages.getString("SerialVer.ClassNotFound"), name); //$NON-NLS-1$
|
171 |
|
|
}
|
172 |
|
|
}
|
173 |
|
|
}
|
174 |
|
|
|
175 |
|
|
public static void main(String[] args)
|
176 |
|
|
{
|
177 |
|
|
new SerialVer().run(args);
|
178 |
|
|
}
|
179 |
|
|
}
|