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