001package serp.bytecode;
002
003import serp.bytecode.visitor.*;
004
005/**
006 * Code blocks compiled from source have local variable type tables mapping
007 * generics-using locals used in opcodes to their names and signatures.
008 *
009 * @author Abe White
010 */
011public class LocalVariableTypeTable extends LocalTable {
012    LocalVariableTypeTable(int nameIndex, Attributes owner) {
013        super(nameIndex, owner);
014    }
015
016    /**
017     * Return all the locals of this method.
018     */
019    public LocalVariableType[] getLocalVariableTypes() {
020        return (LocalVariableType[]) getLocals();
021    }
022
023    /**
024     * Return the local with the given locals index, or null if none.
025     */
026    public LocalVariableType getLocalVariableType(int local) {
027        return (LocalVariableType) 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 LocalVariableType getLocalVariableType(String name) {
035        return (LocalVariableType) getLocal(name);
036    }
037
038    /**
039     * Return all locals with the given name, or empty array if none.
040     */
041    public LocalVariableType[] getLocalVariableTypes(String name) {
042        return (LocalVariableType[]) 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 LocalVariableType addLocalVariableType(LocalVariableType local) {
052        return (LocalVariableType) addLocal(local);
053    }
054
055    /**
056     * Add a local to this table.
057     */
058    public LocalVariableType addLocalVariableType() {
059        return (LocalVariableType) addLocal();
060    }
061
062    /**
063     * Add a local to this table.
064     */
065    public LocalVariableType addLocalVariableType(String name, String type) {
066        return (LocalVariableType) addLocal(name, type);
067    }
068
069    public void acceptVisit(BCVisitor visit) {
070        visit.enterLocalVariableTypeTable(this);
071        LocalVariableType[] locals = (LocalVariableType[]) getLocals();
072        for (int i = 0; i < locals.length; i++)
073            locals[i].acceptVisit(visit);
074        visit.exitLocalVariableTypeTable(this);
075    }
076
077    protected Local newLocal() {
078        return new LocalVariableType(this);
079    }
080
081    protected Local[] newLocalArray(int size) {
082        return new LocalVariableType[size];
083    }
084}