1 15 16 package javassist.reflect; 17 18 import java.lang.reflect.Method ; 19 import java.io.Serializable ; 20 import java.io.IOException ; 21 import java.io.ObjectInputStream ; 22 import java.io.ObjectOutputStream ; 23 24 45 public class Metaobject implements Serializable { 46 protected ClassMetaobject classmetaobject; 47 protected Metalevel baseobject; 48 protected Method [] methods; 49 50 59 public Metaobject(Object self, Object [] args) { 60 baseobject = (Metalevel)self; 61 classmetaobject = baseobject._getClass(); 62 methods = classmetaobject.getReflectiveMethods(); 63 } 64 65 70 protected Metaobject() { 71 baseobject = null; 72 classmetaobject = null; 73 methods = null; 74 } 75 76 private void writeObject(ObjectOutputStream out) throws IOException { 77 out.writeObject(baseobject); 78 } 79 80 private void readObject(ObjectInputStream in) 81 throws IOException , ClassNotFoundException 82 { 83 baseobject = (Metalevel)in.readObject(); 84 classmetaobject = baseobject._getClass(); 85 methods = classmetaobject.getReflectiveMethods(); 86 } 87 88 93 public final ClassMetaobject getClassMetaobject() { 94 return classmetaobject; 95 } 96 97 100 public final Object getObject() { 101 return baseobject; 102 } 103 104 109 public final void setObject(Object self) { 110 baseobject = (Metalevel)self; 111 classmetaobject = baseobject._getClass(); 112 methods = classmetaobject.getReflectiveMethods(); 113 114 baseobject._setMetaobject(this); 116 } 117 118 122 public final String getMethodName(int identifier) { 123 String mname = methods[identifier].getName(); 124 int j = ClassMetaobject.methodPrefixLen; 125 for (;;) { 126 char c = mname.charAt(j++); 127 if (c < '0' || '9' < c) 128 break; 129 } 130 131 return mname.substring(j); 132 } 133 134 139 public final Class [] getParameterTypes(int identifier) { 140 return methods[identifier].getParameterTypes(); 141 } 142 143 147 public final Class getReturnType(int identifier) { 148 return methods[identifier].getReturnType(); 149 } 150 151 158 public Object trapFieldRead(String name) { 159 Class jc = getClassMetaobject().getJavaClass(); 160 try { 161 return jc.getField(name).get(getObject()); 162 } 163 catch (NoSuchFieldException e) { 164 throw new RuntimeException (e.toString()); 165 } 166 catch (IllegalAccessException e) { 167 throw new RuntimeException (e.toString()); 168 } 169 } 170 171 178 public void trapFieldWrite(String name, Object value) { 179 Class jc = getClassMetaobject().getJavaClass(); 180 try { 181 jc.getField(name).set(getObject(), value); 182 } 183 catch (NoSuchFieldException e) { 184 throw new RuntimeException (e.toString()); 185 } 186 catch (IllegalAccessException e) { 187 throw new RuntimeException (e.toString()); 188 } 189 } 190 191 223 public Object trapMethodcall(int identifier, Object [] args) 224 throws Throwable 225 { 226 try { 227 return methods[identifier].invoke(getObject(), args); 228 } 229 catch (java.lang.reflect.InvocationTargetException e) { 230 throw e.getTargetException(); 231 } 232 catch (java.lang.IllegalAccessException e) { 233 throw new CannotInvokeException(e); 234 } 235 } 236 } 237 | Popular Tags |