View Javadoc

    1   /*
    2    $Id: MethodNode.java,v 1.24 2006/03/07 13:22:34 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.util.List;
    49
    50  import org.codehaus.groovy.ast.stmt.BlockStatement;
    51  import org.codehaus.groovy.ast.stmt.Statement;
    52  import org.objectweb.asm.Opcodes;
    53
    54  /***
    55   * Represents a method declaration
    56   *
    57   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    58   * @version $Revision: 1.24 $
    59   */
    60  public class MethodNode extends AnnotatedNode implements Opcodes {
    61
    62      private String name;
    63      private int modifiers;
    64      private ClassNode returnType;
    65      private Parameter[] parameters;
    66      private boolean hasDefaultValue = false;
    67      private Statement code;
    68      private boolean dynamicReturnType;
    69      private VariableScope variableScope;
    70      private ClassNode[] exceptions;
    71
    72      public MethodNode(String name, int modifiers, ClassNode returnType, Parameter[] parameters, ClassNode[] exceptions, Statement code) {
    73          this.name = name;
    74          this.modifiers = modifiers;
    75          this.parameters = parameters;
    76          this.code = code;
    77          this.returnType = returnType;
    78          if (returnType==null) this.returnType = ClassHelper.OBJECT_TYPE;
    79
    80          variableScope = new VariableScope();
    81          if (parameters != null && parameters.length > 0) {
    82              for (int i = 0; i < parameters.length; i++) {
    83                  Parameter para = parameters[i];
    84                  if (para.hasInitialExpression()) {
    85                      this.hasDefaultValue = true;
    86                  }
    87                  para.setInStaticContext(isStatic());
    88                  variableScope.getDeclaredVariables().put(para.getName(),para);
    89              }
    90          }
    91          variableScope.setInStaticContext(isStatic());
    92
    93          this.exceptions = exceptions;
    94      }
    95
    96      /***
    97       * The type descriptor for a method node is a string containing the name of the method, its return type,
    98       * and its parameter types in a canonical form. For simplicity, I'm using the format of a Java declaration
    99       * without parameter names, and with $dynamic as the type for any dynamically typed values.
    100      *
    101      * @return
    102      */
    103     // TODO: add test case for type descriptor
    104     public String getTypeDescriptor() {
    105         StringBuffer buf = new StringBuffer();
    106         // buf.append(dynamicReturnType ? "$dynamic" : cleanupTypeName(returnType));
    107         //
    108         buf.append(returnType.getName()); // br  to replace the above. Dynamic type returns Object.
    109         //
    110         buf.append(' ');
    111         buf.append(name);
    112         buf.append('(');
    113         for (int i = 0; i < parameters.length; i++) {
    114             if (i > 0) {
    115                 buf.append(',');
    116             }
    117             Parameter param = parameters[i];
    118             buf.append(param.getType().getName());
    119         }
    120         buf.append(')');
    121         return buf.toString();
    122     }
    123
    124     public boolean isVoidMethod() {
    125         return returnType==ClassHelper.VOID_TYPE;
    126     }
    127
    128     public Statement getCode() {
    129         return code;
    130     }
    131
    132     public void setCode(Statement code) {
    133         this.code = code;
    134     }
    135
    136     public int getModifiers() {
    137         return modifiers;
    138     }
    139
    140     public void setModifiers(int modifiers) {
    141         this.modifiers = modifiers;
    142     }
    143
    144     public String getName() {
    145         return name;
    146     }
    147
    148     public Parameter[] getParameters() {
    149         return parameters;
    150     }
    151
    152     public ClassNode getReturnType() {
    153         return returnType;
    154     }
    155
    156     public VariableScope getVariableScope() {
    157         return variableScope;
    158     }
    159
    160     public void setVariableScope(VariableScope variableScope) {
    161         this.variableScope = variableScope;
    162     }
    163
    164     public boolean isDynamicReturnType() {
    165         return dynamicReturnType;
    166     }
    167
    168     public boolean isAbstract() {
    169         return (modifiers & ACC_ABSTRACT) != 0;
    170     }
    171
    172     public boolean isStatic() {
    173         return (modifiers & ACC_STATIC) != 0;
    174     }
    175
    176     public boolean hasDefaultValue() {
    177         return this.hasDefaultValue;
    178     }
    179
    180     public String toString() {
    181         return super.toString() + "[name: " + name + "]";
    182     }
    183
    184     public void setReturnType(ClassNode returnType) {
    185         this.returnType = returnType;
    186     }
    187
    188     public ClassNode[] getExceptions() {
    189         return exceptions;
    190     }
    191
    192     public Statement getFirstStatement(){
    193         if (code == null) return null;
    194         if (code instanceof BlockStatement) {
    195             List list = ((BlockStatement) code).getStatements();
    196             if (list.size()>0) return (Statement) list.get(0);
    197             return null;
    198         }
    199         return code;
    200     }
    201 }