01: /* CheckCastOperator Copyright (C) 1998-2002 Jochen Hoenicke.
02: *
03: * This program is free software; you can redistribute it and/or modify
04: * it under the terms of the GNU Lesser General Public License as published by
05: * the Free Software Foundation; either version 2, or (at your option)
06: * any later version.
07: *
08: * This program is distributed in the hope that it will be useful,
09: * 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:
22: import jode.type.Type;
23: import jode.decompiler.TabbedPrintWriter;
24:
25: public class CheckCastOperator extends Operator {
26: Type castType;
27:
28: public CheckCastOperator(Type type) {
29: super (type, 0);
30: castType = type;
31: initOperands(1);
32: }
33:
34: public int getPriority() {
35: return 700;
36: }
37:
38: public void updateSubTypes() {
39: subExpressions[0].setType(Type.tUObject);
40: }
41:
42: public void updateType() {
43: }
44:
45: public Expression simplify() {
46: if (subExpressions[0].getType().getCanonic().isOfType(
47: Type.tSubType(castType)))
48: /* This is an unnecessary widening cast, probably that inserted
49: * by jikes for inner classes constructors.
50: */
51: return subExpressions[0].simplify();
52: return super .simplify();
53: }
54:
55: public void dumpExpression(TabbedPrintWriter writer)
56: throws java.io.IOException {
57: writer.print("(");
58: writer.printType(castType);
59: writer.print(") ");
60: writer.breakOp();
61:
62: /* There are special cases where a cast isn't allowed. We must cast
63: * to the common super type before. This cases always give a runtime
64: * error, but we want to decompile even bad programs.
65: */
66: Type super Type = castType.getCastHelper(subExpressions[0]
67: .getType());
68: if (super Type != null) {
69: writer.print("(");
70: writer.printType(super Type);
71: writer.print(") ");
72: writer.breakOp();
73: }
74: subExpressions[0].dumpExpression(writer, 700);
75: }
76: }
|