KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > expr > Handler


1 /*
2  * Javassist, a Java-bytecode translator toolkit.
3  * Copyright (C) 1999-2005 Shigeru Chiba. All Rights Reserved.
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. Alternatively, the contents of this file may be used under
8  * the terms of the GNU Lesser General Public License Version 2.1 or later.
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  */

15
16 package javassist.expr;
17
18 import javassist.*;
19 import javassist.bytecode.*;
20 import javassist.compiler.*;
21
22 /**
23  * Catch clause.
24  */

25 public class Handler extends Expr {
26     private static String JavaDoc EXCEPTION_NAME = "$1";
27     private ExceptionTable etable;
28     private int index;
29
30     /**
31      * Undocumented constructor. Do not use; internal-use only.
32      */

33     protected Handler(ExceptionTable et, int nth,
34                       CodeIterator it, CtClass declaring, MethodInfo m) {
35         super(et.handlerPc(nth), it, declaring, m);
36         etable = et;
37         index = nth;
38     }
39
40     /**
41      * Returns the method or constructor containing the catch clause.
42      */

43     public CtBehavior where() { return super.where(); }
44
45     /**
46      * Returns the source line number of the catch clause.
47      *
48      * @return -1 if this information is not available.
49      */

50     public int getLineNumber() {
51         return super.getLineNumber();
52     }
53
54     /**
55      * Returns the source file containing the catch clause.
56      *
57      * @return null if this information is not available.
58      */

59     public String JavaDoc getFileName() {
60         return super.getFileName();
61     }
62
63     /**
64      * Returns the list of exceptions that the catch clause may throw.
65      */

66     public CtClass[] mayThrow() {
67         return super.mayThrow();
68     }
69
70     /**
71      * Returns the type handled by the catch clause.
72      */

73     public CtClass getType() throws NotFoundException {
74         ConstPool cp = getConstPool();
75         String JavaDoc name = cp.getClassInfo(etable.catchType(index));
76         return Descriptor.toCtClass(name, thisClass.getClassPool());
77     }
78
79     /**
80      * This method has not been implemented yet.
81      *
82      * @param statement a Java statement.
83      */

84     public void replace(String JavaDoc statement) throws CannotCompileException {
85         throw new RuntimeException JavaDoc("not implemented yet");
86     }
87
88     /**
89      * Inserts bytecode at the beginning of the catch clause.
90      * The caught exception is stored in <code>$1</code>.
91      *
92      * @param src the source code representing the inserted bytecode.
93      * It must be a single statement or block.
94      */

95     public void insertBefore(String JavaDoc src) throws CannotCompileException {
96         edited = true;
97
98         ConstPool cp = getConstPool();
99         CodeAttribute ca = iterator.get();
100         Javac jv = new Javac(thisClass);
101         Bytecode b = jv.getBytecode();
102         b.setStackDepth(1);
103         b.setMaxLocals(ca.getMaxLocals());
104
105         try {
106             CtClass type = getType();
107             int var = jv.recordVariable(type, EXCEPTION_NAME);
108             jv.recordReturnType(type, false);
109             b.addAstore(var);
110             jv.compileStmnt(src);
111             b.addAload(var);
112
113             int oldHandler = etable.handlerPc(index);
114             b.addOpcode(Opcode.GOTO);
115             b.addIndex(oldHandler - iterator.getCodeLength()
116                        - b.currentPc() + 1);
117
118             maxStack = b.getMaxStack();
119             maxLocals = b.getMaxLocals();
120
121             int pos = iterator.append(b.get());
122             iterator.append(b.getExceptionTable(), pos);
123             etable.setHandlerPc(index, pos);
124         }
125         catch (NotFoundException e) {
126             throw new CannotCompileException(e);
127         }
128         catch (CompileError e) {
129             throw new CannotCompileException(e);
130         }
131     }
132 }
133
Popular Tags