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.
| Modifier and Type | Field and Description |
|---|---|
| private boolean | checkDataFlow
Whether the bytecode must be checked with a BasicVerifier. |
| private static final String | |
| private Map | 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 |
| private boolean | visitEndCalled
Whether the |
| private boolean | visitModuleCalled
Whether the |
| private boolean | visitNestHostCalled
Whether the |
| private boolean | visitOuterClassCalled
Whether the |
| private boolean | visitSourceCalled
Whether the |
| Access | Constructor and Description |
|---|---|
| public | CheckClassAdapter(final ClassVisitor
the class visitor to which this adapter must delegate calls. classVisitor)Constructs a new |
| 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 |
| protected | CheckClassAdapter(final int
the ASM API version implemented by this visitor. Must be one of the api, final ClassVisitor ASMx values in Opcodes.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 checkDataFlow)CheckMethodAdapter).Constructs a new |
| Modifier and Type | Method 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 | |
| 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.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.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.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.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 | |
| private static int | Returns: the index of the first character after the checked part.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.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.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.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.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 | |
| private static String | |
| public static void | |
| 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.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 loader, final boolean ClassLoader which will be used to load referenced classes. May be
null.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 access, final String Opcodes). This parameter also indicates if
the class is deprecated Opcodes#ACC_DEPRECATED or a record Opcodes#ACC_RECORD.the internal name of the class (see name, final String Type#getInternalName()).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 superName, final String[] Type#getInternalName()).
For interfaces, the super class is Object. May be null, but only for the
Object class.the internal names of the class's interfaces (see interfaces)Type#getInternalName()). May be null.Overrides org. |
| public AnnotationVisitor | visitAnnotation(final String
the class descriptor of the annotation class. descriptor, final boolean true if the annotation is visible at runtime. visible)
Visits an annotation of the class.
|
| public void | visitAttribute(final Attribute
an attribute. attribute)
Visits a non standard attribute of the class.
|
| public void | |
| public FieldVisitor | visitField(final int
the field's access flags (see access, final String Opcodes). This parameter also indicates if
the field is synthetic and/or deprecated.the field's name. name, final String the field's descriptor (see descriptor, final String Type).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 value)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.Overrides org. |
| public void | visitInnerClass(final String
the internal name of C (see name, final String Type#getInternalName()).the internal name of the class or interface C is a member of (see outerName, final String Type#getInternalName()). Must be null if C is not the member of a class or
interface (e.g. for local or anonymous classes).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)
Visits information about an inner class.
|
| public MethodVisitor | visitMethod(final int
the method's access flags (see access, final String Opcodes). This parameter also indicates if
the method is synthetic and/or deprecated.the method's name. name, final String the method's descriptor (see descriptor, final String Type).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 exceptions)Type#getInternalName()). May be null.Overrides org. |
| public ModuleVisitor | visitModule(final String
the fully qualified name (using dots) of the module. name, final int the module access flags, among access, final String ACC_OPEN, ACC_SYNTHETIC and ACC_MANDATED.the module version, or null. version)Overrides org. |
| public void | visitNestHost(final String
the internal name of the host class of the nest (see nestHost)Type#getInternalName()).Overrides org. |
| public void | visitNestMember(final String
the internal name of a nest member (see nestMember)
Visits a member of the nest.
Type#getInternalName()). |
| public void | visitOuterClass(final String
internal name of the enclosing class of the class (see owner, final String Type#getInternalName()).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)
Visits the enclosing class of the class.
|
| public void | visitPermittedSubclass(final String
the internal name of a permitted subclass (see permittedSubclass)Type#getInternalName()).Overrides org. |
| public RecordComponentVisitor | visitRecordComponent(final String
the record component name. name, final String the record component descriptor (see descriptor, final String Type).the record component signature. May be null if the record component
type does not use generic types. signature)Overrides org. |
| 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. |
| public AnnotationVisitor | visitTypeAnnotation(final int
a reference to the annotated type. The sort of this type reference must be
typeRef, final TypePath TypeReference#CLASS_TYPE_PARAMETER, TypeReference#CLASS_TYPE_PARAMETER_BOUND or TypeReference#CLASS_EXTENDS. See
TypeReference.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. |