1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.codehaus.groovy.ast;
47
48 import java.lang.reflect.Field;
49
50 import org.codehaus.groovy.ast.expr.Expression;
51 import org.objectweb.asm.Opcodes;
52
53 /***
54 * Represents a field (member variable)
55 *
56 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
57 * @version $Revision: 1.18 $
58 */
59 public class FieldNode extends AnnotatedNode implements Opcodes, Variable {
60
61 private String name;
62 private int modifiers;
63 private ClassNode type;
64 private ClassNode owner;
65 private Expression initialValueExpression;
66 private boolean dynamicTyped;
67 private boolean holder;
68 private boolean closureShare = false;
69
70 public static FieldNode newStatic(Class theClass, String name) throws SecurityException, NoSuchFieldException {
71 Field field = theClass.getField(name);
72 ClassNode fldType = ClassHelper.make(field.getType());
73 return new FieldNode(name, ACC_PUBLIC | ACC_STATIC, fldType, ClassHelper.make(theClass), null);
74 }
75
76 public FieldNode(String name, int modifiers, ClassNode type, ClassNode owner, Expression initialValueExpression) {
77 this.name = name;
78 this.modifiers = modifiers;
79 this.type = type;
80 if (this.type==ClassHelper.DYNAMIC_TYPE && initialValueExpression!=null) this.setType(initialValueExpression.getType());
81 this.setType(type);
82 this.owner = owner;
83 this.initialValueExpression = initialValueExpression;
84 }
85
86 public Expression getInitialExpression() {
87 return initialValueExpression;
88 }
89
90 public int getModifiers() {
91 return modifiers;
92 }
93
94 public String getName() {
95 return name;
96 }
97
98 public ClassNode getType() {
99 return type;
100 }
101
102 public void setType(ClassNode type) {
103 this.type = type;
104 dynamicTyped |= type==ClassHelper.DYNAMIC_TYPE;
105 }
106
107 public ClassNode getOwner() {
108 return owner;
109 }
110
111 public boolean isHolder() {
112 return holder;
113 }
114
115 public void setHolder(boolean holder) {
116 this.holder = holder;
117 }
118
119 public boolean isDynamicTyped() {
120 return dynamicTyped;
121 }
122
123 public void setModifiers(int modifiers) {
124 this.modifiers = modifiers;
125 }
126
127 /***
128 * @return true if the field is static
129 */
130 public boolean isStatic() {
131 return (modifiers & ACC_STATIC) != 0;
132 }
133 /***
134 * @param owner The owner to set.
135 */
136 public void setOwner(ClassNode owner) {
137 this.owner = owner;
138 }
139
140 public boolean hasInitialExpression() {
141 return initialValueExpression!=null;
142 }
143
144 public boolean isInStaticContext() {
145 return isStatic();
146 }
147 public Expression getInitialValueExpression() {
148 return initialValueExpression;
149 }
150 public void setInitialValueExpression(Expression initialValueExpression) {
151 this.initialValueExpression = initialValueExpression;
152 }
153
154 public boolean isClosureSharedVariable() {
155 return false;
156 }
157
158 public void setClosureSharedVariable(boolean inClosure) {
159 closureShare = inClosure;
160 }
161 }