From 1d6d01a72556a1fb661d172c98cff49dc748553b Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 25 Jul 2020 17:29:32 +0200 Subject: [PATCH] Remove ObjectProcessor#getInputClass Replace an explicit input class declaration with nasty reflection code. --- .../server/net/ObjectMessageProcessor.java | 20 ++++++++++++------- .../processors/ContactOperationProcessor.java | 3 --- .../processors/GroupCreationProcessor.java | 3 --- .../processors/GroupMessageProcessor.java | 3 --- .../GroupMessageStatusChangeProcessor.java | 3 --- .../processors/GroupResizeProcessor.java | 3 --- .../IDGeneratorRequestProcessor.java | 3 --- .../server/processors/IsTypingProcessor.java | 3 --- .../processors/LoginCredentialProcessor.java | 3 --- .../server/processors/MessageProcessor.java | 3 --- .../MessageStatusChangeProcessor.java | 3 --- .../processors/NameChangeProcessor.java | 3 --- .../server/processors/ObjectProcessor.java | 6 ------ .../processors/UserSearchProcessor.java | 3 --- .../processors/UserStatusChangeProcessor.java | 3 --- 15 files changed, 13 insertions(+), 52 deletions(-) diff --git a/server/src/main/java/envoy/server/net/ObjectMessageProcessor.java b/server/src/main/java/envoy/server/net/ObjectMessageProcessor.java index ec8488a..d75aa75 100755 --- a/server/src/main/java/envoy/server/net/ObjectMessageProcessor.java +++ b/server/src/main/java/envoy/server/net/ObjectMessageProcessor.java @@ -3,6 +3,7 @@ package envoy.server.net; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.ObjectInputStream; +import java.lang.reflect.ParameterizedType; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; @@ -50,14 +51,19 @@ public class ObjectMessageProcessor implements IMessageProcessor { logger.fine("Received " + obj); - // Process object - processors.stream().filter(p -> p.getInputClass().equals(obj.getClass())).forEach((@SuppressWarnings("rawtypes") ObjectProcessor p) -> { - try { - p.process(p.getInputClass().cast(obj), message.socketId, new ObjectWriteProxy(writeProxy)); - } catch (IOException e) { - logger.log(Level.SEVERE, "Exception during processor execution: ", e); + // Get processor and input class and process object + for (@SuppressWarnings("rawtypes") + ObjectProcessor p : processors) { + Class c = (Class) ((ParameterizedType) p.getClass().getGenericInterfaces()[0]).getActualTypeArguments()[0]; + if (c.equals(obj.getClass())) { + try { + p.process(c.cast(obj), message.socketId, new ObjectWriteProxy(writeProxy)); + break; + } catch (IOException e) { + logger.log(Level.SEVERE, "Exception during processor execution: ", e); + } } - }); + } } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } diff --git a/server/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/server/src/main/java/envoy/server/processors/ContactOperationProcessor.java index 4363f60..60952d3 100755 --- a/server/src/main/java/envoy/server/processors/ContactOperationProcessor.java +++ b/server/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -41,7 +41,4 @@ public class ContactOperationProcessor implements ObjectProcessor getInputClass() { return ContactOperation.class; } } diff --git a/server/src/main/java/envoy/server/processors/GroupCreationProcessor.java b/server/src/main/java/envoy/server/processors/GroupCreationProcessor.java index 78b1596..06a85a0 100644 --- a/server/src/main/java/envoy/server/processors/GroupCreationProcessor.java +++ b/server/src/main/java/envoy/server/processors/GroupCreationProcessor.java @@ -40,7 +40,4 @@ public class GroupCreationProcessor implements ObjectProcessor { .map(connectionManager::getSocketID) .forEach(memberSocketID -> writeProxy.write(memberSocketID, new ContactOperation(group.toCommon(), ElementOperation.ADD))); } - - @Override - public Class getInputClass() { return GroupCreation.class; } } diff --git a/server/src/main/java/envoy/server/processors/GroupMessageProcessor.java b/server/src/main/java/envoy/server/processors/GroupMessageProcessor.java index 8be1a57..2099b76 100644 --- a/server/src/main/java/envoy/server/processors/GroupMessageProcessor.java +++ b/server/src/main/java/envoy/server/processors/GroupMessageProcessor.java @@ -61,7 +61,4 @@ public class GroupMessageProcessor implements ObjectProcessor { logger.warning("Received a groupMessage with an ID that already exists"); } } - - @Override - public Class getInputClass() { return GroupMessage.class; } } diff --git a/server/src/main/java/envoy/server/processors/GroupMessageStatusChangeProcessor.java b/server/src/main/java/envoy/server/processors/GroupMessageStatusChangeProcessor.java index 9e1077b..e24902e 100644 --- a/server/src/main/java/envoy/server/processors/GroupMessageStatusChangeProcessor.java +++ b/server/src/main/java/envoy/server/processors/GroupMessageStatusChangeProcessor.java @@ -63,7 +63,4 @@ public class GroupMessageStatusChangeProcessor implements ObjectProcessor getInputClass() { return GroupMessageStatusChange.class; } } diff --git a/server/src/main/java/envoy/server/processors/GroupResizeProcessor.java b/server/src/main/java/envoy/server/processors/GroupResizeProcessor.java index a571811..fede288 100644 --- a/server/src/main/java/envoy/server/processors/GroupResizeProcessor.java +++ b/server/src/main/java/envoy/server/processors/GroupResizeProcessor.java @@ -47,7 +47,4 @@ public class GroupResizeProcessor implements ObjectProcessor { .map(connectionManager::getSocketID) .forEach(memberSocketID -> writeProxy.write(memberSocketID, commonGroup)); } - - @Override - public Class getInputClass() { return GroupResize.class; } } diff --git a/server/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java b/server/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java index c2f423a..73c7f11 100755 --- a/server/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java +++ b/server/src/main/java/envoy/server/processors/IDGeneratorRequestProcessor.java @@ -21,9 +21,6 @@ public class IDGeneratorRequestProcessor implements ObjectProcessor getInputClass() { return IDGeneratorRequest.class; } - @Override public void process(IDGeneratorRequest input, long socketID, ObjectWriteProxy writeProxy) throws IOException { writeProxy.write(socketID, createIDGenerator()); diff --git a/server/src/main/java/envoy/server/processors/IsTypingProcessor.java b/server/src/main/java/envoy/server/processors/IsTypingProcessor.java index bea5f97..bda22ee 100644 --- a/server/src/main/java/envoy/server/processors/IsTypingProcessor.java +++ b/server/src/main/java/envoy/server/processors/IsTypingProcessor.java @@ -23,9 +23,6 @@ public class IsTypingProcessor implements ObjectProcessor { private static final ConnectionManager connectionManager = ConnectionManager.getInstance(); private static final PersistenceManager persistenceManager = PersistenceManager.getInstance(); - @Override - public Class getInputClass() { return IsTyping.class; } - @Override public void process(IsTyping event, long socketID, ObjectWriteProxy writeProxy) throws IOException { final var contact = persistenceManager.getContactByID(event.get()); diff --git a/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 598d2d4..87da681 100755 --- a/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/server/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -191,7 +191,4 @@ public final class LoginCredentialProcessor implements ObjectProcessor getInputClass() { return LoginCredentials.class; } } diff --git a/server/src/main/java/envoy/server/processors/MessageProcessor.java b/server/src/main/java/envoy/server/processors/MessageProcessor.java index e4058e2..ae34581 100755 --- a/server/src/main/java/envoy/server/processors/MessageProcessor.java +++ b/server/src/main/java/envoy/server/processors/MessageProcessor.java @@ -58,7 +58,4 @@ public class MessageProcessor implements ObjectProcessor { logger.log(Level.WARNING, "Received " + message + " with an ID that already exists!"); } } - - @Override - public Class getInputClass() { return Message.class; } } diff --git a/server/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java b/server/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java index 1c76f77..507a83f 100755 --- a/server/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java +++ b/server/src/main/java/envoy/server/processors/MessageStatusChangeProcessor.java @@ -42,7 +42,4 @@ public class MessageStatusChangeProcessor implements ObjectProcessor getInputClass() { return MessageStatusChange.class; } } diff --git a/server/src/main/java/envoy/server/processors/NameChangeProcessor.java b/server/src/main/java/envoy/server/processors/NameChangeProcessor.java index 75f713c..7e782f7 100644 --- a/server/src/main/java/envoy/server/processors/NameChangeProcessor.java +++ b/server/src/main/java/envoy/server/processors/NameChangeProcessor.java @@ -28,7 +28,4 @@ public class NameChangeProcessor implements ObjectProcessor { // Notify online contacts of the name change writeProxy.writeToOnlineContacts(toUpdate.getContacts(), nameChange); } - - @Override - public Class getInputClass() { return NameChange.class; } } diff --git a/server/src/main/java/envoy/server/processors/ObjectProcessor.java b/server/src/main/java/envoy/server/processors/ObjectProcessor.java index a542d8f..ab6666e 100755 --- a/server/src/main/java/envoy/server/processors/ObjectProcessor.java +++ b/server/src/main/java/envoy/server/processors/ObjectProcessor.java @@ -18,12 +18,6 @@ import envoy.server.net.ObjectWriteProxy; */ public interface ObjectProcessor { - /** - * @return the class of the request object - * @since Envoy Server Standalone v0.1-alpha - */ - Class getInputClass(); - /** * @param input the request object * @param socketID the ID of the socket from which the object was received diff --git a/server/src/main/java/envoy/server/processors/UserSearchProcessor.java b/server/src/main/java/envoy/server/processors/UserSearchProcessor.java index 73d9558..65679fb 100755 --- a/server/src/main/java/envoy/server/processors/UserSearchProcessor.java +++ b/server/src/main/java/envoy/server/processors/UserSearchProcessor.java @@ -38,7 +38,4 @@ public class UserSearchProcessor implements ObjectProcessor { .map(User::toCommon) .collect(Collectors.toList()))); } - - @Override - public Class getInputClass() { return UserSearchRequest.class; } } diff --git a/server/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java b/server/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java index 87dd179..264f263 100755 --- a/server/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java +++ b/server/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java @@ -26,9 +26,6 @@ public class UserStatusChangeProcessor implements ObjectProcessor getInputClass() { return UserStatusChange.class; } - @Override public void process(UserStatusChange input, long socketID, ObjectWriteProxy writeProxy) { // new status should not equal old status