Skip to content

Commit

Permalink
Fix @OverRide annotations emitting twice (#330)
Browse files Browse the repository at this point in the history
* fix override annotations emitting twice

* don't look for @OverRide in every method
  • Loading branch information
mudkipdev authored Jan 21, 2024
1 parent e65c0f6 commit a3de77e
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/org/jetbrains/java/decompiler/main/ClassWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1072,13 +1072,24 @@ public boolean writeMethod(ClassNode node, StructMethod mt, int methodIndex, Tex

appendAnnotations(buffer, indent, mt, TypeAnnotation.METHOD_RETURN_TYPE);

StructAnnotationAttribute annotationAttribute = mt.getAttribute(StructGeneralAttribute.ATTRIBUTE_RUNTIME_VISIBLE_ANNOTATIONS);
boolean shouldApplyOverride = DecompilerContext.getOption(IFernflowerPreferences.OVERRIDE_ANNOTATION)
&& mt.getBytecodeVersion().hasOverride()
&& !CodeConstants.INIT_NAME.equals(mt.getName())
&& !CodeConstants.CLINIT_NAME.equals(mt.getName())
&& !mt.hasModifier(CodeConstants.ACC_STATIC)
&& !mt.hasModifier(CodeConstants.ACC_PRIVATE);

// Try append @Override after all other annotations
if (DecompilerContext.getOption(IFernflowerPreferences.OVERRIDE_ANNOTATION) && mt.getBytecodeVersion().hasOverride() && !CodeConstants.INIT_NAME.equals(mt.getName()) && !CodeConstants.CLINIT_NAME.equals(mt.getName()) && !mt.hasModifier(CodeConstants.ACC_STATIC) && !mt.hasModifier(CodeConstants.ACC_PRIVATE)) {
if (shouldApplyOverride) {
// Search superclasses for methods that match the name and descriptor of this one.
// Make sure not to search the current class otherwise it will return the current method itself!
// TODO: record overrides
boolean isOverride = searchForMethod(cl, mt.getName(), md, false);
if (isOverride) {
boolean alreadyHasOverride = annotationAttribute != null && annotationAttribute.getAnnotations()
.stream().anyMatch(annotation -> "java/lang/Override".equals(annotation.getClassName()));

if (isOverride && !alreadyHasOverride) {
buffer.appendIndent(indent);
buffer.append("@Override");
buffer.appendLineSeparator();
Expand Down Expand Up @@ -1599,7 +1610,7 @@ public static List<String> getErrorComment() {
private static void appendComment(TextBuffer buffer, String comment, int indent) {
buffer.appendIndent(indent).append("// $VF: ").append(comment).appendLineSeparator();
}

private static void appendJavadoc(TextBuffer buffer, String javaDoc, int indent) {
if (javaDoc == null) return;
buffer.appendIndent(indent).append("/**").appendLineSeparator();
Expand Down

0 comments on commit a3de77e

Please sign in to comment.