KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.aspectj.compiler.base.cst.*;
29
30 import java.util.*;
31
32 /**
33   */

34 public class MovingWalker extends Walker {
35     protected Map remapNodes = new HashMap();
36     protected Stack lexicalTypes = new Stack();
37     public Type toType;
38     public boolean isStatic;
39
40     //XXX this should probably go away
41
public MovingWalker(JavaCompiler compiler) {
42         super(compiler);
43     }
44
45     public MovingWalker(JavaCompiler compiler, Type fromType, Type toType) {
46         super(compiler);
47         this.toType = toType;
48         pushLexicalType(fromType);
49     }
50
51     public void addMappings(Map map) {
52         remapNodes.putAll(map);
53     }
54
55     public boolean hasToType() { return toType != null; }
56
57     public Type getToType() { return toType; }
58
59     public void addMapping(Object JavaDoc oldObject, Object JavaDoc newObject) {
60         remapNodes.put(oldObject, newObject);
61     }
62
63     public boolean inTopLexicalType() {
64         return lexicalTypes.size() == 1;
65     }
66
67     public boolean hasLexicalType() {
68         return lexicalTypes.size() > 0;
69     }
70
71     public Type getLexicalType() {
72         return (Type)lexicalTypes.peek();
73     }
74
75     public Type getTopLexicalType() {
76         return (Type)lexicalTypes.get(0);
77     }
78
79     public void pushLexicalType(Type t) {
80         lexicalTypes.push(t);
81     }
82
83     public void popLexicalType() {
84         lexicalTypes.pop();
85     }
86
87     public Expr moveThisExpr(ThisExpr thisExpr, Type thisType) {
88         //System.out.println(this + ": " + thisType + ", " + lexicalTypes);
89

90         if (hasLexicalType() && thisType == getTopLexicalType()) {
91             Expr ret = makeThisExpr();
92             //System.out.println(" making: " + ret);
93
if (ret != null) {
94                 return ret;
95             }
96         }
97         return thisExpr;
98     }
99     
100     /**
101      * returns what a varExpr should remap to.
102      */

103     public Expr moveVarExpr(VarExpr var) {
104         VarDec ret = (VarDec)remapNodes.get(var.getVarDec());
105         if (ret != null) {
106             if (ret instanceof FieldDec) {
107                 return getAST().makeGet((FieldDec)ret);
108             } else {
109                 var.setVarDec(ret);
110                 var.setType(null);
111                 var.getType();
112                 return var;
113             }
114         } else {
115             return handleFreeVar(var);
116         }
117     }
118     
119     /** returns what a free varExpr should remap to.
120      */

121     protected Expr handleFreeVar(VarExpr var) {
122         return var;
123     }
124
125     protected Expr makeThisExpr() { return null; }
126
127     public Type moveType(Type type) {
128         SemanticObject ret = (SemanticObject)remapNodes.get(type);
129         if (ret != null && ret instanceof Type) return (Type)ret;
130         return type;
131     }
132
133     public void moveLink(SOLink link) {
134         SemanticObject ret = (SemanticObject)remapNodes.get(link.getTarget());
135         if (ret != null) link.setTarget(ret);
136     }
137
138     public Expr moveLinkExpr(SOLink link) {
139         SemanticObject ret = (SemanticObject)remapNodes.get(link.getTarget());
140         if (ret != null) link.setTarget(ret);
141         return (Expr)link;
142     }
143
144     public ASTObject process(ASTObject object) {
145         object.preMove(this);
146         object.walk(this);
147         return object.postMove(this);
148     }
149 }
150
Popular Tags