diff --git a/vertx-mutiny-clients/vertx-mutiny-runtime/src/main/java/io/smallrye/mutiny/vertx/MutinyDelegate.java b/vertx-mutiny-clients/vertx-mutiny-runtime/src/main/java/io/smallrye/mutiny/vertx/MutinyDelegate.java new file mode 100644 index 000000000..ad7913af2 --- /dev/null +++ b/vertx-mutiny-clients/vertx-mutiny-runtime/src/main/java/io/smallrye/mutiny/vertx/MutinyDelegate.java @@ -0,0 +1,13 @@ +package io.smallrye.mutiny.vertx; + +/** + * Interface implemented by every generated Mutiny type. + */ +public interface MutinyDelegate { + + /** + * @return the delegate used by this Mutiny object of generated type + */ + Object getDelegate(); + +} diff --git a/vertx-mutiny-clients/vertx-mutiny-runtime/src/main/java/io/smallrye/mutiny/vertx/MutinyHelper.java b/vertx-mutiny-clients/vertx-mutiny-runtime/src/main/java/io/smallrye/mutiny/vertx/MutinyHelper.java index f20d4f0b2..6028b056e 100644 --- a/vertx-mutiny-clients/vertx-mutiny-runtime/src/main/java/io/smallrye/mutiny/vertx/MutinyHelper.java +++ b/vertx-mutiny-clients/vertx-mutiny-runtime/src/main/java/io/smallrye/mutiny/vertx/MutinyHelper.java @@ -2,13 +2,16 @@ import java.util.concurrent.Executor; import java.util.concurrent.Flow.Subscriber; +import java.util.function.Consumer; import java.util.function.Function; import io.smallrye.mutiny.vertx.impl.WriteStreamSubscriberImpl; +import io.vertx.core.Handler; import io.vertx.core.Vertx; import io.vertx.core.WorkerExecutor; import io.vertx.core.streams.WriteStream; +@SuppressWarnings({ "unchecked", "rawtypes" }) public class MutinyHelper { /** @@ -94,6 +97,27 @@ public static Class unwrap(Class type) { return type; } + /** + * Convert a handler for a generated Mutiny type to a handler for the corresponding core type. + */ + public static Handler convertHandler(Handler mutinyHandler, Function mapper) { + if (mutinyHandler instanceof MutinyDelegate) { + MutinyDelegate mutinyDelegate = (MutinyDelegate) mutinyHandler; + return (Handler) mutinyDelegate.getDelegate(); + } + return new DelegatingHandler<>(mutinyHandler, mapper); + } + + /** + * Convert a consumer for a generated Mutiny type to a handler of the same type. + */ + public static Handler convertConsumer(Consumer mutinyConsumer) { + if (mutinyConsumer instanceof MutinyDelegate) { + return (Handler) mutinyConsumer; + } + return mutinyConsumer != null ? new DelegatingConsumerHandler<>(mutinyConsumer) : null; + } + /** * Adapts a Vert.x {@link WriteStream} to a Mutiny {@link Subscriber}. *

diff --git a/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/ClassDeclarationCodeWriter.java b/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/ClassDeclarationCodeWriter.java index a08286709..3f49ad7da 100644 --- a/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/ClassDeclarationCodeWriter.java +++ b/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/ClassDeclarationCodeWriter.java @@ -32,6 +32,7 @@ public void generate(ClassModel model, PrintWriter writer) { } List interfaces = new ArrayList<>(); + interfaces.add("io.smallrye.mutiny.vertx.MutinyDelegate"); if ("io.vertx.core.buffer.Buffer".equals(type.getName())) { interfaces.add("io.vertx.core.shareddata.impl.ClusterSerializable"); } diff --git a/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/CodeGenHelper.java b/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/CodeGenHelper.java index 61e699b29..8ef990c77 100644 --- a/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/CodeGenHelper.java +++ b/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/CodeGenHelper.java @@ -190,7 +190,7 @@ public static String genConvParam(Map> methodT ClassKind eventKind = eventType.getKind(); if (eventKind == ASYNC_RESULT) { TypeInfo resultType = ((ParameterizedTypeInfo) eventType).getArg(0); - return "new io.smallrye.mutiny.vertx.DelegatingHandler<>(" + expr + ", ar -> ar.map(event -> " + genConvReturn(methodTypeArgMap, resultType, method, "event") + "))"; + return "io.smallrye.mutiny.vertx.MutinyHelper.convertHandler(" + expr + ", ar -> ar.map(event -> " + genConvReturn(methodTypeArgMap, resultType, method, "event") + "))"; } else if (eventType.isParameterized() && eventType.getRaw().getName().equals(Promise.class.getName())) { return "new Handler<" + genTypeName(eventType) + ">() {\n" + " public void handle(" + genTypeName(eventType) + " event) {\n" + @@ -198,7 +198,7 @@ public static String genConvParam(Map> methodT " }\n" + " }"; } else { - return "new io.smallrye.mutiny.vertx.DelegatingHandler<>(" + expr + ", event -> " + genConvReturn(methodTypeArgMap, eventType, method, "event") + ")"; + return "io.smallrye.mutiny.vertx.MutinyHelper.convertHandler(" + expr + ", event -> " + genConvReturn(methodTypeArgMap, eventType, method, "event") + ")"; } } else if (kind == FUNCTION) { TypeInfo argType = parameterizedTypeInfo.getArg(0); diff --git a/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/GetDelegateMethodCodeWriter.java b/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/GetDelegateMethodCodeWriter.java index 4dbb86785..579130a68 100644 --- a/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/GetDelegateMethodCodeWriter.java +++ b/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/lang/GetDelegateMethodCodeWriter.java @@ -7,6 +7,7 @@ public class GetDelegateMethodCodeWriter implements ConditionalCodeWriter { @Override public void generate(ClassModel model, PrintWriter writer) { + writer.println(" @Override"); writer.print(" public "); writer.print(model.getType().getName()); writer.println(" getDelegate() {"); diff --git a/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/methods/ConsumerMethodGenerator.java b/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/methods/ConsumerMethodGenerator.java index a5c8ceab8..735866e92 100644 --- a/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/methods/ConsumerMethodGenerator.java +++ b/vertx-mutiny-generator/src/main/java/io/smallrye/mutiny/vertx/codegen/methods/ConsumerMethodGenerator.java @@ -60,7 +60,7 @@ private void generateBody(MutinyMethodDescriptor descriptor) { } else if(param.getType().getName().equals(Runnable.class.getName())) { writer.println("ignored -> " + param.getName() + ".run()"); } else { - writer.print(param.getName() + " != null ? new io.smallrye.mutiny.vertx.DelegatingConsumerHandler(" + param.getName() + ") : null"); + writer.print("io.smallrye.mutiny.vertx.MutinyHelper.convertConsumer(" + param.getName() + ")"); } } }