1 54 package org.logicalcobwebs.cglib.proxy; 55 56 import java.lang.reflect.*; 57 import java.util.Arrays ; 58 import java.util.List ; 59 import org.logicalcobwebs.cglib.core.CodeGenerationException; 60 import org.logicalcobwebs.cglib.core.ReflectUtils; 61 import org.logicalcobwebs.cglib.core.Signature; 62 import org.logicalcobwebs.cglib.reflect.*; 63 64 71 public class MethodProxy { 72 private Signature sig; 73 private String superName; 74 private FastClass f1; 75 private FastClass f2; 76 private int i1; 77 private int i2; 78 79 83 public static MethodProxy create(ClassLoader loader, Class c1, Class c2, String desc, String name1, String name2) { 84 final Signature sig1 = new Signature(name1, desc); 85 Signature sig2 = new Signature(name2, desc); 86 FastClass f1 = helper(loader, c1); 87 FastClass f2 = helper(loader, c2); 88 int i1 = f1.getIndex(sig1); 89 int i2 = f2.getIndex(sig2); 90 91 MethodProxy proxy; 92 if (i1 < 0) { 93 proxy = new MethodProxy() { 94 public Object invoke(Object obj, Object [] args) throws Throwable { 95 throw new IllegalArgumentException ("Protected method: " + sig1); 96 } 97 }; 98 } else { 99 proxy = new MethodProxy(); 100 } 101 102 proxy.f1 = f1; 103 proxy.f2 = f2; 104 proxy.i1 = i1; 105 proxy.i2 = i2; 106 proxy.sig = sig1; 107 proxy.superName = name2; 108 return proxy; 109 } 110 111 private static FastClass helper(ClassLoader loader, Class type) { 112 FastClass.Generator g = new FastClass.Generator(); 113 g.setType(type); 114 g.setClassLoader(loader); 115 return g.create(); 116 } 117 118 private MethodProxy() { 119 } 120 121 124 public Signature getSignature() { 125 return sig; 126 } 127 128 134 public String getSuperName() { 135 return superName; 136 } 137 138 145 public int getSuperIndex() { 146 return i2; 147 } 148 149 157 public static MethodProxy find(Class type, Signature sig) { 158 try { 159 Method m = type.getDeclaredMethod(MethodInterceptorGenerator.FIND_PROXY_NAME, 160 MethodInterceptorGenerator.FIND_PROXY_TYPES); 161 return (MethodProxy)m.invoke(null, new Object []{ sig }); 162 } catch (NoSuchMethodException e) { 163 throw new IllegalArgumentException ("Class " + type + " does not use a MethodInterceptor"); 164 } catch (IllegalAccessException e) { 165 throw new CodeGenerationException(e); 166 } catch (InvocationTargetException e) { 167 throw new CodeGenerationException(e); 168 } 169 } 170 171 181 public Object invoke(Object obj, Object [] args) throws Throwable { 182 try { 183 return f1.invoke(i1, obj, args); 184 } catch (InvocationTargetException e) { 185 throw e.getTargetException(); 186 } 187 } 188 189 199 public Object invokeSuper(Object obj, Object [] args) throws Throwable { 200 try { 201 return f2.invoke(i2, obj, args); 202 } catch (InvocationTargetException e) { 203 throw e.getTargetException(); 204 } 205 } 206 } 207 | Popular Tags |