001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.utils.bytecode;
022
023/**
024 * A local variable.
025 *
026 * <p>
027 * Local variables are used as identifiers for things that can be stored and
028 * loaded from the local variable slots associated with each method. By using
029 * LocalVariable intances, you are removed from the taudry task of book-keeping
030 * these slots.
031 * </p>
032 *
033 * <p>
034 * To use a local variable, create an intance and then use it in a code
035 * generator. The method will keep track of which local variables are in scope,
036 * and will handle all the nastiness for you. You can re-use the same
037 * local variable instance in different contexts, and it will be sanely
038 * allocated different or the same slots.
039 * </p>
040 *
041 * <p>
042 * The JVM stores some things in single words, and others in pairs of words.
043 * To hide this detail from you, local variables take a class that indicates
044 * the type of thing they will store. Please populate this sensibly.
045 * </p>
046 *
047 * @author Thomas Down
048 * @author Matthew Pocock
049 */
050public final class LocalVariable {
051  private final String name;
052  private final CodeClass clazz;
053
054  /**
055   * Create a new local variable that will store values of a given type.
056   *
057   * @param clazz  the type of the values stored in this variable
058   */
059  public LocalVariable(CodeClass clazz) {
060    this.clazz = clazz;
061    this.name = null;
062  }
063
064  /**
065   * Create a new local variable with a type and a name.
066   *
067   * <p>
068   * The name may appear in debug output from the generator, and possibly in
069   * stack-traces.
070   * </p>
071   *
072   * @param clazz  the type of the values stored in this variable
073   * @param name   the name of the variable
074   */
075  public LocalVariable(CodeClass clazz, String name) {
076    this.clazz = clazz;
077    this.name = name;
078  }
079
080  public String getName() {
081    return name;
082  }
083
084  public int needSlots() {
085    return (clazz == CodeUtils.TYPE_LONG || clazz == CodeUtils.TYPE_DOUBLE) ? 2 : 1;
086  }
087
088  public CodeClass getType() {
089    return clazz;
090  }
091
092  public String toString() {
093    return
094            super.toString() +
095            "[slots: " + needSlots() + ", name: " + name +
096            ", class: " + clazz + "]";
097  }
098}