Args.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.util;

  18. import org.apache.bcel.Const;
  19. import org.apache.bcel.classfile.ClassFormatException;

  20. /**
  21.  * Argument validation.
  22.  *
  23.  * @since 6.7.0
  24.  */
  25. public class Args {

  26.     /**
  27.      * Requires a specific value.
  28.      *
  29.      * @param value    The value to test.
  30.      * @param required The required value.
  31.      * @param message  The message prefix
  32.      * @return The value to test.
  33.      */
  34.     public static int require(final int value, final int required, final String message) {
  35.         if (value != required) {
  36.             throw new ClassFormatException(String.format("%s [Value must be 0: %,d]", message, value));
  37.         }
  38.         return value;
  39.     }

  40.     /**
  41.      * Requires a 0 value.
  42.      *
  43.      * @param value   The value to test.
  44.      * @param message The message prefix
  45.      * @return The value to test.
  46.      */
  47.     public static int require0(final int value, final String message) {
  48.         return require(value, 0, message);
  49.     }

  50.     /**
  51.      * Requires a u1 value.
  52.      *
  53.      * @param value   The value to test.
  54.      * @param message The message prefix
  55.      * @return The value to test.
  56.      */
  57.     public static int requireU1(final int value, final String message) {
  58.         if (value < 0 || value > Const.MAX_BYTE) {
  59.             throw new ClassFormatException(String.format("%s [Value out of range (0 - %,d) for type u1: %,d]", message, Const.MAX_BYTE, value));
  60.         }
  61.         return value;
  62.     }

  63.     /**
  64.      * Requires a u2 value of at least {@code min} and not above {@code max}.
  65.      *
  66.      * @param value   The value to test.
  67.      * @param min     The minimum required u2 value.
  68.      * @param max     The maximum required u2 value.
  69.      * @param message The message prefix
  70.      * @return The value to test.
  71.      */
  72.     public static int requireU2(final int value, final int min, final int max, final String message) {
  73.         if (max > Const.MAX_SHORT) {
  74.             throw new IllegalArgumentException(String.format("%s programming error: max %,d > %,d", message, max, Const.MAX_SHORT));
  75.         }
  76.         if (min < 0) {
  77.             throw new IllegalArgumentException(String.format("%s programming error: min %,d < 0", message, min));
  78.         }
  79.         if (value < min || value > max) {
  80.             throw new ClassFormatException(String.format("%s [Value out of range (%,d - %,d) for type u2: %,d]", message, min, Const.MAX_SHORT, value));
  81.         }
  82.         return value;
  83.     }

  84.     /**
  85.      * Requires a u2 value of at least {@code min}.
  86.      *
  87.      * @param value   The value to test.
  88.      * @param min     The minimum required value.
  89.      * @param message The message prefix
  90.      * @return The value to test.
  91.      */
  92.     public static int requireU2(final int value, final int min, final String message) {
  93.         return requireU2(value, min, Const.MAX_SHORT, message);
  94.     }

  95.     /**
  96.      * Requires a u2 value.
  97.      *
  98.      * @param value   The value to test.
  99.      * @param message The message prefix
  100.      * @return The value to test.
  101.      */
  102.     public static int requireU2(final int value, final String message) {
  103.         return requireU2(value, 0, message);
  104.     }

  105.     /**
  106.      * Requires a u4 value of at least {@code min}.
  107.      *
  108.      * @param value   The value to test.
  109.      * @param min     The minimum required value.
  110.      * @param message The message prefix
  111.      * @return The value to test.
  112.      */
  113.     public static int requireU4(final int value, final int min, final String message) {
  114.         if (min < 0) {
  115.             throw new IllegalArgumentException(String.format("%s programming error: min %,d < 0", message, min));
  116.         }
  117.         if (value < min) {
  118.             throw new ClassFormatException(
  119.                     String.format("%s [Value out of range (%,d - %,d) for type u2: %,d]", message, min, Integer.MAX_VALUE, value & 0xFFFFFFFFL));
  120.         }
  121.         return value;
  122.     }

  123.     /**
  124.      * Requires a u4 value.
  125.      *
  126.      * @param value   The value to test.
  127.      * @param message The message prefix
  128.      * @return The value to test.
  129.      */
  130.     public static int requireU4(final int value, final String message) {
  131.         return requireU4(value, 0, message);
  132.     }
  133. }