KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > expr > IfThenElseOperator


1 /* IfThenElseOperator 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: IfThenElseOperator.java,v 2.17.2.3 2002/05/28 17:34:06 hoenicke Exp $
18  */

19
20 package jode.expr;
21 import jode.type.Type;
22 import jode.decompiler.FieldAnalyzer;
23 import jode.decompiler.TabbedPrintWriter;
24
25 public class IfThenElseOperator extends Operator {
26     public IfThenElseOperator(Type type) {
27         super(type, 0);
28     initOperands(3);
29     }
30     
31     public int getPriority() {
32         return 200;
33     }
34
35     public void updateSubTypes() {
36     subExpressions[0].setType(Type.tBoolean);
37     subExpressions[1].setType(Type.tSubType(type));
38     subExpressions[2].setType(Type.tSubType(type));
39     }
40
41     public void updateType() {
42     Type commonType = Type.tSuperType(subExpressions[1].getType())
43         .intersection(Type.tSuperType(subExpressions[2].getType()));
44     updateParentType(commonType);
45     }
46
47     public Expression simplify() {
48     if (getType().isOfType(Type.tBoolean)) {
49             if (subExpressions[1] instanceof ConstOperator
50         && subExpressions[2] instanceof ConstOperator) {
51                 ConstOperator c1 = (ConstOperator) subExpressions[1];
52                 ConstOperator c2 = (ConstOperator) subExpressions[2];
53                 if (c1.getValue().equals(new Integer JavaDoc(1)) &&
54                     c2.getValue().equals(new Integer JavaDoc(0)))
55                     return subExpressions[0].simplify();
56                 if (c2.getValue().equals(new Integer JavaDoc(1)) &&
57                     c1.getValue().equals(new Integer JavaDoc(0)))
58                     return subExpressions[0].negate().simplify();
59             }
60         }
61     if (subExpressions[0] instanceof CompareUnaryOperator
62         && ((((CompareUnaryOperator) subExpressions[0])
63         .getOperatorIndex() & ~1) == Operator.COMPARE_OP)) {
64         CompareUnaryOperator cmp
65         = (CompareUnaryOperator) subExpressions[0];
66         int cmpType = cmp.getOperatorIndex() & 1;
67         if ((subExpressions[2 - cmpType] instanceof GetFieldOperator)
68         && (subExpressions[1 + cmpType] instanceof StoreInstruction)) {
69         // Check for
70
// class$classname != null ? class$classname :
71
// (class$classname = class$("classname"))
72
// and replace with
73
// classname.class
74
GetFieldOperator get
75             = (GetFieldOperator) subExpressions[2 - cmpType];
76         StoreInstruction put
77             = (StoreInstruction) subExpressions[1 + cmpType];
78         int opIndex = cmp.getOperatorIndex();
79         FieldAnalyzer field;
80         if (put.getLValue() instanceof PutFieldOperator
81             && ((field = ((PutFieldOperator)put.getLValue())
82              .getField()) != null) && field.isSynthetic()
83             && put.lvalueMatches(get)
84             && (cmp.subExpressions[0] instanceof GetFieldOperator)
85             && put.lvalueMatches((GetFieldOperator)
86                      cmp.subExpressions[0])
87             && put.subExpressions[1] instanceof InvokeOperator) {
88             InvokeOperator invoke = (InvokeOperator)
89             put.subExpressions[1];
90             if (invoke.isGetClass()
91             && invoke.subExpressions[0] instanceof ConstOperator
92             && (invoke.subExpressions[0].getType()
93                 .equals(Type.tString))) {
94             String JavaDoc clazz = (String JavaDoc)
95                 ((ConstOperator)invoke.subExpressions[0])
96                 .getValue();
97             if (field.setClassConstant(clazz))
98                 return new ClassFieldOperator
99                 (clazz.charAt(0) == '['
100                  ? Type.tType(clazz) : Type.tClass(clazz));
101             }
102         }
103         }
104     }
105     return super.simplify();
106     }
107
108     public boolean opEquals(Operator o) {
109     return (o instanceof IfThenElseOperator);
110     }
111
112     public void dumpExpression(TabbedPrintWriter writer)
113     throws java.io.IOException JavaDoc {
114     subExpressions[0].dumpExpression(writer, 201);
115     writer.breakOp();
116     writer.print(" ? ");
117     int subPriority = 0;
118     if (!subExpressions[1].getType().getHint().isOfType
119         (subExpressions[2].getType())) {
120         writer.startOp(writer.IMPL_PAREN, 2);
121         /* We need a cast here */
122         writer.print("(");
123         writer.printType(getType().getHint());
124         writer.print(") ");
125         subPriority = 700;
126     }
127     subExpressions[1].dumpExpression(writer, subPriority);
128     if (subPriority == 700)
129         writer.endOp();
130     writer.breakOp();
131     writer.print(" : ");
132     subExpressions[2].dumpExpression(writer, 200);
133     }
134 }
135
Popular Tags