View Javadoc
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  
18  package org.apache.bcel.util;
19  
20  import org.apache.bcel.Const;
21  import org.apache.bcel.classfile.ClassFormatException;
22  
23  /**
24   * Argument validation.
25   *
26   * @since 6.7.0
27   */
28  public class Args {
29  
30      /**
31       * Requires a specific value.
32       *
33       * @param value    The value to test.
34       * @param required The required value.
35       * @param message  The message prefix
36       * @return The value to test.
37       */
38      public static int require(final int value, final int required, final String message) {
39          if (value != required) {
40              throw new ClassFormatException(String.format("%s [Value must be 0: %,d]", message, value));
41          }
42          return value;
43      }
44  
45      /**
46       * Requires a 0 value.
47       *
48       * @param value   The value to test.
49       * @param message The message prefix
50       * @return The value to test.
51       */
52      public static int require0(final int value, final String message) {
53          return require(value, 0, message);
54      }
55  
56      /**
57       * Requires a u1 value.
58       *
59       * @param value   The value to test.
60       * @param message The message prefix
61       * @return The value to test.
62       */
63      public static int requireU1(final int value, final String message) {
64          if (value < 0 || value > Const.MAX_BYTE) {
65              throw new ClassFormatException(String.format("%s [Value out of range (0 - %,d) for type u1: %,d]", message, Const.MAX_BYTE, value));
66          }
67          return value;
68      }
69  
70      /**
71       * Requires a u2 value of at least {@code min} and not above {@code max}.
72       *
73       * @param value   The value to test.
74       * @param min     The minimum required u2 value.
75       * @param max     The maximum required u2 value.
76       * @param message The message prefix
77       * @return The value to test.
78       */
79      public static int requireU2(final int value, final int min, final int max, final String message) {
80          if (max > Const.MAX_SHORT) {
81              throw new IllegalArgumentException(String.format("%s programming error: max %,d > %,d", message, max, Const.MAX_SHORT));
82          }
83          if (min < 0) {
84              throw new IllegalArgumentException(String.format("%s programming error: min %,d < 0", message, min));
85          }
86          if (value < min || value > max) {
87              throw new ClassFormatException(String.format("%s [Value out of range (%,d - %,d) for type u2: %,d]", message, min, Const.MAX_SHORT, value));
88          }
89          return value;
90      }
91  
92      /**
93       * Requires a u2 value of at least {@code min}.
94       *
95       * @param value   The value to test.
96       * @param min     The minimum required value.
97       * @param message The message prefix
98       * @return The value to test.
99       */
100     public static int requireU2(final int value, final int min, final String message) {
101         return requireU2(value, min, Const.MAX_SHORT, message);
102     }
103 
104     /**
105      * Requires a u2 value.
106      *
107      * @param value   The value to test.
108      * @param message The message prefix
109      * @return The value to test.
110      */
111     public static int requireU2(final int value, final String message) {
112         return requireU2(value, 0, message);
113     }
114 
115     /**
116      * Requires a u4 value of at least {@code min}.
117      *
118      * @param value   The value to test.
119      * @param min     The minimum required value.
120      * @param message The message prefix
121      * @return The value to test.
122      */
123     public static int requireU4(final int value, final int min, final String message) {
124         if (min < 0) {
125             throw new IllegalArgumentException(String.format("%s programming error: min %,d < 0", message, min));
126         }
127         if (value < min) {
128             throw new ClassFormatException(
129                     String.format("%s [Value out of range (%,d - %,d) for type u2: %,d]", message, min, Integer.MAX_VALUE, value & 0xFFFFFFFFL));
130         }
131         return value;
132     }
133 
134     /**
135      * Requires a u4 value.
136      *
137      * @param value   The value to test.
138      * @param message The message prefix
139      * @return The value to test.
140      */
141     public static int requireU4(final int value, final String message) {
142         return requireU4(value, 0, message);
143     }
144 }