Skip to content

Commit

Permalink
new deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
caoli5288 committed Jan 6, 2021
1 parent 41d2c07 commit 5bc0a80
Show file tree
Hide file tree
Showing 14 changed files with 294 additions and 211 deletions.
12 changes: 5 additions & 7 deletions src/main/java/com/mengcraft/simpleorm/EbeanHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
import java.util.UUID;
import java.util.logging.Level;

import static com.mengcraft.simpleorm.lib.Reflector.invoke;

@EqualsAndHashCode(of = "id")
public class EbeanHandler {

Expand Down Expand Up @@ -260,20 +258,20 @@ public void initialize() throws DatabaseException {
conf.addClass(type);
}

ClassLoader mainctx = Thread.currentThread().getContextClassLoader();
ClassLoader mainCtx = Thread.currentThread().getContextClassLoader();
try {
URLClassLoader plugctx = (URLClassLoader) Reflect.getLoader(plugin);
URLClassLoader cl = (URLClassLoader) Reflect.getLoader(plugin);
mapping.forEach(clz -> {
for (URL jar : ((URLClassLoader) clz.getClassLoader()).getURLs()) {
invoke(plugctx, "addURL", jar);
Utils.addUrl(cl, jar);
}
});
Thread.currentThread().setContextClassLoader(plugctx);
Thread.currentThread().setContextClassLoader(cl);
server = EbeanServerFactory.create(conf);
} catch (Exception e) {
throw new DatabaseException(e);
} finally {
Thread.currentThread().setContextClassLoader(mainctx);
Thread.currentThread().setContextClassLoader(mainCtx);
}
}

Expand Down
15 changes: 2 additions & 13 deletions src/main/java/com/mengcraft/simpleorm/ORM.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@
import com.mengcraft.simpleorm.lib.GsonUtils;
import com.mengcraft.simpleorm.lib.LibraryLoader;
import com.mengcraft.simpleorm.lib.MavenLibrary;
import com.mengcraft.simpleorm.lib.Reflector;
import com.mengcraft.simpleorm.provider.IDataSourceProvider;
import com.mengcraft.simpleorm.provider.IRedisProvider;
import com.mengcraft.simpleorm.redis.RedisProviders;
import com.mengcraft.simpleorm.serializable.SerializableTypes;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Getter;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.val;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
Expand All @@ -30,8 +29,6 @@
import java.util.function.Supplier;
import java.util.logging.Level;

import static com.mengcraft.simpleorm.lib.Tuple.tuple;

public class ORM extends JavaPlugin {

public static final String PLAYER_METADATA_KEY = "ORM_METADATA";
Expand Down Expand Up @@ -192,15 +189,7 @@ public static Gson json() {
}

public static <T> T deserialize(Class<T> clz, Map<String, Object> map) {
if (ConfigurationSerializable.class.isAssignableFrom(clz)) {
try {
return Reflector.object(clz, tuple(Map.class, map));
} catch (Exception ignored) {
return Reflector.invoke(clz, "deserialize", tuple(Map.class, map));
}
}
val json = json();
return json.fromJson(json.toJsonTree(map), clz);
return SerializableTypes.asDeserializer(clz).deserialize(clz, map);
}

public static <T> T attr(Player player, @NonNull String key) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/mengcraft/simpleorm/Reflect.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mengcraft.simpleorm;

import com.avaje.ebean.EbeanServer;
import com.mengcraft.simpleorm.lib.Utils;
import lombok.SneakyThrows;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
Expand All @@ -9,12 +10,11 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import static com.mengcraft.simpleorm.lib.Reflector.getField;

public final class Reflect {

private static final Method SET_DATABASE_ENABLED = INIT_SET_DATABASE_ENABLED();
private static final Field EBEAN = INIT_EBEAN();
private static final Field JAVA_PLUGIN_classLoader = Utils.getAccessibleField(JavaPlugin.class, "classLoader");

private static Method INIT_SET_DATABASE_ENABLED() {
try {
Expand Down Expand Up @@ -48,8 +48,9 @@ public static void setEbeanServer(Plugin proxy, EbeanServer in) {
}
}

@SneakyThrows
public static ClassLoader getLoader(Plugin plugin) {
return getField(plugin, "classLoader");
return (ClassLoader) JAVA_PLUGIN_classLoader.get(plugin);
}

public static boolean isLegacy() {
Expand Down
23 changes: 13 additions & 10 deletions src/main/java/com/mengcraft/simpleorm/lib/GsonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.mengcraft.simpleorm.serializable.CustomTypeAdapter;
import com.mengcraft.simpleorm.serializable.GsonDeserializer;
import com.mengcraft.simpleorm.serializable.IDeserializer;
import com.mengcraft.simpleorm.serializable.SerializableTypes;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.bukkit.configuration.serialization.ConfigurationSerializable;

Expand All @@ -24,7 +28,6 @@
import java.util.stream.StreamSupport;

import static com.mengcraft.simpleorm.ORM.nil;
import static com.mengcraft.simpleorm.lib.Tuple.tuple;

public class GsonUtils {

Expand Down Expand Up @@ -60,7 +63,7 @@ public static Gson createJsonInBuk() {

public static Gson createJsonInBuk(FieldNamingPolicy policy) {
GsonBuilder b = new GsonBuilder();
b.registerTypeHierarchyAdapter(ConfigurationSerializable.class, new JsonSerializeAdapter());
b.registerTypeAdapterFactory(CustomTypeAdapter.newTypeHierarchyFactory(ConfigurationSerializable.class, new CustomSerializer()));
b.registerTypeAdapter(ScriptObjectMirror.class, new ScriptObjectSerializer());
if (!nil(policy)) {
b.setFieldNamingPolicy(policy);
Expand All @@ -79,22 +82,22 @@ public JsonElement serialize(ScriptObjectMirror obj, Type t, JsonSerializationCo
}
}

public static class JsonSerializeAdapter implements JsonSerializer<ConfigurationSerializable>, JsonDeserializer<ConfigurationSerializable> {
public static class CustomSerializer implements JsonSerializer<ConfigurationSerializable>, JsonDeserializer<ConfigurationSerializable> {

public JsonElement serialize(ConfigurationSerializable input, Type clz, JsonSerializationContext ctx) {
return ctx.serialize(input.serialize());
}

public ConfigurationSerializable deserialize(JsonElement jsonElement, Type clz, JsonDeserializationContext ctx) throws JsonParseException {
if (!jsonElement.isJsonObject()) {
public ConfigurationSerializable deserialize(JsonElement element, Type type, JsonDeserializationContext ctx) throws JsonParseException {
if (!element.isJsonObject()) {
return null;
}
Tuple<Class, Object> tuple = tuple(Map.class, dump(jsonElement));
try {
return Reflector.object((Class<ConfigurationSerializable>) clz, tuple);
} catch (Exception ignored) {
Class<ConfigurationSerializable> cls = (Class<ConfigurationSerializable>) type;
IDeserializer<ConfigurationSerializable> deserializer = SerializableTypes.asDeserializer(cls);
if (deserializer == GsonDeserializer.INSTANCE) {// delegate to defaults
return null;
}
return Reflector.invoke(clz, "deserialize", tuple);
return deserializer.deserialize(cls, (Map<String, ?>) dump(element));
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/mengcraft/simpleorm/lib/LibraryLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mengcraft.simpleorm.lib;

import com.mengcraft.simpleorm.Reflect;
import lombok.SneakyThrows;
import lombok.val;
import org.bukkit.Bukkit;
Expand All @@ -10,10 +11,6 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static com.mengcraft.simpleorm.lib.Reflector.getField;
import static com.mengcraft.simpleorm.lib.Reflector.invoke;


/**
* Created on 15-12-13.
*/
Expand Down Expand Up @@ -48,9 +45,9 @@ public static void load(JavaPlugin plugin, Library library, boolean global) {
if (global && GLOBAL_LOADER != null) {
classLoader = GLOBAL_LOADER;
} else {
classLoader = getField(plugin, "classLoader");
classLoader = Reflect.getLoader(plugin);
}
invoke(classLoader, "addURL", lib.toURI().toURL());
Utils.addUrl((URLClassLoader) classLoader, lib.toURI().toURL());

plugin.getLogger().info("Load library " + lib + " done");
}
Expand Down
130 changes: 0 additions & 130 deletions src/main/java/com/mengcraft/simpleorm/lib/Reflector.java

This file was deleted.

42 changes: 0 additions & 42 deletions src/main/java/com/mengcraft/simpleorm/lib/Tuple.java

This file was deleted.

9 changes: 9 additions & 0 deletions src/main/java/com/mengcraft/simpleorm/lib/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import javax.persistence.Table;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class Utils {

Expand Down Expand Up @@ -50,4 +52,11 @@ public static String translateSqlName(Class<?> cls) {
}
return separateCamelCase(cls.getSimpleName(), "_").toLowerCase();
}

private static final Method URL_CLASS_LOADER_addURL = getAccessibleMethod(URLClassLoader.class, "addURL", URL.class);

@SneakyThrows
public static void addUrl(URLClassLoader cl, URL url) {
URL_CLASS_LOADER_addURL.invoke(cl, url);
}
}
Loading

0 comments on commit 5bc0a80

Please sign in to comment.