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 groovy.lang.Closure;
19  
20  import org.codehaus.groovy.ast.ClassHelper;
21  import org.codehaus.groovy.ast.ClassNode;
22  import org.codehaus.groovy.ast.GroovyCodeVisitor;
23  
24  /**
25   * Represents a method pointer on an object such as
26   * foo.&bar which means find the method pointer on foo for the method called "bar"
27   * which is equivalent to
28   * <code>
29   * foo.metaClass.getMethodPointer(foo, "bar")
30   *
31   * @version $Revision: 20590 $
32   */
33  public class MethodPointerExpression extends Expression {
34  
35      private Expression expression;
36      private Expression methodName;
37  
38      public MethodPointerExpression(Expression expression, Expression methodName) {
39          this.expression = expression;
40          this.methodName = methodName;
41      }
42  
43      public Expression getExpression() {
44          if (expression == null)
45              return VariableExpression.THIS_EXPRESSION;
46          else
47              return expression;
48      }
49  
50      public Expression getMethodName() {
51          return methodName;
52      }
53  
54      public void visit(GroovyCodeVisitor visitor) {
55          visitor.visitMethodPointerExpression(this);
56      }
57  
58      public Expression transformExpression(ExpressionTransformer transformer) {
59          Expression ret;
60          Expression mname = transformer.transform(methodName);
61          if (expression == null) {
62              ret = new MethodPointerExpression(VariableExpression.THIS_EXPRESSION, mname);
63          } else {
64              ret = new MethodPointerExpression(transformer.transform(expression), mname);
65          }
66          ret.setSourcePosition(this);
67          return ret;
68      }
69  
70      public String getText() {
71          if (expression == null) {
72              return "&" + methodName;
73          } else {
74              return expression.getText() + ".&" + methodName.getText();
75          }
76      }
77  
78      public ClassNode getType() {
79          return ClassHelper.CLOSURE_TYPE;
80      }
81  
82      public boolean isDynamic() {
83          return false;
84      }
85  
86      public Class getTypeClass() {
87          return Closure.class;
88      }
89  }