Class CheckClassAdapter
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.
-
Field Summary
Fields inherited from class org.objectweb.asm.ClassVisitor
api, cv
-
Constructor Summary
ModifierConstructorDescriptionprotected
CheckClassAdapter
(int api, org.objectweb.asm.ClassVisitor classVisitor, boolean checkDataFlow) Constructs a newCheckClassAdapter
.CheckClassAdapter
(org.objectweb.asm.ClassVisitor classVisitor) Constructs a newCheckClassAdapter
.CheckClassAdapter
(org.objectweb.asm.ClassVisitor classVisitor, boolean checkDataFlow) Constructs a newCheckClassAdapter
. -
Method Summary
Modifier and TypeMethodDescriptionstatic void
checkClassSignature
(String signature) Checks a class signature.static void
checkFieldSignature
(String signature) Checks a field signature.static void
checkMethodSignature
(String signature) Checks a method signature.static void
Checks the given class.static void
verify
(org.objectweb.asm.ClassReader classReader, boolean printResults, PrintWriter printWriter) Checks the given class.static void
verify
(org.objectweb.asm.ClassReader classReader, ClassLoader loader, boolean printResults, PrintWriter printWriter) Checks the given class.void
visit
(int version, int access, String name, String signature, String superName, String[] interfaces) org.objectweb.asm.AnnotationVisitor
visitAnnotation
(String descriptor, boolean visible) void
visitAttribute
(org.objectweb.asm.Attribute attribute) void
visitEnd()
org.objectweb.asm.FieldVisitor
visitField
(int access, String name, String descriptor, String signature, Object value) void
visitInnerClass
(String name, String outerName, String innerName, int access) org.objectweb.asm.MethodVisitor
visitMethod
(int access, String name, String descriptor, String signature, String[] exceptions) org.objectweb.asm.ModuleVisitor
visitModule
(String name, int access, String version) void
visitNestHost
(String nestHost) void
visitNestMember
(String nestMember) void
visitOuterClass
(String owner, String name, String descriptor) void
visitPermittedSubclass
(String permittedSubclass) org.objectweb.asm.RecordComponentVisitor
visitRecordComponent
(String name, String descriptor, String signature) void
visitSource
(String file, String debug) org.objectweb.asm.AnnotationVisitor
visitTypeAnnotation
(int typeRef, org.objectweb.asm.TypePath typePath, String descriptor, boolean visible) Methods inherited from class org.objectweb.asm.ClassVisitor
getDelegate
-
Constructor Details
-
CheckClassAdapter
public CheckClassAdapter(org.objectweb.asm.ClassVisitor classVisitor) Constructs a newCheckClassAdapter
. Subclasses must not use this constructor. Instead, they must use theCheckClassAdapter(int, ClassVisitor, boolean)
version.- Parameters:
classVisitor
- the class visitor to which this adapter must delegate calls.
-
CheckClassAdapter
public CheckClassAdapter(org.objectweb.asm.ClassVisitor classVisitor, boolean checkDataFlow) Constructs a newCheckClassAdapter
. Subclasses must not use this constructor. Instead, they must use theCheckClassAdapter(int, ClassVisitor, boolean)
version.- Parameters:
classVisitor
- the class visitor to which this adapter must delegate calls.checkDataFlow
- whether to perform basic data flow checks.- Throws:
IllegalStateException
- If a subclass calls this constructor.
-
CheckClassAdapter
protected CheckClassAdapter(int api, org.objectweb.asm.ClassVisitor classVisitor, boolean checkDataFlow) Constructs a newCheckClassAdapter
.- Parameters:
api
- the ASM API version implemented by this visitor. Must be one of theASM
x values inOpcodes
.classVisitor
- the class visitor to which this adapter must delegate calls.checkDataFlow
- true to perform basic data flow checks, or false to not perform any data flow check (seeCheckMethodAdapter
).
-
-
Method Details
-
visit
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) - Overrides:
visit
in classorg.objectweb.asm.ClassVisitor
-
visitSource
- Overrides:
visitSource
in classorg.objectweb.asm.ClassVisitor
-
visitModule
- Overrides:
visitModule
in classorg.objectweb.asm.ClassVisitor
-
visitNestHost
- Overrides:
visitNestHost
in classorg.objectweb.asm.ClassVisitor
-
visitNestMember
- Overrides:
visitNestMember
in classorg.objectweb.asm.ClassVisitor
-
visitPermittedSubclass
- Overrides:
visitPermittedSubclass
in classorg.objectweb.asm.ClassVisitor
-
visitOuterClass
- Overrides:
visitOuterClass
in classorg.objectweb.asm.ClassVisitor
-
visitInnerClass
- Overrides:
visitInnerClass
in classorg.objectweb.asm.ClassVisitor
-
visitRecordComponent
public org.objectweb.asm.RecordComponentVisitor visitRecordComponent(String name, String descriptor, String signature) - Overrides:
visitRecordComponent
in classorg.objectweb.asm.ClassVisitor
-
visitField
public org.objectweb.asm.FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) - Overrides:
visitField
in classorg.objectweb.asm.ClassVisitor
-
visitMethod
public org.objectweb.asm.MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) - Overrides:
visitMethod
in classorg.objectweb.asm.ClassVisitor
-
visitAnnotation
- Overrides:
visitAnnotation
in classorg.objectweb.asm.ClassVisitor
-
visitTypeAnnotation
public org.objectweb.asm.AnnotationVisitor visitTypeAnnotation(int typeRef, org.objectweb.asm.TypePath typePath, String descriptor, boolean visible) - Overrides:
visitTypeAnnotation
in classorg.objectweb.asm.ClassVisitor
-
visitAttribute
public void visitAttribute(org.objectweb.asm.Attribute attribute) - Overrides:
visitAttribute
in classorg.objectweb.asm.ClassVisitor
-
visitEnd
public void visitEnd()- Overrides:
visitEnd
in classorg.objectweb.asm.ClassVisitor
-
checkClassSignature
Checks a class signature.- Parameters:
signature
- a string containing the signature that must be checked.
-
checkMethodSignature
Checks a method signature.- Parameters:
signature
- a string containing the signature that must be checked.
-
checkFieldSignature
Checks a field signature.- Parameters:
signature
- a string containing the signature that must be checked.
-
main
Checks the given class.Usage: CheckClassAdapter <binary class name or class file name>
- Parameters:
args
- the command line arguments.- Throws:
IOException
- if the class cannot be found, or if an IO exception occurs.
-
verify
public static void verify(org.objectweb.asm.ClassReader classReader, boolean printResults, PrintWriter printWriter) Checks the given class.- Parameters:
classReader
- the class to be checked.printResults
- whether to print the results of the bytecode verification.printWriter
- where the results (or the stack trace in case of error) must be printed.
-
verify
public static void verify(org.objectweb.asm.ClassReader classReader, ClassLoader loader, boolean printResults, PrintWriter printWriter) Checks the given class.- Parameters:
classReader
- the class to be checked.loader
- aClassLoader
which will be used to load referenced classes. May be null.printResults
- whether to print the results of the bytecode verification.printWriter
- where the results (or the stack trace in case of error) must be printed.
-