KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sf.cglib.core.ReflectUtils;
19 import java.lang.reflect.Method JavaDoc;
20 import java.util.*;
21
22 /**
23  * @version $Id: CallbackHelper.java,v 1.2 2004/06/24 21:15:20 herbyderby Exp $
24  */

25 abstract public class CallbackHelper
26 implements CallbackFilter
27 {
28     private Map methodMap = new HashMap();
29     private List callbacks = new ArrayList();
30     
31     public CallbackHelper(Class JavaDoc superclass, Class JavaDoc[] interfaces)
32     {
33         List methods = new ArrayList();
34         Enhancer.getMethods(superclass, interfaces, methods);
35         Map indexes = new HashMap();
36         for (int i = 0, size = methods.size(); i < size; i++) {
37             Method JavaDoc method = (Method JavaDoc)methods.get(i);
38             Object JavaDoc callback = getCallback(method);
39             if (callback == null)
40                 throw new IllegalStateException JavaDoc("getCallback cannot return null");
41             boolean isCallback = callback instanceof Callback;
42             if (!(isCallback || (callback instanceof Class JavaDoc)))
43                 throw new IllegalStateException JavaDoc("getCallback must return a Callback or a Class");
44             if (i > 0 && ((callbacks.get(i - 1) instanceof Callback) ^ isCallback))
45                 throw new IllegalStateException JavaDoc("getCallback must return a Callback or a Class consistently for every Method");
46             Integer JavaDoc index = (Integer JavaDoc)indexes.get(callback);
47             if (index == null) {
48                 index = new Integer JavaDoc(callbacks.size());
49                 indexes.put(callback, index);
50             }
51             methodMap.put(method, index);
52             callbacks.add(callback);
53         }
54     }
55
56     abstract protected Object JavaDoc getCallback(Method JavaDoc method);
57
58     public Callback[] getCallbacks()
59     {
60         if (callbacks.size() == 0)
61             return new Callback[0];
62         if (callbacks.get(0) instanceof Callback) {
63             return (Callback[])callbacks.toArray(new Callback[callbacks.size()]);
64         } else {
65             throw new IllegalStateException JavaDoc("getCallback returned classes, not callbacks; call getCallbackTypes instead");
66         }
67     }
68
69     public Class JavaDoc[] getCallbackTypes()
70     {
71         if (callbacks.size() == 0)
72             return new Class JavaDoc[0];
73         if (callbacks.get(0) instanceof Callback) {
74             return ReflectUtils.getClasses(getCallbacks());
75         } else {
76             return (Class JavaDoc[])callbacks.toArray(new Class JavaDoc[callbacks.size()]);
77         }
78     }
79
80     public int accept(Method JavaDoc method)
81     {
82         return ((Integer JavaDoc)methodMap.get(method)).intValue();
83     }
84
85     public int hashCode()
86     {
87         return methodMap.hashCode();
88     }
89     
90     public boolean equals(Object JavaDoc o)
91     {
92         if (o == null)
93             return false;
94         if (!(o instanceof CallbackHelper))
95             return false;
96         return methodMap.equals(((CallbackHelper)o).methodMap);
97     }
98 }
99
Popular Tags