View Javadoc

    1   /*
    2    * Copyright  2000-2004 The Apache Software Foundation
    3    *
    4    *  Licensed under the Apache License, Version 2.0 (the "License");
    5    *  you may not use this file except in compliance with the License.
    6    *  You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   *  Unless required by applicable law or agreed to in writing, software
    11   *  distributed under the License is distributed on an "AS IS" BASIS,
    12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   *  See the License for the specific language governing permissions and
    14   *  limitations under the License.
    15   *
    16   */
    17  package org.apache.bcel.verifier.statics;
    18
    19
    20  import java.util.Hashtable;
    21  import org.apache.bcel.generic.Type;
    22  import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;
    23
    24  /***
    25   * A utility class holding the information about
    26   * the name and the type of a local variable in
    27   * a given slot (== index). This information
    28   * often changes in course of byte code offsets.
    29   *
    30   * @version $Id: LocalVariableInfo.java 371539 2006-01-23 14:08:00Z tcurdt $
    31   * @author Enver Haase
    32   */
    33  public class LocalVariableInfo{
    34
    35  	/*** The types database. KEY: String representing the offset integer. */
    36  	private Hashtable types = new Hashtable();
    37  	/*** The names database. KEY: String representing the offset integer. */
    38  	private Hashtable names = new Hashtable();
    39
    40  	/***
    41  	 * Adds a name of a local variable and a certain slot to our 'names'
    42  	 * (Hashtable) database.
    43  	 */
    44  	private void setName(int offset, String name){
    45  		names.put( ((Integer.toString(offset))), name);
    46  	}
    47  	/***
    48  	 * Adds a type of a local variable and a certain slot to our 'types'
    49  	 * (Hashtable) database.
    50  	 */
    51  	private void setType(int offset, Type t){
    52  		types.put( ((Integer.toString(offset))), t);
    53  	}
    54
    55  	/***
    56  	 * Returns the type of the local variable that uses this local
    57  	 * variable slot at the given bytecode offset.
    58  	 * Care for legal bytecode offsets yourself, otherwise the return value
    59  	 * might be wrong.
    60  	 * May return 'null' if nothing is known about the type of this local
    61  	 * variable slot at the given bytecode offset.
    62  	 */
    63  	public Type getType(int offset){
    64  		return (Type) types.get(Integer.toString(offset));
    65  	}
    66  	/***
    67  	 * Returns the name of the local variable that uses this local
    68  	 * variable slot at the given bytecode offset.
    69  	 * Care for legal bytecode offsets yourself, otherwise the return value
    70  	 * might be wrong.
    71  	 * May return 'null' if nothing is known about the type of this local
    72  	 * variable slot at the given bytecode offset.
    73  	 */
    74  	public String getName(int offset){
    75  		return (String) (names.get(Integer.toString(offset)));
    76  	}
    77  	/***
    78  	 * Adds some information about this local variable (slot).
    79  	 * @throws LocalVariableInfoInconsistentException if the new information conflicts
    80  	 *         with already gathered information.
    81  	 */
    82  	public void add(String name, int startpc, int length, Type t) throws LocalVariableInfoInconsistentException{
    83  		for (int i=startpc; i<=startpc+length; i++){ // incl/incl-notation!
    84  			add(i,name,t);
    85  		}
    86  	}
    87
    88  	/***
    89  	 * Adds information about name and type for a given offset.
    90  	 * @throws LocalVariableInfoInconsistentException if the new information conflicts
    91  	 *         with already gathered information.
    92  	 */
    93  	private void add(int offset, String name, Type t) throws LocalVariableInfoInconsistentException{
    94  		if (getName(offset) != null){
    95  			if (! getName(offset).equals(name)){
    96  				throw new LocalVariableInfoInconsistentException("At bytecode offset '"+offset+"' a local variable has two different names: '"+getName(offset)+"' and '"+name+"'.");
    97  			}
    98  		}
    99  		if (getType(offset) != null){
    100 			if (! getType(offset).equals(t)){
    101 				throw new LocalVariableInfoInconsistentException("At bytecode offset '"+offset+"' a local variable has two different types: '"+getType(offset)+"' and '"+t+"'.");
    102 			}
    103 		}
    104 		setName(offset, name);
    105 		setType(offset, t);
    106 	}
    107 }