KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > bytecode > ASTConnection


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.bytecode;
26
27 import org.aspectj.compiler.base.JavaCompiler;
28 import org.aspectj.compiler.base.CompilerObject;
29 import org.aspectj.compiler.base.parser.SourceInfo;
30 import org.aspectj.compiler.base.ast.*;
31 import org.aspectj.compiler.base.*;
32
33
34 import java.util.*;
35 import java.lang.reflect.Modifier JavaDoc;
36
37 public class ASTConnection extends CompilerObject {
38
39     public ASTConnection(JavaCompiler compiler) {
40         super(compiler);
41     }
42     
43     protected SourceLocation dummySource = new DummySourceLocation(getCompiler());
44
45     // at the top are more general methods that might be moved elsewhere
46

47     private TypeDs classesToTypeDs(ConstantPool.Class[] classes) {
48         if (classes == null || classes.length == 0) {
49             return null;
50         }
51
52         TypeDs names = new TypeDs(dummySource);
53
54         final int N = classes.length;
55         for(int i=0; i<N; i++) {
56             //System.out.println(i + ": " + makeTypeD(classes[i]));
57
names.add(makeTypeD(classes[i]));
58         }
59         return names;
60     }
61
62     // at the bottom are clear AST connections
63
public TypeDec makeTypeDec(ClassFile classFile) {
64         int modifiers = classFile.modifiers;
65         boolean isInterface = Modifier.isInterface(modifiers);
66         modifiers = modifiers & ~Modifier.INTERFACE;
67         Decs body = new Decs(dummySource);
68
69         String JavaDoc fullName = classFile.thisClass.getName();
70         String JavaDoc name = fullName;
71         String JavaDoc packageName = null;
72         int dot = fullName.lastIndexOf('.');
73         if (dot != -1) {
74             packageName = fullName.substring(0, dot);
75             name = fullName.substring(dot+1);
76         }
77         
78         String JavaDoc id = name;
79         String JavaDoc enclosingName = null;
80         int dollar = id.lastIndexOf('$');
81         if (dollar != -1) {
82             name = id.substring(dollar+1);
83             enclosingName = id.substring(0, dollar);
84         }
85
86         CompilationUnit cu = new CompilationUnit(dummySource,
87                 packageName, null, new Decs(dummySource),
88                 new SourceInfo(null, null),true);
89         SourceLocation loc = new TextSourceLocation(cu, -1, -1);
90         //cu.setContext(new SourceContext(getCompiler(),cu));
91

92         //System.err.println(packageName+" :: "+name);
93

94         TypeD superClass;
95         if (classFile.superClass == null) {
96             superClass = null;
97         } else {
98             superClass = makeTypeD(classFile.superClass);
99         }
100
101         TypeDs _implements = classesToTypeDs(classFile.interfaces);
102
103         TypeDec typeDec;
104         if (isInterface) {
105             typeDec = getAST().makeInterface(getAST().makeModifiers(modifiers),
106                         /*fullName,*/ name, _implements);
107         } else {
108             typeDec = getAST().makeClass(getAST().makeModifiers(modifiers),
109                         /*fullName,*/ name, superClass, _implements);
110         }
111         typeDec.setBody(body);
112
113         typeDec.setSourceLocation(loc);
114         if (enclosingName != null) {
115             Type t = getTypeManager().getType(packageName, enclosingName);
116             t.getTypeDec().getBody().add(typeDec);
117             typeDec.setEnclosingTypeDec(t.getTypeDec());
118         } else {
119             cu.getDecs().add(typeDec);
120         }
121         //typeDec.setContext(cu.getContext());
122

123         //Context typeContext = new TypeContext(getCompiler(),typeDec.getContext(), typeDec.getType());
124
//typeDec.fullName = fullName;
125
//typeDec.cacheDecs();
126
//body.setContext(typeContext);
127

128         for(int i=0; i<classFile.fields.length; i++) {
129             FieldDec dec = makeFieldDec(classFile.fields[i]);
130             dec.setSourceLocation(loc);
131             //dec.setContext(typeContext);
132
body.add(dec);
133             //typeDec.addDec(dec, true);
134
//body.append(makeFieldDec(classFile.fields[i]));
135
}
136
137         for(int i=0; i<classFile.methods.length; i++) {
138             CodeDec dec = makeCodeDec(classFile.methods[i]);
139             if (dec == null) {
140                 //!!!ystem.err.println("null code in "+fullName);
141
continue;
142             }
143             dec.setSourceLocation(loc);
144             //dec.setContext(typeContext);
145
body.add(dec);
146             //typeDec.addDec(dec, true);
147
//body.append(makeMethodDec(classFile.methods[i]));
148
}
149
150         Attributes.InnerClassesAttribute attr =
151             (Attributes.InnerClassesAttribute)
152             classFile.attributes.findAttribute(Attributes.InnerClassesAttribute.class);
153         if (attr != null) {
154             typeDec.memberTypeNames = attr.getContainedTypeNames(fullName);
155 // System.out.println("Inners of class " + fullName);
156
// System.out.println(attr);
157
// System.out.println(" " + typeDec.memberTypeNames);
158
if (! attr.isPackageLevel(fullName)) {
159                 Modifiers m = new Modifiers(dummySource, attr.getModifiers(fullName));
160 // System.out.println(" " + "modifiers " + m);
161
typeDec.setModifiers(m);
162                 if (! m.isStatic() && ! (typeDec instanceof InterfaceDec)) {
163                     for (Iterator i = typeDec.getBody().iterator(); i.hasNext(); ) {
164                         Object JavaDoc dec = i.next();
165                         if (dec instanceof ConstructorDec) {
166                             ConstructorDec cdec = (ConstructorDec) dec;
167                             Formals f = cdec.getFormals();
168                             cdec.setEnclosingInstanceFormal(f.get(0));
169                             f.remove(0);
170                         }
171                     }
172                     typeDec.setInnerDiscoveries(false, true, false);
173                 } else {
174                     typeDec.setInnerDiscoveries(false, false, false);
175                 }
176             } else {
177                 typeDec.setInnerDiscoveries(true, false, false);
178             }
179         } else {
180             typeDec.setInnerDiscoveries(true, false, false);
181         }
182
183         //typeDec.cacheDecs();
184
//typeDec.buildTypeGraph();
185

186         //System.err.println("about to build context for: "+typeDec.getId());
187

188         //typeDec.buildContext(new EmptyContext(), Context.CODE_BLOCKS);
189

190         return typeDec;
191     }
192
193     public TypeD makeTypeD(ConstantPool.Class cpClass) {
194         String JavaDoc name = cpClass.getName();
195         int lastDot = name.lastIndexOf('.');
196         if (lastDot == -1) {
197             return new LazyNameTypeD(dummySource,null, name);
198         } else {
199             return new LazyNameTypeD(dummySource,name.substring(0, lastDot), name.substring(lastDot+1));
200         }
201     }
202
203     public FieldDec makeFieldDec(ClassFile.FieldInfo field) {
204         int modifiers = field.modifiers;
205         TypeD typeD = makeTypeD(field.descriptor.value);
206         String JavaDoc name = field.name.value;
207         Expr initializer = null;
208
209         Attributes.ConstantValueAttribute attr =
210             (Attributes.ConstantValueAttribute)
211             field.attributes.findAttribute(Attributes.ConstantValueAttribute.class);
212         if (attr != null) {
213             ConstantPool.Entry entry = attr.value;
214             if (entry instanceof ConstantPool.String) {
215                 ConstantPool.String stringEntry = (ConstantPool.String)entry;
216                 initializer = new StringLiteralExpr(dummySource,stringEntry.value.value);
217             } else if (entry instanceof ConstantPool.Float) {
218                 ConstantPool.Float floatEntry = (ConstantPool.Float)entry;
219                 initializer = new FloatLiteralExpr(dummySource,floatEntry.value);
220             } else if (entry instanceof ConstantPool.Long) {
221                 ConstantPool.Long longEntry = (ConstantPool.Long)entry;
222                 initializer = new LongLiteralExpr(dummySource,longEntry.value);
223             } else if (entry instanceof ConstantPool.Double) {
224                 ConstantPool.Double doubleEntry = (ConstantPool.Double)entry;
225                 initializer = new DoubleLiteralExpr(dummySource,doubleEntry.value);
226             } else /* (entry instanceof ConstantPool.Integer) */ {
227                 ConstantPool.Integer intEntry = (ConstantPool.Integer)entry;
228                 int init = intEntry.value;
229                 if (typeD.getType().isBoolean()) {
230                     initializer = new BooleanLiteralExpr(dummySource,init != 0);
231                 } else {
232                     initializer = new IntLiteralExpr(dummySource,init);
233                 }
234             }
235         }
236         return new FieldDec(dummySource,new Modifiers(dummySource,modifiers), typeD, name, initializer);
237     }
238
239
240     public CodeDec makeCodeDec(ClassFile.MethodInfo method) {
241         int modifiers = method.modifiers;
242         String JavaDoc name = method.name.value;
243
244         TypeDs _throws = classesToTypeDs(method.getExceptions());
245 // if (name.equals("finalize")) System.out.println(" .class throws: " + _throws +
246
// ", " + method.getExceptions().length + ": " + method.getExceptions()[0]);
247
Formals formals = getFormals(method.descriptor.value);
248
249         if (name.equals("<init>")) {
250             CodeDec ret = new ConstructorDec(dummySource,new Modifiers(dummySource,modifiers), formals, _throws, null);
251             if (method.isDeprecated()) ret.setDeprecated(true);
252             return ret;
253         } else if (name.equals("<clinit>")) {
254             //!!!System.err.println("found class initializer");
255
return null;
256         }
257
258         //!!! this dualness probably indicates that a signature class is needed
259
TypeD typeD = getReturnTypeD(method.descriptor.value);
260
261
262         CodeDec ret = new MethodDec(dummySource,new Modifiers(dummySource,modifiers), typeD, name,
263                              formals, _throws, null);
264         if (method.isDeprecated()) {
265             ret.setDeprecated(true);
266             //System.out.println("added deprecated method: " + ret.toShortString() + ", " + ret.isDeprecated());
267
}
268
269         return ret;
270     }
271
272     TypeD getReturnTypeD(String JavaDoc descriptor) {
273         int lastParen = descriptor.lastIndexOf(')');
274         return makeTypeD(descriptor.substring(lastParen+1));
275     }
276
277     class IndexedChars {
278         char[] chars;
279         int index;
280         public IndexedChars(char[] chars) {
281             this.chars = chars;
282             index = 0;
283         }
284
285         public char read() {
286             return chars[index++];
287         }
288
289         public String JavaDoc readTo(char end) {
290             for(int j=index; j<chars.length; j++) {
291                 if (chars[j] == end) {
292                     String JavaDoc ret = new String JavaDoc(chars, index, j-index);
293                     index = j+1;
294                     return ret;
295                 }
296             }
297             String JavaDoc ret = new String JavaDoc(chars, index, chars.length-index);
298             index = chars.length;
299             return ret;
300         }
301     }
302
303     TypeD readNameTypeD(IndexedChars ic) {
304         char[] chars = ic.chars;
305         int startIndex = ic.index;
306         int endIndex = startIndex;
307         int idStart = startIndex;
308         while (endIndex < chars.length) {
309             if (chars[endIndex] == '/') {
310                 chars[endIndex] = '.';
311                 idStart = endIndex+1;
312             }
313             if (chars[endIndex] == ';') break;
314             endIndex++;
315         }
316
317         String JavaDoc id = new String JavaDoc(chars, idStart, endIndex-idStart);
318         String JavaDoc pname;
319         if (idStart == startIndex) pname = null;
320         else pname = new String JavaDoc(chars, startIndex, idStart-startIndex-1);
321         //System.err.println("read name: "+pname+", "+id);
322
ic.index = endIndex+1;
323         return new LazyNameTypeD(dummySource, pname, id);
324     }
325
326
327     TypeD readTypeD(IndexedChars ic) {
328         switch(ic.read()) {
329         case 'B': return getTypeManager().byteType.makeTypeD();
330         case 'C': return getTypeManager().charType.makeTypeD();
331         case 'D': return getTypeManager().doubleType.makeTypeD();
332         case 'F': return getTypeManager().floatType.makeTypeD();
333         case 'I': return getTypeManager().intType.makeTypeD();
334         case 'J': return getTypeManager().longType.makeTypeD();
335         case 'S': return getTypeManager().shortType.makeTypeD();
336         case 'Z': return getTypeManager().booleanType.makeTypeD();
337         case 'V': return getTypeManager().voidType.makeTypeD();
338         case '[': return new ArrayTypeD(dummySource,readTypeD(ic));
339         case 'L': return readNameTypeD(ic);
340         }
341         throw new RuntimeException JavaDoc("unknown descriptor: "+ic.chars[ic.index-1]+" in "+new String JavaDoc(ic.chars));
342     }
343
344
345     Formals getFormals(String JavaDoc descriptor) {
346         IndexedChars ic = new IndexedChars(descriptor.toCharArray());
347         ic.index++;
348         List formals = new ArrayList();
349         int n = 0;
350         while (ic.chars[ic.index] != ')') {
351             FormalDec formal = new FormalDec(dummySource,new Modifiers(dummySource,0), readTypeD(ic), "__" + n, null);
352             n += 1;
353             formals.add(formal);
354         }
355         return new Formals(dummySource,(FormalDec[])formals.toArray(new FormalDec[formals.size()]));
356     }
357
358     public TypeD makeTypeD(String JavaDoc descriptor) {
359         return readTypeD(new IndexedChars(descriptor.toCharArray()));
360     }
361 }
362
Popular Tags