📄 checkmethodadapter.java
字号:
/**
* Checks that the visitMaxs method has not been called.
*/
void checkEndCode() {
if ( this.endCode ) {
throw new IllegalStateException( "Cannot visit instructions after visitMaxs has been called." );
}
}
/**
* Checks that the visitEnd method has not been called.
*/
void checkEndMethod() {
if ( this.endMethod ) {
throw new IllegalStateException( "Cannot visit elements after visitEnd has been called." );
}
}
/**
* Checks that the type of the given opcode is equal to the given type.
*
* @param opcode the opcode to be checked.
* @param type the expected opcode type.
*/
static void checkOpcode(final int opcode,
final int type) {
if ( opcode < 0 || opcode > 199 || CheckMethodAdapter.TYPE[opcode] != type ) {
throw new IllegalArgumentException( "Invalid opcode: " + opcode );
}
}
/**
* Checks that the given value is a signed byte.
*
* @param value the value to be checked.
* @param msg an message to be used in case of error.
*/
static void checkSignedByte(final int value,
final String msg) {
if ( value < Byte.MIN_VALUE || value > Byte.MAX_VALUE ) {
throw new IllegalArgumentException( msg + " (must be a signed byte): " + value );
}
}
/**
* Checks that the given value is a signed short.
*
* @param value the value to be checked.
* @param msg an message to be used in case of error.
*/
static void checkSignedShort(final int value,
final String msg) {
if ( value < Short.MIN_VALUE || value > Short.MAX_VALUE ) {
throw new IllegalArgumentException( msg + " (must be a signed short): " + value );
}
}
/**
* Checks that the given value is an unsigned short.
*
* @param value the value to be checked.
* @param msg an message to be used in case of error.
*/
static void checkUnsignedShort(final int value,
final String msg) {
if ( value < 0 || value > 65535 ) {
throw new IllegalArgumentException( msg + " (must be an unsigned short): " + value );
}
}
/**
* Checks that the given value is an {@link Integer}, a{@link Float}, a
* {@link Long}, a {@link Double} or a {@link String}.
*
* @param cst the value to be checked.
*/
static void checkConstant(final Object cst) {
if ( !(cst instanceof Integer) && !(cst instanceof Float) && !(cst instanceof Long) && !(cst instanceof Double) && !(cst instanceof String) ) {
throw new IllegalArgumentException( "Invalid constant: " + cst );
}
}
/**
* Checks that the given string is a valid Java identifier.
*
* @param name the string to be checked.
* @param msg a message to be used in case of error.
*/
static void checkIdentifier(final String name,
final String msg) {
checkIdentifier( name,
0,
-1,
msg );
}
/**
* Checks that the given substring is a valid Java identifier.
*
* @param name the string to be checked.
* @param start index of the first character of the identifier (inclusive).
* @param end index of the last character of the identifier (exclusive). -1
* is equivalent to <tt>name.length()</tt> if name is not
* <tt>null</tt>.
* @param msg a message to be used in case of error.
*/
static void checkIdentifier(final String name,
final int start,
final int end,
final String msg) {
if ( name == null || (end == -1 ? name.length() <= start : end <= start) ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must not be null or empty)" );
}
if ( !Character.isJavaIdentifierStart( name.charAt( start ) ) ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must be a valid Java identifier): " + name );
}
final int max = (end == -1 ? name.length() : end);
for ( int i = start + 1; i < max; ++i ) {
if ( !Character.isJavaIdentifierPart( name.charAt( i ) ) ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must be a valid Java identifier): " + name );
}
}
}
/**
* Checks that the given string is a valid Java identifier or is equal to
* '<init>' or '<clinit>'.
*
* @param name the string to be checked.
* @param msg a message to be used in case of error.
*/
static void checkMethodIdentifier(final String name,
final String msg) {
if ( name == null || name.length() == 0 ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must not be null or empty)" );
}
if ( name.equals( "<init>" ) || name.equals( "<clinit>" ) ) {
return;
}
if ( !Character.isJavaIdentifierStart( name.charAt( 0 ) ) ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must be a '<init>', '<clinit>' or a valid Java identifier): " + name );
}
for ( int i = 1; i < name.length(); ++i ) {
if ( !Character.isJavaIdentifierPart( name.charAt( i ) ) ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must be '<init>' or '<clinit>' or a valid Java identifier): " + name );
}
}
}
/**
* Checks that the given string is a valid internal class name.
*
* @param name the string to be checked.
* @param msg a message to be used in case of error.
*/
static void checkInternalName(final String name,
final String msg) {
checkInternalName( name,
0,
-1,
msg );
}
/**
* Checks that the given substring is a valid internal class name.
*
* @param name the string to be checked.
* @param start index of the first character of the identifier (inclusive).
* @param end index of the last character of the identifier (exclusive). -1
* is equivalent to <tt>name.length()</tt> if name is not
* <tt>null</tt>.
* @param msg a message to be used in case of error.
*/
static void checkInternalName(final String name,
final int start,
final int end,
final String msg) {
if ( name == null || name.length() == 0 ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must not be null or empty)" );
}
final int max = (end == -1 ? name.length() : end);
try {
int begin = start;
int slash;
do {
slash = name.indexOf( '/',
begin + 1 );
if ( slash == -1 || slash > max ) {
slash = max;
}
checkIdentifier( name,
begin,
slash,
null );
begin = slash + 1;
} while ( slash != max );
} catch ( final IllegalArgumentException _ ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must be a fully qualified class name in internal form): " + name );
}
}
/**
* Checks that the given string is a valid type descriptor.
*
* @param desc the string to be checked.
* @param canBeVoid <tt>true</tt> if <tt>V</tt> can be considered valid.
*/
static void checkDesc(final String desc,
final boolean canBeVoid) {
final int end = checkDesc( desc,
0,
canBeVoid );
if ( end != desc.length() ) {
throw new IllegalArgumentException( "Invalid descriptor: " + desc );
}
}
/**
* Checks that a the given substring is a valid type descriptor.
*
* @param desc the string to be checked.
* @param start index of the first character of the identifier (inclusive).
* @param canBeVoid <tt>true</tt> if <tt>V</tt> can be considered valid.
* @return the index of the last character of the type decriptor, plus one.
*/
static int checkDesc(final String desc,
final int start,
final boolean canBeVoid) {
if ( desc == null || start >= desc.length() ) {
throw new IllegalArgumentException( "Invalid type descriptor (must not be null or empty)" );
}
int index;
switch ( desc.charAt( start ) ) {
case 'V' :
if ( canBeVoid ) {
return start + 1;
} else {
throw new IllegalArgumentException( "Invalid descriptor: " + desc );
}
case 'Z' :
case 'C' :
case 'B' :
case 'S' :
case 'I' :
case 'F' :
case 'J' :
case 'D' :
return start + 1;
case '[' :
index = start + 1;
while ( index < desc.length() && desc.charAt( index ) == '[' ) {
++index;
}
if ( index < desc.length() ) {
return checkDesc( desc,
index,
false );
} else {
throw new IllegalArgumentException( "Invalid descriptor: " + desc );
}
case 'L' :
index = desc.indexOf( ';',
start );
if ( index == -1 || index - start < 2 ) {
throw new IllegalArgumentException( "Invalid descriptor: " + desc );
}
try {
checkInternalName( desc,
start + 1,
index,
null );
} catch ( final IllegalArgumentException _ ) {
throw new IllegalArgumentException( "Invalid descriptor: " + desc );
}
return index + 1;
default :
throw new IllegalArgumentException( "Invalid descriptor: " + desc );
}
}
/**
* Checks that the given string is a valid method descriptor.
*
* @param desc the string to be checked.
*/
static void checkMethodDesc(final String desc) {
if ( desc == null || desc.length() == 0 ) {
throw new IllegalArgumentException( "Invalid method descriptor (must not be null or empty)" );
}
if ( desc.charAt( 0 ) != '(' || desc.length() < 3 ) {
throw new IllegalArgumentException( "Invalid descriptor: " + desc );
}
int start = 1;
if ( desc.charAt( start ) != ')' ) {
do {
if ( desc.charAt( start ) == 'V' ) {
throw new IllegalArgumentException( "Invalid descriptor: " + desc );
}
start = checkDesc( desc,
start,
false );
} while ( start < desc.length() && desc.charAt( start ) != ')' );
}
start = checkDesc( desc,
start + 1,
true );
if ( start != desc.length() ) {
throw new IllegalArgumentException( "Invalid descriptor: " + desc );
}
}
/**
* Checks that the given label is not null. This method can also check that
* the label has been visited.
*
* @param label the label to be checked.
* @param checkVisited <tt>true</tt> to check that the label has been
* visited.
* @param msg a message to be used in case of error.
*/
void checkLabel(final Label label,
final boolean checkVisited,
final String msg) {
if ( label == null ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must not be null)" );
}
if ( checkVisited && this.labels.get( label ) == null ) {
throw new IllegalArgumentException( "Invalid " + msg + " (must be visited first)" );
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -