1 16 package net.sf.cglib.proxy; 17 18 import java.lang.reflect.*; 19 import java.util.Arrays ; 20 import java.util.List ; 21 import net.sf.cglib.core.*; 22 import net.sf.cglib.reflect.*; 23 24 31 public class MethodProxy { 32 private Signature sig1; 33 private Signature sig2; 34 private CreateInfo createInfo; 35 private FastClass f1; 36 private FastClass f2; 37 private int i1; 38 private int i2; 39 40 44 public static MethodProxy create(Class c1, Class c2, String desc, String name1, String name2) { 45 MethodProxy proxy = new MethodProxy(); 46 proxy.sig1 = new Signature(name1, desc); 47 proxy.sig2 = new Signature(name2, desc); 48 proxy.createInfo = new CreateInfo(c1, c2); 49 return proxy; 50 } 51 52 private void init() 53 { 54 CreateInfo ci = createInfo; 55 if (ci != null) { 56 f1 = helper(ci, ci.c1); 57 f2 = helper(ci, ci.c2); 58 i1 = f1.getIndex(sig1); 59 i2 = f2.getIndex(sig2); 60 createInfo = null; 61 } 62 } 63 64 private static class CreateInfo 65 { 66 Class c1; 67 Class c2; 68 NamingPolicy namingPolicy; 69 GeneratorStrategy strategy; 70 boolean attemptLoad; 71 72 public CreateInfo(Class c1, Class c2) 73 { 74 this.c1 = c1; 75 this.c2 = c2; 76 AbstractClassGenerator fromEnhancer = AbstractClassGenerator.getCurrent(); 77 if (fromEnhancer != null) { 78 namingPolicy = fromEnhancer.getNamingPolicy(); 79 strategy = fromEnhancer.getStrategy(); 80 attemptLoad = fromEnhancer.getAttemptLoad(); 81 } 82 } 83 } 84 85 private static FastClass helper(CreateInfo ci, Class type) { 86 FastClass.Generator g = new FastClass.Generator(); 87 g.setType(type); 88 g.setClassLoader(ci.c2.getClassLoader()); 89 g.setNamingPolicy(ci.namingPolicy); 90 g.setStrategy(ci.strategy); 91 g.setAttemptLoad(ci.attemptLoad); 92 return g.create(); 93 } 94 95 private MethodProxy() { 96 } 97 98 101 public Signature getSignature() { 102 return sig1; 103 } 104 105 111 public String getSuperName() { 112 return sig2.getName(); 113 } 114 115 122 public int getSuperIndex() { 123 init(); 124 return i2; 125 } 126 127 135 public static MethodProxy find(Class type, Signature sig) { 136 try { 137 Method m = type.getDeclaredMethod(MethodInterceptorGenerator.FIND_PROXY_NAME, 138 MethodInterceptorGenerator.FIND_PROXY_TYPES); 139 return (MethodProxy)m.invoke(null, new Object []{ sig }); 140 } catch (NoSuchMethodException e) { 141 throw new IllegalArgumentException ("Class " + type + " does not use a MethodInterceptor"); 142 } catch (IllegalAccessException e) { 143 throw new CodeGenerationException(e); 144 } catch (InvocationTargetException e) { 145 throw new CodeGenerationException(e); 146 } 147 } 148 149 159 public Object invoke(Object obj, Object [] args) throws Throwable { 160 try { 161 if (f1 == null) 162 init(); 163 return f1.invoke(i1, obj, args); 164 } catch (InvocationTargetException e) { 165 throw e.getTargetException(); 166 } catch (IllegalArgumentException e) { 167 if (i1 < 0) 168 throw new IllegalArgumentException ("Protected method: " + sig1); 169 throw e; 170 } 171 } 172 173 183 public Object invokeSuper(Object obj, Object [] args) throws Throwable { 184 try { 185 if (f2 == null) 186 init(); 187 return f2.invoke(i2, obj, args); 188 } catch (InvocationTargetException e) { 189 throw e.getTargetException(); 190 } 191 } 192 } 193 | Popular Tags |