1
0
mirror of https://e.coding.net/circlecloud/GsonAgent.git synced 2024-11-22 01:48:50 +00:00

移除反射调用...

Signed-off-by: 502647092 <jtb1@163.com>
This commit is contained in:
502647092 2016-01-09 13:14:53 +08:00
parent 6121d924b5
commit 7405e4fda0
3 changed files with 190 additions and 213 deletions

View File

@ -9,10 +9,8 @@ import cn.citycraft.GsonAgent.api.JsonNull;
import cn.citycraft.GsonAgent.api.JsonObject; import cn.citycraft.GsonAgent.api.JsonObject;
import cn.citycraft.GsonAgent.api.JsonParser; import cn.citycraft.GsonAgent.api.JsonParser;
import cn.citycraft.GsonAgent.api.JsonPrimitive; import cn.citycraft.GsonAgent.api.JsonPrimitive;
import cn.citycraft.GsonAgent.api.TypeAdapter;
import cn.citycraft.GsonAgent.api.stream.JsonReader; import cn.citycraft.GsonAgent.api.stream.JsonReader;
import cn.citycraft.GsonAgent.api.stream.JsonWriter; import cn.citycraft.GsonAgent.api.stream.JsonWriter;
import cn.citycraft.GsonAgent.api.utils.Utils;
/** /**
* Minecraft Gson代理类(用于兼容1.7-1.8Gson类库) * Minecraft Gson代理类(用于兼容1.7-1.8Gson类库)
@ -21,124 +19,103 @@ import cn.citycraft.GsonAgent.api.utils.Utils;
* @since 2016年1月5日 下午12:51:30 * @since 2016年1月5日 下午12:51:30
* @author ,曲尘 * @author ,曲尘
*/ */
@SuppressWarnings({ "unchecked", "rawtypes", "unused" })
public class GsonAgent { public class GsonAgent {
public static boolean newVersion = false; public static boolean newVersion = false;
private static String internalPackageName;
private static Class TypeAdapter;
static { static {
internalPackageName = "cn.citycraft.GsonAgent."; try {
try { Thread.currentThread().getContextClassLoader().loadClass("com.google.gson.Gson");
Thread.currentThread().getContextClassLoader().loadClass("com.google.gson.Gson"); newVersion = true;
newVersion = true; } catch (final Exception | Error e) {
internalPackageName += "normal."; }
} catch (final Exception | Error e) { }
internalPackageName += "nms.";
}
try {
TypeAdapter = getInternalClass("bind.TypeAdapter");
} catch (final Exception e) {
}
}
/** /**
* 初始化GsonAgent * 初始化GsonAgent
* *
* @return 根据新老版本获得不同的实例 * @return 根据新老版本获得不同的实例
*/ */
public static Gson newGson() { public static Gson newGson() {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.GsonHandle(); return new cn.citycraft.GsonAgent.normal.GsonHandle();
} }
return new cn.citycraft.GsonAgent.nms.GsonHandle(); return new cn.citycraft.GsonAgent.nms.GsonHandle();
} }
public static JsonArray newJsonArray() { public static JsonArray newJsonArray() {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonArrayHandle(); return new cn.citycraft.GsonAgent.normal.JsonArrayHandle();
} }
return new cn.citycraft.GsonAgent.nms.JsonArrayHandle(); return new cn.citycraft.GsonAgent.nms.JsonArrayHandle();
} }
public static JsonNull newJsonNull() { public static JsonNull newJsonNull() {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonNullHandle(); return new cn.citycraft.GsonAgent.normal.JsonNullHandle();
} }
return new cn.citycraft.GsonAgent.nms.JsonNullHandle(); return new cn.citycraft.GsonAgent.nms.JsonNullHandle();
} }
public static JsonObject newJsonObject() { public static JsonObject newJsonObject() {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonObjectHandle(); return new cn.citycraft.GsonAgent.normal.JsonObjectHandle();
} }
return new cn.citycraft.GsonAgent.nms.JsonObjectHandle(); return new cn.citycraft.GsonAgent.nms.JsonObjectHandle();
} }
public static JsonParser newJsonParser() { public static JsonParser newJsonParser() {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonParserHandle(); return new cn.citycraft.GsonAgent.normal.JsonParserHandle();
} }
return new cn.citycraft.GsonAgent.normal.JsonParserHandle(); return new cn.citycraft.GsonAgent.normal.JsonParserHandle();
} }
public static JsonPrimitive newJsonPrimitive(final Boolean value) { public static JsonPrimitive newJsonPrimitive(final Boolean value) {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value); return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value);
} }
return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value); return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value);
} }
public static JsonPrimitive newJsonPrimitive(final Character value) { public static JsonPrimitive newJsonPrimitive(final Character value) {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value); return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value);
} }
return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value); return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value);
} }
public static JsonPrimitive newJsonPrimitive(final Number value) { public static JsonPrimitive newJsonPrimitive(final Number value) {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value); return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value);
} }
return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value); return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value);
} }
public static JsonPrimitive newJsonPrimitive(final String value) { public static JsonPrimitive newJsonPrimitive(final String value) {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value); return new cn.citycraft.GsonAgent.normal.JsonPrimitiveHandle(value);
} }
return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value); return new cn.citycraft.GsonAgent.nms.JsonPrimitiveHandle(value);
} }
public static JsonReader newJsonReader(final Reader in) { public static JsonReader newJsonReader(final Reader in) {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.stream.JsonReaderHandle(in); return new cn.citycraft.GsonAgent.normal.stream.JsonReaderHandle(in);
} }
return new cn.citycraft.GsonAgent.nms.stream.JsonReaderHandle(in); return new cn.citycraft.GsonAgent.nms.stream.JsonReaderHandle(in);
} }
public static JsonWriter newJsonWriter(final Writer out) { public static JsonWriter newJsonWriter(final Writer out) {
if (newVersion) { if (newVersion) {
return new cn.citycraft.GsonAgent.normal.stream.JsonWriterHandle(out); return new cn.citycraft.GsonAgent.normal.stream.JsonWriterHandle(out);
} }
return new cn.citycraft.GsonAgent.nms.stream.JsonWriterHandle(out); return new cn.citycraft.GsonAgent.nms.stream.JsonWriterHandle(out);
} }
// FIXME ###会造成java.lang.StackOverflowError###
public static TypeAdapter newTypeAdapter() { // public static <T> TypeAdapter newTypeAdapter(final TypeAdapter<T> typeAdapter) {
return (TypeAdapter) Utils.newInstance(TypeAdapter); // if (newVersion) {
} // return new cn.citycraft.GsonAgent.normal.bind.TypeAdapterHandle((com.google.gson.TypeAdapter) typeAdapter);
// }
private static Class<?> getInternalClass(final String className) throws ClassNotFoundException { // return new cn.citycraft.GsonAgent.nms.bind.TypeAdapterHandle((net.minecraft.util.com.google.gson.TypeAdapter) typeAdapter);
return Class.forName(internalPackageName + className + "Handle"); // }
}
private void none() {
Class clazz;
clazz = cn.citycraft.GsonAgent.api.bind.TypeAdapter.class;
clazz = cn.citycraft.GsonAgent.nms.bind.TypeAdapterHandle.class;
clazz = cn.citycraft.GsonAgent.normal.bind.TypeAdapterHandle.class;
clazz = cn.citycraft.GsonAgent.api.bind.TypeAdapterFactory.class;
clazz = cn.citycraft.GsonAgent.nms.bind.TypeAdapterFactoryHandle.class;
clazz = cn.citycraft.GsonAgent.normal.bind.TypeAdapterFactoryHandle.class;
}
} }

View File

@ -10,68 +10,68 @@ import net.minecraft.util.com.google.gson.TypeAdapter;
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
public class TypeAdapterHandle<T> implements cn.citycraft.GsonAgent.api.TypeAdapter<T> { public class TypeAdapterHandle<T> implements cn.citycraft.GsonAgent.api.TypeAdapter<T> {
private final TypeAdapter<T> handle; private final TypeAdapter<T> handle;
public TypeAdapterHandle(final TypeAdapter handle) { public TypeAdapterHandle(final TypeAdapter<T> handle) {
this.handle = handle; this.handle = handle;
} }
@Override @Override
public T fromJson(final Reader in) throws IOException { public T fromJson(final Reader in) throws IOException {
return getHandle().fromJson(in); return getHandle().fromJson(in);
} }
@Override @Override
public T fromJson(final String json) throws IOException { public T fromJson(final String json) throws IOException {
return getHandle().fromJson(json); return getHandle().fromJson(json);
} }
@Override @Override
public T fromJsonTree(final cn.citycraft.GsonAgent.api.JsonElement jsonTree) { public T fromJsonTree(final cn.citycraft.GsonAgent.api.JsonElement jsonTree) {
return getHandle().fromJsonTree(((JsonElementHandle) jsonTree).getHandle()); return getHandle().fromJsonTree(((JsonElementHandle) jsonTree).getHandle());
} }
@Override @Override
public TypeAdapter<T> getHandle() { public TypeAdapter<T> getHandle() {
return handle; return handle;
} }
@Override @Override
public TypeAdapterHandle<T> nullSafe() { public TypeAdapterHandle<T> nullSafe() {
return new TypeAdapterHandle(this.handle); return new TypeAdapterHandle(this.handle);
} }
@Override @Override
public T read(final cn.citycraft.GsonAgent.api.stream.JsonReader reader) throws IOException { public T read(final cn.citycraft.GsonAgent.api.stream.JsonReader reader) throws IOException {
if (reader.peek().name().equals("NULL")) { if (reader.peek().name().equals("NULL")) {
reader.nextNull(); reader.nextNull();
return null; return null;
} }
return this.read(reader); return this.read(reader);
} }
@Override @Override
public String toJson(final T value) throws IOException { public String toJson(final T value) throws IOException {
return getHandle().toJson(value); return getHandle().toJson(value);
} }
@Override @Override
public void toJson(final Writer out, final T value) throws IOException { public void toJson(final Writer out, final T value) throws IOException {
getHandle().toJson(out, value); getHandle().toJson(out, value);
} }
@Override @Override
public JsonElementHandle toJsonTree(final T value) { public JsonElementHandle toJsonTree(final T value) {
return new JsonElementHandle(getHandle().toJsonTree(value)); return new JsonElementHandle(getHandle().toJsonTree(value));
} }
@Override @Override
public void write(final cn.citycraft.GsonAgent.api.stream.JsonWriter out, final T value) throws IOException { public void write(final cn.citycraft.GsonAgent.api.stream.JsonWriter out, final T value) throws IOException {
if (value == null) { if (value == null) {
out.nullValue(); out.nullValue();
} else { } else {
this.write(out, value); this.write(out, value);
} }
} }
} }

View File

@ -11,68 +11,68 @@ import cn.citycraft.GsonAgent.normal.JsonElementHandle;
@SuppressWarnings({ "unchecked", "rawtypes" }) @SuppressWarnings({ "unchecked", "rawtypes" })
public class TypeAdapterHandle<T> implements cn.citycraft.GsonAgent.api.TypeAdapter<T> { public class TypeAdapterHandle<T> implements cn.citycraft.GsonAgent.api.TypeAdapter<T> {
private final TypeAdapter<T> handle; private final TypeAdapter<T> handle;
public TypeAdapterHandle(final TypeAdapter handle) { public TypeAdapterHandle(final TypeAdapter<T> handle) {
this.handle = handle; this.handle = handle;
} }
@Override @Override
public T fromJson(final Reader in) throws IOException { public T fromJson(final Reader in) throws IOException {
return getHandle().fromJson(in); return getHandle().fromJson(in);
} }
@Override @Override
public T fromJson(final String json) throws IOException { public T fromJson(final String json) throws IOException {
return getHandle().fromJson(json); return getHandle().fromJson(json);
} }
@Override @Override
public T fromJsonTree(final cn.citycraft.GsonAgent.api.JsonElement jsonTree) { public T fromJsonTree(final cn.citycraft.GsonAgent.api.JsonElement jsonTree) {
return getHandle().fromJsonTree(((JsonElementHandle) jsonTree).getHandle()); return getHandle().fromJsonTree(((JsonElementHandle) jsonTree).getHandle());
} }
@Override @Override
public TypeAdapter<T> getHandle() { public TypeAdapter<T> getHandle() {
return handle; return handle;
} }
@Override @Override
public TypeAdapterHandle<T> nullSafe() { public TypeAdapterHandle<T> nullSafe() {
return new TypeAdapterHandle(this.handle); return new TypeAdapterHandle(this.handle);
} }
@Override @Override
public T read(final cn.citycraft.GsonAgent.api.stream.JsonReader reader) throws IOException { public T read(final cn.citycraft.GsonAgent.api.stream.JsonReader reader) throws IOException {
if (reader.peek().name().equals("NULL")) { if (reader.peek().name().equals("NULL")) {
reader.nextNull(); reader.nextNull();
return null; return null;
} }
return this.read(reader); return this.read(reader);
} }
@Override @Override
public String toJson(final T value) throws IOException { public String toJson(final T value) throws IOException {
return getHandle().toJson(value); return getHandle().toJson(value);
} }
@Override @Override
public void toJson(final Writer out, final T value) throws IOException { public void toJson(final Writer out, final T value) throws IOException {
getHandle().toJson(out, value); getHandle().toJson(out, value);
} }
@Override @Override
public JsonElementHandle toJsonTree(final T value) { public JsonElementHandle toJsonTree(final T value) {
return new JsonElementHandle(getHandle().toJsonTree(value)); return new JsonElementHandle(getHandle().toJsonTree(value));
} }
@Override @Override
public void write(final cn.citycraft.GsonAgent.api.stream.JsonWriter out, final T value) throws IOException { public void write(final cn.citycraft.GsonAgent.api.stream.JsonWriter out, final T value) throws IOException {
if (value == null) { if (value == null) {
out.nullValue(); out.nullValue();
} else { } else {
this.write(out, value); this.write(out, value);
} }
} }
} }