1 15 16 package javassist.reflect; 17 18 import java.lang.reflect.*; 19 import java.util.Arrays ; 20 import java.io.Serializable ; 21 import java.io.IOException ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 25 41 public class ClassMetaobject implements Serializable { 42 47 static final String methodPrefix = "_m_"; 48 static final int methodPrefixLen = 3; 49 50 private Class javaClass; 51 private Constructor[] constructors; 52 private Method[] methods; 53 54 62 public static boolean useContextClassLoader = false; 63 64 70 public ClassMetaobject(String [] params) 71 { 72 try { 73 javaClass = getClassObject(params[0]); 74 } 75 catch (ClassNotFoundException e) { 76 javaClass = null; 77 } 78 79 constructors = javaClass.getConstructors(); 80 methods = null; 81 } 82 83 private void writeObject(ObjectOutputStream out) throws IOException { 84 out.writeUTF(javaClass.getName()); 85 } 86 87 private void readObject(ObjectInputStream in) 88 throws IOException , ClassNotFoundException 89 { 90 javaClass = getClassObject(in.readUTF()); 91 constructors = javaClass.getConstructors(); 92 methods = null; 93 } 94 95 private Class getClassObject(String name) throws ClassNotFoundException { 96 if (useContextClassLoader) 97 return Thread.currentThread().getContextClassLoader() 98 .loadClass(name); 99 else 100 return Class.forName(name); 101 } 102 103 106 public final Class getJavaClass() { 107 return javaClass; 108 } 109 110 113 public final String getName() { 114 return javaClass.getName(); 115 } 116 117 120 public final boolean isInstance(Object obj) { 121 return javaClass.isInstance(obj); 122 } 123 124 129 public final Object newInstance(Object [] args) 130 throws CannotCreateException 131 { 132 int n = constructors.length; 133 for (int i = 0; i < n; ++i) { 134 try { 135 return constructors[i].newInstance(args); 136 } 137 catch (IllegalArgumentException e) { 138 } 140 catch (InstantiationException e) { 141 throw new CannotCreateException(e); 142 } 143 catch (IllegalAccessException e) { 144 throw new CannotCreateException(e); 145 } 146 catch (InvocationTargetException e) { 147 throw new CannotCreateException(e); 148 } 149 } 150 151 throw new CannotCreateException("no constructor matches"); 152 } 153 154 161 public Object trapFieldRead(String name) { 162 Class jc = getJavaClass(); 163 try { 164 return jc.getField(name).get(null); 165 } 166 catch (NoSuchFieldException e) { 167 throw new RuntimeException (e.toString()); 168 } 169 catch (IllegalAccessException e) { 170 throw new RuntimeException (e.toString()); 171 } 172 } 173 174 181 public void trapFieldWrite(String name, Object value) { 182 Class jc = getJavaClass(); 183 try { 184 jc.getField(name).set(null, value); 185 } 186 catch (NoSuchFieldException e) { 187 throw new RuntimeException (e.toString()); 188 } 189 catch (IllegalAccessException e) { 190 throw new RuntimeException (e.toString()); 191 } 192 } 193 194 200 static public Object invoke(Object target, int identifier, Object [] args) 201 throws Throwable 202 { 203 Method[] allmethods = target.getClass().getMethods(); 204 int n = allmethods.length; 205 String head = methodPrefix + identifier; 206 for (int i = 0; i < n; ++i) 207 if (allmethods[i].getName().startsWith(head)) { 208 try { 209 return allmethods[i].invoke(target, args); 210 } catch (java.lang.reflect.InvocationTargetException e) { 211 throw e.getTargetException(); 212 } catch (java.lang.IllegalAccessException e) { 213 throw new CannotInvokeException(e); 214 } 215 } 216 217 throw new CannotInvokeException("cannot find a method"); 218 } 219 220 228 public Object trapMethodcall(int identifier, Object [] args) 229 throws Throwable 230 { 231 try { 232 Method[] m = getReflectiveMethods(); 233 return m[identifier].invoke(null, args); 234 } 235 catch (java.lang.reflect.InvocationTargetException e) { 236 throw e.getTargetException(); 237 } 238 catch (java.lang.IllegalAccessException e) { 239 throw new CannotInvokeException(e); 240 } 241 } 242 243 247 public final Method[] getReflectiveMethods() { 248 if (methods != null) 249 return methods; 250 251 Class baseclass = getJavaClass(); 252 Method[] allmethods = baseclass.getDeclaredMethods(); 253 int n = allmethods.length; 254 methods = new Method[n]; 255 for (int i = 0; i < n; ++i) { 256 Method m = allmethods[i]; 257 String mname = m.getName(); 258 if (mname.startsWith(methodPrefix)) { 259 int k = 0; 260 for (int j = methodPrefixLen;; ++j) { 261 char c = mname.charAt(j); 262 if ('0' <= c && c <= '9') 263 k = k * 10 + c - '0'; 264 else 265 break; 266 } 267 268 methods[k] = m; 269 } 270 } 271 272 return methods; 273 } 274 275 287 public final Method getMethod(int identifier) { 288 return getReflectiveMethods()[identifier]; 289 } 290 291 295 public final String getMethodName(int identifier) { 296 String mname = getReflectiveMethods()[identifier].getName(); 297 int j = ClassMetaobject.methodPrefixLen; 298 for (;;) { 299 char c = mname.charAt(j++); 300 if (c < '0' || '9' < c) 301 break; 302 } 303 304 return mname.substring(j); 305 } 306 307 312 public final Class [] getParameterTypes(int identifier) { 313 return getReflectiveMethods()[identifier].getParameterTypes(); 314 } 315 316 320 public final Class getReturnType(int identifier) { 321 return getReflectiveMethods()[identifier].getReturnType(); 322 } 323 324 342 public final int getMethodIndex(String originalName, Class [] argTypes) 343 throws NoSuchMethodException 344 { 345 Method[] mthds = getReflectiveMethods(); 346 for (int i = 0; i < mthds.length; i++) { 347 if (mthds[i] == null) 348 continue; 349 350 if (getMethodName(i).equals(originalName) 352 && Arrays.equals(argTypes, mthds[i].getParameterTypes())) 353 return i; 354 } 355 356 throw new NoSuchMethodException ("Method " + originalName 357 + " not found"); 358 } 359 } 360 | Popular Tags |