-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor FileWriter stubs, fix some statics
- Loading branch information
1 parent
88c60e3
commit 8042d3b
Showing
7 changed files
with
137 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 108 additions & 26 deletions
134
java-api/src/java11/java/xyz/wagyourtail/jvmdg/j11/stub/java_base/J_I_FileWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,130 @@ | ||
package xyz.wagyourtail.jvmdg.j11.stub.java_base; | ||
|
||
|
||
import xyz.wagyourtail.jvmdg.version.Adapter; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.Type; | ||
import org.objectweb.asm.tree.InsnList; | ||
import org.objectweb.asm.tree.InsnNode; | ||
import org.objectweb.asm.tree.MethodInsnNode; | ||
import org.objectweb.asm.tree.MethodNode; | ||
import xyz.wagyourtail.jvmdg.util.Utils; | ||
import xyz.wagyourtail.jvmdg.version.Modify; | ||
import xyz.wagyourtail.jvmdg.version.Ref; | ||
|
||
import java.io.*; | ||
import java.io.FileWriter; | ||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.CharsetEncoder; | ||
import java.nio.charset.CodingErrorAction; | ||
|
||
@Adapter("Ljava/io/FileWriter;") | ||
public class J_I_FileWriter extends OutputStreamWriter { | ||
|
||
public J_I_FileWriter(String fileName) throws FileNotFoundException { | ||
super(new FileOutputStream(fileName)); | ||
//@Adapter("Ljava/io/FileWriter;") | ||
public class J_I_FileWriter { | ||
private static final MethodHandles.Lookup IMPL_LOOKUP = Utils.getImplLookup(); | ||
private static final MethodHandle seGetter; | ||
private static final MethodHandle encSetter; | ||
static { | ||
MethodHandle seGet = null; | ||
MethodHandle encSet = null; | ||
try { | ||
Class<?> se = Class.forName("sun.nio.cs.StreamEncoder"); | ||
seGet = IMPL_LOOKUP.findGetter(FileWriter.class, "se", se); | ||
encSet = IMPL_LOOKUP.findSetter(se, "encoder", CharsetEncoder.class); | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
seGetter = seGet; | ||
encSetter = encSet; | ||
} | ||
|
||
public J_I_FileWriter(String fileName, boolean append) throws FileNotFoundException { | ||
super(new FileOutputStream(fileName, append)); | ||
public static void setCharset(FileWriter fw, Charset charset) { | ||
try { | ||
Object se = seGetter.invoke(fw); | ||
encSetter.invoke(se, charset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE)); | ||
} catch (Throwable t) { | ||
Utils.sneakyThrow(t); | ||
} | ||
} | ||
|
||
public J_I_FileWriter(File file) throws FileNotFoundException { | ||
super(new FileOutputStream(file)); | ||
} | ||
// public J_I_FileWriter(File file, Charset charset) throws IOException { | ||
// super(new FileOutputStream(file), charset); | ||
// } | ||
// | ||
// public J_I_FileWriter(File file, Charset charset, boolean append) throws IOException { | ||
// super(new FileOutputStream(file, append), charset); | ||
// } | ||
|
||
public J_I_FileWriter(File file, boolean append) throws FileNotFoundException { | ||
super(new FileOutputStream(file, append)); | ||
} | ||
|
||
public J_I_FileWriter(FileDescriptor fd) { | ||
super(new FileOutputStream(fd)); | ||
} | ||
@Modify(ref = @Ref(value = "java/io/FileWriter", member = "<init>", desc = "(Ljava/lang/String;Ljava/nio/charset/Charset;)V")) | ||
public static void init1(MethodNode mnode, int i) { | ||
MethodInsnNode node = (MethodInsnNode) mnode.instructions.get(i); | ||
Type firstArgType = Type.getArgumentTypes(node.desc)[0]; | ||
InsnList list = new InsnList(); | ||
// stack: (U) FileWriter, String, Charset | ||
list.add(new InsnNode(Opcodes.DUP_X2)); | ||
// stack: Charset, (U) FileWriter, String, Charset | ||
list.add(new InsnNode(Opcodes.POP)); | ||
// stack: Charset, (U) FileWriter, String | ||
list.add(new InsnNode(Opcodes.DUP2)); | ||
// stack: Charset, (U) FileWriter, String, (U) FileWriter, String | ||
// call init | ||
list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/io/FileWriter", "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, firstArgType), false)); | ||
// stack: Charset, FileWriter, String | ||
list.add(new InsnNode(Opcodes.POP)); | ||
// stack: Charset, FileWriter | ||
list.add(new InsnNode(Opcodes.SWAP)); | ||
// stack: FileWriter, Charset | ||
list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getType(J_I_FileWriter.class).getInternalName(), "setCharset", "(Ljava/io/FileWriter;Ljava/nio/charset/Charset;)V", false)); | ||
|
||
public J_I_FileWriter(String fileName, Charset charset) throws IOException { | ||
super(new FileOutputStream(fileName), charset); | ||
mnode.instructions.insert(node, list); | ||
mnode.instructions.remove(node); | ||
} | ||
|
||
public J_I_FileWriter(String fileName, Charset charset, boolean append) throws IOException { | ||
super(new FileOutputStream(fileName, append), charset); | ||
@Modify(ref = @Ref(value = "java/io/FileWriter", member = "<init>", desc = "(Ljava/lang/String;Ljava/nio/charset/Charset;Z)V")) | ||
public static void init2(MethodNode mnode, int i) { | ||
MethodInsnNode node = (MethodInsnNode) mnode.instructions.get(i); | ||
Type firstArgType = Type.getArgumentTypes(node.desc)[0]; | ||
InsnList list = new InsnList(); | ||
// stack: (U) FileWriter, String, Charset, boolean | ||
list.add(new InsnNode(Opcodes.DUP2_X2)); | ||
// stack: Charset, boolean, (U) FileWriter, String, Charset, boolean | ||
list.add(new InsnNode(Opcodes.SWAP)); | ||
// stack: Charset, boolean, (U) FileWriter, String, boolean, Charset | ||
list.add(new InsnNode(Opcodes.POP)); | ||
// stack: Charset, boolean, (U) FileWriter, String, boolean | ||
list.add(new InsnNode(Opcodes.DUP2_X1)); | ||
// stack: Charset, boolean, String, boolean, (U) FileWriter, String, boolean | ||
list.add(new InsnNode(Opcodes.POP2)); | ||
// stack: Charset, boolean, String, boolean, (U) FileWriter | ||
list.add(new InsnNode(Opcodes.DUP_X2)); | ||
// stack: Charset, boolean, (U) FileWriter, String, boolean, (U) FileWriter | ||
list.add(new InsnNode(Opcodes.DUP_X2)); | ||
// stack: Charset, boolean, (U) FileWriter, (U) FileWriter, String, boolean, (U) FileWriter | ||
list.add(new InsnNode(Opcodes.POP)); | ||
// stack: Charset, boolean, (U) FileWriter, (U) FileWriter, String, boolean | ||
// call init | ||
list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL, "java/io/FileWriter", "<init>", Type.getMethodDescriptor(Type.VOID_TYPE, firstArgType, Type.BOOLEAN_TYPE), false)); | ||
// stack: Charset, boolean, FileWriter | ||
list.add(new InsnNode(Opcodes.SWAP)); | ||
// stack: Charset, FileWriter, boolean | ||
list.add(new InsnNode(Opcodes.POP)); | ||
// stack: Charset, FileWriter | ||
list.add(new InsnNode(Opcodes.SWAP)); | ||
// stack: FileWriter, Charset | ||
list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, Type.getType(J_I_FileWriter.class).getInternalName(), "setCharset", "(Ljava/io/FileWriter;Ljava/nio/charset/Charset;)V", false)); | ||
|
||
mnode.instructions.insert(node, list); | ||
mnode.instructions.remove(node); | ||
} | ||
|
||
public J_I_FileWriter(File file, Charset charset) throws IOException { | ||
super(new FileOutputStream(file), charset); | ||
@Modify(ref = @Ref(value = "java/io/FileWriter", member = "<init>", desc = "(Ljava/io/File;Ljava/nio/charset/Charset;)V")) | ||
public static void init3(MethodNode mnode, int i) { | ||
init1(mnode, i); | ||
} | ||
|
||
public J_I_FileWriter(File file, Charset charset, boolean append) throws IOException { | ||
super(new FileOutputStream(file, append), charset); | ||
@Modify(ref = @Ref(value = "java/io/FileWriter", member = "<init>", desc = "(Ljava/io/File;Ljava/nio/charset/Charset;Z)V")) | ||
public static void init4(MethodNode mnode, int i) { | ||
init2(mnode, i); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters