KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jjs > ast > JMethodCall


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.jjs.ast;
17
18 import com.google.gwt.dev.jjs.SourceInfo;
19
20 import java.util.ArrayList JavaDoc;
21
22 /**
23  * Java method call expression.
24  */

25 public class JMethodCall extends JExpression {
26
27   private ArrayList JavaDoc args = new ArrayList JavaDoc();
28   private JExpression instance;
29   private final JMethod method;
30   private final JType overrideReturnType;
31   private boolean staticDispatchOnly = false;
32
33   public JMethodCall(JProgram program, SourceInfo info, JExpression instance,
34       JMethod method) {
35     super(program, info);
36     assert (instance != null || method.isStatic());
37     this.instance = instance;
38     this.method = method;
39     this.staticDispatchOnly = false;
40     this.overrideReturnType = null;
41   }
42
43   /**
44    * Create a method call whose type is overriden to the specified type,
45    * ignoring the return type of the target method. This constructor is used
46    * during normalizing transformations to preserve type semantics when calling
47    * externally-defined compiler implementation methods.
48    *
49    * For example, Cast.dynamicCast() returns Object but that method is used to
50    * implement the cast operation. Using a stronger type on the call expression
51    * allows us to preserve type information during the latter phases of
52    * compilation.
53    */

54   public JMethodCall(JProgram program, SourceInfo info, JExpression instance,
55       JMethod method, JType overrideReturnType) {
56     super(program, info);
57     this.instance = instance;
58     this.method = method;
59     assert (overrideReturnType != null);
60     this.overrideReturnType = overrideReturnType;
61   }
62
63   public JMethodCall(JProgram program, SourceInfo info, JExpression instance,
64       JMethod method, boolean staticDispatchOnly) {
65     super(program, info);
66     this.instance = instance;
67     this.method = method;
68     this.staticDispatchOnly = staticDispatchOnly;
69     this.overrideReturnType = null;
70   }
71
72   public boolean canBePolymorphic() {
73     return !staticDispatchOnly && !method.isFinal() && !method.isStatic();
74   }
75
76   public ArrayList JavaDoc getArgs() {
77     return args;
78   }
79
80   public JExpression getInstance() {
81     return instance;
82   }
83
84   public JMethod getTarget() {
85     return method;
86   }
87
88   public JType getType() {
89     if (overrideReturnType != null) {
90       return overrideReturnType;
91     } else {
92       return method.getType();
93     }
94   }
95
96   public boolean hasSideEffects() {
97     // TODO(later): optimize? Be sure to check for clinit when we do.
98
return true;
99   }
100
101   public boolean isStaticDispatchOnly() {
102     return staticDispatchOnly;
103   }
104
105   public void setStaticDispatchOnly() {
106     this.staticDispatchOnly = true;
107   }
108
109   public void traverse(JVisitor visitor, Context ctx) {
110     if (visitor.visit(this, ctx)) {
111       if (instance != null) {
112         instance = visitor.accept(instance);
113       }
114       visitor.accept(args);
115     }
116     visitor.endVisit(this, ctx);
117   }
118
119 }
120
Popular Tags