KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > instrumentor > MethodCallStrategy


1 /*
2  * Copyright (C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.instrumentor;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import alt.jiapi.JiapiException;
25 import alt.jiapi.Rule;
26 import alt.jiapi.Runtime;
27 import alt.jiapi.reflect.Instruction;
28 import alt.jiapi.reflect.InstructionList;
29
30 import alt.jiapi.reflect.instruction.Invocation;
31 import alt.jiapi.reflect.instruction.OpcodeGroups;
32
33 /**
34  * This Strategy finds invocations made to methods.
35  * Each part in a List returned, points to a method invocation
36  * instruction.
37  *
38  * @author Mika Riekkinen
39  * @author Joni Suominen
40  * @version $Revision: 1.6 $ $Date: 2004/03/15 14:47:53 $
41  */

42 public class MethodCallStrategy extends AbstractStrategy {
43     public MethodCallStrategy() {
44         this("*");
45     }
46
47     /**
48      * Constructor.
49      * @param matcher A matcher that is used
50      */

51     public MethodCallStrategy(String JavaDoc matcher) {
52         super(matcher);
53     }
54
55     /**
56      * Constructor.
57      * @param matchers An array of matchers
58      * @param reverse if true, matching is reversed.
59      */

60     public MethodCallStrategy(String JavaDoc [] matchers, boolean reverse) {
61         super(matchers, reverse);
62     }
63
64     /**
65      * Scans InstructionList for method invocations made. Each known spot
66      * returned is of length 1, pointing to the method invocation being
67      * made.
68      *
69      * @param il InstructionList to scan for method invocation instructions.
70      * @return A List of known spots, each of which points to method
71      * invocation instruction.
72      */

73     public List JavaDoc findHotSpots(InstructionList il) {
74         List JavaDoc hotSpots = new ArrayList JavaDoc();
75         int index = 0;
76         
77         // Find invokeXXX instructions
78
while ((index = il.indexOf(OpcodeGroups.INVOKE_INSTRUCTIONS,
79                                    index)) != -1) {
80 // Instruction ins = il.get(index);
81
// Operand o = ins.getOperand();
82
Invocation ins = (Invocation)il.get(index);
83             String JavaDoc targetName = ins.getClassName() + "." + ins.getMethodName();
84             // Grep
85
//if (match(o.toString())) {
86
if (match(targetName)) {
87                 Instrumentation i = getInstrumentation();
88                 //i.setTargetName(o.toString());
89
i.setTargetName(targetName);
90                 HotSpot h = new HotSpot(index, index + 1);
91                 hotSpots.add(h);
92             }
93
94             index++;
95         }
96
97         return hotSpots;
98     }
99 }
100
Popular Tags