/Users/lyon/j4p/src/javassist/expr/Handler.java

1    /* 
2     * Javassist, a Java-bytecode translator toolkit. 
3     * Copyright (C) 1999-2003 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   import javassist.compiler.ast.ASTList; 
22    
23   /** 
24    * Catch clause. 
25    */ 
26   public class Handler extends Expr { 
27       private static String EXCEPTION_NAME = "$1"; 
28       private ExceptionTable etable; 
29       private int index; 
30    
31       /** 
32        * Undocumented constructor.  Do not use; internal-use only. 
33        */ 
34       Handler(ExceptionTable et, int nth, 
35               CodeIterator it, CtClass declaring, MethodInfo m) { 
36           super(et.handlerPc(nth), it, declaring, m); 
37           etable = et; 
38           index = nth; 
39       } 
40    
41       /** 
42        * Returns the method or constructor containing the catch clause. 
43        */ 
44       public CtBehavior where() { 
45           return super.where(); 
46       } 
47    
48       /** 
49        * Returns the source line number of the catch clause. 
50        * 
51        * @return -1       if this information is not available. 
52        */ 
53       public int getLineNumber() { 
54           return super.getLineNumber(); 
55       } 
56    
57       /** 
58        * Returns the source file containing the catch clause. 
59        * 
60        * @return null     if this information is not available. 
61        */ 
62       public String getFileName() { 
63           return super.getFileName(); 
64       } 
65    
66       /** 
67        * Returns the list of exceptions that the catch clause may throw. 
68        */ 
69       public CtClass[] mayThrow() { 
70           return super.mayThrow(); 
71       } 
72    
73       /** 
74        * Returns the type handled by the catch clause. 
75        */ 
76       public CtClass getType() throws NotFoundException { 
77           ConstPool cp = getConstPool(); 
78           String name = cp.getClassInfo(etable.catchType(index)); 
79           return Descriptor.toCtClass(name, thisClass.getClassPool()); 
80       } 
81    
82       /** 
83        * This method has not been implemented yet. 
84        * 
85        * @param statement         a Java statement. 
86        */ 
87       public void replace(String statement) throws CannotCompileException { 
88           throw new RuntimeException("not implemented yet"); 
89       } 
90    
91       /** 
92        * Inserts bytecode at the beginning of the catch clause. 
93        * The caught exception is stored in <code>$1</code>. 
94        * 
95        * @param src       the source code representing the inserted bytecode. 
96        *                  It must be a single statement or block. 
97        */ 
98       public void insertBefore(String src) throws CannotCompileException { 
99           edited = true; 
100   
101          ConstPool cp = getConstPool(); 
102          CodeAttribute ca = iterator.get(); 
103          Javac jv = new Javac(thisClass); 
104          Bytecode b = jv.getBytecode(); 
105          b.setStackDepth(1); 
106          b.setMaxLocals(ca.getMaxLocals()); 
107   
108          try { 
109              CtClass type = getType(); 
110              int var = jv.recordVariable(type, EXCEPTION_NAME); 
111              jv.recordReturnType(type, false); 
112              b.addAstore(var); 
113              jv.compileStmnt(src); 
114              b.addAload(var); 
115   
116              int oldHandler = etable.handlerPc(index); 
117              b.addOpcode(Opcode.GOTO); 
118              b.addIndex(oldHandler - iterator.getCodeLength() 
119                      - b.currentPc() + 1); 
120   
121              maxStack = b.getMaxStack(); 
122              maxLocals = b.getMaxLocals(); 
123   
124              int pos = iterator.append(b.get()); 
125              iterator.append(b.getExceptionTable(), pos); 
126              etable.setHandlerPc(index, pos); 
127          } catch (NotFoundException e) { 
128              throw new CannotCompileException(e); 
129          } catch (CompileError e) { 
130              throw new CannotCompileException(e); 
131          } 
132      } 
133  } 
134