diff --git a/recaf-core/src/main/java/software/coley/recaf/services/comment/CommentInsertingVisitor.java b/recaf-core/src/main/java/software/coley/recaf/services/comment/CommentInsertingVisitor.java index ac1cf289d..2b85b5d23 100644 --- a/recaf-core/src/main/java/software/coley/recaf/services/comment/CommentInsertingVisitor.java +++ b/recaf-core/src/main/java/software/coley/recaf/services/comment/CommentInsertingVisitor.java @@ -18,6 +18,7 @@ public class CommentInsertingVisitor extends ClassVisitor { private final ClassComments comments; private final ClassPathNode classPath; + private int insertions; /** * @param comments @@ -33,6 +34,13 @@ public CommentInsertingVisitor(@Nonnull ClassComments comments, @Nonnull ClassPa this.classPath = classPath; } + /** + * @return Number of inserted comment annotations. + */ + public int getInsertions() { + return insertions; + } + @Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { super.visit(version, access, name, signature, superName, interfaces); @@ -42,6 +50,7 @@ public void visit(int version, int access, String name, String signature, String if (comment != null) { CommentKey key = CommentKey.id(classPath); visitAnnotation(key.annotationDescriptor(), true); + insertions++; } } @@ -56,6 +65,7 @@ public FieldVisitor visitField(int access, String name, String descriptor, Strin if (field != null) { CommentKey key = CommentKey.id(classPath.child(field)); fv.visitAnnotation(key.annotationDescriptor(), true); + insertions++; } } @@ -73,6 +83,7 @@ public MethodVisitor visitMethod(int access, String name, String descriptor, Str if (method != null) { CommentKey key = CommentKey.id(classPath.child(method)); mv.visitAnnotation(key.annotationDescriptor(), true); + insertions++; } } diff --git a/recaf-core/src/main/java/software/coley/recaf/services/comment/CommentManager.java b/recaf-core/src/main/java/software/coley/recaf/services/comment/CommentManager.java index 90ef4b54e..44960ed44 100644 --- a/recaf-core/src/main/java/software/coley/recaf/services/comment/CommentManager.java +++ b/recaf-core/src/main/java/software/coley/recaf/services/comment/CommentManager.java @@ -98,8 +98,11 @@ public byte[] filter(@Nonnull Workspace workspace, @Nonnull JvmClassInfo initial // Adapt with comment annotations. ClassWriter writer = new ClassWriter(0); ClassReader reader = new ClassReader(bytecode); - reader.accept(new CommentInsertingVisitor(classComments, classPath, writer), 0); - return writer.toByteArray(); + CommentInsertingVisitor inserter = new CommentInsertingVisitor(classComments, classPath, writer); + reader.accept(inserter, 0); + if (inserter.getInsertions() > 0) + return writer.toByteArray(); + return bytecode; } }; OutputTextFilter keyReplacementFilter = new OutputTextFilter() {