1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
27
28
29
30 public class Args {
31
32
33
34
35
36
37
38
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
49
50
51
52
53
54 public static int require0(final int value, final String message) {
55 return require(value, 0, message);
56 }
57
58
59
60
61
62
63
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
74
75
76
77
78
79
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, Const.MAX_SHORT, value));
90 }
91 return value;
92 }
93
94
95
96
97
98
99
100
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
108
109
110
111
112
113 public static int requireU2(final int value, final String message) {
114 return requireU2(value, 0, message);
115 }
116
117
118
119
120
121
122
123
124
125 public static int requireU4(final int value, final int min, final String message) {
126 if (min < 0) {
127 throw new IllegalArgumentException(String.format("%s programming error: min %,d < 0", message, min));
128 }
129 if (value < min) {
130 throw new ClassFormatException(
131 String.format("%s [Value out of range (%,d - %,d) for type u2: %,d]", message, min, Integer.MAX_VALUE, value & 0xFFFFFFFFL));
132 }
133 return value;
134 }
135
136
137
138
139
140
141
142
143 public static int requireU4(final int value, final String message) {
144 return requireU4(value, 0, message);
145 }
146 }