Class CheckClassAdapter

java.lang.Object
org.objectweb.asm.ClassVisitor
org.objectweb.asm.util.CheckClassAdapter

public class CheckClassAdapter extends org.objectweb.asm.ClassVisitor
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.

  • Constructor Details

    • CheckClassAdapter

      public CheckClassAdapter(org.objectweb.asm.ClassVisitor classVisitor)
      Constructs a new CheckClassAdapter. Subclasses must not use this constructor. Instead, they must use the CheckClassAdapter(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 new CheckClassAdapter. Subclasses must not use this constructor. Instead, they must use the CheckClassAdapter(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 new CheckClassAdapter.
      Parameters:
      api - the ASM API version implemented by this visitor. Must be one of the ASMx values in Opcodes.
      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 (see CheckMethodAdapter).
  • Method Details

    • visit

      public void visit(int version, int access, String name, String signature, String superName, String[] interfaces)
      Overrides:
      visit in class org.objectweb.asm.ClassVisitor
    • visitSource

      public void visitSource(String file, String debug)
      Overrides:
      visitSource in class org.objectweb.asm.ClassVisitor
    • visitModule

      public org.objectweb.asm.ModuleVisitor visitModule(String name, int access, String version)
      Overrides:
      visitModule in class org.objectweb.asm.ClassVisitor
    • visitNestHost

      public void visitNestHost(String nestHost)
      Overrides:
      visitNestHost in class org.objectweb.asm.ClassVisitor
    • visitNestMember

      public void visitNestMember(String nestMember)
      Overrides:
      visitNestMember in class org.objectweb.asm.ClassVisitor
    • visitPermittedSubclass

      public void visitPermittedSubclass(String permittedSubclass)
      Overrides:
      visitPermittedSubclass in class org.objectweb.asm.ClassVisitor
    • visitOuterClass

      public void visitOuterClass(String owner, String name, String descriptor)
      Overrides:
      visitOuterClass in class org.objectweb.asm.ClassVisitor
    • visitInnerClass

      public void visitInnerClass(String name, String outerName, String innerName, int access)
      Overrides:
      visitInnerClass in class org.objectweb.asm.ClassVisitor
    • visitRecordComponent

      public org.objectweb.asm.RecordComponentVisitor visitRecordComponent(String name, String descriptor, String signature)
      Overrides:
      visitRecordComponent in class org.objectweb.asm.ClassVisitor
    • visitField

      public org.objectweb.asm.FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value)
      Overrides:
      visitField in class org.objectweb.asm.ClassVisitor
    • visitMethod

      public org.objectweb.asm.MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions)
      Overrides:
      visitMethod in class org.objectweb.asm.ClassVisitor
    • visitAnnotation

      public org.objectweb.asm.AnnotationVisitor visitAnnotation(String descriptor, boolean visible)
      Overrides:
      visitAnnotation in class org.objectweb.asm.ClassVisitor
    • visitTypeAnnotation

      public org.objectweb.asm.AnnotationVisitor visitTypeAnnotation(int typeRef, org.objectweb.asm.TypePath typePath, String descriptor, boolean visible)
      Overrides:
      visitTypeAnnotation in class org.objectweb.asm.ClassVisitor
    • visitAttribute

      public void visitAttribute(org.objectweb.asm.Attribute attribute)
      Overrides:
      visitAttribute in class org.objectweb.asm.ClassVisitor
    • visitEnd

      public void visitEnd()
      Overrides:
      visitEnd in class org.objectweb.asm.ClassVisitor
    • checkClassSignature

      public static void checkClassSignature(String signature)
      Checks a class signature.
      Parameters:
      signature - a string containing the signature that must be checked.
    • checkMethodSignature

      public static void checkMethodSignature(String signature)
      Checks a method signature.
      Parameters:
      signature - a string containing the signature that must be checked.
    • checkFieldSignature

      public static void checkFieldSignature(String signature)
      Checks a field signature.
      Parameters:
      signature - a string containing the signature that must be checked.
    • main

      public static void main(String[] args) throws IOException
      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 - a ClassLoader 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.