001: /*
002: $Id: FieldNode.java 3419 2006-01-19 00:07:02Z blackdrag $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package org.codehaus.groovy.ast;
047:
048: import java.lang.reflect.Field;
049:
050: import org.codehaus.groovy.ast.expr.Expression;
051: import org.objectweb.asm.Opcodes;
052:
053: /**
054: * Represents a field (member variable)
055: *
056: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
057: * @version $Revision: 3419 $
058: */
059: public class FieldNode extends AnnotatedNode implements Opcodes,
060: Variable {
061:
062: private String name;
063: private int modifiers;
064: private ClassNode type;
065: private ClassNode owner;
066: private Expression initialValueExpression;
067: private boolean dynamicTyped;
068: private boolean holder;
069: private boolean closureShare = false;
070:
071: public static FieldNode newStatic(Class theClass, String name)
072: throws SecurityException, NoSuchFieldException {
073: Field field = theClass.getField(name);
074: ClassNode fldType = ClassHelper.make(field.getType());
075: return new FieldNode(name, ACC_PUBLIC | ACC_STATIC, fldType,
076: ClassHelper.make(theClass), null);
077: }
078:
079: public FieldNode(String name, int modifiers, ClassNode type,
080: ClassNode owner, Expression initialValueExpression) {
081: this .name = name;
082: this .modifiers = modifiers;
083: this .type = type;
084: if (this .type == ClassHelper.DYNAMIC_TYPE
085: && initialValueExpression != null)
086: this .setType(initialValueExpression.getType());
087: this .setType(type);
088: this .owner = owner;
089: this .initialValueExpression = initialValueExpression;
090: }
091:
092: public Expression getInitialExpression() {
093: return initialValueExpression;
094: }
095:
096: public int getModifiers() {
097: return modifiers;
098: }
099:
100: public String getName() {
101: return name;
102: }
103:
104: public ClassNode getType() {
105: return type;
106: }
107:
108: public void setType(ClassNode type) {
109: this .type = type;
110: dynamicTyped |= type == ClassHelper.DYNAMIC_TYPE;
111: }
112:
113: public ClassNode getOwner() {
114: return owner;
115: }
116:
117: public boolean isHolder() {
118: return holder;
119: }
120:
121: public void setHolder(boolean holder) {
122: this .holder = holder;
123: }
124:
125: public boolean isDynamicTyped() {
126: return dynamicTyped;
127: }
128:
129: public void setModifiers(int modifiers) {
130: this .modifiers = modifiers;
131: }
132:
133: /**
134: * @return true if the field is static
135: */
136: public boolean isStatic() {
137: return (modifiers & ACC_STATIC) != 0;
138: }
139:
140: /**
141: * @param owner The owner to set.
142: */
143: public void setOwner(ClassNode owner) {
144: this .owner = owner;
145: }
146:
147: public boolean hasInitialExpression() {
148: return initialValueExpression != null;
149: }
150:
151: public boolean isInStaticContext() {
152: return isStatic();
153: }
154:
155: public Expression getInitialValueExpression() {
156: return initialValueExpression;
157: }
158:
159: public void setInitialValueExpression(
160: Expression initialValueExpression) {
161: this .initialValueExpression = initialValueExpression;
162: }
163:
164: public boolean isClosureSharedVariable() {
165: return false;
166: }
167:
168: public void setClosureSharedVariable(boolean inClosure) {
169: closureShare = inClosure;
170: }
171: }
|