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 */
017package org.apache.bcel.verifier.statics;
018
019import java.util.Hashtable;
020
021import org.apache.bcel.generic.Type;
022import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;
023
024/**
025 * A utility class holding the information about the name and the type of a local variable in a given slot (== index).
026 * This information often changes in course of byte code offsets.
027 */
028public class LocalVariableInfo {
029
030    /** The types database. KEY: String representing the offset integer. */
031    private final Hashtable<String, Type> types = new Hashtable<>();
032
033    /** The names database. KEY: String representing the offset integer. */
034    private final Hashtable<String, String> names = new Hashtable<>();
035
036    /**
037     * Adds information about name and type for a given offset.
038     *
039     * @throws LocalVariableInfoInconsistentException if the new information conflicts with already gathered information.
040     */
041    private void add(final int offset, final String name, final Type t) throws LocalVariableInfoInconsistentException {
042        if (getName(offset) != null && !getName(offset).equals(name)) {
043            throw new LocalVariableInfoInconsistentException(
044                "At bytecode offset '" + offset + "' a local variable has two different names: '" + getName(offset) + "' and '" + name + "'.");
045        }
046        if (getType(offset) != null && !getType(offset).equals(t)) {
047            throw new LocalVariableInfoInconsistentException(
048                "At bytecode offset '" + offset + "' a local variable has two different types: '" + getType(offset) + "' and '" + t + "'.");
049        }
050        setName(offset, name);
051        setType(offset, t);
052    }
053
054    /**
055     * Adds some information about this local variable (slot).
056     *
057     * @param name variable name
058     * @param startPc Range in which the variable is valid.
059     * @param length length of ...
060     * @param type variable type
061     *
062     * @throws LocalVariableInfoInconsistentException if the new information conflicts with already gathered information.
063     */
064    public void add(final String name, final int startPc, final int length, final Type type) throws LocalVariableInfoInconsistentException {
065        for (int i = startPc; i <= startPc + length; i++) { // incl/incl-notation!
066            add(i, name, type);
067        }
068    }
069
070    /**
071     * Returns the name of the local variable that uses this local variable slot at the given bytecode offset. Care for
072     * legal bytecode offsets yourself, otherwise the return value might be wrong. May return 'null' if nothing is known
073     * about the type of this local variable slot at the given bytecode offset.
074     *
075     * @param offset bytecode offset.
076     * @return the name of the local variable that uses this local variable slot at the given bytecode offset.
077     */
078    public String getName(final int offset) {
079        return names.get(Integer.toString(offset));
080    }
081
082    /**
083     * Returns the type of the local variable that uses this local variable slot at the given bytecode offset. Care for
084     * legal bytecode offsets yourself, otherwise the return value might be wrong. May return 'null' if nothing is known
085     * about the type of this local variable slot at the given bytecode offset.
086     *
087     * @param offset bytecode offset.
088     * @return the type of the local variable that uses this local variable slot at the given bytecode offset.
089     */
090    public Type getType(final int offset) {
091        return types.get(Integer.toString(offset));
092    }
093
094    /**
095     * Adds a name of a local variable and a certain slot to our 'names' (Hashtable) database.
096     */
097    private void setName(final int offset, final String name) {
098        names.put(Integer.toString(offset), name);
099    }
100
101    /**
102     * Adds a type of a local variable and a certain slot to our 'types' (Hashtable) database.
103     */
104    private void setType(final int offset, final Type t) {
105        types.put(Integer.toString(offset), t);
106    }
107}