View Javadoc
Minimize
Table

Bug Overview

linebug prioritybugbug descriptionexisting sinceauthor
61mediumTODODer Code ist an dieser Stelle eventuell unfertig oder fehlerhaft. Bitte überprüfen!18.06.2012-10:31UNKNOWN

1   /*
2    * Copyright 2003-2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.codehaus.groovy.ast.expr;
17  
18  import org.codehaus.groovy.ast.ClassHelper;
19  import org.codehaus.groovy.ast.GroovyCodeVisitor;
20  
21  /**
22   * Represents a constant expression such as null, true, false
23   * 
24   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
25   * @version $Revision: 21186 $
26   */
27  public class ConstantExpression extends Expression {
28      // The following fields are only used internally; every occurrence of a user-defined expression of the same kind
29      // has its own instance so as to preserve line information. Consequently, to test for such an expression, don't
30      // compare against the field but call isXXXExpression() instead.
31      public static final ConstantExpression NULL = new ConstantExpression(null);
32      public static final ConstantExpression TRUE = new ConstantExpression(Boolean.TRUE);
33      public static final ConstantExpression FALSE = new ConstantExpression(Boolean.FALSE);
34      public static final ConstantExpression EMPTY_STRING = new ConstantExpression("");
35      public static final ConstantExpression PRIM_TRUE = new ConstantExpression(Boolean.TRUE, true);
36      public static final ConstantExpression PRIM_FALSE = new ConstantExpression(Boolean.FALSE, true);
37      //public static final Expression EMPTY_ARRAY = new PropertyExpression(new ClassExpression(ArgumentListExpression.class.getName()), "EMPTY_ARRAY");
38  
39      // the following fields are only used internally; there are no user-defined expressions of the same kind
40      public static final ConstantExpression VOID = new ConstantExpression(Void.class);
41      public static final ConstantExpression EMPTY_EXPRESSION = new ConstantExpression(null);
42      
43      private Object value;
44      private String constantName;
45  
46      public ConstantExpression(Object value) {
47          this(value,false);
48      }
49  
50      public ConstantExpression(Object value, boolean keepPrimitive) {
51          this.value = value;
52          if (value != null) {
53              if (keepPrimitive) {
54                  if (value instanceof Integer) {
55                      setType(ClassHelper.int_TYPE);
56                  } else if (value instanceof Boolean) {
57                      setType(ClassHelper.boolean_TYPE);
58                  } else {
59                      setType(ClassHelper.make(value.getClass()));
60                  }
61 bug overview                    //TODO: more cases here 
62              } else {
63                  setType(ClassHelper.make(value.getClass()));
64              }
65          }
66      }
67  
68      public String toString() {
69          return "ConstantExpression[" + value + "]";
70      }
71  
72      public void visit(GroovyCodeVisitor visitor) {
73          visitor.visitConstantExpression(this);
74      }
75  
76      public Expression transformExpression(ExpressionTransformer transformer) {
77          return this;
78      }
79  
80      /**
81       * @return the value of this constant expression
82       */    
83      public Object getValue() {
84          return value;
85      }
86  
87      public String getText() {
88          return (value == null) ? "null" : value.toString();
89      }
90  
91      public String getConstantName() {
92          return constantName;
93      }
94  
95      public void setConstantName(String constantName) {
96          this.constantName = constantName;
97      }
98  
99      public boolean isNullExpression() {
100         return value == null;
101     }
102 
103     public boolean isTrueExpression() {
104         return Boolean.TRUE.equals(value);
105     }
106 
107     public boolean isFalseExpression() {
108         return Boolean.FALSE.equals(value);
109     }
110 
111     public boolean isEmptyStringExpression() {
112         return "".equals(value);
113     }
114 }