Archiviert
13
0

Make #deserializeLegacy() able to access private members (#386)

Dieser Commit ist enthalten in:
dadus33 2017-07-31 05:31:18 +03:00 committet von Dan Mulloy
Ursprung 9e5bdf4124
Commit d891a057c0

Datei anzeigen

@ -20,6 +20,7 @@ import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Constructor;
/** /**
* Handles component parsing in 1.8 * Handles component parsing in 1.8
@ -27,6 +28,11 @@ import java.lang.reflect.Method;
*/ */
public class ComponentParser { public class ComponentParser {
private static Constructor readerConstructor;
private static Method setLenient;
private static Method getAdapter;
private static Method read;
private ComponentParser() { private ComponentParser() {
} }
@ -45,13 +51,21 @@ public class ComponentParser {
// Should only be needed on 1.8. // Should only be needed on 1.8.
private static Object deserializeLegacy(Object gson, Class<?> component, StringReader str) { private static Object deserializeLegacy(Object gson, Class<?> component, StringReader str) {
try { try {
Class<?> readerClass = Class.forName("org.bukkit.craftbukkit.libs.com.google.gson.stream.JsonReader"); if(readerConstructor == null){
Object reader = readerClass.getConstructor(Reader.class).newInstance(str); Class<?> readerClass = Class.forName("org.bukkit.craftbukkit.libs.com.google.gson.stream.JsonReader");
Method setLenient = readerClass.getMethod("setLenient", boolean.class); readerConstructor = readerClass.getDeclaredConstructor(Reader.class);
readerConstructor.setAccessible(true);
setLenient = readerClass.getDeclaredMethod("setLenient", boolean.class);
setLenient.setAccessible(true);
getAdapter = gson.getClass().getDeclaredMethod("getAdapter", Class.class);
getAdapter.setAccessible(true);
Object adapter = getAdapter.invoke(gson, component);
read = adapter.getClass().getDeclaredMethod("read", readerClass);
read.setAccessible(true);
}
Object reader = readerConstructor.newInstance(str);
setLenient.invoke(reader, true); setLenient.invoke(reader, true);
Method getAdapter = gson.getClass().getMethod("getAdapter", Class.class);
Object adapter = getAdapter.invoke(gson, component); Object adapter = getAdapter.invoke(gson, component);
Method read = adapter.getClass().getMethod("read", readerClass);
return read.invoke(adapter, reader); return read.invoke(adapter, reader);
} catch (ReflectiveOperationException ex) { } catch (ReflectiveOperationException ex) {
throw new RuntimeException("Failed to read JSON", ex); throw new RuntimeException("Failed to read JSON", ex);