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, Variable {
060    
061        private String name;
062        private int modifiers;
063        private ClassNode type;
064        private ClassNode owner;
065        private Expression initialValueExpression;
066        private boolean dynamicTyped;
067        private boolean holder;
068        private boolean closureShare = false;
069    
070        public static FieldNode newStatic(Class theClass, String name) throws SecurityException, NoSuchFieldException {
071            Field field = theClass.getField(name);
072            ClassNode fldType = ClassHelper.make(field.getType());
073            return new FieldNode(name, ACC_PUBLIC | ACC_STATIC, fldType, ClassHelper.make(theClass), null);
074        }
075    
076        public FieldNode(String name, int modifiers, ClassNode type, ClassNode owner, Expression initialValueExpression) {
077            this.name = name;
078            this.modifiers = modifiers;
079            this.type = type;
080            if (this.type==ClassHelper.DYNAMIC_TYPE && initialValueExpression!=null) this.setType(initialValueExpression.getType());
081            this.setType(type);
082            this.owner = owner;
083            this.initialValueExpression = initialValueExpression;
084        }
085    
086        public Expression getInitialExpression() {
087            return initialValueExpression;
088        }
089    
090        public int getModifiers() {
091            return modifiers;
092        }
093    
094        public String getName() {
095            return name;
096        }
097    
098        public ClassNode getType() {
099            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    }