Skip to content
This repository was archived by the owner on May 12, 2024. It is now read-only.

Commit ec9ce15

Browse files
Implemented Files.writeString MCR (#53)
* Implement File writeString * Fix bugs
1 parent 16ab461 commit ec9ce15

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

src/main/java/net/raphimc/javadowngrader/transformer/j10/Java11ToJava10.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public Java11ToJava10() {
3636
this.addMethodCallReplacer(Opcodes.INVOKEVIRTUAL, "java/lang/String", "stripTrailing", "()Ljava/lang/String;", new StringStripTrailingMCR());
3737

3838
this.addMethodCallReplacer(Opcodes.INVOKESTATIC, "java/nio/file/Files", "readString", new FilesReadStringMCR());
39+
this.addMethodCallReplacer(Opcodes.INVOKESTATIC, "java/nio/file/Files", "writeString", new FilesWriteStringMCR());
3940

4041
this.addMethodCallReplacer(Opcodes.INVOKESTATIC, "java/nio/file/Path", "of", new PathOfMCR());
4142

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* This file is part of JavaDowngrader - https://github.com/RaphiMC/JavaDowngrader
3+
* Copyright (C) 2023-2024 RK_01/RaphiMC and contributors
4+
*
5+
* This program is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation; either
8+
* version 3 of the License, or (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package net.raphimc.javadowngrader.transformer.j10.methodcallreplacer;
19+
20+
import net.raphimc.javadowngrader.RuntimeDepCollector;
21+
import net.raphimc.javadowngrader.transformer.DowngradeResult;
22+
import net.raphimc.javadowngrader.transformer.MethodCallReplacer;
23+
import net.raphimc.javadowngrader.util.ASMUtil;
24+
import org.objectweb.asm.Opcodes;
25+
import org.objectweb.asm.tree.*;
26+
27+
public class FilesWriteStringMCR implements MethodCallReplacer {
28+
29+
@Override
30+
public InsnList getReplacement(ClassNode classNode, MethodNode methodNode, String originalName, String originalDesc, RuntimeDepCollector depCollector, DowngradeResult result) {
31+
int freeVarIndex = ASMUtil.getFreeVarIndex(methodNode);
32+
final InsnList replacement = new InsnList();
33+
34+
boolean hasStoredCharset;
35+
if (originalDesc.equals("(Ljava/nio/file/Path;Ljava/lang/CharSequence;[Ljava/nio/file/OpenOption;)Ljava/nio/file/Path;")) {
36+
hasStoredCharset = false;
37+
} else if (originalDesc.equals("(Ljava/nio/file/Path;Ljava/lang/CharSequence;Ljava/nio/charset/Charset;[Ljava/nio/file/OpenOption;)Ljava/nio/file/Path;")) {
38+
hasStoredCharset = true;
39+
// Path CharSequence Charset OpenOption
40+
replacement.add(new InsnNode(Opcodes.SWAP));
41+
// Path CharSequence OpenOption Charset
42+
replacement.add(new VarInsnNode(Opcodes.ASTORE, freeVarIndex));
43+
// Path CharSequence OpenOption
44+
} else {
45+
throw new RuntimeException("Unsupported method descriptor: " + originalDesc);
46+
}
47+
48+
// Path CharSequence OpenOption
49+
replacement.add(new InsnNode(Opcodes.SWAP));
50+
// Path OpenOption CharSequence
51+
replacement.add(new MethodInsnNode(Opcodes.INVOKEINTERFACE, "java/lang/CharSequence", "toString", "()Ljava/lang/String;"));
52+
// Path OpenOption String
53+
if (hasStoredCharset) {
54+
replacement.add(new VarInsnNode(Opcodes.ALOAD, freeVarIndex));
55+
// Path OpenOption String Charset
56+
} else {
57+
replacement.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/nio/charset/StandardCharsets", "UTF_8", "Ljava/nio/charset/Charset;"));
58+
// Path OpenOption String Charset
59+
}
60+
replacement.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/lang/String", "getBytes", "(Ljava/nio/charset/Charset;)[B"));
61+
// Path OpenOption byte[]
62+
replacement.add(new InsnNode(Opcodes.SWAP));
63+
// Path byte[] OpenOption
64+
replacement.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "java/nio/file/Files", "write", "(Ljava/nio/file/Path;[B[Ljava/nio/file/OpenOption;)Ljava/nio/file/Path;"));
65+
// Path
66+
67+
return replacement;
68+
}
69+
70+
}

0 commit comments

Comments
 (0)