Skip to content

Commit

Permalink
Generify InvocationExprent parameter removal
Browse files Browse the repository at this point in the history
  • Loading branch information
sschr15 committed Sep 7, 2023
1 parent 7237f9d commit e9767c2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Exprent> 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(")");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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<Exprent> 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);
}
}

0 comments on commit e9767c2

Please sign in to comment.