001: /*
002: $Id: PropertyExpression.java 4287 2006-12-01 13:00:13Z blackdrag $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package org.codehaus.groovy.ast.expr;
047:
048: import java.lang.reflect.Field;
049: import java.lang.reflect.Method;
050: import java.lang.reflect.Modifier;
051:
052: import org.codehaus.groovy.ast.ClassHelper;
053: import org.codehaus.groovy.ast.GroovyCodeVisitor;
054:
055: /**
056: * Represents a property access such as the expression "foo.bar".
057: *
058: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
059: * @version $Revision: 4287 $
060: */
061: public class PropertyExpression extends Expression {
062:
063: private Expression objectExpression;
064: private Expression property;
065: private boolean spreadSafe = false;
066: private boolean safe = false;
067: private boolean isStatic = false;
068:
069: private Method getter = null;
070: private Method setter = null;
071:
072: private Field field = null;
073: private int access = -1;
074: private boolean implicitThis = false;
075:
076: public boolean isStatic() {
077: return isStatic;
078: }
079:
080: public PropertyExpression(Expression objectExpression,
081: String property) {
082: this (objectExpression, new ConstantExpression(property), false);
083: }
084:
085: public PropertyExpression(Expression objectExpression,
086: Expression property) {
087: this (objectExpression, property, false);
088: }
089:
090: public PropertyExpression(Expression objectExpression,
091: Expression property, boolean safe) {
092: this .objectExpression = objectExpression;
093: this .property = property;
094: this .safe = safe;
095: }
096:
097: public void visit(GroovyCodeVisitor visitor) {
098: visitor.visitPropertyExpression(this );
099: }
100:
101: public boolean isDynamic() {
102: return true;
103: }
104:
105: public Expression transformExpression(
106: ExpressionTransformer transformer) {
107: return this ;
108: }
109:
110: public Expression getObjectExpression() {
111: return objectExpression;
112: }
113:
114: public void setObjectExpression(Expression exp) {
115: objectExpression = exp;
116: }
117:
118: public Expression getProperty() {
119: return property;
120: }
121:
122: public String getPropertyAsString() {
123: if (property == null)
124: return null;
125: if (!(property instanceof ConstantExpression))
126: return null;
127: ConstantExpression constant = (ConstantExpression) property;
128: return constant.getText();
129: }
130:
131: public String getText() {
132: return objectExpression.getText() + "." + property.getText();
133: }
134:
135: /**
136: * @return is this a safe navigation, i.e. if true then if the source object is null
137: * then this navigation will return null
138: */
139: public boolean isSafe() {
140: return safe;
141: }
142:
143: public boolean isSpreadSafe() {
144: return spreadSafe;
145: }
146:
147: public void setSpreadSafe(boolean value) {
148: spreadSafe = value;
149: }
150:
151: public String toString() {
152: return super .toString() + "[object: " + objectExpression
153: + " property: " + property + "]";
154: }
155:
156: public void setStatic(boolean aStatic) {
157: this .isStatic = aStatic;
158: }
159:
160: public Method getGetter() {
161: return getter;
162: }
163:
164: public Method getSetter() {
165: return setter;
166: }
167:
168: public void setField(Field fld) {
169: field = fld;
170: setStatic(Modifier.isStatic(fld.getModifiers()));
171: setType(ClassHelper.make(fld.getType()));
172: }
173:
174: public Field getField() {
175: return field;
176: }
177:
178: public void setAccess(int access) {
179: this .access = access;
180: }
181:
182: public int getAccess() {
183: return access;
184: }
185:
186: public boolean isImplicitThis() {
187: return implicitThis;
188: }
189:
190: public void setImplicitThis(boolean it) {
191: implicitThis = it;
192: }
193: }
|