KickJava   Java API By Example, From Geeks To Geeks.

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


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.base.ast.*;
28 import org.aspectj.compiler.base.*;
29 import org.aspectj.compiler.crosscuts.*;
30
31 import java.util.*;
32
33 /**
34   * @grammar ...
35   * @child Exprs args
36   * @child MethodDec methodDec
37   *
38   */

39 public class AnonymousMethodExpr extends Expr {
40     protected Type discoverType() { return methodDec.getResultType(); }
41
42     public void grabContext() {
43         ExprMaker em = new ExprMaker(getCompiler());
44         em.process(methodDec);
45         Map inParams = em.getInParams();
46         
47         Exprs args = getArgs();
48         Formals formals = methodDec.getFormals();
49         
50         // loop over inParams to create call args and formals together
51
for (Iterator i = inParams.entrySet().iterator(); i.hasNext(); ) {
52             Map.Entry entry = (Map.Entry)i.next();
53             VarDec varDec = (VarDec)entry.getKey();
54             varDec.getModifiers().setFinal(true); //??? assumes too much
55
args.add(getAST().makeVar(varDec));
56             
57             FormalDec formalDec = (FormalDec)entry.getValue();
58             formals.add(formalDec);
59         }
60     }
61     
62     public ASTObject postMove(MovingWalker walker) {
63         //XXX This architecture causes us to walk dec twice
64
//XXX I'm not thrilled about that
65

66         //if (!em.needsThis()) dec.getModifiers().setStatic(true);
67
grabContext();
68         return this;
69     }
70     
71     //INTRO from CopyWalker
72
// public ASTObject postCopy(CopyWalker walker, ASTObject oldObject) {
73
//
74
// getExpr().setMethod(getDec().getMethod());
75
// return this;
76
// }
77

78     public void fixAST(ASTFixerPass fixer) {
79         // only walk args
80
// methodDec will be walked after it's been moved in the tree
81
args = (Exprs)fixer.process(args);
82     }
83
84     // INTRO from ASTFixerPass
85
public ASTObject postFixAST(final ASTFixerPass fixer) {
86         if (inStaticContext()) {
87             methodDec.getModifiers().setStatic(true);
88         } else {
89             methodDec.getModifiers().setStatic(false);
90         }
91         
92         TypeDec inTypeDec = getBytecodeTypeDec();
93         if (methodDec.isStatic() && inTypeDec.isInner()) {
94             inTypeDec = inTypeDec.getOutermostTypeDec();
95         }
96
97         Decs body = inTypeDec.getBody();
98         methodDec.getModifiers().setPrivate(true);
99         // we don't need to anonymize these since NameHygienePass will fix them for us
100
body.add(getMethodDec());
101         getMethodDec().setAllEnclosingTypes(inTypeDec.getType());
102         
103         Expr thisExpr;
104         if (methodDec.isStatic()) {
105             thisExpr = getAST().makeTypeExpr(inTypeDec.getType());
106         } else {
107             thisExpr = getAST().makeThis(inTypeDec.getType());
108         }
109         
110         return getAST().makeCall(getMethodDec(), thisExpr, args);
111     }
112     
113     //INTRO from MixinImplementationPass
114
public void implementMixin(MixinImplementationPass fixer) {
115         return;
116     }
117     
118     
119     public void unparse(CodeWriter writer) {
120         writer.write("DecInExpr[");
121         writer.write(args);
122         writer.write(" -> ");
123         writer.write(methodDec);
124         writer.write("]");
125     }
126     
127     //BEGIN: Generated from @child and @property
128
protected Exprs args;
129     public Exprs getArgs() { return args; }
130     public void setArgs(Exprs _args) {
131         if (_args != null) _args.setParent(this);
132         args = _args;
133     }
134     
135     protected MethodDec methodDec;
136     public MethodDec getMethodDec() { return methodDec; }
137     public void setMethodDec(MethodDec _methodDec) {
138         if (_methodDec != null) _methodDec.setParent(this);
139         methodDec = _methodDec;
140     }
141     
142     public AnonymousMethodExpr(SourceLocation location, Exprs _args, MethodDec _methodDec) {
143         super(location);
144         setArgs(_args);
145         setMethodDec(_methodDec);
146     }
147     protected AnonymousMethodExpr(SourceLocation source) {
148         super(source);
149     }
150     
151     public ASTObject copyWalk(CopyWalker walker) {
152         AnonymousMethodExpr ret = new AnonymousMethodExpr(getSourceLocation());
153         ret.preCopy(walker, this);
154         if (args != null) ret.setArgs( (Exprs)walker.process(args) );
155         if (methodDec != null) ret.setMethodDec( (MethodDec)walker.process(methodDec) );
156         return ret;
157     }
158     
159     public ASTObject getChildAt(int childIndex) {
160         switch(childIndex) {
161         case 0: return args;
162         case 1: return methodDec;
163         default: return super.getChildAt(childIndex);
164         }
165     }
166      public String JavaDoc getChildNameAt(int childIndex) {
167         switch(childIndex) {
168         case 0: return "args";
169         case 1: return "methodDec";
170         default: return super.getChildNameAt(childIndex);
171         }
172     }
173      public void setChildAt(int childIndex, ASTObject child) {
174         switch(childIndex) {
175         case 0: setArgs((Exprs)child); return;
176         case 1: setMethodDec((MethodDec)child); return;
177         default: super.setChildAt(childIndex, child); return;
178         }
179     }
180      public int getChildCount() {
181         return 2;
182     }
183     
184     public String JavaDoc getDefaultDisplayName() {
185         return "AnonymousMethodExpr()";
186     }
187     
188     //END: Generated from @child and @property
189
}
190
Popular Tags