Top Description Fields Constructors Methods
org.objectweb.asm.util

public Class CheckClassAdapter

extends ClassVisitor
Class Inheritance
Imports
java.io.FileInputStream, .IOException, .InputStream, .PrintWriter, java.util.ArrayList, .HashMap, .List, .Map, org.objectweb.asm.AnnotationVisitor, .Attribute, .ClassReader, .ClassVisitor, .ClassWriter, .FieldVisitor, .Label, .MethodVisitor, .ModuleVisitor, .Opcodes, .RecordComponentVisitor, .Type, .TypePath, .TypeReference, org.objectweb.asm.tree.ClassNode, .MethodNode, .TryCatchBlockNode, org.objectweb.asm.tree.analysis.Analyzer, .AnalyzerException, .BasicValue, .Frame, .SimpleVerifier

A ClassVisitor that checks that its methods are properly used. More precisely this class adapter checks each method call individually, based only on its arguments, but does not check the sequence of method calls. For example, the invalid sequence visitField(ACC_PUBLIC, "i", "I", null) visitField(ACC_PUBLIC, "i", "D", null) will not be detected by this class adapter.

CheckClassAdapter can be also used to verify bytecode transformations in order to make sure that the transformed bytecode is sane. For example:

InputStream inputStream = ...; // get bytes for the source class
ClassReader classReader = new ClassReader(inputStream);
ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_MAXS);
ClassVisitor classVisitor = new MyClassAdapter(new CheckClassAdapter(classWriter, true));
classReader.accept(classVisitor, 0);

StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
CheckClassAdapter.verify(new ClassReader(classWriter.toByteArray()), false, printWriter);
assertTrue(stringWriter.toString().isEmpty());

The above code pass the transformed bytecode through a CheckClassAdapter, with data flow checks enabled. These checks are not exactly the same as the JVM verification, but provide some basic type checking for each method instruction. If the bytecode has errors, the output text shows the erroneous instruction number, and a dump of the failed method with information about the type of the local variables and of the operand stack slots for each instruction. For example (format is - insnNumber locals : stack):

org.objectweb.asm.tree.analysis.AnalyzerException: Error at instruction 71: Expected I, but found .
  at org.objectweb.asm.tree.analysis.Analyzer.analyze(Analyzer.java:...)
  at org.objectweb.asm.util.CheckClassAdapter.verify(CheckClassAdapter.java:...)
...
remove()V
00000 LinkedBlockingQueue$Itr . . . . . . . .  : ICONST_0
00001 LinkedBlockingQueue$Itr . . . . . . . .  : I ISTORE 2
00001 LinkedBlockingQueue$Itr . I . . . . . .  :
...
00071 LinkedBlockingQueue$Itr . I . . . . . .  : ILOAD 1
00072 ? INVOKESPECIAL java/lang/Integer.<init> (I)V
...

The above output shows that the local variable 1, loaded by the ILOAD 1 instruction at position 00071 is not initialized, whereas the local variable 2 is initialized and contains an int value.

Author
Eric Bruneton

Field Summary

Modifier and TypeField and Description
private boolean
checkDataFlow

Whether the bytecode must be checked with a BasicVerifier.

private static final String
private Map<Label, Integer>
labelInsnIndices

The index of the instruction designated by each visited label so far.

private String
nestMemberPackageName

The common package of all the nest members.

private static final String
USAGE

The help message shown when command line arguments are incorrect.

private int
version

The class version number.

private boolean
visitCalled

Whether the visit method has been called.

private boolean
visitEndCalled

Whether the visitEnd method has been called.

private boolean
visitModuleCalled

Whether the visitModule method has been called.

private boolean
visitNestHostCalled

Whether the visitNestHost method has been called.

private boolean
visitOuterClassCalled

Whether the visitOuterClass method has been called.

private boolean
visitSourceCalled

Whether the visitSource method has been called.

Inherited from org.objectweb.asm.ClassVisitor:
apicv

Constructor Summary

AccessConstructor and Description
public
CheckClassAdapter(final ClassVisitor
the class visitor to which this adapter must delegate calls.
classVisitor
)

Constructs a new CheckClassAdapter.

public
CheckClassAdapter(final ClassVisitor
the class visitor to which this adapter must delegate calls.
classVisitor
,
final boolean
whether to perform basic data flow checks.
checkDataFlow
)

Constructs a new CheckClassAdapter.

protected
CheckClassAdapter(final int
the ASM API version implemented by this visitor. Must be one of the ASMx values in Opcodes.
api
,
final ClassVisitor
the class visitor to which this adapter must delegate calls.
classVisitor
,
final boolean
true to perform basic data flow checks, or false to not perform any data flow check (see CheckMethodAdapter).
checkDataFlow
)

Constructs a new CheckClassAdapter.

Method Summary

Modifier and TypeMethod and Description
pack-priv static void
checkAccess(final int
the access flags to be checked.
access
,
final int
the valid access flags.
possibleAccess
)

Checks that the given access flags do not contain invalid flags.

private static int

Returns:

the index of the first character after the checked part.
checkChar
(final char
a character.
c
,
final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
pos
)

Checks a single character.

public static void
checkClassSignature(final String
a string containing the signature that must be checked.
signature
)

Checks a class signature.

private static int

Returns:

the index of the first character after the checked part.
checkClassTypeSignature
(final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
startPos
)

Checks a class type signature.

public static void
checkFieldSignature(final String
a string containing the signature that must be checked.
signature
)

Checks a field signature.

pack-priv static void
checkFullyQualifiedName(final int
the class version.
version
,
final String
the name to be checked.
name
,
final String
the source of 'name' (e.g 'module' for a module name).
source
)

Checks that the given name is a fully qualified name, using dots.

private static int

Returns:

the index of the first character after the checked part.
checkJavaTypeSignature
(final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
startPos
)

Checks a Java type signature.

private static void
checkMethodAccess(final int
the class version.
version
,
final int
the method access flags to be checked.
access
,
final int
the valid access flags.
possibleAccess
)

Checks that the given access flags do not contain invalid flags for a method.

public static void
checkMethodSignature(final String
a string containing the signature that must be checked.
signature
)

Checks a method signature.

private static int

Returns:

the index of the first character after the checked part.
checkReferenceTypeSignature
(final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
pos
)

Checks a reference type signature.

private static int

Returns:

the index of the first character after the checked part.
checkSignatureIdentifier
(final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
startPos
)

Checks an identifier.

private void
checkState()

Checks that the visit method has been called and that visitEnd has not been called.

private static int

Returns:

the index of the first character after the checked part.
checkTypeArgument
(final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
startPos
)

Checks a type argument in a class type signature.

private static int

Returns:

the index of the first character after the checked part.
checkTypeArguments
(final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
startPos
)

Checks the type arguments in a class type signature.

private static int

Returns:

the index of the first character after the checked part.
checkTypeParameter
(final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
startPos
)

Checks a type parameter of a class or method signature.

private static int

Returns:

the index of the first character after the checked part.
checkTypeParameters
(final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
startPos
)

Checks the type parameters of a class or method signature.

pack-priv static void
checkTypeRef(final int
a reference to an annotated type.
typeRef
)

Checks the reference to a type in a type annotation.

private static int

Returns:

the index of the first character after the checked part.
checkTypeVariableSignature
(final String
a string containing the signature that must be checked.
signature
,
final int
index of first character to be checked.
startPos
)

Checks a type variable signature.

private static char

Returns:

the character at the given index, or 0 if there is no such character.
getChar
(final String
a string.
string
,
final int
an index in 'string'.
pos
)

Returns the string character at the given index, or 0.

private static String
public static void
main(final String[]
the command line arguments.
args
)

Checks the given class.

pack-priv static void
main(final String[]
the command line arguments.
args
,
final PrintWriter
where to log errors.
logger
)

Checks the given class.

private static String

Returns:

the package name or "" if there is no package.
packageName
(final String
an internal name.
name
)

Returns the package name of an internal name.

pack-priv static void
printAnalyzerResult(final MethodNode method, final Analyzer<BasicValue> analyzer, final PrintWriter printWriter)

public static void
verify(final ClassReader
the class to be checked.
classReader
,
final boolean
whether to print the results of the bytecode verification.
printResults
,
final PrintWriter
where the results (or the stack trace in case of error) must be printed.
printWriter
)

Checks the given class.

public static void
verify(final ClassReader
the class to be checked.
classReader
,
final ClassLoader
a ClassLoader which will be used to load referenced classes. May be null.
loader
,
final boolean
whether to print the results of the bytecode verification.
printResults
,
final PrintWriter
where the results (or the stack trace in case of error) must be printed.
printWriter
)

Checks the given class.

public void
visit(final int
the class version. The minor version is stored in the 16 most significant bits, and the major version in the 16 least significant bits.
version
,
final int
the class's access flags (see Opcodes). This parameter also indicates if the class is deprecated Opcodes#ACC_DEPRECATED or a record Opcodes#ACC_RECORD.
access
,
final String
the internal name of the class (see Type#getInternalName()).
name
,
final String
the signature of this class. May be null if the class is not a generic one, and does not extend or implement generic classes or interfaces.
signature
,
final String
the internal of name of the super class (see Type#getInternalName()). For interfaces, the super class is Object. May be null, but only for the Object class.
superName
,
final String[]
the internal names of the class's interfaces (see Type#getInternalName()). May be null.
interfaces
)

Overrides org.objectweb.asm.ClassVisitor.visit.

Visits the header of the class.
public AnnotationVisitor
visitAnnotation(final String
the class descriptor of the annotation class.
descriptor
,
final boolean
true if the annotation is visible at runtime.
visible
)

Overrides org.objectweb.asm.ClassVisitor.visitAnnotation.

Visits an annotation of the class.
public void
visitAttribute(final Attribute
an attribute.
attribute
)

Overrides org.objectweb.asm.ClassVisitor.visitAttribute.

Visits a non standard attribute of the class.
public void
visitEnd()

Overrides org.objectweb.asm.ClassVisitor.visitEnd.

Visits the end of the class.
public FieldVisitor
visitField(final int
the field's access flags (see Opcodes). This parameter also indicates if the field is synthetic and/or deprecated.
access
,
final String
the field's name.
name
,
final String
the field's descriptor (see Type).
descriptor
,
final String
the field's signature. May be null if the field's type does not use generic types.
signature
,
final Object
the field's initial value. This parameter, which may be null if the field does not have an initial value, must be an Integer, a Float, a Long, a Double or a String (for int, float, long or String fields respectively). This parameter is only used for static fields. Its value is ignored for non static fields, which must be initialized through bytecode instructions in constructors or methods.
value
)

Overrides org.objectweb.asm.ClassVisitor.visitField.

Visits a field of the class.
public void
visitInnerClass(final String
the internal name of C (see Type#getInternalName()).
name
,
final String
the internal name of the class or interface C is a member of (see Type#getInternalName()). Must be null if C is not the member of a class or interface (e.g. for local or anonymous classes).
outerName
,
final String
the (simple) name of C. Must be null for anonymous inner classes.
innerName
,
final int
the access flags of C originally declared in the source code from which this class was compiled.
access
)

Overrides org.objectweb.asm.ClassVisitor.visitInnerClass.

Visits information about an inner class.
public MethodVisitor
visitMethod(final int
the method's access flags (see Opcodes). This parameter also indicates if the method is synthetic and/or deprecated.
access
,
final String
the method's name.
name
,
final String
the method's descriptor (see Type).
descriptor
,
final String
the method's signature. May be null if the method parameters, return type and exceptions do not use generic types.
signature
,
final String[]
the internal names of the method's exception classes (see Type#getInternalName()). May be null.
exceptions
)

Overrides org.objectweb.asm.ClassVisitor.visitMethod.

Visits a method of the class.
public ModuleVisitor
visitModule(final String
the fully qualified name (using dots) of the module.
name
,
final int
the module access flags, among ACC_OPEN, ACC_SYNTHETIC and ACC_MANDATED.
access
,
final String
the module version, or null.
version
)

Overrides org.objectweb.asm.ClassVisitor.visitModule.

Visit the module corresponding to the class.
public void
visitNestHost(final String
the internal name of the host class of the nest (see Type#getInternalName()).
nestHost
)

Overrides org.objectweb.asm.ClassVisitor.visitNestHost.

Visits the nest host class of the class.
public void
visitNestMember(final String
the internal name of a nest member (see Type#getInternalName()).
nestMember
)

Overrides org.objectweb.asm.ClassVisitor.visitNestMember.

Visits a member of the nest.
public void
visitOuterClass(final String
internal name of the enclosing class of the class (see Type#getInternalName()).
owner
,
final String
the name of the method that contains the class, or null if the class is not enclosed in a method or constructor of its enclosing class (e.g. if it is enclosed in an instance initializer, static initializer, instance variable initializer, or class variable initializer).
name
,
final String
the descriptor of the method that contains the class, or null if the class is not enclosed in a method or constructor of its enclosing class (e.g. if it is enclosed in an instance initializer, static initializer, instance variable initializer, or class variable initializer).
descriptor
)

Overrides org.objectweb.asm.ClassVisitor.visitOuterClass.

Visits the enclosing class of the class.
public void
visitPermittedSubclass(final String
the internal name of a permitted subclass (see Type#getInternalName()).
permittedSubclass
)

Overrides org.objectweb.asm.ClassVisitor.visitPermittedSubclass.

Visits a permitted subclasses.
public RecordComponentVisitor
visitRecordComponent(final String
the record component name.
name
,
final String
the record component descriptor (see Type).
descriptor
,
final String
the record component signature. May be null if the record component type does not use generic types.
signature
)

Overrides org.objectweb.asm.ClassVisitor.visitRecordComponent.

Visits a record component of the class.
public void
visitSource(final String
the name of the source file from which the class was compiled. May be null.
file
,
final String
additional debug information to compute the correspondence between source and compiled elements of the class. May be null.
debug
)

Overrides org.objectweb.asm.ClassVisitor.visitSource.

Visits the source of the class.
public AnnotationVisitor
visitTypeAnnotation(final int
a reference to the annotated type. The sort of this type reference must be TypeReference#CLASS_TYPE_PARAMETER, TypeReference#CLASS_TYPE_PARAMETER_BOUND or TypeReference#CLASS_EXTENDS. See TypeReference.
typeRef
,
final TypePath
the path to the annotated type argument, wildcard bound, array element type, or static inner type within 'typeRef'. May be null if the annotation targets 'typeRef' as a whole.
typePath
,
final String
the class descriptor of the annotation class.
descriptor
,
final boolean
true if the annotation is visible at runtime.
visible
)

Overrides org.objectweb.asm.ClassVisitor.visitTypeAnnotation.

Visits an annotation on a type in the class signature.
Inherited from org.objectweb.asm.ClassVisitor:
getDelegate