1 package org.sourceforge.jemm.client;
2
3 import java.lang.reflect.Field;
4 import java.lang.reflect.Method;
5
6 import org.sourceforge.jemm.database.FieldType;
7 import org.sourceforge.jemm.lifecycle.AttributeUse;
8 import org.sourceforge.jemm.lifecycle.ShadowData;
9 import org.sourceforge.jemm.lifecycle.ShadowObject;
10 import org.sourceforge.jemm.lifecycle.ShadowUserObject;
11 import org.sourceforge.jemm.lifecycle.Uses;
12 import org.sourceforge.jemm.types.ID;
13 import org.sourceforge.jemm.util.JEMMObject;
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class Entity {
30 JEMMObject obj;
31
32 public Entity(JEMMObject obj) {
33 this.obj = obj;
34 }
35
36
37
38
39
40
41 public ShadowUserObject getShadowUserObject() {
42 ShadowObject so = getShadowObject();
43 return (ShadowUserObject) so;
44 }
45
46
47
48
49
50
51 public ShadowObject getShadowObject() {
52 try {
53 ClassHierarchyIterator iterator = new ClassHierarchyIterator(obj.getClass());
54 Class<?> parent=null;
55 while(iterator.hasNext())
56 parent = iterator.next();
57
58 Field f = parent.getDeclaredField("jemmOIF");
59 f.setAccessible(true);
60 return (ShadowObject)f.get(obj);
61 } catch (Exception e) {
62 throw new RuntimeException(e);
63 }
64 }
65
66
67 public ShadowData getShadowData() {
68 return getShadowUserObject().getData();
69 }
70
71 public int getVersion() {
72 return getShadowData().getVersion();
73 }
74
75 public void setVersion(int newVersion) {
76 getShadowData().setVersion(newVersion);
77 }
78
79
80
81
82
83
84
85 public void setShadowObject(ShadowUserObject so) {
86 try {
87 ClassHierarchyIterator iterator = new ClassHierarchyIterator(obj.getClass());
88 for(Class<?> clazz : iterator) {
89 try {
90 Field f = clazz.getDeclaredField("jemmOIF");
91 f.setAccessible(true);
92 f.set(obj, so);
93 }catch (NoSuchFieldException e) {
94
95 }
96 }
97 } catch (Exception e) {
98 throw new RuntimeException(e);
99 }
100 }
101
102
103
104
105
106
107 public ID getID() {
108 return getShadowObject().getID();
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124 public void setField(FieldKey fkey, Object value) {
125 try {
126 Class<?> c = fkey.getClazz();
127 Field f = c.getDeclaredField(fkey.getName());
128 f.setAccessible(true);
129 f.set(obj, value);
130 } catch (Exception e) {
131 throw new RuntimeException(e);
132 }
133 }
134
135
136
137
138
139
140
141
142 public Object getField(FieldKey fkey) {
143 try {
144 Class<?> c = fkey.getClazz();
145 Field f = c.getDeclaredField(fkey.getName());
146 f.setAccessible(true);
147 Object value = f.get(obj);
148 return value;
149 } catch (Exception e) {
150 throw new RuntimeException(e);
151 }
152 }
153
154
155
156
157
158
159
160 public FieldType getFieldType(FieldKey fKey) {
161 try {
162 Field f = fKey.getField();
163 return FieldType.convert(f.getType());
164 } catch(Exception e) {
165 throw new RuntimeException(e);
166 }
167
168 }
169
170
171
172
173
174
175
176
177
178 public AttributeUse[] methodUses(Descriptor methodSignature) {
179 try {
180 Method m =
181 methodSignature.getMethod();
182 Uses uses = m.getAnnotation(Uses.class);
183 if(uses==null)
184 return new AttributeUse[0];
185 return uses.value();
186 } catch (Exception e) {
187 throw new RuntimeException(e);
188 }
189 }
190 }