AddInitTransformer.java revision 674060f01e9090cd21b3c5656cc3204912ad17a6
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 */ 16package org.mockito.cglib.transform.impl; 17 18import java.lang.reflect.Method; 19 20 21import org.mockito.asm.Attribute; 22import org.mockito.asm.Type; 23import org.mockito.cglib.core.CodeEmitter; 24import org.mockito.cglib.core.Constants; 25import org.mockito.cglib.core.MethodInfo; 26import org.mockito.cglib.core.ReflectUtils; 27import org.mockito.cglib.core.Signature; 28import org.mockito.cglib.transform.ClassEmitterTransformer; 29 30/** 31 * @author Mark Hobson 32 */ 33public class AddInitTransformer extends ClassEmitterTransformer { 34 private MethodInfo info; 35 36 public AddInitTransformer(Method method) { 37 info = ReflectUtils.getMethodInfo(method); 38 39 Type[] types = info.getSignature().getArgumentTypes(); 40 if (types.length != 1 || 41 !types[0].equals(Constants.TYPE_OBJECT) || 42 !info.getSignature().getReturnType().equals(Type.VOID_TYPE)) { 43 throw new IllegalArgumentException(method + " illegal signature"); 44 } 45 } 46 47 public CodeEmitter begin_method(int access, Signature sig, Type[] exceptions) { 48 final CodeEmitter emitter = super.begin_method(access, sig, exceptions); 49 if (sig.getName().equals(Constants.CONSTRUCTOR_NAME)) { 50 return new CodeEmitter(emitter) { 51 public void visitInsn(int opcode) { 52 if (opcode == Constants.RETURN) { 53 load_this(); 54 invoke(info); 55 } 56 super.visitInsn(opcode); 57 } 58 }; 59 } 60 return emitter; 61 } 62} 63 64