InvocationHandlerGenerator.java revision 674060f01e9090cd21b3c5656cc3204912ad17a6
1/* 2 * Copyright 2003,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 */ 16package org.mockito.cglib.proxy; 17 18import java.util.*; 19 20import org.mockito.asm.Type; 21import org.mockito.cglib.core.*; 22 23class InvocationHandlerGenerator 24implements CallbackGenerator 25{ 26 public static final InvocationHandlerGenerator INSTANCE = new InvocationHandlerGenerator(); 27 28 private static final Type INVOCATION_HANDLER = 29 TypeUtils.parseType("org.mockito.cglib.proxy.InvocationHandler"); 30 private static final Type UNDECLARED_THROWABLE_EXCEPTION = 31 TypeUtils.parseType("org.mockito.cglib.proxy.UndeclaredThrowableException"); 32 private static final Type METHOD = 33 TypeUtils.parseType("java.lang.reflect.Method"); 34 private static final Signature INVOKE = 35 TypeUtils.parseSignature("Object invoke(Object, java.lang.reflect.Method, Object[])"); 36 37 public void generate(ClassEmitter ce, Context context, List methods) { 38 for (Iterator it = methods.iterator(); it.hasNext();) { 39 MethodInfo method = (MethodInfo)it.next(); 40 Signature impl = context.getImplSignature(method); 41 ce.declare_field(Constants.PRIVATE_FINAL_STATIC, impl.getName(), METHOD, null); 42 43 CodeEmitter e = context.beginMethod(ce, method); 44 Block handler = e.begin_block(); 45 context.emitCallback(e, context.getIndex(method)); 46 e.load_this(); 47 e.getfield(impl.getName()); 48 e.create_arg_array(); 49 e.invoke_interface(INVOCATION_HANDLER, INVOKE); 50 e.unbox(method.getSignature().getReturnType()); 51 e.return_value(); 52 handler.end(); 53 EmitUtils.wrap_undeclared_throwable(e, handler, method.getExceptionTypes(), UNDECLARED_THROWABLE_EXCEPTION); 54 e.end_method(); 55 } 56 } 57 58 public void generateStatic(CodeEmitter e, Context context, List methods) { 59 for (Iterator it = methods.iterator(); it.hasNext();) { 60 MethodInfo method = (MethodInfo)it.next(); 61 EmitUtils.load_method(e, method); 62 e.putfield(context.getImplSignature(method).getName()); 63 } 64 } 65} 66