Archiviert
13
0

Merge remote-tracking branch 'origin/master'

Dieser Commit ist enthalten in:
Dan Mulloy 2017-08-04 14:01:24 -04:00
Commit 1f5692a0c7

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 {
if(readerConstructor == null){
Class<?> readerClass = Class.forName("org.bukkit.craftbukkit.libs.com.google.gson.stream.JsonReader"); Class<?> readerClass = Class.forName("org.bukkit.craftbukkit.libs.com.google.gson.stream.JsonReader");
Object reader = readerClass.getConstructor(Reader.class).newInstance(str); readerConstructor = readerClass.getDeclaredConstructor(Reader.class);
Method setLenient = readerClass.getMethod("setLenient", boolean.class); readerConstructor.setAccessible(true);
setLenient.invoke(reader, true); setLenient = readerClass.getDeclaredMethod("setLenient", boolean.class);
Method getAdapter = gson.getClass().getMethod("getAdapter", Class.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);
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);