KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > InitializerDec


1 /* -*- Mode: Java; 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.ast;
26
27 import org.aspectj.compiler.base.*;
28 // to get the passes for introductions
29
import org.aspectj.compiler.crosscuts.*;
30 import org.aspectj.compiler.base.bcg.CodeBuilder;
31 import java.util.*;
32
33 import org.aspectj.compiler.base.bcg.ClassfileBuilder;
34 /**
35   * @grammar [static] {body}
36   */

37
38 public class InitializerDec extends CodeDec {
39
40     public InitializerDec(SourceLocation location, Modifiers _modifiers, CodeBody _body) {
41         this(location, _modifiers, new Formals(location), new TypeDs(location), _body);
42     }
43
44     // ------------------------------
45
// INTRO from FlowCheckerPass
46

47     public final void walkFlow(FlowCheckerPass w) {
48         setupFlowWalker(w);
49         w.setLive(true);
50         w.process(getBody());
51         if (! w.isLive()) {
52             showError("initializer must be able to complete normally");
53         }
54     }
55     // end INTRO
56

57     public String JavaDoc getId() { return getModifiers().isStatic() ? "<clinit>" : "<init>"; }
58
59     public TypeD getResultTypeD() {
60         return getTypeManager().voidType.makeTypeD();
61     }
62
63     public boolean conflictsWith(Dec otherDec) {
64         return false;
65     }
66
67     public String JavaDoc toShortString() {
68         return "{}";
69     }
70
71     public String JavaDoc getKind() { return "initializer"; }
72
73     //INTRO from MixinImplementationPass
74
public ASTObject postImplementMixin(MixinImplementationPass fixer) {
75         final TypeDec inTypeDec = getBytecodeTypeDec();
76
77         if (!(inTypeDec instanceof InterfaceDec) || getBody() == null || isStatic()) return this;
78
79         Set topmostImplementors =
80             Type.filterTopTypes(Type.filterConcreteTypes(inTypeDec.getType().getSubTypes()));
81
82         for (Iterator i = topmostImplementors.iterator(); i.hasNext(); ) {
83             final Type implType = (Type)i.next();
84             TypeDec implDec = implType.getTypeDec();
85
86             CodeDec newDec = (CodeDec)fixer.copyToClass(this, implDec);
87
88             //XXX the position to add is important, this algorithm is a total hack
89
Decs decs = implDec.getBody();
90             int j = decs.size()-1;
91             for (; j >= 0; j--) {
92                 if (decs.get(j) instanceof InitializerDec) {
93                     implDec.getBody().add(j, newDec);
94                     break;
95                 }
96             }
97             if (j < 0) implDec.getBody().add( newDec );
98
99         }
100
101         return null;
102     }
103
104
105     public void unparse(CodeWriter writer) {
106         if (body == null || body.getStmts() == null || body.getStmts().size() == 0) return;
107
108         // odd unparser for static initializers in interfaces
109
// this doesn't work because of the blank final rules in Java (not JVM)
110
// if (isStatic() && getBytecodeTypeDec() instanceof InterfaceDec) {
111
// writer.write("private static class AJC_INIT");
112
// writer.openBlock();
113
// writer.write("static");
114
// writer.write(body);
115
// writer.writeln("static int ajc_field = 0;");
116
// writer.closeBlock();
117
// writer.writeln("static int ajc_init = AJC_INIT.ajc_field;");
118
// } else {
119
writeModifiers(writer);
120             writer.write(body);
121             writer.newLine();
122 // }
123
}
124
125     // ------------------------------
126
// Intro: ForwardReferenceChecker
127

128     public void walkForwardReference(ForwardReferenceChecker w) {
129         this.walk(w.createInitializerChecker(isStatic()));
130     }
131
132     // ------------------------------
133
// bcg
134

135     public ASTObject postCleanup(ByteCodeCleanupPass walker) {
136         if (getBody().isEmpty()) {
137             return null;
138         } else {
139             return super.postCleanup(walker);
140         }
141     }
142
143     protected void cgCodeMember(CodeBuilder cb) {
144         // no args, nothing on frame
145
cb.setMaxFrame(getFrameSize());
146         cb.enterBlock();
147         getBody().cgTop(cb);
148         cb.exitBlock();
149     }
150
151     public String JavaDoc getDescriptor() {
152         return "()V";
153     }
154
155     public int getStackDelta() { return 0; }
156
157     // ------------------------------
158
// INTRO: LocalClassPass.AnalysisWalker
159

160     public void walkAnalysis(LocalClassPass.AnalysisWalker walker) {
161         walker.enterCodeDec(this);
162         if (isStatic()) walker.inCode(true);
163         else walker.inConstructor();
164         this.walk(walker);
165         walker.leaveCodeDec();
166     }
167
168     public String JavaDoc toString() {
169         return "InitializerDec(" + (isStatic() ? "static" : "non-static") + ")";
170     }
171
172     //BEGIN: Generated from @child and @property
173

174     public InitializerDec(SourceLocation location, Modifiers _modifiers, Formals _formals, TypeDs __throws, CodeBody _body) {
175         super(location, _modifiers, _formals, __throws, _body);
176
177     }
178     protected InitializerDec(SourceLocation source) {
179         super(source);
180     }
181
182     public ASTObject copyWalk(CopyWalker walker) {
183         InitializerDec ret = new InitializerDec(getSourceLocation());
184         ret.preCopy(walker, this);
185         if (modifiers != null) ret.setModifiers( (Modifiers)walker.process(modifiers) );
186         if (formals != null) ret.setFormals( (Formals)walker.process(formals) );
187         if (_throws != null) ret.setThrows( (TypeDs)walker.process(_throws) );
188         if (body != null) ret.setBody( (CodeBody)walker.process(body) );
189         return ret;
190     }
191
192
193     public String JavaDoc getDefaultDisplayName() {
194         return "InitializerDec()";
195     }
196
197     //END: Generated from @child and @property
198
}
199
Popular Tags