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 org.mockito.asm.Attribute; 19import org.mockito.cglib.core.*; 20import org.mockito.cglib.transform.*; 21 22/** 23 * A {@link GeneratorStrategy} suitable for use with {@link org.mockito.cglib.Enhancer} which 24 * causes all undeclared exceptions thrown from within a proxied method to be wrapped 25 * in an alternative exception of your choice. 26 */ 27public class UndeclaredThrowableStrategy extends DefaultGeneratorStrategy { 28 private ClassTransformer t; 29 30 /** 31 * Create a new instance of this strategy. 32 * @param wrapper a class which extends either directly or 33 * indirectly from <code>Throwable</code> and which has at least one 34 * constructor that takes a single argument of type 35 * <code>Throwable</code>, for example 36 * <code>java.lang.reflect.UndeclaredThrowableException.class</code> 37 */ 38 public UndeclaredThrowableStrategy(Class wrapper) { 39 t = new UndeclaredThrowableTransformer(wrapper); 40 t = new MethodFilterTransformer(TRANSFORM_FILTER, t); 41 } 42 43 private static final MethodFilter TRANSFORM_FILTER = new MethodFilter() { 44 public boolean accept(int access, String name, String desc, String signature, String[] exceptions) { 45 return !TypeUtils.isPrivate(access) && name.indexOf('$') < 0; 46 } 47 }; 48 49 protected ClassGenerator transform(ClassGenerator cg) throws Exception { 50 return new TransformingClassGenerator(cg, t); 51 } 52} 53 54