You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@plc4x.apache.org by ld...@apache.org on 2020/11/08 00:51:01 UTC

[plc4x] 02/02: PLC4X-257 Swap anonymous lambdas which static classes.

This is an automated email from the ASF dual-hosted git repository.

ldywicki pushed a commit to branch issue/PLC4X-257
in repository https://gitbox.apache.org/repos/asf/plc4x.git

commit 2bb462549ea2ae660adce2906866c50e7270a499
Author: Ɓukasz Dywicki <lu...@code-house.org>
AuthorDate: Sun Nov 8 01:23:00 2020 +0100

    PLC4X-257 Swap anonymous lambdas which static classes.
    
    Lambdas which are present on stack trace have a big impact on debugging capabilities.
    They do not point place in code which is faulty, instead they represent kind of named state for code which formed it.
    In order to improve overall developer experience swap to regular classes gives us a possiblity to find which `only` call refused message from further processing.
---
 .../apache/plc4x/java/spi/Plc4xNettyWrapper.java   |  4 +-
 .../spi/internal/DefaultSendRequestContext.java    | 46 +++++++++++++++++++---
 2 files changed, 42 insertions(+), 8 deletions(-)

diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java
index c199eca..5929ffa 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/Plc4xNettyWrapper.java
@@ -160,7 +160,7 @@ public class Plc4xNettyWrapper<T> extends MessageToMessageCodec<T, Object> {
                         Predicate predicate = either.get();
                         if (predicate.test(instance) == false) {
                             // We do not match -> cannot handle
-                            logger.trace("Registration {} does not match object {} (currently wrapped to {})", registration, t.getClass().getSimpleName(), instance.getClass().getSimpleName());
+                            logger.trace("Registration {} with predicate {} does not match object {} (currently wrapped to {})", registration, predicate, t.getClass().getSimpleName(), instance.getClass().getSimpleName());
                             continue registrations;
                         }
                     }
@@ -174,7 +174,7 @@ public class Plc4xNettyWrapper<T> extends MessageToMessageCodec<T, Object> {
                 return;
             }
         }
-        logger.trace("No registered handler found for message {}, using default decode method", t);
+        logger.trace("None of {} registered handlers could handle message {}, using default decode method", this.registeredHandlers.size(), t);
         protocolBase.decode(new DefaultConversationContext<>(channelHandlerContext, passive), t);
     }
 
diff --git a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java
index 376c0fd..51141a6 100644
--- a/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java
+++ b/plc4j/spi/src/main/java/org/apache/plc4x/java/spi/internal/DefaultSendRequestContext.java
@@ -77,7 +77,7 @@ public class DefaultSendRequestContext<T> implements ConversationContext.SendReq
             throw new ConversationContext.PlcWiringException("can't expect class of type " + clazz + " as we already expecting clazz of type " + expectClazz);
         }
         expectClazz = clazz;
-        commands.addLast(Either.right(clazz::isInstance));
+        commands.addLast(Either.right(new TypePredicate<>(clazz)));
         return this;
     }
 
@@ -123,9 +123,7 @@ public class DefaultSendRequestContext<T> implements ConversationContext.SendReq
             throw new ConversationContext.PlcWiringException("expectResponse must be called before first unwrap");
         }
         if (onTimeoutConsumer == null) {
-            onTimeoutConsumer = e -> {
-                // NOOP
-            };
+            onTimeoutConsumer = new NoopTimeoutConsumer();
         }
         commands.addLast(Either.left(unwrapper));
         return new DefaultSendRequestContext<>(commands, timeout, finisher, request, context, expectClazz, packetConsumer, onTimeoutConsumer, errorConsumer);
@@ -133,8 +131,44 @@ public class DefaultSendRequestContext<T> implements ConversationContext.SendReq
 
     @Override
     public <R> ConversationContext.SendRequestContext<R> only(Class<R> clazz) {
-        this.check(clazz::isInstance);
-        return this.unwrap(clazz::cast);
+        this.check(new TypePredicate<>(clazz));
+        return this.unwrap(new CastFunction<>(clazz));
     }
 
+    static class TypePredicate<T, R> implements Predicate<R> {
+
+        private final Class<T> type;
+
+        TypePredicate(Class<T> type) {
+            this.type = type;
+        }
+
+        @Override
+        public boolean test(R value) {
+            return type.isInstance(value);
+        }
+    }
+
+    static class CastFunction<T, R> implements Function<R, T> {
+
+        private final Class<T> type;
+
+        CastFunction(Class<T> type) {
+            this.type = type;
+        }
+
+        @Override
+        public T apply(R value) {
+            return type.cast(value);
+        }
+
+    }
+
+    static class NoopTimeoutConsumer implements Consumer<TimeoutException> {
+
+        @Override
+        public void accept(TimeoutException e) {
+
+        }
+    }
 }