2 *******************************************************************************
3 * Copyright (C) 1996-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 package com.ibm.icu.dev.tool.serializable;
11 import java.io.ByteArrayInputStream;
12 import java.io.ByteArrayOutputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.io.ObjectInputStream;
17 import java.io.ObjectOutputStream;
18 import java.lang.reflect.Modifier;
20 import java.util.Arrays;
21 import java.util.Iterator;
22 import java.util.List;
24 import com.ibm.icu.dev.test.serializable.SerializableTest;
25 import com.ibm.icu.impl.URLHandler;
28 * This class examines all the classes in a Jar file or a directory
29 * and lists all those classes that implement <code>Serializable</code>. It also checks
30 * to make sure that those classes have the <code>serialVersionUID</code>
34 public class SerializableChecker implements URLHandler.URLVisitor
36 private static Class serializable;
37 //private static Class throwable;
39 private String path = null;
41 //private boolean write;
43 public SerializableChecker(String path)
48 File dir = new File(path);
58 serializable = Class.forName("java.io.Serializable");
59 //throwable = Class.forName("java.lang.Throwable");
60 } catch (Exception e) {
61 // we're in deep trouble...
62 System.out.println("Woops! Can't get class info for Serializable and Throwable.");
66 private void writeFile(String className, byte bytes[])
68 File file = new File(path + File.separator + className + ".dat");
69 FileOutputStream stream;
72 stream = new FileOutputStream(file);
76 } catch (Exception e) {
77 System.out.print(" - can't write file!");
81 public void visit(String str)
83 int ix = str.lastIndexOf(".class");
86 String className = "com.ibm.icu" + str.substring(0, ix).replace('/', '.');
88 // Skip things in com.ibm.icu.dev; they're not relevant.
89 if (className.startsWith("com.ibm.icu.dev.")) {
94 Class c = Class.forName(className);
95 int m = c.getModifiers();
97 if (serializable.isAssignableFrom(c) /*&&
98 (! throwable.isAssignableFrom(c) || c.getDeclaredFields().length > 0)*/) {
101 System.out.print(className + " (" + Modifier.toString(m) + ") - ");
103 if(!Modifier.isInterface(m)){
106 c.getDeclaredField("serialVersionUID");
107 } catch (Exception e) {
108 System.out.print("no serialVersionUID - ");
112 if (Modifier.isPublic(m)) {
113 SerializableTest.Handler handler = SerializableTest.getHandler(className);
115 if (!Modifier.isInterface(m) && handler != null) {
116 Object objectsOut[] = handler.getTestObjects();
118 boolean passed = true;
120 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
121 ObjectOutputStream out = new ObjectOutputStream(byteOut);
124 out.writeObject(objectsOut);
127 } catch (IOException e) {
128 System.out.println("Eror writing test objects:" + e.toString());
133 writeFile(className, byteOut.toByteArray());
136 ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
137 ObjectInputStream in = new ObjectInputStream(byteIn);
140 objectsIn = (Object[]) in.readObject();
143 } catch (Exception e) {
144 System.out.println("Error reading test objects:" + e.toString());
148 for(int i = 0; i < objectsIn.length; i += 1) {
149 if (! handler.hasSameBehavior(objectsIn[i], objectsOut[i])) {
151 System.out.println("Object " + i + " failed behavior test.");
156 System.out.print("test passed.");
159 // it's OK to not have tests for abstract classes...
160 if (! Modifier.isAbstract(m)) {
161 System.out.print("no test.");
166 System.out.println();
168 } catch (Exception e) {
169 System.out.println("Error processing " + className + ": " + e.toString());
174 public static void main(String[] args)
176 List argList = Arrays.asList(args);
179 for (Iterator it = argList.iterator(); it.hasNext(); /*anything?*/) {
180 String arg = (String) it.next();
182 if (arg.equals("-w")) {
184 path = (String) it.next();
186 System.out.println("Missing directory name on -w command.");
192 //URL jarURL = new URL("jar:file:/dev/eclipse/workspace/icu4j/icu4j.jar!/com/ibm/icu");
193 //URL fileURL = new URL("file:/dev/eclipse/workspace/icu4j/classes/com/ibm/icu");
194 URL url = new URL(arg);
195 URLHandler handler = URLHandler.get(url);
196 SerializableChecker checker = new SerializableChecker(path);
198 System.out.println("Checking classes from " + arg + ":");
199 handler.guide(checker, true, false);
200 } catch (Exception e) {
201 System.out.println("Error processing URL \"" + arg + "\" - " + e.getMessage());