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

View File

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

View File

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