KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.*;
29
30 /**
31  * @grammar ...
32  */

33
34 public class InterfaceDec extends TypeDec {
35     public String JavaDoc getKind() { return "interface"; }
36
37     public Type getSuperClassType() {
38         return getTypeManager().getObjectType();
39     }
40
41     public boolean isAnonymous() { return false; }
42     
43     public void checkSpec() {
44         if (isLocal()) {
45             showError("interface may not be defined in code body");
46         }
47         super.checkSpec();
48     }
49     
50     
51     protected void addFieldDec(FieldDec fieldDec) {
52         if (fieldDec.fromSource()) {
53             fieldDec.getModifiers().setPublic(true);
54             fieldDec.getModifiers().setStatic(true);
55             fieldDec.getModifiers().setFinal(true);
56         }
57         super.addFieldDec(fieldDec);
58     }
59
60     protected void addMethodDec(MethodDec methodDec) {
61         if (methodDec.fromSource()) {
62             methodDec.getModifiers().setPublic(true);
63             methodDec.getModifiers().setAbstract(true);
64         }
65         super.addMethodDec(methodDec);
66     }
67     
68     protected void addTypeDec(TypeDec typeDec) {
69         if (typeDec.fromSource()) {
70             typeDec.getModifiers().setPublic(true);
71             typeDec.getModifiers().setStatic(true);
72         }
73         super.addTypeDec(typeDec);
74     }
75     
76     
77     protected void addInitializerDec(InitializerDec dec) {
78         if (dec.fromSource()) {
79             dec.showError("initializer not allowed in interface");
80         }
81         super.addInitializerDec(dec);
82     }
83         
84     /* This collection of methods provides a helper class inside of
85      * an interface as a place to hold introduced static methods and fields.
86      * It might also hows a static initializer???
87      */

88     public void addToHelperClass(Dec dec) {
89         ClassDec helper = getHelperClass();
90         dec.setDeclaringType(helper.getType());
91         helper.getBody().add(dec);
92     }
93
94     static final String JavaDoc HELPER_CLASS_NAME = "AJC_HELPER_CLASS";
95     ClassDec getHelperClass() {
96         Decs body = getBody();
97         for (int i = 0; i<body.size(); i++) {
98             if (isHelperClass(body.get(i))) return (ClassDec)body.get(i);
99         }
100         ClassDec helper = makeHelperClass();
101         body.add(helper);
102
103         return helper;
104     }
105
106     public static boolean isHelperClass(Dec dec) {
107         return dec != null && dec instanceof ClassDec && dec.getName().equals(HELPER_CLASS_NAME);
108     }
109
110     ClassDec makeHelperClass() {
111         final AST ast = getAST();
112         ClassDec ret = ast.makeClass(ast.makeModifiers(Modifiers.PUBLIC|Modifiers.STATIC),
113                 HELPER_CLASS_NAME, getTypeManager().getObjectType().makeTypeD(),
114                 ast.makeTypeDs());
115         ret.setEnclosingTypeDec(this);
116
117         // deliberately creating a circular reference to the helper class for
118
// order of class loading issues
119
FieldDec ref1 = ast.makeField(ast.makeModifiers(Modifiers.PRIVATE|Modifiers.FINAL|Modifiers.STATIC),
120                 getTypeManager().getObjectType(), "AJC_INNER_REF");
121         FieldDec ref2 = ast.makeField(ast.makeModifiers(Modifiers.PUBLIC|Modifiers.FINAL|Modifiers.STATIC),
122                 getTypeManager().getObjectType(), "AJC_OUTER_REF");
123         ret.getBody().add(ref1);
124         getBody().add(ref2);
125         ref1.setInitializer(ast.makeGet(ref2.getField()));
126         ref2.setInitializer(ast.makeGet(ref1.getField()));
127         return ret;
128     }
129
130     public void unparse(CodeWriter writer) {
131         writer.write(modifiers);
132         writer.writeKeyword("interface");
133         writer.requiredSpace();
134         writer.write(id);
135
136         writeNames(writer, "extends", superInterfaces);
137
138         if (!writer.isOnlySignatures()) {
139             writer.optionalSpace();
140             writer.openBlock();
141             writer.write(body);
142             writer.closeBlock();
143         }
144     }
145
146     public boolean isStatic() {
147         return true;
148     }
149
150     // ------------------------------
151
// INTRO: ByteCodeCleanupPass
152

153     public ASTObject postCleanup(ByteCodeCleanupPass walker) {
154         final int len = getBody().size();
155         for (int i = 0; i < len; i++ ) {
156             Dec dec = getBody().get(i);
157             Modifiers m = dec.getModifiers();
158             if (dec instanceof FieldDec) {
159                 m.setPublic(true);
160                 m.setStatic(true);
161                 m.setFinal(true);
162             } else if (dec instanceof MethodDec) {
163                 m.setPublic(true);
164                 m.setAbstract(true);
165             }
166         }
167         return this;
168     }
169
170     // ------------------------------
171
// INTRO from AssignmentCheckerPass
172

173     public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) {
174         return this;
175     }
176
177     //BEGIN: Generated from @child and @property
178

179     public InterfaceDec(SourceLocation location, Modifiers _modifiers, String JavaDoc _id, TypeDs _superInterfaces, Decs _body) {
180         super(location, _modifiers, _id, _superInterfaces, _body);
181
182     }
183     protected InterfaceDec(SourceLocation source) {
184         super(source);
185     }
186
187     public ASTObject copyWalk(CopyWalker walker) {
188         InterfaceDec ret = new InterfaceDec(getSourceLocation());
189         ret.preCopy(walker, this);
190         if (modifiers != null) ret.setModifiers( (Modifiers)walker.process(modifiers) );
191         ret.id = id;
192         if (superInterfaces != null) ret.setSuperInterfaces( (TypeDs)walker.process(superInterfaces) );
193         if (body != null) ret.setBody( (Decs)walker.process(body) );
194         return ret;
195     }
196
197
198     public String JavaDoc getDefaultDisplayName() {
199         return "InterfaceDec(id: "+id+")";
200     }
201
202     //END: Generated from @child and @property
203
}
204
Popular Tags