KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javassist > expr > Instanceof


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  * Instanceof operator.
25  */

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

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

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

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

57     public String JavaDoc getFileName() {
58         return super.getFileName();
59     }
60
61     /**
62      * Returns the <code>CtClass</code> object representing
63      * the type name on the right hand side
64      * of the instanceof operator.
65      */

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

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

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

117             checkResultValue(retType, statement);
118
119             Bytecode bytecode = jc.getBytecode();
120             storeStack(params, true, paramVar, bytecode);
121             jc.recordLocalVariables(ca, pos);
122
123             bytecode.addConstZero(retType);
124             bytecode.addStore(retVar, retType); // initialize $_
125

126             jc.compileStmnt(statement);
127             bytecode.addLoad(retVar, retType);
128
129             replace0(pos, bytecode, 3);
130         }
131         catch (CompileError e) { throw new CannotCompileException(e); }
132         catch (NotFoundException e) { throw new CannotCompileException(e); }
133         catch (BadBytecode e) {
134             throw new CannotCompileException("broken method");
135         }
136     }
137
138     /* boolean $proceed(Object obj)
139      */

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