KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 import java.util.*;
29
30 import org.aspectj.compiler.base.JavaCompiler;
31 import org.aspectj.compiler.base.*;
32 import org.aspectj.compiler.base.cst.*;
33
34 /**
35  * @grammar ...
36  * @child TypeD superClass @before superInterfaces
37  */

38
39 public class ClassDec extends TypeDec {
40     public String JavaDoc getKind() { return "class"; }
41
42     private boolean isAnonymousFlag = false;
43     public boolean isAnonymous() { return isAnonymousFlag || getId().equals("ANONYMOUS"); }
44     public void setIsAnonymous() { isAnonymousFlag = true; }
45
46     public ASTObject postCopy(CopyWalker walker, ASTObject oldObject) {
47         this.isAnonymousFlag = ((ClassDec)oldObject).isAnonymousFlag;
48         this.isInnerFlag = ((ClassDec)oldObject).isInnerFlag;
49         // !!!If moved to a static context, then this class can no
50
// longer be inner. These are ramifications to this that need to be explored
51
if (walker.isStatic) this.isInnerFlag = false;
52         return super.postCopy(walker, oldObject);
53     }
54
55     protected void walkExtendsAndImplements(ScopeWalker walker) {
56         if (superClass != null) {
57             setSuperClass((TypeD)walker.process(superClass));
58         }
59         //System.out.println(superClass);
60
super.walkExtendsAndImplements(walker);
61     }
62
63
64     public Type getSuperClassType() {
65         Type superType;
66         if (superClass == null) superType = getTypeManager().getObjectType();
67         else superType = superClass.getType();
68
69         //XXX bad modularity putting this here
70
if (!type.isAspect() && superType.isAspect()) {
71             superClass.showError("class may not extend an aspect");
72         }
73
74         if (superType.isClass()) return superType;
75
76         //XXX bad design
77
if (isAnonymous()) {
78             setSuperInterfaces(getAST().makeTypeDs(superClass));
79             superType = getTypeManager().getObjectType();
80             setSuperClass(superType.makeTypeD());
81         } else {
82             superClass.showError(toShortString() + " must extend a class, not " + superType.toShortString());
83             return getTypeManager().TYPE_NOT_FOUND;
84         }
85         return superType;
86     }
87
88
89     public void unparse(CodeWriter writer) {
90         writer.write(modifiers);
91         writer.writeKeyword("class");
92         writer.requiredSpace();
93         writer.write(id);
94
95         if (superClass != null) {
96             writer.requiredSpace();
97             writer.write("extends");
98             writer.requiredSpace();
99             writer.write(superClass);
100         }
101
102         writeNames(writer, "implements", superInterfaces);
103
104         if (!writer.isOnlySignatures()) {
105             writer.optionalSpace();
106             writer.openBlock();
107             writer.write(body);
108             writer.newLine();
109             writer.closeBlock();
110         }
111     }
112
113
114     // ------------------------------
115
// INTRO from InnerInfoPass
116

117     public void walkInnerInfo(InnerInfoPass w) {
118         setIsInner(! isStatic() && w.isInDynamicContext());
119         super.walkInnerInfo(w);
120     }
121
122     // ------------------------------
123
// Inner class stuff
124

125     // Only classDecs can be inner (not interfaces, not aspects).
126

127     private boolean isInnerFlag = false;
128     public void setIsInner(boolean b) { isInnerFlag = b; }
129     public boolean isInner() { return isInnerFlag; }
130
131     //BEGIN: Generated from @child and @property
132
protected TypeD superClass;
133     public TypeD getSuperClass() { return superClass; }
134     public void setSuperClass(TypeD _superClass) {
135         if (_superClass != null) _superClass.setParent(this);
136         superClass = _superClass;
137     }
138
139     public ClassDec(SourceLocation location, Modifiers _modifiers, String JavaDoc _id, TypeD _superClass, TypeDs _superInterfaces, Decs _body) {
140         super(location, _modifiers, _id, _superInterfaces, _body);
141         setSuperClass(_superClass);
142     }
143     protected ClassDec(SourceLocation source) {
144         super(source);
145     }
146
147     public ASTObject copyWalk(CopyWalker walker) {
148         ClassDec ret = new ClassDec(getSourceLocation());
149         ret.preCopy(walker, this);
150         if (modifiers != null) ret.setModifiers( (Modifiers)walker.process(modifiers) );
151         ret.id = id;
152         if (superClass != null) ret.setSuperClass( (TypeD)walker.process(superClass) );
153         if (superInterfaces != null) ret.setSuperInterfaces( (TypeDs)walker.process(superInterfaces) );
154         if (body != null) ret.setBody( (Decs)walker.process(body) );
155         return ret;
156     }
157
158     public ASTObject getChildAt(int childIndex) {
159         switch(childIndex) {
160         case 0: return modifiers;
161         case 1: return superClass;
162         case 2: return superInterfaces;
163         case 3: return body;
164         default: return super.getChildAt(childIndex);
165         }
166     }
167      public String JavaDoc getChildNameAt(int childIndex) {
168         switch(childIndex) {
169         case 0: return "modifiers";
170         case 1: return "superClass";
171         case 2: return "superInterfaces";
172         case 3: return "body";
173         default: return super.getChildNameAt(childIndex);
174         }
175     }
176      public void setChildAt(int childIndex, ASTObject child) {
177         switch(childIndex) {
178         case 0: setModifiers((Modifiers)child); return;
179         case 1: setSuperClass((TypeD)child); return;
180         case 2: setSuperInterfaces((TypeDs)child); return;
181         case 3: setBody((Decs)child); return;
182         default: super.setChildAt(childIndex, child); return;
183         }
184     }
185      public int getChildCount() {
186         return 4;
187     }
188
189     public String JavaDoc getDefaultDisplayName() {
190         return "ClassDec(id: "+id+")";
191     }
192
193     //END: Generated from @child and @property
194
}
195
Popular Tags