KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > expr > Cast


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 import javassist.compiler.ast.ASTList;
22
23 /**
24  * Explicit type cast.
25  */

26 public class Cast extends Expr {
27     /**
28      * Undocumented constructor. Do not use; internal-use only.
29      */

30     protected Cast(int pos, CodeIterator i, CtClass declaring, MethodInfo m) {
31         super(pos, i, declaring, m);
32     }
33
34     /**
35      * Returns the method or constructor containing the type cast
36      * expression represented by this object.
37      */

38     public CtBehavior where() { return super.where(); }
39
40     /**
41      * Returns the line number of the source line containing the
42      * type-cast expression.
43      *
44      * @return -1 if this information is not available.
45      */

46     public int getLineNumber() {
47         return super.getLineNumber();
48     }
49
50     /**
51      * Returns the source file containing the type-cast expression.
52      *
53      * @return null if this information is not available.
54      */

55     public String JavaDoc getFileName() {
56         return super.getFileName();
57     }
58
59     /**
60      * Returns the <code>CtClass</code> object representing
61      * the type specified by the cast.
62      */

63     public CtClass getType() throws NotFoundException {
64         ConstPool cp = getConstPool();
65         int pos = currentPos;
66         int index = iterator.u16bitAt(pos + 1);
67         String JavaDoc name = cp.getClassInfo(index);
68         return Descriptor.toCtClass(name, thisClass.getClassPool());
69     }
70
71     /**
72      * Returns the list of exceptions that the expression may throw.
73      * This list includes both the exceptions that the try-catch statements
74      * including the expression can catch and the exceptions that
75      * the throws declaration allows the method to throw.
76      */

77     public CtClass[] mayThrow() {
78         return super.mayThrow();
79     }
80
81     /**
82      * Replaces the explicit cast operator with the bytecode derived from
83      * the given source text.
84      *
85      * <p>$0 is available but the value is <code>null</code>.
86      *
87      * @param statement a Java statement.
88      */

89     public void replace(String JavaDoc statement) throws CannotCompileException {
90         ConstPool constPool = getConstPool();
91         int pos = currentPos;
92         int index = iterator.u16bitAt(pos + 1);
93
94         Javac jc = new Javac(thisClass);
95         ClassPool cp = thisClass.getClassPool();
96         CodeAttribute ca = iterator.get();
97
98         try {
99             CtClass[] params
100                 = new CtClass[] { cp.get(javaLangObject) };
101             CtClass retType = getType();
102
103             int paramVar = ca.getMaxLocals();
104             jc.recordParams(javaLangObject, params, true, paramVar,
105                             withinStatic());
106             int retVar = jc.recordReturnType(retType, true);
107             jc.recordProceed(new ProceedForCast(index, retType));
108
109             /* Is $_ included in the source code?
110              */

111             checkResultValue(retType, statement);
112
113             Bytecode bytecode = jc.getBytecode();
114             storeStack(params, true, paramVar, bytecode);
115             jc.recordLocalVariables(ca, pos);
116
117             bytecode.addConstZero(retType);
118             bytecode.addStore(retVar, retType); // initialize $_
119

120             jc.compileStmnt(statement);
121             bytecode.addLoad(retVar, retType);
122
123             replace0(pos, bytecode, 3);
124         }
125         catch (CompileError e) { throw new CannotCompileException(e); }
126         catch (NotFoundException e) { throw new CannotCompileException(e); }
127         catch (BadBytecode e) {
128             throw new CannotCompileException("broken method");
129         }
130     }
131
132     /* <type> $proceed(Object obj)
133      */

134     static class ProceedForCast implements ProceedHandler {
135         int index;
136         CtClass retType;
137
138         ProceedForCast(int i, CtClass t) {
139             index = i;
140             retType = t;
141         }
142
143         public void doit(JvstCodeGen gen, Bytecode bytecode, ASTList args)
144             throws CompileError
145         {
146             if (gen.getMethodArgsLength(args) != 1)
147                 throw new CompileError(Javac.proceedName
148                         + "() cannot take more than one parameter "
149                         + "for cast");
150
151             gen.atMethodArgs(args, new int[1], new int[1], new String JavaDoc[1]);
152             bytecode.addOpcode(Opcode.CHECKCAST);
153             bytecode.addIndex(index);
154             gen.setType(retType);
155         }
156         
157         public void setReturnType(JvstTypeChecker c, ASTList args)
158             throws CompileError
159         {
160             c.atMethodArgs(args, new int[1], new int[1], new String JavaDoc[1]);
161             c.setType(retType);
162         }
163     }
164 }
165
Popular Tags