Better Stream.toList conversion

This commit is contained in:
Josiah (Gaming32) Glosson 2023-05-13 07:33:55 -05:00
parent 384f8ca581
commit 1e1b8be79b

View File

@ -309,16 +309,25 @@ public class Java17ToJava8 implements IBytecodeTransformer {
final InsnList list = new InsnList(); final InsnList list = new InsnList();
if (min.name.equals("toList")) { if (min.name.equals("toList")) {
int freeVarIndex = ASMUtils.getFreeVarIndex(method); list.add(new MethodInsnNode(
list.add(new VarInsnNode(Opcodes.ASTORE, freeVarIndex)); Opcodes.INVOKESTATIC,
"java/util/stream/Collectors",
list.add(new TypeInsnNode(Opcodes.NEW, "java/util/ArrayList")); "toList",
list.add(new InsnNode(Opcodes.DUP)); "()Ljava/util/stream/Collector;"
list.add(new VarInsnNode(Opcodes.ALOAD, freeVarIndex)); ));
list.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/util/stream/Stream", "toArray", "()[Ljava/lang/Object;")); list.add(new MethodInsnNode(
list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Arrays", "asList", "([Ljava/lang/Object;)Ljava/util/List;")); Opcodes.INVOKEINTERFACE,
list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/util/ArrayList", "<init>", "(Ljava/util/Collection;)V")); "java/util/stream/Stream",
list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/util/Collections", "unmodifiableList", "(Ljava/util/List;)Ljava/util/List;")); "collect",
"(Ljava/util/stream/Collector;)Ljava/lang/Object;"
));
list.add(new TypeInsnNode(Opcodes.CHECKCAST, "java/util/List"));
list.add(new MethodInsnNode(
Opcodes.INVOKESTATIC,
"java/util/Collections",
"unmodifiableList",
"(Ljava/util/List;)Ljava/util/List;")
);
} }
if (list.size() != 0) { if (list.size() != 0) {