1 package serp.bytecode;
2
3 import serp.bytecode.visitor.*;
4
5 /***
6 * Code blocks compiled from source have local variable tables mapping
7 * locals used in opcodes to their names and descriptions.
8 *
9 * @author Abe White
10 */
11 public class LocalVariableTable extends LocalTable {
12 LocalVariableTable(int nameIndex, Attributes owner) {
13 super(nameIndex, owner);
14 }
15
16 /***
17 * Return all the locals of this method.
18 */
19 public LocalVariable[] getLocalVariables() {
20 return (LocalVariable[]) getLocals();
21 }
22
23 /***
24 * Return the local with the given locals index, or null if none.
25 */
26 public LocalVariable getLocalVariable(int local) {
27 return (LocalVariable) getLocal(local);
28 }
29
30 /***
31 * Return the local with the given name, or null if none. If multiple
32 * locals have the given name, which is returned is undefined.
33 */
34 public LocalVariable getLocalVariable(String name) {
35 return (LocalVariable) getLocal(name);
36 }
37
38 /***
39 * Return all locals with the given name, or empty array if none.
40 */
41 public LocalVariable[] getLocalVariables(String name) {
42 return (LocalVariable[]) getLocals(name);
43 }
44
45 /***
46 * Import a local from another method/class. Note that
47 * the program counter and length from the given local is copied
48 * directly, and thus will be incorrect unless this method is the same
49 * as the one the local is copied from, or the pc and length are reset.
50 */
51 public LocalVariable addLocalVariable(LocalVariable local) {
52 return (LocalVariable) addLocal(local);
53 }
54
55 /***
56 * Add a local to this table.
57 */
58 public LocalVariable addLocalVariable() {
59 return (LocalVariable) addLocal();
60 }
61
62 /***
63 * Add a local to this table.
64 */
65 public LocalVariable addLocalVariable(String name, String type) {
66 return (LocalVariable) addLocal(name, type);
67 }
68
69 /***
70 * Add a local to this table.
71 */
72 public LocalVariable addLocalVariable(String name, Class type) {
73 String typeName = (type == null) ? null : type.getName();
74 return addLocalVariable(name, typeName);
75 }
76
77 /***
78 * Add a local to this table.
79 */
80 public LocalVariable addLocalVariable(String name, BCClass type) {
81 String typeName = (type == null) ? null : type.getName();
82 return addLocalVariable(name, typeName);
83 }
84
85 public void acceptVisit(BCVisitor visit) {
86 visit.enterLocalVariableTable(this);
87 LocalVariable[] locals = (LocalVariable[]) getLocals();
88 for (int i = 0; i < locals.length; i++)
89 locals[i].acceptVisit(visit);
90 visit.exitLocalVariableTable(this);
91 }
92
93 protected Local newLocal() {
94 return new LocalVariable(this);
95 }
96
97 protected Local[] newLocalArray(int size) {
98 return new LocalVariable[size];
99 }
100 }