KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > types > FieldInstance_c


1 package polyglot.ext.jl.types;
2
3 import polyglot.types.*;
4 import polyglot.util.*;
5
6 /**
7  * A <code>FieldInstance</code> contains type information for a field.
8  */

9 public class FieldInstance_c extends VarInstance_c implements FieldInstance
10 {
11     protected ReferenceType container;
12
13     /** Used for deserializing types. */
14     protected FieldInstance_c() { }
15
16     public FieldInstance_c(TypeSystem ts, Position pos,
17                ReferenceType container,
18                        Flags flags, Type type, String JavaDoc name) {
19         super(ts, pos, flags, type, name);
20     this.container = container;
21     }
22
23     public ReferenceType container() {
24         return container;
25     }
26
27     /** Destructive update of constant value. */
28     public void setConstantValue(Object JavaDoc constantValue) {
29     if (! (constantValue == null) &&
30         ! (constantValue instanceof Boolean JavaDoc) &&
31         ! (constantValue instanceof Number JavaDoc) &&
32         ! (constantValue instanceof Character JavaDoc) &&
33         ! (constantValue instanceof String JavaDoc)) {
34
35         throw new InternalCompilerError(
36         "Can only set constant value to a primitive or String.");
37     }
38
39         this.constantValue = constantValue;
40         this.isConstant = true;
41     }
42
43     /** Non-destructive update of constant value. */
44     public FieldInstance constantValue(Object JavaDoc constantValue) {
45         if (this.constantValue != constantValue) {
46             FieldInstance_c n = (FieldInstance_c) copy();
47             n.setConstantValue(constantValue);
48             return n;
49         }
50         return this;
51     }
52
53     public FieldInstance container(ReferenceType container) {
54         if (this.container != container) {
55             FieldInstance_c n = (FieldInstance_c) copy();
56             n.container = container;
57             return n;
58         }
59         return this;
60     }
61
62     public FieldInstance flags(Flags flags) {
63         if (!flags.equals(this.flags)) {
64             FieldInstance_c n = (FieldInstance_c) copy();
65             n.flags = flags;
66             return n;
67         }
68         return this;
69     }
70
71     public FieldInstance name(String JavaDoc name) {
72         if ((name != null && !name.equals(this.name)) ||
73             (name == null && name != this.name)) {
74             FieldInstance_c n = (FieldInstance_c) copy();
75             n.name = name;
76             return n;
77         }
78         return this;
79     }
80
81     public FieldInstance type(Type type) {
82         if (this.type != type) {
83             FieldInstance_c n = (FieldInstance_c) copy();
84             n.type = type;
85             return n;
86         }
87         return this;
88     }
89     
90     public boolean equalsImpl(TypeObject o) {
91         if (o instanceof FieldInstance) {
92         FieldInstance i = (FieldInstance) o;
93         return super.equalsImpl(i) && ts.equals(container, i.container());
94     }
95
96     return false;
97     }
98
99     public String JavaDoc toString() {
100         Object JavaDoc v = constantValue;
101         if (v instanceof String JavaDoc) {
102           String JavaDoc s = (String JavaDoc) v;
103
104           if (s.length() > 8) {
105             s = s.substring(0, 8) + "...";
106           }
107
108           v = "\"" + s + "\"";
109         }
110
111         return "field " + flags.translate() + type + " " + name +
112         (v != null ? (" = " + v) : "");
113     }
114
115     public boolean isCanonical() {
116     return container.isCanonical() && type.isCanonical();
117     }
118 }
119
Popular Tags