LocalVariablesInfo.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.bcel.verifier.statics;

  18. import java.util.Arrays;

  19. import org.apache.bcel.generic.Type;
  20. import org.apache.bcel.verifier.exc.AssertionViolatedException;
  21. import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;

  22. /**
  23.  * A utility class holding the information about the names and the types of the local variables in a given method.
  24.  */
  25. public class LocalVariablesInfo {

  26.     /** The information about the local variables is stored here. */
  27.     private final LocalVariableInfo[] localVariableInfos;

  28.     /** The constructor. */
  29.     LocalVariablesInfo(final int maxLocals) {
  30.         localVariableInfos = new LocalVariableInfo[maxLocals];
  31.         Arrays.setAll(localVariableInfos, i -> new LocalVariableInfo());
  32.     }

  33.     /**
  34.      * Adds information about the local variable in slot 'slot'. Automatically adds information for slot+1 if 't' is
  35.      * Type.LONG or Type.DOUBLE.
  36.      *
  37.      * @param slot Slot number for local variable information
  38.      * @param name variable name
  39.      * @param startPc Range in which the variable is valid.
  40.      * @param length length of ...
  41.      * @param type variable type
  42.      * @throws LocalVariableInfoInconsistentException if the new information conflicts with already gathered information.
  43.      */
  44.     public void add(final int slot, final String name, final int startPc, final int length, final Type type) throws LocalVariableInfoInconsistentException {
  45.         // The add operation on LocalVariableInfo may throw the '...Inconsistent...' exception, we don't throw it explicitly
  46.         // here.

  47.         if (slot < 0 || slot >= localVariableInfos.length) {
  48.             throw new AssertionViolatedException("Slot number for local variable information out of range.");
  49.         }

  50.         localVariableInfos[slot].add(name, startPc, length, type);
  51.         if (type == Type.LONG) {
  52.             localVariableInfos[slot + 1].add(name, startPc, length, LONG_Upper.theInstance());
  53.         }
  54.         if (type == Type.DOUBLE) {
  55.             localVariableInfos[slot + 1].add(name, startPc, length, DOUBLE_Upper.theInstance());
  56.         }
  57.     }

  58.     /**
  59.      * Returns the LocalVariableInfo for the given slot.
  60.      *
  61.      * @param slot Slot to query.
  62.      * @return The LocalVariableInfo for the given slot.
  63.      */
  64.     public LocalVariableInfo getLocalVariableInfo(final int slot) {
  65.         if (slot < 0 || slot >= localVariableInfos.length) {
  66.             throw new AssertionViolatedException("Slot number for local variable information out of range.");
  67.         }
  68.         return localVariableInfos[slot];
  69.     }
  70. }