Search in sources :

Example 1 with QuarkusClassVisitor

use of io.quarkus.deployment.QuarkusClassVisitor in project quarkus by quarkusio.

the class ClassTransformingBuildStep method transformClass.

private byte[] transformClass(String className, List<BiFunction<String, ClassVisitor, ClassVisitor>> visitors, byte[] classData, List<BiFunction<String, byte[], byte[]>> preVisitFunctions, int classReaderOptions) {
    for (BiFunction<String, byte[], byte[]> i : preVisitFunctions) {
        classData = i.apply(className, classData);
        if (classData == null) {
            return null;
        }
    }
    byte[] data;
    if (!visitors.isEmpty()) {
        ClassReader cr = new ClassReader(classData);
        ClassWriter writer = new QuarkusClassWriter(cr, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
        ClassVisitor visitor = writer;
        for (BiFunction<String, ClassVisitor, ClassVisitor> i : visitors) {
            visitor = i.apply(className, visitor);
            if (visitor instanceof QuarkusClassVisitor) {
                ((QuarkusClassVisitor) visitor).setOriginalClassReaderOptions(classReaderOptions);
            }
        }
        cr.accept(visitor, classReaderOptions);
        data = writer.toByteArray();
    } else {
        data = classData;
    }
    if (BootstrapDebug.DEBUG_TRANSFORMED_CLASSES_DIR != null) {
        File debugPath = new File(BootstrapDebug.DEBUG_TRANSFORMED_CLASSES_DIR);
        if (!debugPath.exists()) {
            debugPath.mkdir();
        }
        File classFile = new File(debugPath, className.replace('.', '/') + ".class");
        classFile.getParentFile().mkdirs();
        try (FileOutputStream classWriter = new FileOutputStream(classFile)) {
            classWriter.write(data);
        } catch (Exception e) {
            log.errorf(e, "Failed to write transformed class %s", className);
        }
        log.infof("Wrote transformed class to %s", classFile.getAbsolutePath());
    }
    return data;
}
Also used : FileOutputStream(java.io.FileOutputStream) ClassReader(org.objectweb.asm.ClassReader) QuarkusClassVisitor(io.quarkus.deployment.QuarkusClassVisitor) QuarkusClassWriter(io.quarkus.deployment.QuarkusClassWriter) QuarkusClassVisitor(io.quarkus.deployment.QuarkusClassVisitor) ClassVisitor(org.objectweb.asm.ClassVisitor) File(java.io.File) QuarkusClassWriter(io.quarkus.deployment.QuarkusClassWriter) ClassWriter(org.objectweb.asm.ClassWriter) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

QuarkusClassVisitor (io.quarkus.deployment.QuarkusClassVisitor)1 QuarkusClassWriter (io.quarkus.deployment.QuarkusClassWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 ExecutionException (java.util.concurrent.ExecutionException)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 ClassWriter (org.objectweb.asm.ClassWriter)1