KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > go > trove > classfile > LineNumberTableAttr


1 /* ====================================================================
2  * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group
3  * ====================================================================
4  * The Tea Software License, Version 1.1
5  *
6  * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Walt Disney Internet Group (http://opensource.go.com/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact opensource@dig.com.
31  *
32  * 5. Products derived from this software may not be called "Tea",
33  * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet",
34  * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior
35  * written permission of the Walt Disney Internet Group.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
43  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
44  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
45  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  * For more information about Tea, please see http://opensource.go.com/.
51  */

52
53 package com.go.trove.classfile;
54
55 import java.util.*;
56 import java.io.*;
57
58 /******************************************************************************
59  * This class corresponds to the LineNumberTable_attribute structure as
60  * defined in section 4.7.6 of <i>The Java Virtual Machine Specification</i>.
61  *
62  * @author Brian S O'Neill
63  * @version
64  * <!--$$Revision:--> 30 <!-- $-->, <!--$$JustDate:--> 00/11/27 <!-- $-->
65  */

66 class LineNumberTableAttr extends Attribute {
67     private List mEntries = new ArrayList();
68     private boolean mClean = false;
69     
70     public LineNumberTableAttr(ConstantPool cp) {
71         super(cp, LINE_NUMBER_TABLE);
72     }
73     
74     public int getLineNumber(Location start) {
75         clean();
76         int index = Collections.binarySearch(mEntries, new Entry(start, 0));
77         if (index < 0) {
78             if ((index = -index - 2) < 0) {
79                 return -1;
80             }
81         }
82         return ((Entry)mEntries.get(index)).mLineNumber;
83     }
84
85     public void addEntry(Location start, int line_number) {
86         check("line number", line_number);
87         mEntries.add(new Entry(start, line_number));
88         mClean = false;
89     }
90     
91     public int getLength() {
92         clean();
93         return 2 + 4 * mEntries.size();
94     }
95     
96     public void writeDataTo(DataOutput dout) throws IOException {
97         int size = mEntries.size();
98         dout.writeShort(size);
99         for (int i=0; i<size; i++) {
100             Entry entry = (Entry)mEntries.get(i);
101             
102             int start_pc = entry.mStart.getLocation();
103
104             check("line number table entry start PC", start_pc);
105
106             dout.writeShort(start_pc);
107             dout.writeShort(entry.mLineNumber);
108         }
109     }
110
111     private void check(String JavaDoc type, int addr) throws RuntimeException JavaDoc {
112         if (addr < 0 || addr > 65535) {
113             throw new RuntimeException JavaDoc("Value for " + type + " out of " +
114                                        "valid range: " + addr);
115
116         }
117     }
118
119     private void clean() {
120         if (!mClean) {
121             mClean = true;
122
123             // Clean things up by removing multiple mappings of the same
124
// start_pc to line numbers. Only keep the last one.
125
// This has to be performed now because the Labels should have
126
// a pc location, but before they did not. Since entries must be
127
// sorted ascending by start_pc, use a sorted set.
128

129             Set reduced = new TreeSet();
130             for (int i = mEntries.size(); --i >= 0; ) {
131                 reduced.add(mEntries.get(i));
132             }
133
134             mEntries = new ArrayList(reduced);
135         }
136     }
137
138     static Attribute define(ConstantPool cp,
139                             String JavaDoc name,
140                             int length,
141                             DataInput din) throws IOException {
142
143         LineNumberTableAttr lineNumbers = new LineNumberTableAttr(cp);
144
145         int size = din.readUnsignedShort();
146         for (int i=0; i<size; i++) {
147             int start_pc = din.readUnsignedShort();
148             int line_number = din.readUnsignedShort();
149
150             lineNumbers.addEntry(new FixedLocation(start_pc), line_number);
151         }
152
153         return lineNumbers;
154     }
155
156     private static class Entry implements Comparable JavaDoc {
157         public final Location mStart;
158         public final int mLineNumber;
159         
160         public Entry(Location start, int line_number) {
161             mStart = start;
162             mLineNumber = line_number;
163         }
164
165         public int compareTo(Object JavaDoc other) {
166             int thisLoc = mStart.getLocation();
167             int thatLoc = ((Entry)other).mStart.getLocation();
168             
169             if (thisLoc < thatLoc) {
170                 return -1;
171             }
172             else if (thisLoc > thatLoc) {
173                 return 1;
174             }
175             else {
176                 return 0;
177             }
178         }
179
180         public boolean equals(Object JavaDoc other) {
181             if (other instanceof Entry) {
182                 return mStart.getLocation() ==
183                     ((Entry)other).mStart.getLocation();
184             }
185             return false;
186         }
187
188         public String JavaDoc toString() {
189             return "start_pc=" + mStart.getLocation() + " => " +
190                 "line_number=" + mLineNumber;
191         }
192     }
193 }
194
Popular Tags