001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * https://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.bcel.verifier.statics; 020 021import java.util.Hashtable; 022 023import org.apache.bcel.generic.Type; 024import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException; 025 026/** 027 * A utility class holding the information about the name and the type of a local variable in a given slot (== index). 028 * This information often changes in course of byte code offsets. 029 */ 030public class LocalVariableInfo { 031 032 /** The types database. KEY: String representing the offset integer. */ 033 private final Hashtable<String, Type> types = new Hashtable<>(); 034 035 /** The names database. KEY: String representing the offset integer. */ 036 private final Hashtable<String, String> names = new Hashtable<>(); 037 038 /** 039 * Adds information about name and type for a given offset. 040 * 041 * @throws LocalVariableInfoInconsistentException if the new information conflicts with already gathered information. 042 */ 043 private void add(final int offset, final String name, final Type t) throws LocalVariableInfoInconsistentException { 044 if (getName(offset) != null && !getName(offset).equals(name)) { 045 throw new LocalVariableInfoInconsistentException( 046 "At bytecode offset '" + offset + "' a local variable has two different names: '" + getName(offset) + "' and '" + name + "'."); 047 } 048 if (getType(offset) != null && !getType(offset).equals(t)) { 049 throw new LocalVariableInfoInconsistentException( 050 "At bytecode offset '" + offset + "' a local variable has two different types: '" + getType(offset) + "' and '" + t + "'."); 051 } 052 setName(offset, name); 053 setType(offset, t); 054 } 055 056 /** 057 * Adds some information about this local variable (slot). 058 * 059 * @param name variable name 060 * @param startPc Range in which the variable is valid. 061 * @param length length of ... 062 * @param type variable type 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}