KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > coffi > LocalVariableTable_attribute


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1997 Clark Verbrugge
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
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31
32 package soot.coffi;
33 import soot.*;
34
35 import java.io.*;
36
37 /** A debugging attribute, this gives the names of local variables
38  * within blocks of bytecode.
39  * @see attribute_info
40  * @author Clark Verbrugge
41  */

42 class LocalVariableTable_attribute extends attribute_info {
43    /** Length of the local variable table. */
44    public int local_variable_table_length;
45    /** Actual table of local variables. */
46    public local_variable_table_entry local_variable_table[];
47
48    /** Locates the first name found for a given local variable.
49     * @param constant_pool constant pool for the associated class.
50     * @param idx local variable index.
51     * @return name of the local variable, or <i>null</i> if not found.
52     * @see LocalVariableTable_attribute#getLocalVariableName(cp_info[], int, int)
53     */

54    public String JavaDoc getLocalVariableName(cp_info constant_pool[],int idx) {
55       return getLocalVariableName(constant_pool,idx,-1);
56    }
57    /** Locates the name of the given local variable for the specified code offset.
58     * @param constant_pool constant pool for the associated class.
59     * @param idx local variable index.
60     * @param code code offset for variable name; use -1 to return the first name found
61     * for that local variable.
62     * @return name of the local variable, or <i>null</i> if not found.
63     * @see LocalVariableTable_attribute#getLocalVariableName(cp_info[], int)
64     */

65    public String JavaDoc getLocalVariableName(cp_info constant_pool[],int idx,int code) {
66       local_variable_table_entry e;
67       CONSTANT_Utf8_info cu;
68       int i;
69
70       // G.v().out.println("searching for name of local: " + idx + "at: " + code);
71
// now to find that variable
72
for (i=0;i<local_variable_table_length;i++) {
73          e = local_variable_table[i];
74          if (e.index==idx &&
75              (code==-1 ||
76           (code>=e.start_pc && code<=e.start_pc+e.length))){
77           // (code>=e.start_pc && code<e.start_pc+e.length))) {
78
// found the variable, now find its name.
79

80             //G.v().out.println("found entry: " + i);
81

82             if (constant_pool[e.name_index] instanceof CONSTANT_Utf8_info)
83         {
84            String JavaDoc n = ((CONSTANT_Utf8_info)(constant_pool[e.name_index])).convert();
85            if (Util.v().isValidJimpleName(n))
86            return n;
87            else
88            return null;
89         }
90             else {
91                throw new RuntimeException JavaDoc( "What? A local variable table "
92                        +"name_index isn't a UTF8 entry?");
93             }
94          }
95       }
96       return null;
97    }
98    
99    public String JavaDoc toString()
100    {
101         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
102         
103         for(int i = 0; i < local_variable_table_length; i++)
104         {
105             buffer.append(local_variable_table[i].toString() + "\n");
106         }
107         
108         return buffer.toString();
109    }
110 }
111
112
Popular Tags