KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSX > SerialPF


1 /** investigate:
2     * serialPersistentFields, GetField and PutField
3     **/

4 package JSX;
5
6 import java.lang.reflect.*;
7 import java.io.*;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Iterator JavaDoc;
11
12 public class SerialPF {
13     private static final int NOT_SERIALIZED = Modifier.STATIC|Modifier.TRANSIENT;
14     private static final boolean DEBUG = false;
15     //private static final boolean DEBUG = true;
16

17     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
18         if (args.length==0) {
19             args = new String JavaDoc[] {"java.math.BigInteger"};
20             System.err.println("USAGE: java SerialPF className ["+ args[0] +"]");
21             System.err.println();
22         }
23         Class JavaDoc clazz = Class.forName(args[0]);
24
25         Map JavaDoc map = getFieldNameType(clazz);
26         // System.err.println(map);
27
for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext();) {
28             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
29             String JavaDoc name = (String JavaDoc)entry.getKey();
30             Class JavaDoc type = (Class JavaDoc)entry.getValue();
31             System.err.println("\t"+type.getName()+" "+name);
32         }
33     }
34
35 /** This is *only* for defWO. Always returns true if default (only really
36     * needed for serialPersistentFields).
37     *
38     * Check through all serializable fields (in map) to check they are
39     * - defined
40     * - not static
41     * - types match
42     **/

43     static public boolean areSerializableFieldsValidFor_defWO(Map JavaDoc map, Class JavaDoc clazz) {
44         boolean soFarOK = true; // only needed for testing
45
// check all
46
for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext();) {
47             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
48             String JavaDoc name = (String JavaDoc)entry.getKey();
49             Class JavaDoc type = (Class JavaDoc)entry.getValue();
50             if (DEBUG) System.err.println("\tChecking: "+type.getName()+" "+name);
51         try { //for testing
52
// get Field
53
Field field = null;
54             try {
55                 field = clazz.getDeclaredField(name);
56             } catch (NoSuchFieldException JavaDoc e) {
57                 // if not defined...
58
throw new InvalidClassException("unmatched serializable field(s) declared - '"+name+"' is not defined in "+clazz);
59             }
60             // if static (not an instance field)...
61
if ( Modifier.isStatic(field.getModifiers()) ) {
62                 throw new InvalidClassException("unmatched serializable field(s) declared - '"+name+"' in "+clazz+" is static (necessarily declared in 'serialPersistentFields')");
63             }
64             // if types don't match...
65
if (field.getType()!=type) {
66                 throw new InvalidClassException("unmatched serializable field(s) declared - '"+name+"' in "+clazz+" is of type "+field.getType().getName()+", but the serializable field was declared to be of type "+type.getName()+" (necessarily declared in 'serialPersistentFields')");
67             }
68          } catch (InvalidClassException e) { // for testing
69
System.err.println(e);
70             soFarOK = false; // only needed for testing.
71
}
72         } //...for
73
return soFarOK; // only needed for testing
74
}
75
76     // use serialPersistentFields, else defined fields (non-serializable)
77
static public Map JavaDoc getFieldNameType(Class JavaDoc clazz) {
78         try {
79             Field spf = clazz.getDeclaredField("serialPersistentFields");
80             spf.setAccessible(true);
81             return getSerialPersistentFields(spf); // get spf
82

83         } catch (NoSuchFieldException JavaDoc e) {
84             return getDefaultDefinedFields(clazz); // get non-T instance fields
85
}
86     }
87
88     static public boolean isFieldSerializable(Field f) {
89         Class JavaDoc clazz = f.getDeclaringClass(); // getType() so named to distinguish
90
if (Modifier.isStatic(f.getModifiers()))
91             return false; // this applies to both
92
try {
93             Field spf = clazz.getDeclaredField("serialPersistentFields");
94             spf.setAccessible(true);
95             Map JavaDoc map = getSerialPersistentFields(spf); // get spf
96
// to do: can't check primitive type, because JSX doesn't record it.
97
return map.containsKey(f.getName()); // probably should check type, too.
98

99         } catch (NoSuchFieldException JavaDoc e) { // test: is it a non-T instance field?
100
if ( !Modifier.isTransient(f.getModifiers()) ) {
101                 return true;
102             } else {
103                 return false; // yes! looks ineff, but very clear & compiler zaps it!
104
}
105         }
106     }
107
108
109     static Map JavaDoc getSerialPersistentFields(Field spf) {
110         HashMap JavaDoc map = new HashMap JavaDoc();
111         ObjectStreamField[] osfs = null;
112         try {
113             osfs = (ObjectStreamField[])spf.get(null); // null, because static
114
} catch (IllegalAccessException JavaDoc e) {
115             throw new InternalError JavaDoc(e.toString());
116         }
117         for (int i=0; i<osfs.length; i++) {
118             ObjectStreamField f = osfs[i];
119             map.put(f.getName(), f.getType()); // so can cut and paste with Field
120
}
121         return map;
122     }
123
124     static HashMap JavaDoc getDefaultDefinedFields(Class JavaDoc clazz) {
125         HashMap JavaDoc map = new HashMap JavaDoc();
126         Field[] fields = clazz.getDeclaredFields();
127         for (int i=0; i<fields.length; i++) {
128             Field f = fields[i];
129             if (!Modifier.isStatic(f.getModifiers()) &&
130                     !Modifier.isTransient(f.getModifiers()) ) {
131                 map.put(f.getName(), f.getType());
132             }
133         }
134         return map;
135     }
136
137 }
138
Popular Tags