Skip to content

Commit

Permalink
Don't rewrite classes when no comments are inserted into the class ev…
Browse files Browse the repository at this point in the history
…en if a comment container exists
  • Loading branch information
Col-E committed Jan 19, 2025
1 parent e267dab commit 442320a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class CommentInsertingVisitor extends ClassVisitor {
private final ClassComments comments;
private final ClassPathNode classPath;
private int insertions;

/**
* @param comments
Expand All @@ -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);
Expand All @@ -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++;
}
}

Expand All @@ -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++;
}
}

Expand All @@ -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++;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 442320a

Please sign in to comment.