]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/tools/misc/src/com/ibm/icu/dev/tool/serializable/SerializableChecker.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / tools / misc / src / com / ibm / icu / dev / tool / serializable / SerializableChecker.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 1996-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  *
7  */
8
9 package com.ibm.icu.dev.tool.serializable;
10
11 import java.io.ByteArrayInputStream;
12 import java.io.ByteArrayOutputStream;
13 import java.io.File;
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;
19 import java.net.URL;
20 import java.util.Arrays;
21 import java.util.Iterator;
22 import java.util.List;
23
24 import com.ibm.icu.dev.test.serializable.SerializableTest;
25 import com.ibm.icu.impl.URLHandler;
26
27 /**
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>
31  * field define.
32  * 
33  */
34 public class SerializableChecker implements URLHandler.URLVisitor
35 {
36     private static Class serializable;
37     //private static Class throwable;
38     
39     private String path = null;
40     
41     //private boolean write;
42     
43     public SerializableChecker(String path)
44     {
45         this.path = path;
46         
47         if (path != null) {
48             File dir = new File(path);
49             
50             if (!dir.exists()) {
51                 dir.mkdirs();
52             }
53         }
54     }
55     
56     static {
57         try {    
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.");
63         }
64     }
65     
66     private void writeFile(String className, byte bytes[])
67     {
68         File file = new File(path + File.separator + className + ".dat");
69         FileOutputStream stream;
70         
71         try {
72             stream = new FileOutputStream(file);
73             
74             stream.write(bytes);
75             stream.close();
76         } catch (Exception e) {
77             System.out.print(" - can't write file!");
78         }
79     }
80     
81     public void visit(String str)
82     {
83         int ix = str.lastIndexOf(".class");
84         
85         if (ix >= 0) {
86             String className = "com.ibm.icu" + str.substring(0, ix).replace('/', '.');
87             
88             // Skip things in com.ibm.icu.dev; they're not relevant.
89             if (className.startsWith("com.ibm.icu.dev.")) {
90                 return;
91             }
92             
93             try {
94                 Class c = Class.forName(className);
95                 int   m = c.getModifiers();
96                 
97                 if (serializable.isAssignableFrom(c) /*&&
98                     (! throwable.isAssignableFrom(c) || c.getDeclaredFields().length > 0)*/) {
99                     //Field uid;
100                     
101                     System.out.print(className + " (" + Modifier.toString(m) + ") - ");
102                     
103                     if(!Modifier.isInterface(m)){ 
104                         try {
105                             /* uid = */
106                             c.getDeclaredField("serialVersionUID");
107                         } catch (Exception e) {
108                             System.out.print("no serialVersionUID - ");
109                         }
110                     }
111                     
112                     if (Modifier.isPublic(m)) {
113                         SerializableTest.Handler handler = SerializableTest.getHandler(className);
114                         
115                         if (!Modifier.isInterface(m) && handler != null) {
116                             Object objectsOut[] = handler.getTestObjects();
117                             Object objectsIn[];
118                             boolean passed = true;
119                             
120                             ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
121                             ObjectOutputStream out = new ObjectOutputStream(byteOut);
122                             
123                             try {
124                                 out.writeObject(objectsOut);
125                                 out.close();
126                                 byteOut.close();
127                             } catch (IOException e) {
128                                 System.out.println("Eror writing test objects:" + e.toString());
129                                 return;
130                             }
131                             
132                             if (path != null) {
133                                 writeFile(className, byteOut.toByteArray());
134                             }
135                             
136                             ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
137                             ObjectInputStream in = new ObjectInputStream(byteIn);
138                             
139                             try {
140                                 objectsIn = (Object[]) in.readObject();
141                                 in.close();
142                                 byteIn.close();
143                             } catch (Exception e) {
144                                 System.out.println("Error reading test objects:" + e.toString());
145                                 return;
146                             }
147
148                             for(int i = 0; i < objectsIn.length; i += 1) {
149                                 if (! handler.hasSameBehavior(objectsIn[i], objectsOut[i])) {
150                                     passed = false;
151                                     System.out.println("Object " + i + " failed behavior test.");
152                                 }
153                             }
154                             
155                             if (passed) {
156                                 System.out.print("test passed.");
157                             }
158                         } else {
159                             // it's OK to not have tests for abstract classes...
160                             if (! Modifier.isAbstract(m)) {
161                                 System.out.print("no test.");
162                             }
163                         }
164                     }
165                     
166                     System.out.println();
167                 }
168            } catch (Exception e) {
169                 System.out.println("Error processing " + className + ": " + e.toString());
170             }
171         }
172     }
173
174     public static void main(String[] args)
175     {
176         List argList = Arrays.asList(args);
177         String path = null;
178         
179         for (Iterator it = argList.iterator(); it.hasNext(); /*anything?*/) {
180             String arg = (String) it.next();
181             
182             if (arg.equals("-w")) {
183                 if (it.hasNext()) {
184                     path = (String) it.next();
185                 } else {
186                     System.out.println("Missing directory name on -w command.");
187                 }
188             } else {
189                 
190     
191                 try {
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);
197                     
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());
202                 }
203             }
204         }
205     }
206 }