KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import org.aspectj.compiler.base.JavaCompiler;
30 import org.aspectj.util.JavaStrings;
31
32 import java.util.*;
33
34
35 public class MethodCallJp extends CallJp {
36     public MethodCallJp(CallExpr callExpr, JoinPoint enclosingJp) {
37         super(callExpr, enclosingJp);
38     }
39
40     public static final Kind KIND = new Kind("method-call", METHOD_CALL);
41
42     public Kind getKind() { return KIND; }
43
44     public boolean isMethodCall() { return true; }
45
46     public Type getTargetExprType() {
47         if (getCalledCodeDec().isStatic()) return null;
48         return getCallExpr().getExpr().getType();
49     }
50
51     public Type getTargetType() {
52         return getCallExpr().getExpr().getType();
53     }
54
55     protected AnyCallExpr makeBasicInnerCall(Expr targetExpr, Exprs argsExprs) {
56         AnyCallExpr innerCall = getCallExpr();
57         innerCall = (AnyCallExpr)innerCall.copy();
58         innerCall.setArgs(argsExprs);
59         innerCall.setExpr(targetExpr);
60         return innerCall;
61     }
62     
63     protected ASTObject makeInnerCall(Expr targetExpr, Exprs argsExprs) {
64         AnyCallExpr innerCall = makeBasicInnerCall(targetExpr, argsExprs);
65         
66         if (skipTypes != null) {
67             ASTObject ret = innerCall;
68             boolean isVoid = innerCall.getType().isVoid();
69             
70             for (Iterator i = skipTypes.entrySet().iterator(); i.hasNext(); ) {
71                 Map.Entry entry = (Map.Entry)i.next();
72                 Type type = (Type)entry.getKey();
73                 MethodDec md = (MethodDec)entry.getValue();
74                 
75                 final AST ast = getAST();
76                 if (md.isStatic()) {
77                     return ast.makeStaticCall(md.getMethod(), (Exprs)innerArgs.copy());
78                 }
79                 
80                 Expr innerExpr = (Expr)targetExpr.copy();
81                 
82                 if (!innerExpr.getType().isCoercableTo(type)) continue;
83                 
84                 Expr skipCall = ast.makeCall(md.getMethod(),
85                                         ast.makeCast(type, innerExpr),
86                                         (Exprs)innerArgs.copy());
87                 
88                 if (innerExpr.getType().isSubtypeOf(type)) {
89                     return skipCall;
90                 } else {
91                     Expr instanceTest = ast.makeInstanceof((Expr)innerExpr.copy(), type);
92                     if (isVoid) {
93                         Stmt stmt;
94                         if (ret instanceof Stmt) stmt = (Stmt)ret;
95                         else stmt = ast.makeStmt((Expr)ret);
96                         ret = ast.makeIf(instanceTest, ast.makeStmt(skipCall), stmt);
97                     } else {
98                         ret = ast.makeTriTest(instanceTest, skipCall, (Expr)ret);
99                     }
100                 }
101             }
102             return ret;
103         }
104         return innerCall;
105     }
106
107     protected void preImplement() {
108         if (!hasPlans()) return;
109         
110         CodeDec codeDec = getCalledCodeDec();
111         MethodDec targetMethodDec = (MethodDec)codeDec;
112         
113         boolean needsCallSiteAdvice = false;
114         Map skipAdviceOnTypes = new HashMap();
115         
116         //System.out.println("call point: " + this);
117

118         for (Iterator i = targetMethodDec.getMatchingMethods().iterator(); i.hasNext(); ) {
119             MethodDec dec = (MethodDec)i.next();
120             //System.out.println(" md: " + dec.toShortString());
121
CalleeSideCallJp targetPoint =
122                 (CalleeSideCallJp)getWorld().calleeSideCallPoints.get(dec);
123             //System.out.println(" tp: " + targetPoint + ", " + targetPoint.hasPlans());
124
if (targetPoint == null || !targetPoint.hasPlans()) {
125                 needsCallSiteAdvice = true;
126             } else {
127                 skipAdviceOnTypes.put(targetPoint.getCodeDec().getDeclaringType(),
128                                       targetPoint.getPostMethodDec());
129                 if (this.plansMatch(targetPoint)) {
130                     //System.out.println(" plans match"); // do nothing
131
} else {
132                     needsCallSiteAdvice = true;
133                 }
134             }
135         }
136         
137         if (!needsCallSiteAdvice) {
138             //???makeCorrespondences();
139
forgetPlans();
140         } else if (skipAdviceOnTypes.size() > 0) {
141             Set keepTypes = Type.filterTopTypes(skipAdviceOnTypes.keySet());
142             skipAdviceOnTypes.keySet().retainAll(keepTypes);
143             skipAdviceOnTypes(skipAdviceOnTypes);
144         }
145         
146         //System.err.println("planning: " + this + " plans: " + plans);
147
}
148
149     private Map skipTypes = null;
150     public void skipAdviceOnTypes(Map onTypes) {
151         //System.out.println(" top skipAdviceOnTypes: " + onTypes);
152
skipTypes = onTypes;
153     }
154 }
155
Popular Tags