1 2 package org.objectweb.jac.core.dist; 3 4 import java.lang.reflect.Field ; 5 import java.util.Hashtable ; 6 7 import org.objectweb.jac.core.rtti.ClassRepository; 8 9 public class ObjectState { 10 11 public static Field [] getFields(Object object) { 12 13 Hashtable fields = ClassRepository.getDirectFieldAccess(object.getClass()); 14 Object [] res = fields.values().toArray(); 15 Field [] ares = new Field [res.length]; 16 System.arraycopy ( res, 0, ares, 0, ares.length ); 17 return ares; 18 19 } 20 21 public static Field getField(Object object,String name) { 22 23 Hashtable fields = ClassRepository.getDirectFieldAccess(object.getClass()); 24 return (Field ) fields.get(name); 25 26 } 27 28 public static Object getFieldValue (Object substance,String fieldName) { 29 Hashtable ht = ClassRepository.getDirectFieldAccess(substance.getClass()); 30 Field field = (Field ) ht.get(fieldName); 31 if ( field == null ) return null; 32 Object ret = null; 33 try { 34 ret = field.get(substance); 35 } catch ( Exception e ) {} 36 return ret; 37 } 38 39 public static Object [] getState (Object substance) { 40 41 Hashtable fields = ClassRepository.getDirectFieldAccess(substance.getClass()); 42 Object [] tmp = fields.keySet().toArray(); 43 String [] names = new String [tmp.length]; 44 System.arraycopy( tmp, 0, names, 0, tmp.length ); 45 Object [] values = new Object [tmp.length]; 46 for ( int i = 0; i < names.length; i ++ ) { 47 Field f = (Field ) fields.get ( names[i] ); 48 try { 49 values[i] = f.get(substance); 50 } catch ( Exception e ) {} 51 } 52 53 return new Object [] { names, values }; 54 } 55 56 public static Object [] getState (Object substance,String [] fieldNames) { 57 58 Hashtable tmpfields = ClassRepository.getDirectFieldAccess(substance.getClass()); 59 Hashtable fields = new Hashtable (); 60 for ( int i = 0; i < fieldNames.length; i ++ ) { 61 Field f = (Field ) tmpfields.get( fieldNames[i] ); 62 if ( f != null ) { 63 fields.put( fieldNames[i], f ); 64 } 65 } 66 Object [] tmp = fields.keySet().toArray(); 67 String [] names = new String [tmp.length]; 68 System.arraycopy( tmp, 0, names, 0, tmp.length ); 69 Object [] values = new Object [tmp.length]; 70 for ( int i = 0; i < names.length; i ++ ) { 71 Field f = (Field ) fields.get ( names[i] ); 72 try { 73 values[i] = f.get(substance); 74 } catch ( Exception e ) {} 75 } 76 77 return new Object [] { names, values }; 78 } 79 80 public static void setState (Object substance, Object [] state ) { 81 82 Hashtable fields = ClassRepository.getDirectFieldAccess(substance.getClass()); 83 String [] names = (String [])state[0]; 84 Object [] values = (Object [])state[1]; 85 for ( int i = 0; i < names.length; i ++ ) { 86 Field f = (Field ) fields.get ( names[i] ); 87 try { 88 f.set(substance,values[i]); 89 } catch ( Exception e ) {} 90 } 91 } 92 93 public static void setFieldValue (Object substance, String fieldName, Object value) { 94 Hashtable ht = ClassRepository.getDirectFieldAccess(substance.getClass()); 95 Field field = (Field ) ht.get( fieldName ); 96 if ( field == null ) return; 97 try { 98 field.set(substance, value); 99 } catch ( Exception e ) {} 100 } 101 102 } 103 | Popular Tags |