KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > expr > ConstantArrayOperator


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

19
20 package jode.expr;
21 import jode.type.Type;
22 import jode.type.ArrayType;
23 import jode.decompiler.TabbedPrintWriter;
24
25 public class ConstantArrayOperator extends Operator {
26     boolean isInitializer;
27     ConstOperator empty;
28     Type argType;
29
30     public ConstantArrayOperator(Type type, int size) {
31         super(type);
32         argType = (type instanceof ArrayType)
33             ? Type.tSubType(((ArrayType)type).getElementType()) : Type.tError;
34
35     Object JavaDoc emptyVal;
36     if (argType == type.tError || argType.isOfType(Type.tUObject))
37         emptyVal = null;
38     else if (argType.isOfType(Type.tBoolUInt))
39         emptyVal = new Integer JavaDoc(0);
40     else if (argType.isOfType(Type.tLong))
41         emptyVal = new Long JavaDoc(0);
42     else if (argType.isOfType(Type.tFloat))
43         emptyVal = new Float JavaDoc(0);
44     else if (argType.isOfType(Type.tDouble))
45         emptyVal = new Double JavaDoc(0);
46     else
47         throw new IllegalArgumentException JavaDoc("Illegal Type: "+argType);
48         
49         empty = new ConstOperator(emptyVal);
50     empty.setType(argType);
51         empty.makeInitializer(argType);
52     initOperands(size);
53     for (int i=0; i < subExpressions.length; i++)
54         setSubExpressions(i, empty);
55     }
56
57     public void updateSubTypes() {
58         argType = (type instanceof ArrayType)
59             ? Type.tSubType(((ArrayType)type).getElementType()) : Type.tError;
60     for (int i=0; i< subExpressions.length; i++)
61         if (subExpressions[i] != null)
62         subExpressions[i].setType(argType);
63     }
64
65     public void updateType() {
66     }
67
68     public boolean setValue(int index, Expression value) {
69         if (index < 0 ||
70         index > subExpressions.length ||
71         subExpressions[index] != empty)
72             return false;
73         value.setType(argType);
74         setType(Type.tSuperType(Type.tArray(value.getType())));
75         subExpressions[index] = value;
76         value.parent = this;
77         value.makeInitializer(argType);
78         return true;
79     }
80
81     public int getPriority() {
82         return 200;
83     }
84
85     public void makeInitializer(Type type) {
86     if (type.getHint().isOfType(getType()))
87         isInitializer = true;
88     }
89
90     public Expression simplify() {
91     for (int i=0; i< subExpressions.length; i++) {
92         if (subExpressions[i] != null)
93         subExpressions[i] = subExpressions[i].simplify();
94     }
95     return this;
96     }
97
98     public void dumpExpression(TabbedPrintWriter writer)
99     throws java.io.IOException JavaDoc {
100     if (!isInitializer) {
101         writer.print("new ");
102         writer.printType(type.getHint());
103         writer.breakOp();
104         writer.print(" ");
105     }
106     writer.print("{ ");
107     writer.startOp(writer.EXPL_PAREN, 0);
108         for (int i=0; i< subExpressions.length; i++) {
109             if (i>0) {
110         writer.print(", ");
111         writer.breakOp();
112         }
113         if (subExpressions[i] != null)
114         subExpressions[i].dumpExpression(writer, 0);
115         else
116         empty.dumpExpression(writer, 0);
117         }
118     writer.endOp();
119     writer.print(" }");
120     }
121 }
122
Popular Tags