KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > compiler > MarkedInstruction


1 /*
2  * @(#)MarkedInstruction.java
3  *
4  * Copyright (C) 2002,2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.compiler;
28
29 import java.util.Iterator JavaDoc;
30 import java.util.LinkedList JavaDoc;
31 import java.util.List JavaDoc;
32
33 import org.apache.bcel.generic.Instruction;
34 import org.apache.bcel.generic.InstructionHandle;
35 import org.apache.bcel.generic.InstructionList;
36
37 /**
38  * Contains one original method instruction, along with all the analysis
39  * module marks for this instruction.
40  *
41  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
42  * @version $Date: 2004/04/15 05:48:25 $
43  * @since December 17, 2002
44  */

45 public class MarkedInstruction
46 {
47     private static final org.apache.log4j.Logger LOG =
48         org.apache.log4j.Logger.getLogger( MarkedInstruction.class );
49     private final InstructionHandle origInstr;
50     private final List JavaDoc marks = new LinkedList JavaDoc();
51     private boolean closed = false;
52     
53     
54     /**
55      * constant pool index for the name of the class's signature.
56      */

57     private final int classSigPoolIndex;
58     
59     /**
60      * constant pool index for the method-ref for invoking the logger.
61      */

62     private final int staticMethodPoolIndex;
63     
64     /**
65      * Reference to the owning method's index
66      */

67     private final short methodIndex;
68     
69     /**
70      * @throws IllegalStateException if the class file has already been
71      * modified (identified by a class name field).
72      */

73     MarkedInstruction( short methodIndex, int classSigPoolIndex,
74             int staticMethodPoolIndex, InstructionHandle instr )
75     {
76         if (instr == null)
77         {
78             throw new IllegalArgumentException JavaDoc("no null args");
79         }
80         this.origInstr = instr;
81         this.methodIndex = methodIndex;
82         this.classSigPoolIndex = classSigPoolIndex;
83         this.staticMethodPoolIndex = staticMethodPoolIndex;
84     }
85     
86     
87     /**
88      * Retrieve the list of all the marks (if there are any active ones),
89      * but does not add the original instruction (that is for the owner to
90      * know where it goes - before or after). If there are no active
91      * marks, then <tt>null</tt> will be returned. After this has been
92      * called, all the active marks will be removed (assuming that they
93      * have been added to the modified method instruction list).
94      */

95     InstructionList getMarkedList()
96     {
97         InstructionList markedList = null;
98         if (!this.marks.isEmpty())
99         {
100             markedList = new InstructionList();
101             // mark the instruction first, then add the instruction
102
Iterator JavaDoc iter = this.marks.iterator();
103             while (iter.hasNext())
104             {
105                 LOG.debug( "Adding mark to list." );
106                 ((MeasureMark)iter.next()).addToInstructionList( markedList );
107             }
108             markedList.setPositions( true );
109             // since we just added the marks, we shouldn't add them
110
// again, so remove all current marks.
111
this.marks.clear();
112         }
113         return markedList;
114     }
115     
116     
117     public int getInstructionPosition()
118     {
119         return this.origInstr.getPosition();
120     }
121     
122     
123     /**
124      * Retrieve the instruction represented by this mark.
125      */

126     public Instruction getInstruction()
127     {
128         return this.origInstr.getInstruction();
129     }
130     
131     
132     /**
133      * Mark this instruction with the given measure and mark indicies; the
134      * method index is pre-defined by the particular method this instruction
135      * resides in.
136      */

137     public void addMark( short measureIndex, short markIndex )
138     {
139         MeasureMark mm = new MeasureMark( this.classSigPoolIndex,
140             this.staticMethodPoolIndex, this.methodIndex,
141             measureIndex, markIndex );
142         this.marks.add( mm );
143     }
144     
145     
146     /**
147      * Retrieve the handle this instruction represents.
148      */

149     InstructionHandle getHandle()
150     {
151         return this.origInstr;
152     }
153 }
154
155
Popular Tags