KickJava   Java API By Example, From Geeks To Geeks.

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


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 start 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 HeadInstrumentor extends AbstractInstrumentor {
41     private static Category log = Runtime.getLogCategory(HeadInstrumentor.class);
42     /**
43      * Number of instructions this Instrumentor applies to.
44      */

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

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

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

77     public void instrument(InstructionList il) {
78         log.info("Instrumenting " + getCurrentClass().getName() + "." +
79                  il.getDeclaringMethod().getName());
80         
81
82         InstructionList view = null;
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(0, instructionCount);
93         }
94
95         forward(view);
96     }
97 }
98
Popular Tags