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;
}
Aggregations