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.17 $
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
69 public static FieldNode newStatic(Class theClass, String name) throws SecurityException, NoSuchFieldException {
70 Field field = theClass.getField(name);
71 ClassNode fldType = ClassHelper.make(field.getType());
72 return new FieldNode(name, ACC_PUBLIC | ACC_STATIC, fldType, ClassHelper.make(theClass), null);
73 }
74
75 public FieldNode(String name, int modifiers, ClassNode type, ClassNode owner, Expression initialValueExpression) {
76 this.name = name;
77 this.modifiers = modifiers;
78 this.type = type;
79 if (this.type==ClassHelper.DYNAMIC_TYPE && initialValueExpression!=null) this.setType(initialValueExpression.getType());
80 this.setType(ClassHelper.getWrapper(type));
81 this.owner = owner;
82 this.initialValueExpression = initialValueExpression;
83 }
84
85 public Expression getInitialExpression() {
86 return initialValueExpression;
87 }
88
89 public int getModifiers() {
90 return modifiers;
91 }
92
93 public String getName() {
94 return name;
95 }
96
97 public ClassNode getType() {
98 return type;
99 }
100
101 public void setType(ClassNode type) {
102 this.type = type;
103 dynamicTyped |= type==ClassHelper.DYNAMIC_TYPE;
104 }
105
106 public ClassNode getOwner() {
107 return owner;
108 }
109
110 public boolean isHolder() {
111 return holder;
112 }
113
114 public void setHolder(boolean holder) {
115 this.holder = holder;
116 }
117
118 public boolean isDynamicTyped() {
119 return dynamicTyped;
120 }
121
122 public void setModifiers(int modifiers) {
123 this.modifiers = modifiers;
124 }
125
126 /***
127 * @return true if the field is static
128 */
129 public boolean isStatic() {
130 return (modifiers & ACC_STATIC) != 0;
131 }
132 /***
133 * @param owner The owner to set.
134 */
135 public void setOwner(ClassNode owner) {
136 this.owner = owner;
137 }
138
139 public boolean hasInitialExpression() {
140 return initialValueExpression!=null;
141 }
142
143 public boolean isInStaticContext() {
144 return isStatic();
145 }
146 public Expression getInitialValueExpression() {
147 return initialValueExpression;
148 }
149 public void setInitialValueExpression(Expression initialValueExpression) {
150 this.initialValueExpression = initialValueExpression;
151 }
152 }