KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
21  * Java field reference expression.
22  */

23 public class JFieldRef extends JVariableRef implements HasEnclosingType {
24
25   /**
26    * The enclosing type of this reference.
27    */

28   private final JReferenceType enclosingType;
29
30   /**
31    * The referenced field.
32    */

33   private JField field;
34
35   /**
36    * This can only be null if the referenced field is static.
37    */

38   private JExpression instance;
39
40   public JFieldRef(JProgram program, SourceInfo info, JExpression instance,
41       JField field, JReferenceType enclosingType) {
42     super(program, info, field);
43     assert (instance != null || field.isStatic());
44     this.instance = instance;
45     this.field = field;
46     this.enclosingType = enclosingType;
47   }
48
49   public JReferenceType getEnclosingType() {
50     return enclosingType;
51   }
52
53   public JField getField() {
54     return field;
55   }
56
57   public JExpression getInstance() {
58     return instance;
59   }
60
61   public boolean hasSideEffects() {
62     // A cross-class reference to a static, non constant field forces clinit
63
if (field.isStatic()
64         && (!field.isFinal() || field.constInitializer == null)) {
65       if (program.typeOracle.checkClinit(enclosingType,
66           field.getEnclosingType())) {
67         // Therefore, we have side effects
68
return true;
69       }
70     }
71
72     JExpression expr = instance;
73     if (expr == null) {
74       return false;
75     }
76     return expr.hasSideEffects();
77   }
78
79   public void traverse(JVisitor visitor, Context ctx) {
80     if (visitor.visit(this, ctx)) {
81       if (instance != null) {
82         instance = visitor.accept(instance);
83       }
84     }
85     visitor.endVisit(this, ctx);
86   }
87
88 }
89
Popular Tags