View Javadoc

    1   /*
    2    $Id: FieldNode.java,v 1.18 2006/01/19 00:06:51 blackdrag Exp $
    3
    4    Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
    5
    6    Redistribution and use of this software and associated documentation
    7    ("Software"), with or without modification, are permitted provided
    8    that the following conditions are met:
    9
    10   1. Redistributions of source code must retain copyright
    11      statements and notices.  Redistributions must also contain a
    12      copy of this document.
    13
    14   2. Redistributions in binary form must reproduce the
    15      above copyright notice, this list of conditions and the
    16      following disclaimer in the documentation and/or other
    17      materials provided with the distribution.
    18
    19   3. The name "groovy" must not be used to endorse or promote
    20      products derived from this Software without prior written
    21      permission of The Codehaus.  For written permission,
    22      please contact info@codehaus.org.
    23
    24   4. Products derived from this Software may not be called "groovy"
    25      nor may "groovy" appear in their names without prior written
    26      permission of The Codehaus. "groovy" is a registered
    27      trademark of The Codehaus.
    28
    29   5. Due credit should be given to The Codehaus -
    30      http://groovy.codehaus.org/
    31
    32   THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
    33   ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
    34   NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    35   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
    36   THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
    37   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    38   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    39   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    40   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
    41   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    42   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
    43   OF THE POSSIBILITY OF SUCH DAMAGE.
    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 }