Java Code Examples for org.objectweb.asm.util.CheckClassAdapter#verify()

The following examples show how to use org.objectweb.asm.util.CheckClassAdapter#verify() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ASMBytecodeDisassembler.java    From pinpoint with Apache License 2.0 6 votes vote down vote up
public String dumpVerify(byte[] bytecode, ClassLoader classLoader) {
    if (bytecode == null) {
        throw new NullPointerException("bytecode");
    }
    if (classLoader == null) {
        throw new NullPointerException("classLoader");
    }

    final StringWriter out = new StringWriter();
    final PrintWriter writer = new PrintWriter(out);

    final ClassReader cr = new ClassReader(bytecode);
    CheckClassAdapter.verify(cr, classLoader, true, writer);

    return out.toString();
}
 
Example 2
Source File: DrillCheckClassAdapter.java    From Bats with Apache License 2.0 5 votes vote down vote up
/**
 * See {@link org.objectweb.asm.util.CheckClassAdapter#verify(ClassReader, boolean, PrintWriter)}.
 */
public static void verify(final ClassReader cr, final boolean dump,
    final PrintWriter pw) {
  /*
   * For plain verification, we don't need to restore the original access
   * bytes the way we do when the check adapter is used as part of a chain, so
   * we can just strip it and use the ASM version directly.
   */
  final ClassWriter classWriter = new ClassWriter(0);
  cr.accept(new InnerClassAccessStripper(CompilationConfig.ASM_API_VERSION,
      classWriter), ClassReader.SKIP_DEBUG);
  final ClassReader strippedCr = new ClassReader(classWriter.toByteArray());
  CheckClassAdapter.verify(strippedCr, dump, pw);
}
 
Example 3
Source File: CoverageTransformerTest.java    From QuickTheories with Apache License 2.0 5 votes vote down vote up
private byte[] assertValidClass(final Class<?> clazz)
    throws IllegalClassFormatException {
  final byte[] bs = transform(clazz);
  // printClass(bs);
  final StringWriter sw = new StringWriter();
  CheckClassAdapter.verify(new ClassReader(bs), false, new PrintWriter(sw));
  assertTrue(sw.toString(), sw.toString().length() == 0);
  return bs;

}
 
Example 4
Source File: MutatorTestBase.java    From pitest with Apache License 2.0 5 votes vote down vote up
private void verifyMutant(final Mutant mutant) {
  // printMutant(mutant);
  final StringWriter sw = new StringWriter();
  final PrintWriter pw = new PrintWriter(sw);
  CheckClassAdapter.verify(new ClassReader(mutant.getBytes()), false, pw);
  assertTrue(sw.toString(), sw.toString().length() == 0);

}
 
Example 5
Source File: CoverageTransformerTest.java    From pitest with Apache License 2.0 5 votes vote down vote up
private void assertValidClass(final Class<?> clazz)
    throws IllegalClassFormatException {
  final byte[] bs = transform(clazz);
  // printClass(bs);
  final StringWriter sw = new StringWriter();
  CheckClassAdapter.verify(new ClassReader(bs), false, new PrintWriter(sw));
  assertTrue(sw.toString(), sw.toString().length() == 0);

}
 
Example 6
Source File: AbstractWorkflowRepository.java    From copper-engine with Apache License 2.0 4 votes vote down vote up
protected void instrumentWorkflows(File adaptedTargetDir, Map<String, Clazz> clazzMap, Map<String, ClassInfo> classInfos, ClassLoader tmpClassLoader) throws IOException {
    logger.info("Instrumenting classfiles");
    for (Clazz clazz : clazzMap.values()) {
        byte[] bytes;
        InputStream is = clazz.classfile.openStream();
        try {
            ClassReader cr2 = new ClassReader(is);
            ClassNode cn = new ClassNode();
            cr2.accept(cn, flags);
            traceClassNode(clazz.classname + " - original", cn);

            // Now content of ClassNode can be modified and then serialized back into bytecode:
            new TryCatchBlockHandler().instrument(cn);

            ClassWriter cw2 = new ClassWriter(0);
            cn.accept(cw2);
            bytes = cw2.toByteArray();
            traceBytes(clazz.classname + " - after TryCatchBlockHandler", bytes);

            ClassReader cr = new ClassReader(bytes);
            ClassWriter cw = new ClassWriter(0);

            ScottyClassAdapter cv = new ScottyClassAdapter(cw, clazz.aggregatedInterruptableMethods);
            cr.accept(cv, flags);
            classInfos.put(clazz.classname, cv.getClassInfo());
            bytes = cw.toByteArray();
            traceBytes(clazz.classname + " - after ScottyClassAdapter", bytes);

            // Recompute frames, etc.
            ClassReader cr3 = new ClassReader(bytes);
            ClassWriter cw3 = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
            cr3.accept(cw3, ClassReader.SKIP_FRAMES);
            bytes = cw3.toByteArray();
            traceBytes(clazz.classname + " - after COMPUTE_FRAMES", bytes);

            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            CheckClassAdapter.verify(new ClassReader(cw.toByteArray()), tmpClassLoader, false, pw);
            if (sw.toString().length() != 0) {
                logger.error("CheckClassAdapter.verify failed for class " + cn.name + ":\n" + sw.toString());
            } else {
                logger.info("CheckClassAdapter.verify succeeded for class " + cn.name);
            }

        } finally {
            is.close();
        }

        File adaptedClassfileName = new File(adaptedTargetDir, clazz.classname + ".class");
        adaptedClassfileName.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(adaptedClassfileName);
        try {
            fos.write(bytes);
        } finally {
            fos.close();
        }
    }
}
 
Example 7
Source File: Bytecode.java    From Mixin with MIT License 2 votes vote down vote up
/**
 * Dumps the output of CheckClassAdapter.verify to System.out
 *
 * @param bytes the bytecode of the class to check
 */
public static void dumpClass(byte[] bytes) {
    ClassReader cr = new ClassReader(bytes);
    CheckClassAdapter.verify(cr, true, new PrintWriter(System.out));
}