InvocationHandlerGenerator.java revision 674060f01e9090cd21b3c5656cc3204912ad17a6
17f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project/*
27f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project * Copyright 2003,2004 The Apache Software Foundation
37f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project *
47f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project *  Licensed under the Apache License, Version 2.0 (the "License");
57f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project * you may not use this file except in compliance with the License.
67f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project * You may obtain a copy of the License at
77f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project *
87f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
97f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project *
107f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
117f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
127f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project * See the License for the specific language governing permissions and
147f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project * limitations under the License.
157f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project */
167f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectpackage org.mockito.cglib.proxy;
177f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
187f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectimport java.util.*;
197f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
207f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectimport org.mockito.asm.Type;
217f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectimport org.mockito.cglib.core.*;
227f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
237f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectclass InvocationHandlerGenerator
247f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Projectimplements CallbackGenerator
257f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project{
267f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    public static final InvocationHandlerGenerator INSTANCE = new InvocationHandlerGenerator();
277f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
287f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    private static final Type INVOCATION_HANDLER =
297f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project      TypeUtils.parseType("org.mockito.cglib.proxy.InvocationHandler");
307f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    private static final Type UNDECLARED_THROWABLE_EXCEPTION =
317f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project      TypeUtils.parseType("org.mockito.cglib.proxy.UndeclaredThrowableException");
327f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    private static final Type METHOD =
337f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project      TypeUtils.parseType("java.lang.reflect.Method");
347f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    private static final Signature INVOKE =
357f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project      TypeUtils.parseSignature("Object invoke(Object, java.lang.reflect.Method, Object[])");
367f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
377f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    public void generate(ClassEmitter ce, Context context, List methods) {
387f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project        for (Iterator it = methods.iterator(); it.hasNext();) {
397f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            MethodInfo method = (MethodInfo)it.next();
407f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            Signature impl = context.getImplSignature(method);
417f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            ce.declare_field(Constants.PRIVATE_FINAL_STATIC, impl.getName(), METHOD, null);
427f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
437f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            CodeEmitter e = context.beginMethod(ce, method);
447f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            Block handler = e.begin_block();
457f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            context.emitCallback(e, context.getIndex(method));
467f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            e.load_this();
477f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            e.getfield(impl.getName());
487f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            e.create_arg_array();
497f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            e.invoke_interface(INVOCATION_HANDLER, INVOKE);
507f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            e.unbox(method.getSignature().getReturnType());
517f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            e.return_value();
527f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            handler.end();
537f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            EmitUtils.wrap_undeclared_throwable(e, handler, method.getExceptionTypes(), UNDECLARED_THROWABLE_EXCEPTION);
547f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            e.end_method();
557f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project        }
567f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    }
577f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project
587f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    public void generateStatic(CodeEmitter e, Context context, List methods) {
597f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project        for (Iterator it = methods.iterator(); it.hasNext();) {
607f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            MethodInfo method = (MethodInfo)it.next();
617f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            EmitUtils.load_method(e, method);
627f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project            e.putfield(context.getImplSignature(method).getName());
637f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project        }
647f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project    }
657f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project}
667f81d9b6fa7f2ec161b682622db577a28c90b49fThe Android Open Source Project