1 4 package JSX; 5 6 import java.lang.reflect.*; 7 import java.io.*; 8 import java.util.HashMap ; 9 import java.util.Map ; 10 import java.util.Iterator ; 11 12 public class SerialPF { 13 private static final int NOT_SERIALIZED = Modifier.STATIC|Modifier.TRANSIENT; 14 private static final boolean DEBUG = false; 15 17 public static void main(String [] args) throws Exception { 18 if (args.length==0) { 19 args = new String [] {"java.math.BigInteger"}; 20 System.err.println("USAGE: java SerialPF className ["+ args[0] +"]"); 21 System.err.println(); 22 } 23 Class clazz = Class.forName(args[0]); 24 25 Map map = getFieldNameType(clazz); 26 for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 28 Map.Entry entry = (Map.Entry )i.next(); 29 String name = (String )entry.getKey(); 30 Class type = (Class )entry.getValue(); 31 System.err.println("\t"+type.getName()+" "+name); 32 } 33 } 34 35 43 static public boolean areSerializableFieldsValidFor_defWO(Map map, Class clazz) { 44 boolean soFarOK = true; for (Iterator i = map.entrySet().iterator(); i.hasNext();) { 47 Map.Entry entry = (Map.Entry )i.next(); 48 String name = (String )entry.getKey(); 49 Class type = (Class )entry.getValue(); 50 if (DEBUG) System.err.println("\tChecking: "+type.getName()+" "+name); 51 try { Field field = null; 54 try { 55 field = clazz.getDeclaredField(name); 56 } catch (NoSuchFieldException e) { 57 throw new InvalidClassException("unmatched serializable field(s) declared - '"+name+"' is not defined in "+clazz); 59 } 60 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 (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) { System.err.println(e); 70 soFarOK = false; } 72 } return soFarOK; } 75 76 static public Map getFieldNameType(Class clazz) { 78 try { 79 Field spf = clazz.getDeclaredField("serialPersistentFields"); 80 spf.setAccessible(true); 81 return getSerialPersistentFields(spf); 83 } catch (NoSuchFieldException e) { 84 return getDefaultDefinedFields(clazz); } 86 } 87 88 static public boolean isFieldSerializable(Field f) { 89 Class clazz = f.getDeclaringClass(); if (Modifier.isStatic(f.getModifiers())) 91 return false; try { 93 Field spf = clazz.getDeclaredField("serialPersistentFields"); 94 spf.setAccessible(true); 95 Map map = getSerialPersistentFields(spf); return map.containsKey(f.getName()); 99 } catch (NoSuchFieldException e) { if ( !Modifier.isTransient(f.getModifiers()) ) { 101 return true; 102 } else { 103 return false; } 105 } 106 } 107 108 109 static Map getSerialPersistentFields(Field spf) { 110 HashMap map = new HashMap (); 111 ObjectStreamField[] osfs = null; 112 try { 113 osfs = (ObjectStreamField[])spf.get(null); } catch (IllegalAccessException e) { 115 throw new InternalError (e.toString()); 116 } 117 for (int i=0; i<osfs.length; i++) { 118 ObjectStreamField f = osfs[i]; 119 map.put(f.getName(), f.getType()); } 121 return map; 122 } 123 124 static HashMap getDefaultDefinedFields(Class clazz) { 125 HashMap map = new HashMap (); 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
|