KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > MemberClassMunger


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;
26
27 import org.aspectj.compiler.base.ast.*;
28
29 import java.util.*;
30
31 /**
32  * Deals with inner classes.
33  *
34  * <ul>
35  *
36  * <li>Adds a field to all inner classes (see {@link
37  * TypeDec#walkMemberMunger}).</li>
38  *
39  * <li>Converts qualified this expressions into chains of
40  * references to those fields (see {@link
41  * QualifiedThisExpr#walkMemberMunger}).</li>
42  *
43  * <li>Adds a new formal to constructors of inner classes and sets
44  * the value passed in to the class' field for constructors with
45  * super constructor calls (see {@link
46  * ConstructorDec#walkMemberMunger}). </li>
47  *
48  * </ul>
49  *
50  * <p> The apropriate qualified this expressions (for new instance
51  * expressions and the like) have already been set in the
52  * InnerInfoPass. </p>
53  *
54  * */

55
56 public class MemberClassMunger extends WalkerPass {
57
58     public MemberClassMunger(JavaCompiler compiler) {
59         super(compiler);
60     }
61
62     public String JavaDoc getDisplayName() {
63         return "munging inner classes";
64     }
65
66     private Stack types = new Stack();
67     public void enterType(NameType t) {
68         types.push(t);
69     }
70     public void leaveType() {
71         types.pop();
72     }
73     public NameType currentType() {
74         return (NameType) types.peek();
75     }
76
77     private List/*<FieldDec>*/ fields = new ArrayList();
78     public void pushField(FieldDec dec) {
79         fields.add(dec);
80     }
81     public void popField() {
82         fields.remove(fields.size() - 1);
83     }
84     public FieldDec currentField() {
85         return (FieldDec) fields.get(fields.size() - 1);
86     }
87     // possibly overkill, but will show us errors earlier
88
public List saveFields() {
89         List fields = this.fields;
90         this.fields = new ArrayList();
91         return fields;
92     }
93     public void restoreFields(List fields) {
94         this.fields = fields;
95     }
96
97     public Expr buildExprFromEnclosing(Expr seed, NameType targetType) {
98         return buildExpr(seed, targetType, fields.size() - 2);
99     }
100     public Expr buildExprFromThis(Expr seed, NameType targetType) {
101         return buildExpr(seed, targetType, fields.size() - 1);
102     }
103
104     private Expr buildExpr(Expr seed, NameType targetType, int i) {
105         final AST ast = getAST();
106         while (true) {
107             Type currType = seed.getType();
108             if (currType == targetType) {
109                 return seed;
110             } else {
111                 if (i < 0) {
112                     throw new RuntimeException JavaDoc("Can't get to " + targetType
113                                                + " from " + seed);
114                 }
115                 seed = ast.makeGet(seed, (FieldDec) fields.get(i--));
116             }
117         }
118     }
119
120     public ASTObject process(ASTObject object) {
121         return object.walkMemberMunger(this);
122     }
123 }
124
125
Popular Tags