KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ForwardReferenceChecker


1 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base;
26
27 import org.aspectj.compiler.base.ast.*;
28 import java.util.*;
29
30 /** <p> Checks adherence to JLS 8.3.2.3, "Restrictions on the use of
31  * Fields during Initialization", which says:</p>
32  *
33  * <p>The declaration of a member needs to appear before it is used only if
34  * the member is an instance (respectively static) field of a class or
35  * interface C and all of the following conditions hold:</p>
36  *
37  * <ul>
38  * <li> The usage occurs in an instance (respectively static) variable
39  * initializer of C or in an instance (respectively static) initializer
40  * of C.</li>
41  *
42  * <li> The usage is not on the left hand side of an assignment.</li>
43  *
44  * <li> C is the innermost class or interface enclosing the usage. </li>
45  * </ul>
46  *
47  * <p> A compile-time error occurs if any of the three requirements
48  * above are not met.</p>
49  *
50  * <p> There are four contexts, each represented by a class:</p>
51  *
52  * <ul>
53  * <li>ForwardReferenceChecker: The outermost, just walks compilation
54  * units. </li>
55  *
56  * <li>TypeChecker: walks contents of TypeDecs, can create (and
57  * transfer control to) TypeCheckers (for member types),
58  * InitializerCheckers (for initializers) and BodyCheckers (for methods
59  * and constructors). </li>
60  *
61  * <li>InitializerChecker: walks contents of initializers. Can create
62  * (and transfer control to) TypeCheckers (for local types). </li>
63  *
64  * <li>BodyChecker: walks methods and constructors. Can create (and
65  * transfer control to) TypeCheckers (for local types). </li>
66  * </ul>
67  *
68  */

69
70 public class ForwardReferenceChecker extends WalkerPass {
71     public ForwardReferenceChecker(JavaCompiler compiler) {
72         super(compiler);
73     }
74
75     public String JavaDoc getDisplayName() {
76         return "checking forward references";
77     }
78
79     public ASTObject process(ASTObject node) {
80         //System.err.println("entering " + node + " with " + this);
81
node.walkForwardReference(this);
82         return node;
83     }
84
85     public ForwardReferenceChecker createTypeChecker(TypeDec ty) {
86         return new TypeChecker(getCompiler(), ty);
87     }
88     public ForwardReferenceChecker createInitializerChecker(boolean isStatic) {
89         throw new RuntimeException JavaDoc("found initializer while walking with " + this);
90     }
91     public ForwardReferenceChecker createBodyChecker() {
92         throw new RuntimeException JavaDoc("found body while walking with " + this);
93     }
94     public void addFieldDec(FieldDec f) {
95         throw new RuntimeException JavaDoc("found field while walking with " + this);
96     }
97     public void checkReference(FieldAccessExpr f) { }
98
99     public String JavaDoc toString() {
100         return "<ForwardReferenceChecker in Compilation Unit>";
101     }
102
103     // ------------------------------
104

105     static class TypeChecker extends ForwardReferenceChecker {
106         TypeDec context;
107         TypeChecker(JavaCompiler compiler, TypeDec ty) {
108             super(compiler);
109             context = ty;
110         }
111         Set statics = new HashSet();
112         Set dynamics = new HashSet();
113
114         public ForwardReferenceChecker createInitializerChecker(boolean isStatic) {
115             return new InitializerChecker(getCompiler(), isStatic, context,
116                                           isStatic ? statics : dynamics);
117         }
118         public ForwardReferenceChecker createBodyChecker() {
119             return new BodyChecker(getCompiler());
120         }
121         public void addFieldDec(FieldDec f) {
122             (f.isStatic() ? statics : dynamics).add(f);
123         }
124         public String JavaDoc toString() {
125             return "<ForwardReferenceChecker in TypeDec with dynamics " +
126                 dynamics + " and statics " + statics + ">";
127         }
128     }
129
130     static class InitializerChecker extends ForwardReferenceChecker {
131         boolean isStatic;
132         TypeDec context;
133         Set fields;
134         InitializerChecker(JavaCompiler compiler, boolean isStatic, TypeDec context, Set fields) {
135             super(compiler);
136             this.isStatic = isStatic;
137             this.context = context;
138             this.fields = fields;
139         }
140         public ForwardReferenceChecker createBodyChecker() {
141             return this;
142         }
143         public void checkReference(FieldAccessExpr e) {
144             FieldDec dec = e.getFieldDec();
145             if (dec.isStatic() != isStatic) return;
146             if (dec.getDeclaringType().getTypeDec() != context) return;
147             if (fields.contains(dec)) return;
148             e.showError("Illegal forward reference");
149         }
150         public String JavaDoc toString() {
151             return "<ForwardReferenceChecker in " +
152                 (isStatic ? "static" : "dynamic") +
153                 " Init with fields " + fields + ">";
154         }
155     }
156
157     static class BodyChecker extends ForwardReferenceChecker {
158         BodyChecker(JavaCompiler compiler) { super(compiler); }
159
160         public ForwardReferenceChecker createBodyChecker() {
161             return this;
162         }
163         public String JavaDoc toString() {
164             return "<ForwardReferenceChecker in Body>";
165         }
166
167     }
168 }
169
170
Popular Tags