-
-
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.
fix #27 on newer java versions where reflection is restricted
- Loading branch information
1 parent
704f8ea
commit 0e55409
Showing
2 changed files
with
23 additions
and
7 deletions.
There are no files selected for viewing
28 changes: 22 additions & 6 deletions
28
java-api/src/java18/java/xyz/wagyourtail/jvmdg/j18/stub/java_base/J_I_PrintStream.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,21 +1,37 @@ | ||
package xyz.wagyourtail.jvmdg.j18.stub.java_base; | ||
|
||
import xyz.wagyourtail.jvmdg.util.Utils; | ||
import xyz.wagyourtail.jvmdg.version.Stub; | ||
|
||
import java.io.OutputStreamWriter; | ||
import java.io.PrintStream; | ||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.reflect.Field; | ||
import java.nio.charset.Charset; | ||
|
||
public class J_I_PrintStream { | ||
private static final MethodHandles.Lookup IMPL_LOOKUP = Utils.getImplLookup(); | ||
private static final MethodHandle getCharOut; | ||
|
||
static { | ||
try { | ||
getCharOut = IMPL_LOOKUP.findGetter(PrintStream.class, "charOut", OutputStreamWriter.class); | ||
} catch (NoSuchFieldException | IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Stub | ||
public static Charset charset(PrintStream printStream) throws NoSuchFieldException, IllegalAccessException { | ||
Field charOut = PrintStream.class.getDeclaredField("charOut"); | ||
charOut.setAccessible(true); | ||
OutputStreamWriter writer = (OutputStreamWriter) charOut.get(printStream); | ||
String encoding = writer.getEncoding(); | ||
return Charset.forName(encoding); | ||
public static Charset charset(PrintStream printStream) { | ||
try { | ||
OutputStreamWriter writer = (OutputStreamWriter) getCharOut.invokeExact(printStream); | ||
String encoding = writer.getEncoding(); | ||
return Charset.forName(encoding); | ||
} catch (Throwable e) { | ||
Utils.sneakyThrow(e); | ||
} | ||
return null; | ||
} | ||
|
||
} |
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