|
| 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); |
0 commit comments