View Javadoc
Minimize
Table

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.GroovyCodeVisitor;
19  
20  /**
21   * Represents a property access such as the expression "foo.bar".
22   * 
23   * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
24   * @version $Revision: 21431 $
25   */
26  public class PropertyExpression extends Expression {
27  
28      private Expression objectExpression;
29      private Expression property;
30      private boolean spreadSafe = false;
31      private boolean safe = false;
32      private boolean isStatic = false;
33  
34      private boolean implicitThis = false;
35  
36      public boolean isStatic() {
37          return isStatic;
38      }
39  
40      public PropertyExpression(Expression objectExpression, String property) {
41          this(objectExpression, new ConstantExpression(property), false);
42      }
43      
44      public PropertyExpression(Expression objectExpression, Expression property) {
45          this(objectExpression, property, false);
46      }
47  
48      public PropertyExpression(Expression objectExpression, Expression property, boolean safe) {
49          this.objectExpression = objectExpression;
50          this.property = property;
51          this.safe = safe;
52      }
53  
54      public void visit(GroovyCodeVisitor visitor) {
55          visitor.visitPropertyExpression(this);
56      }
57  
58      public boolean isDynamic() {
59          return true;
60      }
61  
62      public Expression transformExpression(ExpressionTransformer transformer) {
63          PropertyExpression ret = new PropertyExpression(transformer.transform(objectExpression),
64                  transformer.transform(property), safe);
65          ret.setSpreadSafe(spreadSafe);
66          ret.setStatic(isStatic);
67          ret.setImplicitThis(implicitThis);
68          ret.setSourcePosition(this);
69          return ret;
70      }
71  
72      public Expression getObjectExpression() {
73          return objectExpression;
74      }
75  
76      public void setObjectExpression(Expression exp) {
77          objectExpression=exp;
78      }    
79      
80      public Expression getProperty() {
81          return property;
82      }
83      
84      public String getPropertyAsString() {
85          if (property==null) return null;
86          if (! (property instanceof ConstantExpression)) return null;
87          ConstantExpression constant = (ConstantExpression) property;
88          return constant.getText();
89      }
90  
91      public String getText() {
92          String object = objectExpression.getText();
93          String text = property.getText();
94          String spread = isSpreadSafe() ? "*" : "";
95          String safe = isSafe() ? "?" : "";
96          return object + spread + safe + "." + text;
97      }
98  
99      /**
100      * @return is this a safe navigation, i.e. if true then if the source object is null
101      * then this navigation will return null
102      */
103     public boolean isSafe() {
104         return safe;
105     }
106 
107     public boolean isSpreadSafe() {
108         return spreadSafe;
109     }
110 
111     public void setSpreadSafe(boolean value) {
112         spreadSafe = value;
113     }
114 
115     public String toString() {
116         return super.toString() + "[object: " + objectExpression + " property: " + property + "]";
117     }
118 
119     public void setStatic(boolean aStatic) {
120         this.isStatic = aStatic;
121     }
122     
123     public boolean isImplicitThis(){
124         return implicitThis;
125     }
126     
127     public void setImplicitThis(boolean it) {
128         implicitThis  = it;
129     }
130 }