diff --git a/plugins/kotlin/src/main/java/org/vineflower/kotlin/struct/KConstructor.java b/plugins/kotlin/src/main/java/org/vineflower/kotlin/struct/KConstructor.java index 2f46285e81..7de917314a 100644 --- a/plugins/kotlin/src/main/java/org/vineflower/kotlin/struct/KConstructor.java +++ b/plugins/kotlin/src/main/java/org/vineflower/kotlin/struct/KConstructor.java @@ -30,6 +30,8 @@ import java.util.Map; public class KConstructor { + private static final VarType DEFAULT_CONSTRUCTOR_MARKER = new VarType("kotlin/jvm/internal/DefaultConstructorMarker", true); + public final ProtobufFlags.Constructor flags; public final KParameter[] parameters; public final boolean isPrimary; @@ -248,14 +250,7 @@ public boolean writePrimaryConstructor(TextBuffer buffer, int indent) { // replace "super" with the actual class name buf.append(superClass).append('('); - VarType[] params = invocation.getDescriptor().params; - if (params.length > 0 && params[0].value.equals("kotlin/jvm/internal/DefaultConstructorMarker")) { - // Remove the first parameter (the DefaultConstructorMarker is not meant to be used in user-facing code) - invocation.setStringDescriptor(invocation.getStringDescriptor().replace("Lkotlin/jvm/internal/DefaultConstructorMarker;", "")); - List args = new ArrayList<>(invocation.getLstParameters()); - args.remove(0); - invocation.setLstParameters(args); - } + KUtils.removeArguments(invocation, DEFAULT_CONSTRUCTOR_MARKER); buf.append(invocation.appendParamList(indent + 1)); buf.append(")"); diff --git a/plugins/kotlin/src/main/java/org/vineflower/kotlin/util/KUtils.java b/plugins/kotlin/src/main/java/org/vineflower/kotlin/util/KUtils.java index 2b5598913e..32ee2cbebd 100644 --- a/plugins/kotlin/src/main/java/org/vineflower/kotlin/util/KUtils.java +++ b/plugins/kotlin/src/main/java/org/vineflower/kotlin/util/KUtils.java @@ -2,6 +2,7 @@ import kotlin.reflect.jvm.internal.impl.metadata.ProtoBuf; import org.jetbrains.java.decompiler.modules.decompiler.exps.*; +import org.jetbrains.java.decompiler.struct.gen.VarType; import org.jetbrains.java.decompiler.util.TextBuffer; import org.vineflower.kotlin.KotlinPreferences; import org.vineflower.kotlin.expr.*; @@ -65,4 +66,45 @@ public static void appendVisibility(TextBuffer buf, ProtoBuf.Visibility visibili .append(' '); } } + + public static void removeArguments(InvocationExprent expr, VarType toRemove) { + removeArguments(expr, toRemove, null); + } + + public static void removeArguments(InvocationExprent expr, VarType toRemove, VarType replaceReturnType) { + if (expr.getLstParameters().isEmpty()) { + return; + } + + if (expr.getLstParameters().size() == 1) { + VarType argType = expr.getDescriptor().params[0]; + if (argType.equals(toRemove)) { + expr.getLstParameters().clear(); + } + return; + } + + VarType[] params = expr.getDescriptor().params; + List lst = expr.getLstParameters(); + for (int i = 0; i < params.length; i++) { + VarType argType = params[i]; + if (argType.equals(toRemove)) { + lst.set(i, null); + } + } + lst.removeIf(Objects::isNull); + + String newDesc = expr.getStringDescriptor() + .replace(toRemove.toString(), ""); + + if (newDesc.endsWith(")")) { + if (replaceReturnType == null) { + throw new IllegalStateException("Invalid descriptor: " + newDesc); + } + + newDesc += replaceReturnType.toString(); + } + + expr.setStringDescriptor(newDesc); + } }