KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > proxy > CallbackInfo


1 /*
2  * Copyright 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package net.sf.cglib.proxy;
17
18 import org.objectweb.asm.Type;
19
20 class CallbackInfo
21 {
22     public static Type[] determineTypes(Class JavaDoc[] 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     //////////////////// PRIVATE ////////////////////
47

48     private Class JavaDoc 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 JavaDoc 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 JavaDoc("Callback is null");
71         }
72         return determineType(callback.getClass());
73     }
74
75     private static Type determineType(Class JavaDoc callbackType) {
76         Class JavaDoc 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 JavaDoc("Callback implements both " + cur + " and " + info.cls);
82                 }
83                 cur = info.cls;
84             }
85         }
86         if (cur == null) {
87             throw new IllegalStateException JavaDoc("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 JavaDoc("Unknown callback type " + callbackType);
100     }
101 }
102     
103
104
Popular Tags