KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.aspectj.compiler.crosscuts.*;
29
30 import org.aspectj.compiler.base.ast.*;
31 import org.aspectj.compiler.base.cst.*;
32
33 import org.aspectj.compiler.base.JavaCompiler;
34 import org.aspectj.compiler.base.bcg.ClassfileBuilder;
35
36 import java.util.*;
37
38 /**
39  * @grammar introduction ...
40  * @child GenTypeName targets
41  * @property Dec dec
42  */

43
44 public class IntroducedDec extends Dec implements TypeDecPlanner {
45     private List introducedDecs = new LinkedList();
46
47
48     public Modifiers getModifiers() { return dec.getModifiers(); }
49
50     Scope myLexicalScope = null;
51     public void walkScope(ScopeWalker walker) {
52         walker.process(targets);
53         boolean saveWalkBodies = walker.walkBodies();
54         walker.setWalkBodies(false);
55         walker.process(dec);
56         walker.setWalkBodies(saveWalkBodies);
57
58
59         myLexicalScope = walker.getScope();
60         dec.setParent(this);
61         getWorld().addTypeDecPlanner(this);
62     }
63
64
65     /*
66     //INTRO from IntroductionPlannerPass
67     public void preIntroduction(IntroductionPlannerPass pass) {
68         ScopeWalker.bindNames(targets, getBytecodeTypeDec().getTypeScope());
69         dec.setParent(this);
70         getWorld().addTypeDecPlanner(this);
71     }
72     */

73
74 // public Dec getSavedDec() { return savedDec; }
75
//
76
// public Dec savedDec = null;
77

78     public boolean isStatic() { return true; }
79
80     public String JavaDoc getUniqueIdString(Type onType) {
81         String JavaDoc ret = null;
82
83         if (isPrivate()) {
84             ret = getLexicalType().getPrivateCookieType().getPrettyString();
85         } else if (isProtected()) {
86             showError("protected introduction not allowed");
87             ret = "";
88         } else if (isPublic()) {
89              ret = "";
90         } else { // package protected
91
ret = getLexicalType().getPackageName();
92         }
93
94         if (onType.isInterface() && dec instanceof FieldDec) {
95             ret += "_" + onType.getPrettyString();
96         }
97
98         return ret;
99     }
100
101     public void plan(TypeDec typeDec, int phase) {
102         if (phase != TypeDecPlanner.SIGNATURE) return;
103
104         Type toType = typeDec.getType();
105
106         if (!getTargets().matches(toType)) return;
107         
108         // we can't introduce dec's with inner types onto interfaces because
109
// this would require the inner types to be correctly copied onto the
110
// concrete implementing classes at a later stage which we don't know
111
// how to do correctly
112
//XXX this is too restrictive, revisit later
113
// if (dec.containsTypes() && typeDec instanceof InterfaceDec) {
114
// showError("can't introduce this dec on an interface because it contains local types (compiler limitation)");
115
// }
116

117         //CopyWalker cw = CopyWalker.rawCopy(object);
118
//new CopyWalker(getCompiler(), toType, toType);
119

120         Dec newDec = (Dec)CopyWalker.rawCopy(dec, toType);
121         newDec.setSourceLocation(getAST().getSourceLocation());
122         newDec.setLanguageVisible();
123         
124         // public introductions need to not generate synthetic members
125
// so they will be visible to other compilers
126
if (isPublic()) {
127             newDec.setExplicitlyNonSynthetic();
128         }
129         newDec.setDeclaringType(toType);
130         newDec.setLexicalType(getLexicalType());
131         newDec.setFromLexicalScope(myLexicalScope);
132
133
134         if (!isPublic() || (newDec instanceof FieldDec && typeDec instanceof InterfaceDec)) {
135             String JavaDoc newId = getAST().makeGeneratedName(newDec.getBytecodeId() + '_' +
136                                                       getUniqueIdString(typeDec.getType()));
137             newDec.setBytecodeId(newId);
138         }
139
140         // add this to the typeDec's signature and body
141
typeDec.addIntroducedDec(newDec);
142         typeDec.addExtraWithinType(this.getLexicalType());
143
144         ((AspectJCompiler)getCompiler()).getCorrespondences().addPointsTo(this, typeDec);
145         getCompiler().showMessage(" introduced " + newDec.toShortString() + " on " +
146                 typeDec.getType().toShortString());
147     }
148
149     public String JavaDoc getKind() {
150         return "introduction";
151     }
152
153     public String JavaDoc getId() { return dec.getId(); }
154
155     public String JavaDoc toShortString() {
156         return targets.toShortString()+"."+dec.getId();
157     }
158
159     // ------------------------------
160
// bcg
161

162     // I don't know why these are still around, but it doesn't seem to
163
// be a problem if we don't generate code for them.
164
protected void cgMember(ClassfileBuilder maker) {}
165
166     //BEGIN: Generated from @child and @property
167
protected GenTypeName targets;
168     public GenTypeName getTargets() { return targets; }
169     public void setTargets(GenTypeName _targets) {
170         if (_targets != null) _targets.setParent(this);
171         targets = _targets;
172     }
173
174     protected Dec dec;
175     public Dec getDec() { return dec; }
176     public void setDec(Dec _dec) { dec = _dec; }
177
178     public IntroducedDec(SourceLocation location, GenTypeName _targets, Dec _dec) {
179         super(location);
180         setTargets(_targets);
181         setDec(_dec);
182     }
183     protected IntroducedDec(SourceLocation source) {
184         super(source);
185     }
186
187     public ASTObject copyWalk(CopyWalker walker) {
188         IntroducedDec ret = new IntroducedDec(getSourceLocation());
189         ret.preCopy(walker, this);
190         if (targets != null) ret.setTargets( (GenTypeName)walker.process(targets) );
191         ret.dec = dec;
192         return ret;
193     }
194
195     public ASTObject getChildAt(int childIndex) {
196         switch(childIndex) {
197         case 0: return targets;
198         default: return super.getChildAt(childIndex);
199         }
200     }
201      public String JavaDoc getChildNameAt(int childIndex) {
202         switch(childIndex) {
203         case 0: return "targets";
204         default: return super.getChildNameAt(childIndex);
205         }
206     }
207      public void setChildAt(int childIndex, ASTObject child) {
208         switch(childIndex) {
209         case 0: setTargets((GenTypeName)child); return;
210         default: super.setChildAt(childIndex, child); return;
211         }
212     }
213      public int getChildCount() {
214         return 1;
215     }
216
217     public String JavaDoc getDefaultDisplayName() {
218         return "IntroducedDec(dec: "+dec+")";
219     }
220
221     //END: Generated from @child and @property
222
}
223
224
225
226
Popular Tags