1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
25
26
27
28 public class Args {
29
30
31
32
33
34
35
36
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
47
48
49
50
51
52 public static int require0(final int value, final String message) {
53 return require(value, 0, message);
54 }
55
56
57
58
59
60
61
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
72
73
74
75
76
77
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
94
95
96
97
98
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
106
107
108
109
110
111 public static int requireU2(final int value, final String message) {
112 return requireU2(value, 0, message);
113 }
114
115
116
117
118
119
120
121
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
136
137
138
139
140
141 public static int requireU4(final int value, final String message) {
142 return requireU4(value, 0, message);
143 }
144 }