001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.bcel.verifier.statics;
019
020import java.util.Hashtable;
021
022import org.apache.bcel.generic.Type;
023import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;
024
025/**
026 * A utility class holding the information about the name and the type of a local variable in a given slot (== index).
027 * This information often changes in course of byte code offsets.
028 */
029public class LocalVariableInfo {
030
031    /** The types database. KEY: String representing the offset integer. */
032    private final Hashtable<String, Type> types = new Hashtable<>();
033
034    /** The names database. KEY: String representing the offset integer. */
035    private final Hashtable<String, String> names = new Hashtable<>();
036
037    /**
038     * Adds information about name and type for a given offset.
039     *
040     * @throws LocalVariableInfoInconsistentException if the new information conflicts with already gathered information.
041     */
042    private void add(final int offset, final String name, final Type t) throws LocalVariableInfoInconsistentException {
043        if (getName(offset) != null && !getName(offset).equals(name)) {
044            throw new LocalVariableInfoInconsistentException(
045                "At bytecode offset '" + offset + "' a local variable has two different names: '" + getName(offset) + "' and '" + name + "'.");
046        }
047        if (getType(offset) != null && !getType(offset).equals(t)) {
048            throw new LocalVariableInfoInconsistentException(
049                "At bytecode offset '" + offset + "' a local variable has two different types: '" + getType(offset) + "' and '" + t + "'.");
050        }
051        setName(offset, name);
052        setType(offset, t);
053    }
054
055    /**
056     * Adds some information about this local variable (slot).
057     *
058     * @param name variable name
059     * @param startPc Range in which the variable is valid.
060     * @param length length of ...
061     * @param type variable type
062     *
063     * @throws LocalVariableInfoInconsistentException if the new information conflicts with already gathered information.
064     */
065    public void add(final String name, final int startPc, final int length, final Type type) throws LocalVariableInfoInconsistentException {
066        for (int i = startPc; i <= startPc + length; i++) { // incl/incl-notation!
067            add(i, name, type);
068        }
069    }
070
071    /**
072     * Returns the name of the local variable that uses this local variable slot at the given bytecode offset. Care for
073     * legal bytecode offsets yourself, otherwise the return value might be wrong. May return 'null' if nothing is known
074     * about the type of this local variable slot at the given bytecode offset.
075     *
076     * @param offset bytecode offset.
077     * @return the name of the local variable that uses this local variable slot at the given bytecode offset.
078     */
079    public String getName(final int offset) {
080        return names.get(Integer.toString(offset));
081    }
082
083    /**
084     * Returns the type of the local variable that uses this local variable slot at the given bytecode offset. Care for
085     * legal bytecode offsets yourself, otherwise the return value might be wrong. May return 'null' if nothing is known
086     * about the type of this local variable slot at the given bytecode offset.
087     *
088     * @param offset bytecode offset.
089     * @return the type of the local variable that uses this local variable slot at the given bytecode offset.
090     */
091    public Type getType(final int offset) {
092        return types.get(Integer.toString(offset));
093    }
094
095    /**
096     * Adds a name of a local variable and a certain slot to our 'names' (Hashtable) database.
097     */
098    private void setName(final int offset, final String name) {
099        names.put(Integer.toString(offset), name);
100    }
101
102    /**
103     * Adds a type of a local variable and a certain slot to our 'types' (Hashtable) database.
104     */
105    private void setType(final int offset, final Type t) {
106        types.put(Integer.toString(offset), t);
107    }
108}