KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.apache.log4j.Category;
26
27 import alt.jiapi.Instrumentor;
28 import alt.jiapi.Runtime;
29 import alt.jiapi.reflect.InstructionList;
30
31 /**
32  * This Instrumentor gets in an InstructionList and
33  * selects zero or more instructions from end of the instruction list
34  * given.
35  *
36  * @author Mika Riekkinen
37  * @author Joni Suominen
38  * @version $Revision: 1.9 $ $Date: 2004/03/15 14:47:53 $
39  */

40 public class TailInstrumentor extends AbstractInstrumentor {
41     private static Category log = Runtime.getLogCategory(TailInstrumentor.class);
42     /**
43      * Number of instructions this Instrumentor applies to.
44      */

45     protected int instructionCount = 0;
46
47     /**
48      * Constructor. Selects zero instructions from the enf of the list.
49      * Selecting zero instructions acts as a marker to end of the list.
50      */

51     public TailInstrumentor() {
52         this(0);
53     }
54
55     /**
56      * This constructor selects zero or more instructions from the end of the
57      * list.
58      *
59      * @param instructionCount number of instructions to select
60      */

61     public TailInstrumentor(int instructionCount) {
62         log.info("Instruction count " + instructionCount);
63
64         if (instructionCount < 0) {
65             throw new IllegalArgumentException JavaDoc(instructionCount + " < 0");
66         }
67
68         this.instructionCount = instructionCount;
69     }
70
71     /**
72      * Selects instructionCount instructions from the end of the
73      * InstructionList given.
74      *
75      * @param il InstructionList to consider
76      * @see #instructionCount
77      */

78     public void instrument(InstructionList il) {
79         log.info("Instrumenting " + getCurrentClass().getName() + "." +
80                  il.getDeclaringMethod().getName());
81
82         InstructionList view;
83
84         if (instructionCount > il.size()) {
85             log.warn("List size exceeded: instructions needed : " +
86                      instructionCount + ", available " + il.size() +
87                      ". Using all that is available.");
88
89              view = il.createView(0, il.size());
90         }
91         else {
92              view = il.createView(il.size() - instructionCount, il.size());
93         }
94
95         forward(view);
96     }
97 }
98
Popular Tags