Skip to content

Commit 4fe9bdf

Browse files
authored
Make record canonical constructors that can be compact compact (#111)
* Move throw clause builder into if statement so that the hasDescriptor variable can be moved back to where it was and compact constructor's can't use the throws clause anyway
1 parent f8b2d63 commit 4fe9bdf

3 files changed

Lines changed: 108 additions & 54 deletions
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: LexManos <LexManos@gmail.com>
3+
Date: Thu, 11 Nov 2021 23:44:28 -0800
4+
Subject: [PATCH] Filter out generated Record getters and constructor. Make
5+
canonical constructors that can be compact compact
6+
7+
8+
diff --git a/src/org/jetbrains/java/decompiler/main/ClassWriter.java b/src/org/jetbrains/java/decompiler/main/ClassWriter.java
9+
index 43199af10fde2770ee14b36ba5bbae23ebb5e2cc..f9b40d707e880c27e3d3d29ef9cc0ca224db5ed3 100644
10+
--- a/src/org/jetbrains/java/decompiler/main/ClassWriter.java
11+
+++ b/src/org/jetbrains/java/decompiler/main/ClassWriter.java
12+
@@ -330,6 +330,18 @@ public class ClassWriter {
13+
return str.startsWith("return this." + name + "<invokedynamic>(this");
14+
}
15+
}
16+
+
17+
+ // Default getters
18+
+ for (StructRecordComponent rec : cl.getRecordComponents()) {
19+
+ if (name.equals(rec.getName()) && descriptor.equals("()" + rec.getDescriptor())) {
20+
+ if (code.countLines() == 1) {
21+
+ String str = code.toString().trim();
22+
+ return str.startsWith("return this." + mt.getName() + ';');
23+
+ } else {
24+
+ return false;
25+
+ }
26+
+ }
27+
+ }
28+
}
29+
return false;
30+
}
31+
@@ -781,7 +793,7 @@ public class ClassWriter {
32+
boolean isAnnotation = cl.hasModifier(CodeConstants.ACC_ANNOTATION);
33+
boolean isEnum = cl.hasModifier(CodeConstants.ACC_ENUM) && DecompilerContext.getOption(IFernflowerPreferences.DECOMPILE_ENUM);
34+
boolean isDeprecated = mt.hasAttribute(StructGeneralAttribute.ATTRIBUTE_DEPRECATED);
35+
- boolean clInit = false, init = false, dInit = false;
36+
+ boolean clInit = false, init = false, dInit = false, compact = false;
37+
38+
MethodDescriptor md = MethodDescriptor.parseDescriptor(mt, node);
39+
40+
@@ -832,6 +844,41 @@ public class ClassWriter {
41+
name = node.simpleName;
42+
init = true;
43+
}
44+
+
45+
+ if (cl.getRecordComponents() != null) {
46+
+ StringBuilder buf = new StringBuilder("(");
47+
+ for (StructRecordComponent rec : cl.getRecordComponents()) {
48+
+ buf.append(rec.getDescriptor());
49+
+ }
50+
+ String desc = buf.append(")V").toString();
51+
+ if (desc.equals(mt.getDescriptor())) {
52+
+ boolean[] found = new boolean[1];
53+
+ compact = methodWrapper.getOrBuildGraph().iterateExprents((exprent) -> {
54+
+ if (exprent.type == Exprent.EXPRENT_ASSIGNMENT) {
55+
+ AssignmentExprent assignment = (AssignmentExprent) exprent;
56+
+ if (assignment.getLeft().type == Exprent.EXPRENT_FIELD && assignment.getRight().type != Exprent.EXPRENT_VAR) {
57+
+ return 1;
58+
+ } else if (assignment.getLeft().type == Exprent.EXPRENT_FIELD) {
59+
+ found[0] = true;
60+
+ return 0;
61+
+ }
62+
+ }
63+
+ return found[0] ? 1 : 0;
64+
+ });
65+
+ if (compact) {
66+
+ methodWrapper.getOrBuildGraph().iterateExprents((exprent) -> {
67+
+ if (exprent.type == Exprent.EXPRENT_ASSIGNMENT) {
68+
+ AssignmentExprent assignment = (AssignmentExprent) exprent;
69+
+ if (assignment.getLeft().type == Exprent.EXPRENT_FIELD) {
70+
+ return 2;
71+
+ }
72+
+ }
73+
+ return 0;
74+
+ });
75+
+ hideMethod = methodWrapper.getOrBuildGraph().iterateExprents((exprent) -> 1);
76+
+ }
77+
+ }
78+
+ }
79+
}
80+
else if (CodeConstants.CLINIT_NAME.equals(name)) {
81+
name = "";
82+
@@ -856,6 +903,7 @@ public class ClassWriter {
83+
}
84+
85+
buffer.append(toValidJavaIdentifier(name));
86+
+ if (!compact) {
87+
buffer.append('(');
88+
89+
List<VarVersionPair> mask = methodWrapper.synthParameters;
90+
@@ -954,6 +1002,7 @@ public class ClassWriter {
91+
buffer.append(ExprProcessor.getCastTypeName(type));
92+
}
93+
}
94+
+ }
95+
}
96+
97+
tracer.incrementCurrentSourceLine(buffer.countLines(start_index_method));
98+
@@ -987,7 +1036,7 @@ public class ClassWriter {
99+
BytecodeMappingTracer codeTracer = new BytecodeMappingTracer(tracer.getCurrentSourceLine());
100+
TextBuffer code = root.toJava(indent + 1, codeTracer);
101+
102+
- hideMethod = code.length() == 0 && (clInit || dInit || hideConstructor(node, init, throwsExceptions, paramCount, flags)) ||
103+
+ hideMethod |= code.length() == 0 && (clInit || dInit || hideConstructor(node, init, throwsExceptions, paramCount, flags)) ||
104+
isSyntheticRecordMethod(cl, mt, code);
105+
106+
buffer.append(code);

FernFlower-Patches/0044-Filter-out-generated-Record-getters-and-constructor.patch

Lines changed: 0 additions & 52 deletions
This file was deleted.

FernFlower-Patches/0045-Remove-default-keyword-from-private-interface-method.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ Subject: [PATCH] Remove default keyword from private interface methods
55

66

77
diff --git a/src/org/jetbrains/java/decompiler/main/ClassWriter.java b/src/org/jetbrains/java/decompiler/main/ClassWriter.java
8-
index c35fbd0979b4181f3e50b27982a119d56a6df982..b3ad82a86e844f49f3e4f5003eb3f8350f249aa6 100644
8+
index f9b40d707e880c27e3d3d29ef9cc0ca224db5ed3..64e0635a9835a153ce1a300146d6e40db624c63a 100644
99
--- a/src/org/jetbrains/java/decompiler/main/ClassWriter.java
1010
+++ b/src/org/jetbrains/java/decompiler/main/ClassWriter.java
11-
@@ -852,7 +852,7 @@ public class ClassWriter {
11+
@@ -829,7 +829,7 @@ public class ClassWriter {
1212

1313
appendModifiers(buffer, flags, METHOD_ALLOWED, isInterface, METHOD_EXCLUDED);
1414

0 commit comments

Comments
 (0)