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