diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/gnu/classpath/tools/serialver/Messages.java | 68 | ||||
-rw-r--r-- | tools/gnu/classpath/tools/serialver/SerialVer.java | 131 |
2 files changed, 185 insertions, 14 deletions
diff --git a/tools/gnu/classpath/tools/serialver/Messages.java b/tools/gnu/classpath/tools/serialver/Messages.java new file mode 100644 index 000000000..a6ab67add --- /dev/null +++ b/tools/gnu/classpath/tools/serialver/Messages.java @@ -0,0 +1,68 @@ +/* Messages.java -- translations for serialver tool + Copyright (C) 2006 Free Software Foundation, Inc. + + This file is part of GNU Classpath. + + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ + + +package gnu.classpath.tools.serialver; + +import java.util.MissingResourceException; +import java.util.ResourceBundle; + +public class Messages +{ + private static final String BUNDLE_NAME + = "gnu.classpath.tools.serialver.messages"; //$NON-NLS-1$ + + private static final ResourceBundle RESOURCE_BUNDLE + = ResourceBundle.getBundle(BUNDLE_NAME); + + private Messages() + { + } + + public static String getString(String key) + { + // TODO Auto-generated method stub + try + { + return RESOURCE_BUNDLE.getString(key); + } + catch (MissingResourceException e) + { + return '!' + key + '!'; + } + } +} diff --git a/tools/gnu/classpath/tools/serialver/SerialVer.java b/tools/gnu/classpath/tools/serialver/SerialVer.java index b5189164a..b5a12ec92 100644 --- a/tools/gnu/classpath/tools/serialver/SerialVer.java +++ b/tools/gnu/classpath/tools/serialver/SerialVer.java @@ -17,9 +17,24 @@ along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ + + package gnu.classpath.tools.serialver; +import gnu.classpath.tools.getopt.ClasspathToolParser; +import gnu.classpath.tools.getopt.FileArgumentCallback; +import gnu.classpath.tools.getopt.Option; +import gnu.classpath.tools.getopt.OptionException; +import gnu.classpath.tools.getopt.Parser; + +import java.io.File; import java.io.ObjectStreamClass; +import java.net.URL; +import java.net.URLClassLoader; +import java.text.MessageFormat; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.StringTokenizer; /** * This class is an implementation of the `serialver' program. Any number of @@ -29,32 +44,120 @@ import java.io.ObjectStreamClass; */ public class SerialVer { - public static void main(String[] args) + // List of classes to load. + ArrayList classes = new ArrayList(); + // The class path to use. + String classpath; + + // FIXME: taken from ClassLoader, should share it. + private static void addFileURL(ArrayList list, String file) + { + try + { + list.add(new File(file).toURL()); + } + catch(java.net.MalformedURLException x) + { + } + } + + private ClassLoader getClassLoader() + { + // FIXME: this code is taken from ClassLoader. + // We should share it somewhere. + URL[] urls; + if (classpath == null) + urls = new URL[0]; + else + { + StringTokenizer tok = new StringTokenizer(classpath, + File.pathSeparator, true); + ArrayList list = new ArrayList(); + while (tok.hasMoreTokens()) + { + String s = tok.nextToken(); + if (s.equals(File.pathSeparator)) + addFileURL(list, "."); //$NON-NLS-1$ + else + { + addFileURL(list, s); + if (tok.hasMoreTokens()) + { + // Skip the separator. + tok.nextToken(); + // If the classpath ended with a separator, + // append the current directory. + if (!tok.hasMoreTokens()) + addFileURL(list, "."); //$NON-NLS-1$ + } + } + } + urls = new URL[list.size()]; + urls = (URL[]) list.toArray(urls); + } + return new URLClassLoader(urls); + } + + private void printMessage(String format, String klass) { - if (args.length == 0) + System.err.println(MessageFormat.format(format, new Object[] { klass })); + } + + public void run(String[] args) + { + Parser p = new ClasspathToolParser("serialver", true) //$NON-NLS-1$ + { + protected void validate() throws OptionException { - System.out.println("Usage: serialver [CLASS]..."); - return; + if (classes.isEmpty()) + throw new OptionException(Messages.getString("SerialVer.NoClassesSpecd")); //$NON-NLS-1$ } - Class clazz; - ObjectStreamClass osc; - for (int i = 0; i < args.length; i++) + }; + p.setHeader(Messages.getString("SerialVer.HelpHeader")); //$NON-NLS-1$ + + p.add(new Option(Messages.getString("SerialVer.5"), Messages.getString("SerialVer.ClasspathHelp"), "PATH") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + { + public void parsed(String argument) throws OptionException { + if (classpath != null) + throw new OptionException(Messages.getString("SerialVer.DupClasspath")); //$NON-NLS-1$ + classpath = argument; + } + }); + + p.parse(args, new FileArgumentCallback() + { + public void notifyFile(String fileArgument) throws OptionException + { + classes.add(fileArgument); + } + }); + + ClassLoader loader = getClassLoader(); + Iterator it = classes.iterator(); + while (it.hasNext()) + { + String name = (String) it.next(); try { - clazz = Class.forName(args[i]); - osc = ObjectStreamClass.lookup(clazz); + Class clazz = loader.loadClass(name); + ObjectStreamClass osc = ObjectStreamClass.lookup(clazz); if (osc != null) - System.out.println(clazz.getName() + ": " - + "static final long serialVersionUID = " - + osc.getSerialVersionUID() + "L;"); + System.out.println(clazz.getName() + ": " //$NON-NLS-1$ + + "static final long serialVersionUID = " //$NON-NLS-1$ + + osc.getSerialVersionUID() + "L;"); //$NON-NLS-1$ else - System.err.println("Class " + args[i] + " is not serializable"); + printMessage(Messages.getString("SerialVer.ClassNotSerial"), name); //$NON-NLS-1$ } catch (ClassNotFoundException e) { - System.err.println("Class for " + args[i] + " not found"); + printMessage(Messages.getString("SerialVer.ClassNotFound"), name); //$NON-NLS-1$ } } } + + public static void main(String[] args) + { + new SerialVer().run(args); + } }
\ No newline at end of file |