View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.bcel.util;
21  
22  import org.apache.bcel.Const;
23  import org.apache.bcel.classfile.ClassFormatException;
24  
25  /**
26   * Argument validation.
27   *
28   * @since 6.7.0
29   */
30  public class Args {
31  
32      /**
33       * Requires a specific value.
34       *
35       * @param value    The value to test.
36       * @param required The required value.
37       * @param message  The message prefix.
38       * @return The value to test.
39       */
40      public static int require(final int value, final int required, final String message) {
41          if (value != required) {
42              throw new ClassFormatException(String.format("%s [Value must be 0: %,d]", message, value));
43          }
44          return value;
45      }
46  
47      /**
48       * Requires a 0 value.
49       *
50       * @param value   The value to test.
51       * @param message The message prefix.
52       * @return The value to test.
53       */
54      public static int require0(final int value, final String message) {
55          return require(value, 0, message);
56      }
57  
58      /**
59       * Requires a u1 value.
60       *
61       * @param value   The value to test.
62       * @param message The message prefix.
63       * @return The value to test.
64       */
65      public static int requireU1(final int value, final String message) {
66          if (value < 0 || value > Const.MAX_BYTE) {
67              throw new ClassFormatException(String.format("%s [Value out of range (0 - %,d) for type u1: %,d]", message, Const.MAX_BYTE, value));
68          }
69          return value;
70      }
71  
72      /**
73       * Requires a u2 value of at least {@code min} and not above {@code max}.
74       *
75       * @param value   The value to test.
76       * @param min     The minimum required u2 value.
77       * @param max     The maximum required u2 value.
78       * @param message The message prefix.
79       * @return The value to test.
80       */
81      public static int requireU2(final int value, final int min, final int max, final String message) {
82          if (max > Const.MAX_SHORT) {
83              throw new IllegalArgumentException(String.format("%s programming error: max %,d > %,d", message, max, Const.MAX_SHORT));
84          }
85          if (min < 0) {
86              throw new IllegalArgumentException(String.format("%s programming error: min %,d < 0", message, min));
87          }
88          if (value < min || value > max) {
89              throw new ClassFormatException(String.format("%s [Value out of range (%,d - %,d) for type u2: %,d]", message, min, max, value));
90          }
91          return value;
92      }
93  
94      /**
95       * Requires a u2 value of at least {@code min}.
96       *
97       * @param value   The value to test.
98       * @param min     The minimum required value.
99       * @param message The message prefix.
100      * @return The value to test.
101      */
102     public static int requireU2(final int value, final int min, final String message) {
103         return requireU2(value, min, Const.MAX_SHORT, message);
104     }
105 
106     /**
107      * Requires a u2 value.
108      *
109      * @param value   The value to test.
110      * @param message The message prefix.
111      * @return The value to test.
112      */
113     public static int requireU2(final int value, final String message) {
114         return requireU2(value, 0, message);
115     }
116 
117     /**
118      * Requires a u4 value of at least {@code min}.
119      *
120      * @param value   The value to test.
121      * @param min     The minimum required value.
122      * @param message The message prefix.
123      * @return The value to test.
124      */
125     public static int requireU4(final int value, final int min, final String message) {
126         // Should really be 2^32-1, instead 2^21-1.
127         return requireU4(value, min, Integer.MAX_VALUE, message);
128     }
129 
130     /**
131      * Requires a u4 value of at least {@code min} and not above {@code max}.
132      *
133      * @param value   The value to test.
134      * @param min     The minimum required u4 value.
135      * @param max     The maximum required u4 value.
136      * @param message The message prefix.
137      * @return The value to test.
138      * @throws IllegalArgumentException if {@code min < 0} or {@code max > Integer.MAX_VALUE}.
139      * @throws ClassFormatException     if {@code value < min} or {@code value > max}.
140      * @since 6.12.0
141      */
142     public static int requireU4(final int value, final int min, final int max, final String message) {
143         if (min < 0) {
144             throw new IllegalArgumentException(String.format("%s programming error: min %,d < 0", message, min));
145         }
146         if (value < min || value > max) {
147             throw new ClassFormatException(
148                     String.format("%s [Value out of range (%,d - %,d) for type u4: %,d]", message, min, Integer.MAX_VALUE, value & 0xFFFFFFFFL));
149         }
150         return value;
151     }
152 
153     /**
154      * Requires a u4 value.
155      *
156      * @param value   The value to test.
157      * @param message The message prefix.
158      * @return The value to test.
159      */
160     public static int requireU4(final int value, final String message) {
161         return requireU4(value, 0, message);
162     }
163 
164     /**
165      * Private constructor to prevent instantiation of this utility class.
166      */
167     public Args() {
168         // Utility class
169     }
170 }