1 /* gnu.classpath.tools.SerialVer
2 Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
22 package gnu
.classpath
.tools
.serialver
;
24 import gnu
.classpath
.tools
.common
.ClasspathToolParser
;
25 import gnu
.classpath
.tools
.getopt
.FileArgumentCallback
;
26 import gnu
.classpath
.tools
.getopt
.Option
;
27 import gnu
.classpath
.tools
.getopt
.OptionException
;
28 import gnu
.classpath
.tools
.getopt
.Parser
;
31 import java
.io
.ObjectStreamClass
;
33 import java
.net
.URLClassLoader
;
34 import java
.text
.MessageFormat
;
35 import java
.util
.ArrayList
;
36 import java
.util
.Iterator
;
37 import java
.util
.StringTokenizer
;
40 * This class is an implementation of the `serialver' program. Any number of
41 * class names can be passed as arguments, and the serial version unique
42 * identitfier for each class will be printed in a manner suitable for cuting
43 * and pasting into a Java source file.
45 public class SerialVer
47 // List of classes to load.
48 ArrayList
<String
> classes
= new ArrayList
<String
>();
49 // The class path to use.
52 // FIXME: taken from ClassLoader, should share it.
53 private static void addFileURL(ArrayList
<URL
> list
, String file
)
57 list
.add(new File(file
).toURL());
59 catch(java
.net
.MalformedURLException x
)
64 private ClassLoader
getClassLoader()
66 // FIXME: this code is taken from ClassLoader.
67 // We should share it somewhere.
69 if (classpath
== null)
73 StringTokenizer tok
= new StringTokenizer(classpath
,
74 File
.pathSeparator
, true);
75 ArrayList
<URL
> list
= new ArrayList
<URL
>();
76 while (tok
.hasMoreTokens())
78 String s
= tok
.nextToken();
79 if (s
.equals(File
.pathSeparator
))
80 addFileURL(list
, "."); //$NON-NLS-1$
84 if (tok
.hasMoreTokens())
86 // Skip the separator.
88 // If the classpath ended with a separator,
89 // append the current directory.
90 if (!tok
.hasMoreTokens())
91 addFileURL(list
, "."); //$NON-NLS-1$
95 urls
= new URL
[list
.size()];
96 urls
= (URL
[]) list
.toArray(urls
);
98 return new URLClassLoader(urls
);
101 private void printMessage(String format
, String klass
)
103 System
.err
.println(MessageFormat
.format(format
, new Object
[] { klass
}));
106 public void run(String
[] args
)
108 Parser p
= new ClasspathToolParser("serialver", true) //$NON-NLS-1$
110 protected void validate() throws OptionException
112 if (classes
.isEmpty())
113 throw new OptionException(Messages
.getString("SerialVer.NoClassesSpecd")); //$NON-NLS-1$
116 p
.setHeader(Messages
.getString("SerialVer.HelpHeader")); //$NON-NLS-1$
118 p
.add(new Option(Messages
.getString("SerialVer.5"), Messages
.getString("SerialVer.ClasspathHelp"), "PATH") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
120 public void parsed(String argument
) throws OptionException
122 if (classpath
!= null)
123 throw new OptionException(Messages
.getString("SerialVer.DupClasspath")); //$NON-NLS-1$
124 classpath
= argument
;
128 p
.parse(args
, new FileArgumentCallback()
130 public void notifyFile(String fileArgument
) throws OptionException
132 classes
.add(fileArgument
);
136 ClassLoader loader
= getClassLoader();
137 Iterator it
= classes
.iterator();
140 String name
= (String
) it
.next();
143 Class clazz
= loader
.loadClass(name
);
144 ObjectStreamClass osc
= ObjectStreamClass
.lookup(clazz
);
146 System
.out
.println(clazz
.getName() + ": " //$NON-NLS-1$
147 + "static final long serialVersionUID = " //$NON-NLS-1$
148 + osc
.getSerialVersionUID() + "L;"); //$NON-NLS-1$
150 printMessage(Messages
.getString("SerialVer.ClassNotSerial"), name
); //$NON-NLS-1$
152 catch (ClassNotFoundException e
)
154 printMessage(Messages
.getString("SerialVer.ClassNotFound"), name
); //$NON-NLS-1$
159 public static void main(String
[] args
)
161 new SerialVer().run(args
);