KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > ast > IntroducedSuperDec


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.crosscuts.ast;
26
27 import org.aspectj.compiler.crosscuts.joinpoints.TypeDecPlanner;
28
29 import org.aspectj.compiler.base.ast.*;
30 import org.aspectj.compiler.base.cst.*;
31
32 import org.aspectj.compiler.base.JavaCompiler;
33 import org.aspectj.compiler.crosscuts.*;
34
35 /**
36  * @grammar introduction ...
37  * @child GenTypeName targets
38  * @child TypeDs typeDs
39  * @property boolean isImplements
40  */

41
42 public class IntroducedSuperDec extends Dec implements TypeDecPlanner {
43
44     public Modifiers getModifiers() { return getAST().makeModifiers(0); }
45
46 // void addMissingName(TypeDs namesToAdd, TypeDs toList, TypeDec typeDec) {
47
// }
48

49     public void walkScope(ScopeWalker walker) {
50         super.walkScope(walker);
51         getWorld().addTypeDecPlanner(this);
52     }
53         
54     /*
55     //INTRO from IntroductionPlannerPass
56     public void preIntroduction(IntroductionPlannerPass pass) {
57         ScopeWalker.bindNames(targets, getBytecodeTypeDec().getTypeScope());
58         ScopeWalker.bindNames(typeDs, getBytecodeTypeDec().getTypeScope());
59         getWorld().addTypeDecPlanner(this);
60     }
61     */

62
63     protected void addSuperType(Type toType, Type newSuperType) {
64         if (toType.isSubtypeOf(newSuperType)) return;
65
66         if (toType.isInterface()) {
67             addSuperTypeToInterfaceDec((InterfaceDec)toType.getTypeDec(), newSuperType);
68         } else if (toType.isClass()) {
69             addSuperTypeToClassDec((ClassDec)toType.getTypeDec(), newSuperType);
70         } else {
71             showWarning("can't apply to a " + toType.getTypeDec().getClass().getName());
72         }
73     }
74
75     boolean checkExistingSubtypes(Type thisType, Type newSuperType) {
76         if (thisType.isSubtypeOf(newSuperType)) return true;
77         if (newSuperType.isSubtypeOf(thisType)) {
78             showError("circular inheritance: " + newSuperType.toShortString() +
79                 " is a subtype of " + thisType.toShortString());
80             return true;
81         }
82         return false;
83     }
84
85     protected void addSuperTypeToInterfaceDec(InterfaceDec typeDec, Type newSuperType) {
86         if (!newSuperType.isInterface()) {
87             showError("interface can't extend a class");
88             return;
89         }
90         if (checkExistingSubtypes(typeDec.getType(), newSuperType)) return;
91         typeDec.addSuperInterfaceType(newSuperType);
92         typeDec.getType().addDirectSuperType(newSuperType);
93     }
94
95     protected void addSuperTypeToClassDec(ClassDec typeDec, Type newSuperType) {
96         if (checkExistingSubtypes(typeDec.getType(), newSuperType)) return;
97
98         if (newSuperType.isInterface()) {
99             typeDec.addSuperInterfaceType(newSuperType);
100             typeDec.getType().addDirectSuperType(newSuperType);
101         } else if (newSuperType.isClass()) {
102             Type oldParent = typeDec.getSuperClassType();
103             if (oldParent == null) return;
104
105             if (newSuperType.isSubtypeOf(oldParent)) {
106                 typeDec.setSuperClass(newSuperType.makeTypeD());
107                 typeDec.getType().addDirectSuperType(newSuperType);
108             } else {
109                 showError("introduced superclass must extend old superclass: " +
110                         oldParent.getPrettyString());
111             }
112         } else {
113             showError("classes can't extend " + newSuperType.getTypeDec().getClass().getName());
114         }
115     }
116
117
118
119     public void plan(TypeDec typeDec, int phase) {
120         if (phase != TypeDecPlanner.TYPE) return;
121         
122         if (!getTargets().matches(typeDec.getType())) return;
123
124         final int N = typeDs.size();
125         for (int i = 0; i < N; i++) {
126             TypeD name = typeDs.get(i);
127             getCompiler().showMessage(" introducing " + name.toShortString() +
128                     " on " + typeDec.toShortString());
129
130             addSuperType(typeDec.getType(), name.getType());
131         }
132         ((AspectJCompiler)getCompiler()).getCorrespondences().addPointsTo(this, typeDec);
133     }
134
135     public String JavaDoc getKind() {
136         return "declare parents";
137     }
138
139     public String JavaDoc getId() {
140         return targets.toShortString();
141     }
142
143     public String JavaDoc toShortString() {
144         return "declare parents: " + targets.toShortString();
145     }
146     
147     //BEGIN: Generated from @child and @property
148
protected GenTypeName targets;
149     public GenTypeName getTargets() { return targets; }
150     public void setTargets(GenTypeName _targets) {
151         if (_targets != null) _targets.setParent(this);
152         targets = _targets;
153     }
154     
155     protected TypeDs typeDs;
156     public TypeDs getTypeDs() { return typeDs; }
157     public void setTypeDs(TypeDs _typeDs) {
158         if (_typeDs != null) _typeDs.setParent(this);
159         typeDs = _typeDs;
160     }
161     
162     protected boolean isImplements;
163     public boolean getIsImplements() { return isImplements; }
164     public void setIsImplements(boolean _isImplements) { isImplements = _isImplements; }
165     
166     public IntroducedSuperDec(SourceLocation location, GenTypeName _targets, TypeDs _typeDs, boolean _isImplements) {
167         super(location);
168         setTargets(_targets);
169         setTypeDs(_typeDs);
170         setIsImplements(_isImplements);
171     }
172     protected IntroducedSuperDec(SourceLocation source) {
173         super(source);
174     }
175     
176     public ASTObject copyWalk(CopyWalker walker) {
177         IntroducedSuperDec ret = new IntroducedSuperDec(getSourceLocation());
178         ret.preCopy(walker, this);
179         if (targets != null) ret.setTargets( (GenTypeName)walker.process(targets) );
180         if (typeDs != null) ret.setTypeDs( (TypeDs)walker.process(typeDs) );
181         ret.isImplements = isImplements;
182         return ret;
183     }
184     
185     public ASTObject getChildAt(int childIndex) {
186         switch(childIndex) {
187         case 0: return targets;
188         case 1: return typeDs;
189         default: return super.getChildAt(childIndex);
190         }
191     }
192      public String JavaDoc getChildNameAt(int childIndex) {
193         switch(childIndex) {
194         case 0: return "targets";
195         case 1: return "typeDs";
196         default: return super.getChildNameAt(childIndex);
197         }
198     }
199      public void setChildAt(int childIndex, ASTObject child) {
200         switch(childIndex) {
201         case 0: setTargets((GenTypeName)child); return;
202         case 1: setTypeDs((TypeDs)child); return;
203         default: super.setChildAt(childIndex, child); return;
204         }
205     }
206      public int getChildCount() {
207         return 2;
208     }
209     
210     public String JavaDoc getDefaultDisplayName() {
211         return "IntroducedSuperDec(isImplements: "+isImplements+")";
212     }
213     
214     //END: Generated from @child and @property
215
}
216
217
218
219
Popular Tags