From f61e659e58c650330fae5fb28f6336b8b1b82d09 Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Fri, 7 Aug 2020 17:03:22 +0700 Subject: [PATCH] [java-stubs] Hide synthetic equals/hashCode/toString in records (stubs+decompiler) Makes IDEA-247576 obsolete Review ID: IJ-CR-2597 GitOrigin-RevId: 4dbb09153b683f2c191d8ba89a3c4ad8c3da038d --- .../java/decompiler/main/ClassWriter.java | 18 ++++++++- .../classes/records/TestRecordEmpty.class | Bin 1173 -> 1127 bytes testData/results/TestRecordAnno.dec | 37 +++--------------- testData/results/TestRecordEmpty.dec | 28 +++---------- testData/results/TestRecordGenericVararg.dec | 37 +++--------------- testData/results/TestRecordSimple.dec | 37 +++--------------- testData/results/TestRecordVararg.dec | 37 +++--------------- testData/src/records/TestRecordEmpty.java | 6 ++- 8 files changed, 47 insertions(+), 153 deletions(-) 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 b4b6080c5a20189d025441b7b599832b9dc11353..027b70022032892456f63f405b798d8f6b5dbf3d 100644 GIT binary patch delta 445 zcmZ9HOHPAO7=_Q}Ar#SCt!RDVy8=~-Q>&wnNSwI>5=pD6n3Tq8bqPq^g##U#Fz5nY zhly)&p!L7Nfb&DTZn(|nrhFFdY$|0fI+lu zkD+eam$&|d@Amz$>s}29H(|G(Jh9>Yj)qfMy~JUxvj12Hgw8V9@FNQ4~Z9 zTFI~ANXHE3vuGxssE?`;z>h>>88f6cnp|??Av+&n?{pEcNXnl95lf_c9zY$-G%*-h zA?5H9InN2=E7uY!mq;NaM&P{m210B=)#xmhWiYnVf$z|X0TucJDx6tlo| rf6g0uCgU$C4Hauxr!ORIWQChBGY7WFHG0`5q>4k>#q7|MCusZwteG%L delta 430 zcmZvXJx{`L5XOK1wpb`dEmg3B@1nejZ>uB@OpI=>B$6;FCe|HC*QW7XI5;!7x%d&B zP5c(tYay5@%RP6O=XX!;EB%zQ-oHn$QN$Uif%Rag`(3}^zP;)x%E3h`+sOlqHV*mV zv89%Rx0|r7iiTdTb;3(^Kfh~HVb5gWrb;dRP@i5yfx_+#2EDuXb=UWMzR97Y66Umu zZgLz(TGMsWxMd1{+D>CR>TZ Vwy1@t*b&5~&Ox$JG&z!Ay&s+qEE)g+ 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