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
ConstructorsModifierConstructorDescriptionprotectedCheckClassAdapter(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 voidcheckClassSignature(String signature) Checks a class signature.static voidcheckFieldSignature(String signature) Checks a field signature.static voidcheckMethodSignature(String signature) Checks a method signature.static voidChecks the given class.static voidverify(org.objectweb.asm.ClassReader classReader, boolean printResults, PrintWriter printWriter) Checks the given class.static voidverify(org.objectweb.asm.ClassReader classReader, ClassLoader loader, boolean printResults, PrintWriter printWriter) Checks the given class.voidvisit(int version, int access, String name, String signature, String superName, String[] interfaces) org.objectweb.asm.AnnotationVisitorvisitAnnotation(String descriptor, boolean visible) voidvisitAttribute(org.objectweb.asm.Attribute attribute) voidvisitEnd()org.objectweb.asm.FieldVisitorvisitField(int access, String name, String descriptor, String signature, Object value) voidvisitInnerClass(String name, String outerName, String innerName, int access) org.objectweb.asm.MethodVisitorvisitMethod(int access, String name, String descriptor, String signature, String[] exceptions) org.objectweb.asm.ModuleVisitorvisitModule(String name, int access, String version) voidvisitNestHost(String nestHost) voidvisitNestMember(String nestMember) voidvisitOuterClass(String owner, String name, String descriptor) voidvisitPermittedSubclass(String permittedSubclass) org.objectweb.asm.RecordComponentVisitorvisitRecordComponent(String name, String descriptor, String signature) voidvisitSource(String file, String debug) org.objectweb.asm.AnnotationVisitorvisitTypeAnnotation(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 theASMx 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:
visitin classorg.objectweb.asm.ClassVisitor
-
visitSource
- Overrides:
visitSourcein classorg.objectweb.asm.ClassVisitor
-
visitModule
- Overrides:
visitModulein classorg.objectweb.asm.ClassVisitor
-
visitNestHost
- Overrides:
visitNestHostin classorg.objectweb.asm.ClassVisitor
-
visitNestMember
- Overrides:
visitNestMemberin classorg.objectweb.asm.ClassVisitor
-
visitPermittedSubclass
- Overrides:
visitPermittedSubclassin classorg.objectweb.asm.ClassVisitor
-
visitOuterClass
- Overrides:
visitOuterClassin classorg.objectweb.asm.ClassVisitor
-
visitInnerClass
- Overrides:
visitInnerClassin classorg.objectweb.asm.ClassVisitor
-
visitRecordComponent
public org.objectweb.asm.RecordComponentVisitor visitRecordComponent(String name, String descriptor, String signature) - Overrides:
visitRecordComponentin classorg.objectweb.asm.ClassVisitor
-
visitField
public org.objectweb.asm.FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) - Overrides:
visitFieldin classorg.objectweb.asm.ClassVisitor
-
visitMethod
public org.objectweb.asm.MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) - Overrides:
visitMethodin classorg.objectweb.asm.ClassVisitor
-
visitAnnotation
- Overrides:
visitAnnotationin classorg.objectweb.asm.ClassVisitor
-
visitTypeAnnotation
public org.objectweb.asm.AnnotationVisitor visitTypeAnnotation(int typeRef, org.objectweb.asm.TypePath typePath, String descriptor, boolean visible) - Overrides:
visitTypeAnnotationin classorg.objectweb.asm.ClassVisitor
-
visitAttribute
public void visitAttribute(org.objectweb.asm.Attribute attribute) - Overrides:
visitAttributein classorg.objectweb.asm.ClassVisitor
-
visitEnd
public void visitEnd()- Overrides:
visitEndin 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- aClassLoaderwhich 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.
-