KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jode > decompiler > FieldAnalyzer


1 /* FieldAnalyzer 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: FieldAnalyzer.java.in,v 4.3.2.3 2002/05/28 17:34:03 hoenicke Exp $
18  */

19
20 package jode.decompiler;
21 import jode.type.Type;
22 import jode.bytecode.FieldInfo;
23 import jode.expr.Expression;
24 import jode.expr.ThisOperator;
25 import jode.expr.LocalLoadOperator;
26 import jode.expr.ConstOperator;
27 import jode.expr.OuterLocalOperator;
28
29 import java.lang.reflect.Modifier JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import java.util.Set JavaDoc;
33
34 public class FieldAnalyzer implements Analyzer {
35     ClassAnalyzer clazz;
36     ImportHandler imports;
37     int modifiers;
38     Type type;
39     String JavaDoc fieldName;
40     Expression constant;
41     boolean isSynthetic;
42     boolean isDeprecated;
43     boolean analyzedSynthetic = false;
44     
45     public FieldAnalyzer(ClassAnalyzer cla, FieldInfo fd,
46                          ImportHandler i)
47     {
48         clazz = cla;
49         imports = i;
50
51         modifiers = fd.getModifiers();
52         type = Type.tType(fd.getType());
53         fieldName = fd.getName();
54         constant = null;
55     this.isSynthetic = fd.isSynthetic();
56     this.isDeprecated = fd.isDeprecated();
57         if (fd.getConstant() != null) {
58         constant = new ConstOperator(fd.getConstant());
59         constant.setType(type);
60         constant.makeInitializer(type);
61         }
62     }
63
64     public String JavaDoc getName() {
65         return fieldName;
66     }
67
68     public Type getType() {
69     return type;
70     }
71
72     public ClassAnalyzer getClassAnalyzer() {
73     return clazz;
74     }
75
76     public Expression getConstant() {
77     return constant;
78     }
79
80     public boolean isSynthetic() {
81     return isSynthetic;
82     }
83
84     public boolean isFinal() {
85     return Modifier.isFinal(modifiers);
86     }
87
88     public void analyzedSynthetic() {
89     analyzedSynthetic = true;
90     }
91
92     public boolean setInitializer(Expression expr) {
93     if (constant != null)
94         return constant.equals(expr);
95
96     /* This should check for isFinal(), but sadly, sometimes jikes
97      * doesn't make a val$ field final. I don't know when, or why,
98      * so I currently ignore isFinal.
99      */

100     if (isSynthetic
101         && (fieldName.startsWith("this$")
102         || fieldName.startsWith("val$"))) {
103         if (fieldName.startsWith("val$") && fieldName.length() > 4
104         && expr instanceof OuterLocalOperator) {
105         LocalInfo li = ((OuterLocalOperator) expr).getLocalInfo();
106         li.addHint(fieldName.substring(4), type);
107         }
108         analyzedSynthetic();
109     } else
110         expr.makeInitializer(type);
111
112     constant = expr;
113     return true;
114     }
115
116     public boolean setClassConstant(String JavaDoc clazzName) {
117     if (constant != null)
118         return false;
119     if (clazzName.charAt(0) == '[') {
120         if (clazzName.charAt(clazzName.length()-1) == ';')
121         clazzName = clazzName.substring(0, clazzName.length()-1);
122
123         if (fieldName.equals("array"+ (clazzName.replace('[', '$')
124                        .replace('.', '$')))) {
125         analyzedSynthetic();
126         return true;
127         }
128     } else {
129         if (fieldName.equals("class$" + clazzName.replace('.', '$'))
130         || fieldName.equals("class$L" + clazzName.replace('.', '$'))) {
131         analyzedSynthetic();
132         return true;
133         }
134     }
135     return false;
136     }
137
138     public void analyze() {
139         imports.useType(type);
140     }
141
142     public void makeDeclaration(Set JavaDoc done) {
143     if (constant != null) {
144         constant.makeDeclaration(done);
145         constant = constant.simplify();
146     }
147     }
148
149     public boolean skipWriting() {
150     return analyzedSynthetic;
151     }
152
153     public void dumpSource(TabbedPrintWriter writer) throws IOException JavaDoc
154     {
155     if (isDeprecated) {
156         writer.println("/**");
157         writer.println(" * @deprecated");
158         writer.println(" */");
159     }
160     if (isSynthetic)
161         writer.print("/*synthetic*/ ");
162     int modifiedModifiers = modifiers;
163     /*
164      * JLS-1.0, section 9.3:
165      *
166      * Every field declaration in the body of an interface is
167      * implicitly public, static, and final. It is permitted, but
168      * strongly discouraged as a matter of style, to redundantly
169      * specify any or all of these modifiers for such fields.
170      *
171      * But I personally don't like this style..., move the
172      * comment mark if you think different.
173
174     if (clazz.getClazz().isInterface())
175         modifiedModifiers &= ~(Modifier.PUBLIC
176                    | Modifier.STATIC
177                    | Modifier.FINAL);
178      */

179     writer.startOp(writer.NO_PAREN, 0);
180     String JavaDoc modif = Modifier.toString(modifiedModifiers);
181     if (modif.length() > 0)
182         writer.print(modif+" ");
183
184     writer.printType(type);
185         writer.print(" " + fieldName);
186         if (constant != null) {
187         writer.breakOp();
188         writer.print(" = ");
189         constant.dumpExpression(writer.IMPL_PAREN, writer);
190         }
191     writer.endOp();
192         writer.println(";");
193     }
194
195     public String JavaDoc toString() {
196     return getClass().getName()+"["+clazz.getClazz()+"."+getName()+"]";
197     }
198 }
199
200
Popular Tags