UndeclaredThrowableTransformer.java revision 674060f01e9090cd21b3c5656cc3204912ad17a6
1/* 2 * Copyright 2003 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.Constructor; 19 20import org.mockito.asm.Attribute; 21import org.mockito.asm.ClassVisitor; 22import org.mockito.asm.Type; 23import org.mockito.cglib.core.*; 24import org.mockito.cglib.transform.*; 25 26public class UndeclaredThrowableTransformer extends ClassEmitterTransformer { 27 private Type wrapper; 28 29 public UndeclaredThrowableTransformer(Class wrapper) { 30 this.wrapper = Type.getType(wrapper); 31 boolean found = false; 32 Constructor[] cstructs = wrapper.getConstructors(); 33 for (int i = 0; i < cstructs.length; i++) { 34 Class[] types = cstructs[i].getParameterTypes(); 35 if (types.length == 1 && types[0].equals(Throwable.class)) { 36 found = true; 37 break; 38 } 39 } 40 if (!found) 41 throw new IllegalArgumentException(wrapper + " does not have a single-arg constructor that takes a Throwable"); 42 } 43 44 public CodeEmitter begin_method(int access, final Signature sig, final Type[] exceptions) { 45 CodeEmitter e = super.begin_method(access, sig, exceptions); 46 if (TypeUtils.isAbstract(access) || sig.equals(Constants.SIG_STATIC)) { 47 return e; 48 } 49 return new CodeEmitter(e) { 50 private Block handler; 51 /* init */ { 52 handler = begin_block(); 53 } 54 public void visitMaxs(int maxStack, int maxLocals) { 55 handler.end(); 56 EmitUtils.wrap_undeclared_throwable(this, handler, exceptions, wrapper); 57 super.visitMaxs(maxStack, maxLocals); 58 } 59 }; 60 } 61} 62