KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > transform > impl > UndeclaredThrowableStrategy


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  */

16 package net.sf.cglib.transform.impl;
17
18 import net.sf.cglib.core.*;
19 import net.sf.cglib.transform.*;
20 import org.objectweb.asm.Attribute;
21
22 /**
23  * A {@link GeneratorStrategy} suitable for use with {@link net.sf.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  */

27 public 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 JavaDoc 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 JavaDoc name, String JavaDoc desc, String JavaDoc signature, String JavaDoc[] exceptions) {
45             return !TypeUtils.isPrivate(access) && name.indexOf('$') < 0;
46         }
47     };
48
49     protected ClassGenerator transform(ClassGenerator cg) throws Exception JavaDoc {
50         return new TransformingClassGenerator(cg, t);
51     }
52 }
53
54
Popular Tags