KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > expr > CheckCastOperator


1 /* CheckCastOperator Copyright (C) 1998-2002 Jochen Hoenicke.
2  *
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU Lesser General Public License as published by
5  * the Free Software Foundation; either version 2, or (at your option)
6  * any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; see the file COPYING.LESSER. If not, write to
15  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  * $Id: CheckCastOperator.java,v 4.12.2.1 2002/05/28 17:34:06 hoenicke Exp $
18  */

19
20 package jode.expr;
21 import jode.type.Type;
22 import jode.decompiler.TabbedPrintWriter;
23
24 public class CheckCastOperator extends Operator {
25     Type castType;
26
27     public CheckCastOperator(Type type) {
28         super(type, 0);
29         castType = type;
30     initOperands(1);
31     }
32
33     public int getPriority() {
34         return 700;
35     }
36
37     public void updateSubTypes() {
38     subExpressions[0].setType(Type.tUObject);
39     }
40
41     public void updateType() {
42     }
43
44     public Expression simplify() {
45     if (subExpressions[0].getType().getCanonic()
46         .isOfType(Type.tSubType(castType)))
47         /* This is an unnecessary widening cast, probably that inserted
48          * by jikes for inner classes constructors.
49          */

50         return subExpressions[0].simplify();
51     return super.simplify();
52     }
53
54     public void dumpExpression(TabbedPrintWriter writer)
55     throws java.io.IOException JavaDoc {
56     writer.print("(");
57     writer.printType(castType);
58     writer.print(") ");
59     writer.breakOp();
60
61     /* There are special cases where a cast isn't allowed. We must cast
62      * to the common super type before. This cases always give a runtime
63      * error, but we want to decompile even bad programs.
64      */

65     Type superType = castType.getCastHelper(subExpressions[0].getType());
66     if (superType != null) {
67         writer.print("(");
68         writer.printType(superType);
69         writer.print(") ");
70         writer.breakOp();
71     }
72     subExpressions[0].dumpExpression(writer, 700);
73     }
74 }
75
Popular Tags