KickJava   Java API By Example, From Geeks To Geeks.

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


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