KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > core > dist > ObjectState


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