KickJava   Java API By Example, From Geeks To Geeks.

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


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.InstructionList;
28 import alt.jiapi.reflect.Instruction;
29
30 import alt.jiapi.reflect.instruction.FieldAccess;
31 import alt.jiapi.reflect.instruction.OpcodeGroups;
32 import alt.jiapi.reflect.instruction.Opcodes;
33
34 /**
35  * This strategy finds field access instructions.
36  * Each known spot returned is only of length 1, pointing to
37  * field access Instruction.
38  *
39  * @author Mika Riekkinen
40  * @author Joni Suominen
41  * @version $Revision: 1.8 $ $Date: 2004/03/15 14:47:53 $
42  */

43 public class FieldAccessStrategy extends AbstractStrategy {
44     public static final int READ_ACCESS = 1;
45     public static final int WRITE_ACCESS = 2;
46
47     private int accessPolicy;
48     /**
49      * Empty constructor. Access policy is set to READ_ACCESS + WRITE_ACCESS
50      * and all the field accesses are matched.
51      */

52     public FieldAccessStrategy() {
53         this(new String JavaDoc[] {"*"}, false, READ_ACCESS + WRITE_ACCESS);
54     }
55
56     /**
57      * Constructor with specified access policy. Access policies may be
58      * summed together. For example, 'READ_ACCESS + WRITE_ACCESS'.
59      *
60      * @param accessPolicy a policy indicating which instructions are
61      * matched.
62      * @see #READ_ACCESS
63      * @see #WRITE_ACCESS
64      */

65     public FieldAccessStrategy(String JavaDoc matcher, int accessPolicy) {
66         this(new String JavaDoc[] {matcher}, false, accessPolicy);
67     }
68
69     /**
70      * Constructor with specified access policy. Access policies may be
71      * summed together. For example, 'READ_ACCESS + WRITE_ACCESS'.
72      *
73      * @param accessPolicy a policy indicating which instructions are
74      * matched.
75      * @see #READ_ACCESS
76      * @see #WRITE_ACCESS
77      */

78     public FieldAccessStrategy(String JavaDoc matcher, boolean reverseMatch, int accessPolicy) {
79         this(new String JavaDoc[] {matcher}, reverseMatch, accessPolicy);
80     }
81
82     /**
83      * Constructor with specified access policy. Access policies may be
84      * summed together. For example, 'READ_ACCESS + WRITE_ACCESS'.
85      *
86      * @param accessPolicy a policy indicating which instructions are
87      * matched.
88      * @see #READ_ACCESS
89      * @see #WRITE_ACCESS
90      */

91     public FieldAccessStrategy(String JavaDoc [] matchers, boolean reverseMatch,
92                                int accessPolicy) {
93         super(matchers, reverseMatch);
94         this.accessPolicy = accessPolicy;
95     }
96
97
98     /**
99      * Scans InstructionList for field access Instructions.
100      *
101      * @param il InstructionList to scan for field access instructions.
102      * @return A List of known spots, that points either to read access
103      * or write access instruction.
104      */

105     public List JavaDoc findHotSpots(InstructionList il) {
106         Instrumentation instrumentation = getInstrumentation();
107         List JavaDoc hotSpots = new ArrayList JavaDoc();
108         int index = 0;
109         
110         // Find getXXX or putXXX instructions
111
while ((index = il.indexOf(OpcodeGroups.FIELD_ACCESS_INSTRUCTIONS,
112                                    index)) != -1) {
113             FieldAccess fa = (FieldAccess)il.get(index);
114             int opcode = fa.getOpcode();
115 // Operand o = i.getOperand();
116
String JavaDoc targetName = fa.getFieldName();
117
118             // if (match(o.toString())) { // Resolution
119
if (match(targetName)) { // Resolution
120
if ((accessPolicy & READ_ACCESS) == READ_ACCESS) {
121                     if ((opcode == Opcodes.GETFIELD) ||
122                         (opcode == Opcodes.GETSTATIC)) {
123                         
124                         HotSpot h = new HotSpot(index, index + 1);
125                         //instrumentation.setTargetName(o.toString());
126
instrumentation.setTargetName(targetName);
127                         hotSpots.add(h);
128                     }
129                 }
130
131                 if ((accessPolicy & WRITE_ACCESS) == WRITE_ACCESS) {
132                     if ((opcode == Opcodes.PUTFIELD) ||
133                         (opcode == Opcodes.PUTSTATIC)) {
134
135                         HotSpot h = new HotSpot(index, index + 1);
136                         //instrumentation.setTargetName(o.toString());
137
instrumentation.setTargetName(targetName);
138                         hotSpots.add(h);
139                     }
140                 }
141             }
142             
143             index++;
144         }
145
146         return hotSpots;
147     }
148 }
149
Popular Tags