From 857b09ab54ba861e4bb7d9a508a2a3a8a24d27ea Mon Sep 17 00:00:00 2001 From: CDFN Date: Wed, 21 Apr 2021 23:16:32 +0200 Subject: [PATCH] unsafe goes brrr, no need to make empty constructors in messages anymore --- .../commons/messages/ConfigMessages.java | 6 ------ .../messages/api/MessagePubsubListener.java | 20 +++++++++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/skyblock-common/src/main/java/io/github/cdfn/skyblock/commons/messages/ConfigMessages.java b/skyblock-common/src/main/java/io/github/cdfn/skyblock/commons/messages/ConfigMessages.java index 558d738..b4d9da9 100644 --- a/skyblock-common/src/main/java/io/github/cdfn/skyblock/commons/messages/ConfigMessages.java +++ b/skyblock-common/src/main/java/io/github/cdfn/skyblock/commons/messages/ConfigMessages.java @@ -14,9 +14,6 @@ public static class ConfigRequest implements MessagePackSerializable { private int id; - public ConfigRequest() { - } - public ConfigRequest(int id) { this.id = id; } @@ -46,9 +43,6 @@ public static class ConfigResponse implements MessagePackSerializable { private int id; private String data; - public ConfigResponse() { - } - public ConfigResponse(int id, String data) { this.id = id; this.data = data; diff --git a/skyblock-common/src/main/java/io/github/cdfn/skyblock/commons/messages/api/MessagePubsubListener.java b/skyblock-common/src/main/java/io/github/cdfn/skyblock/commons/messages/api/MessagePubsubListener.java index 7cf4f0f..f280d5e 100644 --- a/skyblock-common/src/main/java/io/github/cdfn/skyblock/commons/messages/api/MessagePubsubListener.java +++ b/skyblock-common/src/main/java/io/github/cdfn/skyblock/commons/messages/api/MessagePubsubListener.java @@ -6,12 +6,23 @@ import io.lettuce.core.RedisClient; import io.lettuce.core.pubsub.RedisPubSubListener; import java.io.IOException; -import java.lang.reflect.InvocationTargetException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import sun.misc.Unsafe; public class MessagePubsubListener implements RedisPubSubListener { + private static Unsafe unsafe; + static { + try { + var field = Unsafe.class.getDeclaredField("theUnsafe"); + field.setAccessible(true); + unsafe = (Unsafe) field.get(null); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + } + } + private static final Logger LOGGER = LoggerFactory.getLogger(MessagePubsubListener.class); private final MessageHandlerRegistry registry; @@ -25,7 +36,6 @@ public void message(String channel, byte[] message) { } @Override - @SuppressWarnings("unchecked") public void message(String pattern, String channel, byte[] message) { // Strip channel's prefix so we get class name var className = channel.replace(RedisConfig.PREFIX, ""); @@ -38,13 +48,11 @@ public void message(String pattern, String channel, byte[] message) { return; } - var messagePackSerializable = ((Class) clazz) - .getDeclaredConstructor() - .newInstance(); + var messagePackSerializable = (MessagePackSerializable) unsafe.allocateInstance(clazz); messagePackSerializable.deserialize(message); registry.callAll(clazz, messagePackSerializable); - } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException | IOException e) { + } catch (ClassNotFoundException | InstantiationException | IOException e) { LOGGER.error("error while handling message on channel {} with classname {}", channel, className, e); } }