View Javadoc

    1   /*
    2    $Id: Expression.java,v 1.3 2004/07/10 03:31:37 bran 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.expr;
    47
    48  import java.util.ArrayList;
    49  import java.util.Iterator;
    50  import java.util.List;
    51
    52  import org.codehaus.groovy.ast.ASTNode;
    53  import org.codehaus.groovy.classgen.AsmClassGenerator2;
    54  import groovy.lang.GroovyRuntimeException;
    55
    56  /***
    57   * Represents a base class for expressions which evaluate as an object
    58   *
    59   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
    60   * @version $Revision: 1.3 $
    61   */
    62  public abstract class Expression extends ASTNode {
    63      protected boolean typeResolved = false;
    64
    65      public boolean isResolveFailed() {
    66          return resolveFailed;
    67      }
    68
    69      public void setResolveFailed(boolean resolveFailed) {
    70          this.resolveFailed = resolveFailed;
    71      }
    72
    73      public String getFailure() {
    74          return failure;
    75      }
    76
    77      public void setFailure(String failure) {
    78          this.failure = failure;
    79      }
    80
    81      String failure = "";
    82      private boolean resolveFailed = false;
    83
    84      public Class getTypeClass() {
    85          return typeClass;
    86      }
    87
    88      public void setTypeClass(Class typeClass) {
    89          if (typeClass != null) {
    90              this.typeClass = typeClass;
    91              type = typeClass.getName();
    92              setTypeResolved(true);
    93          }
    94      }
    95
    96      public Class typeClass = null;
    97
    98      public String getType() {
    99          if (type != null){
    100             return type;
    101         } else {
    102             Class cls = getTypeClass();
    103             return cls != null? cls.getName() : null;//"java.lang.Object";
    104         }
    105     }
    106
    107     /***
    108      * true if the datatype can be changed, false otherwise.
    109      * @return
    110      */
    111     public boolean isDynamic() {
    112         return true;
    113     }
    114
    115     protected String type = null;
    116     /***
    117      * Return a copy of the expression calling the transformer on any nested expressions
    118      * @param transformer
    119      * @return
    120      */
    121     public abstract Expression transformExpression(ExpressionTransformer transformer);
    122
    123     /***
    124      * Transforms the list of expressions
    125      * @return a new list of transformed expressions
    126      */
    127     protected List transformExpressions(List expressions, ExpressionTransformer transformer) {
    128         List list = new ArrayList(expressions.size());
    129         for (Iterator iter = expressions.iterator(); iter.hasNext(); ) {
    130             list.add(transformer.transform((Expression) iter.next()));
    131         }
    132         return list;
    133     }
    134
    135     public void setType(String name) {
    136         if (name == null)
    137             throw new GroovyRuntimeException("cannot set null on type");
    138         // handle primitives first
    139         if (name.equals("int")) {
    140             setTypeClass(Integer.TYPE);
    141             return;
    142         }
    143         else if (name.equals("long")) {
    144             setTypeClass(Long.TYPE);
    145             return;
    146         }
    147         else if (name.equals("short")) {
    148             setTypeClass(Short.TYPE);
    149             return;
    150         }
    151         else if (name.equals("float")) {
    152             setTypeClass(Float.TYPE);
    153             return;
    154         }
    155         else if (name.equals("double")) {
    156             setTypeClass(Double.TYPE);
    157             return;
    158         }
    159         else if (name.equals("byte")) {
    160             setTypeClass(Byte.TYPE);
    161             return;
    162         }
    163         else if (name.equals("char")) {
    164             setTypeClass(Character.TYPE);
    165             return;
    166         }
    167         else if (name.equals("boolean")) {
    168             setTypeClass(Boolean.TYPE);
    169             return;
    170         }
    171
    172         if (name.endsWith("[]")) {
    173             String prefix = "[";
    174             name = name.substring(0, name.length() - 2);
    175
    176             if (name.equals("int")) {
    177                 type = prefix + "I";
    178             }
    179             else if (name.equals("long")) {
    180                 type = prefix + "J";
    181             }
    182             else if (name.equals("short")) {
    183                 type = prefix + "S";
    184             }
    185             else if (name.equals("float")) {
    186                 type = prefix + "F";
    187             }
    188             else if (name.equals("double")) {
    189                 type = prefix + "D";
    190             }
    191             else if (name.equals("byte")) {
    192                 type = prefix + "B";
    193             }
    194             else if (name.equals("char")) {
    195                 type = prefix + "C";
    196             }
    197             else if (name.equals("boolean")) {
    198                 type = prefix + "Z";
    199             } else {
    200                 type = prefix + "L" + name + ";";
    201             }
    202         }
    203         else {
    204             type = name;
    205         }
    206         if (type == null) {
    207             System.out.println("Expression.setType(): null");
    208             System.out.println("name = " + name);
    209         }
    210         try {
    211             this.setTypeClass(Class.forName(type, false, this.getClass().getClassLoader()));
    212         } catch (Throwable e) {
    213             this.typeResolved = false;
    214         }
    215     }
    216
    217     public boolean isTypeResolved() {
    218         return typeResolved;
    219     }
    220
    221     public void setTypeResolved(boolean b) {
    222         this.typeResolved = b;
    223         this.resolveFailed = false;
    224     }
    225
    226     public void resolve(AsmClassGenerator2 cg) {
    227         if (shouldContinue()) {
    228             resolveType(cg);
    229         }
    230     }
    231
    232     protected abstract void resolveType(AsmClassGenerator2 resolver);
    233
    234     protected boolean shouldContinue() {
    235         return !isResolveFailed() && !isTypeResolved();
    236     }
    237
    238 }