1 16 package net.sf.cglib.proxy; 17 18 import org.objectweb.asm.Type; 19 20 class CallbackInfo 21 { 22 public static Type[] determineTypes(Class [] callbackTypes) { 23 Type[] types = new Type[callbackTypes.length]; 24 for (int i = 0; i < types.length; i++) { 25 types[i] = determineType(callbackTypes[i]); 26 } 27 return types; 28 } 29 30 public static Type[] determineTypes(Callback[] callbacks) { 31 Type[] types = new Type[callbacks.length]; 32 for (int i = 0; i < types.length; i++) { 33 types[i] = determineType(callbacks[i]); 34 } 35 return types; 36 } 37 38 public static CallbackGenerator[] getGenerators(Type[] callbackTypes) { 39 CallbackGenerator[] generators = new CallbackGenerator[callbackTypes.length]; 40 for (int i = 0; i < generators.length; i++) { 41 generators[i] = getGenerator(callbackTypes[i]); 42 } 43 return generators; 44 } 45 46 48 private Class cls; 49 private CallbackGenerator generator; 50 private Type type; 51 52 private static final CallbackInfo[] CALLBACKS = { 53 new CallbackInfo(NoOp.class, NoOpGenerator.INSTANCE), 54 new CallbackInfo(MethodInterceptor.class, MethodInterceptorGenerator.INSTANCE), 55 new CallbackInfo(InvocationHandler.class, InvocationHandlerGenerator.INSTANCE), 56 new CallbackInfo(LazyLoader.class, LazyLoaderGenerator.INSTANCE), 57 new CallbackInfo(Dispatcher.class, DispatcherGenerator.INSTANCE), 58 new CallbackInfo(FixedValue.class, FixedValueGenerator.INSTANCE), 59 new CallbackInfo(ProxyRefDispatcher.class, DispatcherGenerator.PROXY_REF_INSTANCE), 60 }; 61 62 private CallbackInfo(Class cls, CallbackGenerator generator) { 63 this.cls = cls; 64 this.generator = generator; 65 type = Type.getType(cls); 66 } 67 68 private static Type determineType(Callback callback) { 69 if (callback == null) { 70 throw new IllegalStateException ("Callback is null"); 71 } 72 return determineType(callback.getClass()); 73 } 74 75 private static Type determineType(Class callbackType) { 76 Class cur = null; 77 for (int i = 0; i < CALLBACKS.length; i++) { 78 CallbackInfo info = CALLBACKS[i]; 79 if (info.cls.isAssignableFrom(callbackType)) { 80 if (cur != null) { 81 throw new IllegalStateException ("Callback implements both " + cur + " and " + info.cls); 82 } 83 cur = info.cls; 84 } 85 } 86 if (cur == null) { 87 throw new IllegalStateException ("Unknown callback type " + callbackType); 88 } 89 return Type.getType(cur); 90 } 91 92 private static CallbackGenerator getGenerator(Type callbackType) { 93 for (int i = 0; i < CALLBACKS.length; i++) { 94 CallbackInfo info = CALLBACKS[i]; 95 if (info.type.equals(callbackType)) { 96 return info.generator; 97 } 98 } 99 throw new IllegalStateException ("Unknown callback type " + callbackType); 100 } 101 } 102 103 104 | Popular Tags |