KickJava   Java API By Example, From Geeks To Geeks.

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


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.reflect.Instruction;
25 import alt.jiapi.reflect.InstructionList;
26 import alt.jiapi.reflect.instruction.Opcodes;
27
28 /**
29  * This class implements Strategy so, that method entrance
30  * is a split boundary.
31  *
32  * NOTE: This Strategy is a bug, and will be removed from Jiapi.
33  * InstructionList passed to findHotSpots() method is not guaranteed
34  * to contain entire body of the method.
35  *
36  * @author Mika Riekkinen
37  * @author Joni Suominen
38  * @version $Revision: 1.7 $ $Date: 2004/03/15 14:47:53 $
39  */

40 public class MethodEntryStrategy extends AbstractStrategy {
41     /**
42      */

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

51     public MethodEntryStrategy(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 MethodEntryStrategy(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 0, 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         String JavaDoc name = il.getDeclaringMethod().getName();
78
79         if (name.equals("<init>")) {
80             // First let the super or other constructor be called.
81
index = il.indexOf(Opcodes.INVOKESPECIAL);
82             if (index == -1) {
83                 index = 0;
84             }
85             else {
86                 index++;
87             }
88         }
89             
90         // Grep
91
if (match(name)) {
92             HotSpot h = new HotSpot(index, index);
93             // Uncomment following, if we want to return first instruction
94
// HotSpot h = new HotSpot(index, index + 1);
95

96             Instrumentation i = getInstrumentation();
97             i.setTargetName(name);
98             hotSpots.add(h);
99         }
100
101         return hotSpots;
102     }
103 }
104
Popular Tags