KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > joinpoints > ExprJp


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 package org.aspectj.compiler.crosscuts.joinpoints;
25
26 import org.aspectj.compiler.base.ast.*;
27 import org.aspectj.compiler.crosscuts.ast.*;
28 import org.aspectj.compiler.crosscuts.*;
29
30 import java.util.*;
31
32
33 public abstract class ExprJp extends CodeBodyJp {
34     public Expr expr;
35
36     AnonymousMethodExpr syntheticCall = null;
37
38     FormalDec targetFormal = null;
39     FormalDec thisFormal = null;
40
41     public ExprJp(Expr expr, JoinPoint enclosingJoinPoint) {
42         super(enclosingJoinPoint);
43         this.expr = expr;
44     }
45
46     public abstract Dec getTargetDec();
47
48     public String JavaDoc toString() {
49         return getKind().toShortString() +
50             "(" + getTargetDec().toShortString() + ")";
51     }
52
53
54     FormalDec getTargetFormal() {
55         if (getTargetExprType() == null) return null;
56
57         if (targetFormal == null) {
58             targetFormal = getAST().makeFormal(getTargetExprType(), "target");
59         }
60         return targetFormal;
61     }
62
63     FormalDec getThisFormal() {
64         return null;
65         /*
66         if (getThisExprType() == null) return null;
67         
68         if (thisFormal == null) {
69             thisFormal = getAST().makeFormal(getThisExprType(), "_this");
70         }
71         return thisFormal;
72         */

73     }
74     
75     public abstract Type getTargetExprType();
76     public abstract Type getTargetType();
77
78     public Expr makeTargetExpr() {
79         FormalDec formal = getTargetFormal();
80         if (formal == null) return null;
81         else return getAST().makeVar(formal);
82     }
83
84     public Expr makeTargetExprOrType() {
85         Expr ret = makeTargetExpr();
86         if (ret != null) return ret;
87         return getAST().makeTypeExpr(getTargetType());
88     }
89
90     public Expr makeThisExpr() {
91         return super.makeThisExpr();
92 // FormalDec formal = getThisFormal();
93
// if (formal == null) return null;
94
// else return getAST().makeVar(formal);
95
}
96
97     public Type getResultType() {
98         return expr.getType();
99     }
100
101     public ASTObject getSourceLocation() { return expr; }
102
103     protected abstract AnonymousMethodExpr makeSyntheticCall();
104
105     protected abstract ASTObject makeInnerCall(Expr targetExpr, Exprs argsExprs);
106
107     protected AnonymousMethodExpr makeAnonMethodExpr(Formals formals,
108                                                      Expr targetExpr, Exprs argsExprs)
109     {
110         final AST ast = getAST();
111
112         FormalDec targetFormal = getTargetFormal();
113         if (targetFormal != null) {
114             formals.add(0, targetFormal);
115             argsExprs.add(0, targetExpr);
116         }
117
118         FormalDec thisFormal = getThisFormal();
119         if (thisFormal != null) {
120             formals.add(0, thisFormal);
121             argsExprs.add(0, ast.makeThis(getThisExprType()));
122         }
123
124         ASTObject innerCall = makeInnerCall(makeTargetExprOrType(), makeArgsExprs());
125
126         Modifiers modifiers = ast.makeModifiers(Modifiers.PRIVATE);
127         if (getThisExprType() == null) modifiers.setStatic(true);
128
129         String JavaDoc innerName =
130             getAST().makeGeneratedName(getTargetDec().getName() + "$" + getKind());
131
132         Type innerResultType = getResultType();
133         Stmt stmt;
134         if (innerResultType.isVoid()) {
135             if (innerCall instanceof Stmt) stmt = (Stmt)innerCall;
136             else stmt = ast.makeStmt((Expr)innerCall);
137         } else {
138             stmt = ast.makeReturn((Expr)innerCall);
139         }
140         
141         
142         SourceLocation loc = getSourceLocation().getSourceLocation();
143         //System.out.println(innerCall + ", " + loc);
144
innerCall.setSourceLocation(loc);
145         stmt.setSourceLocation(loc);
146
147         MethodDec innerMethod = ast.makeMethod(modifiers, innerResultType, innerName,
148                 formals, ast.makeBlock(stmt));
149         //innerMethod.setSourceLocation(loc);
150
innerMethod.setAllEnclosingTypes(getBytecodeType()); // let ast.makeCall work
151

152         return new AnonymousMethodExpr(loc, argsExprs, innerMethod);
153     }
154     
155
156     AnonymousMethodExpr getSyntheticCall() {
157         if (syntheticCall == null) {
158             syntheticCall = makeSyntheticCall();
159             syntheticCall.grabContext();
160             replaceExprWith(syntheticCall);
161         }
162         return syntheticCall;
163     }
164     
165     protected void replaceExprWith(Expr newExpr) {
166         expr.replaceWith(newExpr);
167     }
168     
169     public CodeDec getBaseCodeDec() {
170         return getSyntheticCall().getMethodDec();
171     }
172
173     public Stmts getStmts() {
174         return getBaseCodeDec().getBody().getStmts();
175     }
176     public void setStmts(Stmts stmts) {
177         getBaseCodeDec().getBody().setStmts(stmts);
178     }
179
180     public void finishJoinPoint() {
181         super.finishJoinPoint();
182         getBaseCodeDec().computeMinimalThrows();
183     }
184 }
185
Popular Tags