001 package serp.bytecode; 002 003 import serp.bytecode.visitor.*; 004 005 /** 006 * Code blocks compiled from source have local variable tables mapping 007 * locals used in opcodes to their names and descriptions. 008 * 009 * @author Abe White 010 */ 011 public class LocalVariableTable extends LocalTable { 012 LocalVariableTable(int nameIndex, Attributes owner) { 013 super(nameIndex, owner); 014 } 015 016 /** 017 * Return all the locals of this method. 018 */ 019 public LocalVariable[] getLocalVariables() { 020 return (LocalVariable[]) getLocals(); 021 } 022 023 /** 024 * Return the local with the given locals index, or null if none. 025 */ 026 public LocalVariable getLocalVariable(int local) { 027 return (LocalVariable) getLocal(local); 028 } 029 030 /** 031 * Return the local with the given name, or null if none. If multiple 032 * locals have the given name, which is returned is undefined. 033 */ 034 public LocalVariable getLocalVariable(String name) { 035 return (LocalVariable) getLocal(name); 036 } 037 038 /** 039 * Return all locals with the given name, or empty array if none. 040 */ 041 public LocalVariable[] getLocalVariables(String name) { 042 return (LocalVariable[]) getLocals(name); 043 } 044 045 /** 046 * Import a local from another method/class. Note that 047 * the program counter and length from the given local is copied 048 * directly, and thus will be incorrect unless this method is the same 049 * as the one the local is copied from, or the pc and length are reset. 050 */ 051 public LocalVariable addLocalVariable(LocalVariable local) { 052 return (LocalVariable) addLocal(local); 053 } 054 055 /** 056 * Add a local to this table. 057 */ 058 public LocalVariable addLocalVariable() { 059 return (LocalVariable) addLocal(); 060 } 061 062 /** 063 * Add a local to this table. 064 */ 065 public LocalVariable addLocalVariable(String name, String type) { 066 return (LocalVariable) addLocal(name, type); 067 } 068 069 /** 070 * Add a local to this table. 071 */ 072 public LocalVariable addLocalVariable(String name, Class type) { 073 String typeName = (type == null) ? null : type.getName(); 074 return addLocalVariable(name, typeName); 075 } 076 077 /** 078 * Add a local to this table. 079 */ 080 public LocalVariable addLocalVariable(String name, BCClass type) { 081 String typeName = (type == null) ? null : type.getName(); 082 return addLocalVariable(name, typeName); 083 } 084 085 public void acceptVisit(BCVisitor visit) { 086 visit.enterLocalVariableTable(this); 087 LocalVariable[] locals = (LocalVariable[]) getLocals(); 088 for (int i = 0; i < locals.length; i++) 089 locals[i].acceptVisit(visit); 090 visit.exitLocalVariableTable(this); 091 } 092 093 protected Local newLocal() { 094 return new LocalVariable(this); 095 } 096 097 protected Local[] newLocalArray(int size) { 098 return new LocalVariable[size]; 099 } 100 }