diff --git a/src/org/jetbrains/java/decompiler/main/ClassWriter.java b/src/org/jetbrains/java/decompiler/main/ClassWriter.java index bb5790b..aa03fa5 100644 --- a/src/org/jetbrains/java/decompiler/main/ClassWriter.java +++ b/src/org/jetbrains/java/decompiler/main/ClassWriter.java @@ -262,6 +262,21 @@ public class ClassWriter { DecompilerContext.getLogger().endWriteClass(); } + private static boolean isSyntheticRecordMethod(StructMethod mt, TextBuffer code) { + if (mt.getClassStruct().getRecordComponents() == null) return false; + String name = mt.getName(); + String descriptor = mt.getDescriptor(); + if (name.equals("equals") && descriptor.equals("(Ljava/lang/Object;)Z") || + name.equals("hashCode") && descriptor.equals("()I") || + name.equals("toString") && descriptor.equals("()Ljava/lang/String;")) { + if (code.countLines() == 1) { + String str = code.toString().trim(); + return str.startsWith("return this." + name + "(this"); + } + } + return false; + } + private static void addTracer(StructClass cls, StructMethod method, BytecodeMappingTracer tracer) { StructLineNumberTableAttribute table = method.getAttribute(StructGeneralAttribute.ATTRIBUTE_LINE_NUMBER_TABLE); tracer.setLineNumberTable(table); @@ -880,7 +895,8 @@ public class ClassWriter { BytecodeMappingTracer codeTracer = new BytecodeMappingTracer(tracer.getCurrentSourceLine()); TextBuffer code = root.toJava(indent + 1, codeTracer); - hideMethod = (code.length() == 0) && (clinit || dinit || hideConstructor(node, init, throwsExceptions, paramCount, flags)); + hideMethod = (code.length() == 0) && (clinit || dinit || hideConstructor(node, init, throwsExceptions, paramCount, flags)) + || isSyntheticRecordMethod(mt, code); buffer.append(code); diff --git a/testData/classes/records/TestRecordEmpty.class b/testData/classes/records/TestRecordEmpty.class index b4b6080..027b700 100644 Binary files a/testData/classes/records/TestRecordEmpty.class and b/testData/classes/records/TestRecordEmpty.class differ diff --git a/testData/results/TestRecordAnno.dec b/testData/results/TestRecordAnno.dec index 1df3349..931c3e6 100644 --- a/testData/results/TestRecordAnno.dec +++ b/testData/results/TestRecordAnno.dec @@ -6,18 +6,6 @@ public record TestRecordAnno(@RC @TA int x, int y) { this.y = y; } - public final String toString() { - return this.toString(this); - } - - public final int hashCode() { - return this.hashCode(this); - } - - public final boolean equals(Object o) { - return this.equals(this, o); - } - @TA public int x() { return this.x; @@ -36,31 +24,16 @@ class 'records/TestRecordAnno' { e 6 } - method 'toString ()Ljava/lang/String;' { - 1 9 - 6 9 - } - - method 'hashCode ()I' { - 1 13 - 6 13 - } - - method 'equals (Ljava/lang/Object;)Z' { - 2 17 - 7 17 - } - method 'x ()I' { - 1 22 - 4 22 + 1 10 + 4 10 } method 'y ()I' { - 1 27 - 4 27 + 1 15 + 4 15 } } Lines mapping: -5 <-> 28 +5 <-> 16 diff --git a/testData/results/TestRecordEmpty.dec b/testData/results/TestRecordEmpty.dec index 6111ddf..9ac6460 100644 --- a/testData/results/TestRecordEmpty.dec +++ b/testData/results/TestRecordEmpty.dec @@ -1,35 +1,17 @@ package records; public record TestRecordEmpty() { - public final String toString() { - return this.toString(this); - } - - public final int hashCode() { - return this.hashCode(this); - } - - public final boolean equals(Object o) { - return this.equals(this, o);// 3 + public int hashCode() { + return 0;// 5 } } class 'records/TestRecordEmpty' { - method 'toString ()Ljava/lang/String;' { - 1 4 - 6 4 - } - method 'hashCode ()I' { - 1 8 - 6 8 - } - - method 'equals (Ljava/lang/Object;)Z' { - 2 12 - 7 12 + 0 4 + 1 4 } } Lines mapping: -3 <-> 13 +5 <-> 5 diff --git a/testData/results/TestRecordGenericVararg.dec b/testData/results/TestRecordGenericVararg.dec index 1a52a25..1d417b4 100644 --- a/testData/results/TestRecordGenericVararg.dec +++ b/testData/results/TestRecordGenericVararg.dec @@ -7,18 +7,6 @@ public record TestRecordGenericVararg(T first, T... other) { this.other = other; } - public final String toString() { - return this.toString(this); - } - - public final int hashCode() { - return this.hashCode(this); - } - - public final boolean equals(Object o) { - return this.equals(this, o); - } - public T first() { return this.first; } @@ -35,32 +23,17 @@ class 'records/TestRecordGenericVararg' { e 7 } - method 'toString ()Ljava/lang/String;' { - 1 10 - 6 10 - } - - method 'hashCode ()I' { - 1 14 - 6 14 - } - - method 'equals (Ljava/lang/Object;)Z' { - 2 18 - 7 18 - } - method 'first ()Ljava/lang/Object;' { - 1 22 - 4 22 + 1 10 + 4 10 } method 'other ()[Ljava/lang/Object;' { - 1 26 - 4 26 + 1 14 + 4 14 } } Lines mapping: -3 <-> 27 +3 <-> 15 5 <-> 6 diff --git a/testData/results/TestRecordSimple.dec b/testData/results/TestRecordSimple.dec index fd1bf45..222805f 100644 --- a/testData/results/TestRecordSimple.dec +++ b/testData/results/TestRecordSimple.dec @@ -6,18 +6,6 @@ public record TestRecordSimple(int x, int y) { this.y = y; } - public final String toString() { - return this.toString(this); - } - - public final int hashCode() { - return this.hashCode(this); - } - - public final boolean equals(Object o) { - return this.equals(this, o); - } - public int x() { return this.x; } @@ -34,31 +22,16 @@ class 'records/TestRecordSimple' { e 6 } - method 'toString ()Ljava/lang/String;' { - 1 9 - 6 9 - } - - method 'hashCode ()I' { - 1 13 - 6 13 - } - - method 'equals (Ljava/lang/Object;)Z' { - 2 17 - 7 17 - } - method 'x ()I' { - 1 21 - 4 21 + 1 9 + 4 9 } method 'y ()I' { - 1 25 - 4 25 + 1 13 + 4 13 } } Lines mapping: -3 <-> 26 +3 <-> 14 diff --git a/testData/results/TestRecordVararg.dec b/testData/results/TestRecordVararg.dec index d0f4b66..850ed38 100644 --- a/testData/results/TestRecordVararg.dec +++ b/testData/results/TestRecordVararg.dec @@ -6,18 +6,6 @@ public record TestRecordVararg(int x, int[]... y) { this.y = y; } - public final String toString() { - return this.toString(this); - } - - public final int hashCode() { - return this.hashCode(this); - } - - public final boolean equals(Object o) { - return this.equals(this, o); - } - public int x() { return this.x; } @@ -34,31 +22,16 @@ class 'records/TestRecordVararg' { e 6 } - method 'toString ()Ljava/lang/String;' { - 1 9 - 6 9 - } - - method 'hashCode ()I' { - 1 13 - 6 13 - } - - method 'equals (Ljava/lang/Object;)Z' { - 2 17 - 7 17 - } - method 'x ()I' { - 1 21 - 4 21 + 1 9 + 4 9 } method 'y ()[[I' { - 1 25 - 4 25 + 1 13 + 4 13 } } Lines mapping: -3 <-> 26 +3 <-> 14 diff --git a/testData/src/records/TestRecordEmpty.java b/testData/src/records/TestRecordEmpty.java index 321165a..f478efa 100644 --- a/testData/src/records/TestRecordEmpty.java +++ b/testData/src/records/TestRecordEmpty.java @@ -1,3 +1,7 @@ package records; -public record TestRecordEmpty() {} \ No newline at end of file +public record TestRecordEmpty() { + public int hashCode() { + return 0; + } +} \ No newline at end of file