01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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,
41: JExpression instance, JField field,
42: JReferenceType enclosingType) {
43: super (program, info, field);
44: assert (instance != null || field.isStatic());
45: this .instance = instance;
46: this .field = field;
47: this .enclosingType = enclosingType;
48: }
49:
50: public JReferenceType getEnclosingType() {
51: return enclosingType;
52: }
53:
54: public JField getField() {
55: return field;
56: }
57:
58: public JExpression getInstance() {
59: return instance;
60: }
61:
62: public boolean hasSideEffects() {
63: // A cross-class reference to a static, non constant field forces clinit
64: if (field.isStatic()
65: && (!field.isFinal() || field.constInitializer == null)) {
66: if (program.typeOracle.checkClinit(enclosingType, field
67: .getEnclosingType())) {
68: // Therefore, we have side effects
69: return true;
70: }
71: }
72:
73: JExpression expr = instance;
74: if (expr == null) {
75: return false;
76: }
77: return expr.hasSideEffects();
78: }
79:
80: public void traverse(JVisitor visitor, Context ctx) {
81: if (visitor.visit(this, ctx)) {
82: if (instance != null) {
83: instance = visitor.accept(instance);
84: }
85: }
86: visitor.endVisit(this, ctx);
87: }
88:
89: }
|