From 13cd0fff9050ba91b5df2b844e14c0daaef8df83 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Mon, 3 Jun 2019 09:34:11 +0200 Subject: [PATCH 1/5] Added test for async lambda Signed-off-by: slinkydeveloper --- .../java/io/vertx/codegen/ClassModel.java | 3 + .../java/io/vertx/test/codegen/ClassTest.java | 79 +++++++++++++++++++ .../MethodWithValidAsyncFunctionParams.java | 61 ++++++++++++++ 3 files changed, 143 insertions(+) create mode 100644 src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java diff --git a/src/main/java/io/vertx/codegen/ClassModel.java b/src/main/java/io/vertx/codegen/ClassModel.java index c7f9fe7f7..c07d35c2e 100644 --- a/src/main/java/io/vertx/codegen/ClassModel.java +++ b/src/main/java/io/vertx/codegen/ClassModel.java @@ -482,6 +482,9 @@ private boolean isLegalFunctionType(TypeInfo typeInfo, boolean allowAnyJavaType) TypeInfo paramType = ((ParameterizedTypeInfo) typeInfo).getArgs().get(0); if (isLegalCallbackValueType(paramType, allowAnyJavaType) || paramType.getKind() == ClassKind.THROWABLE) { TypeInfo returnType = ((ParameterizedTypeInfo) typeInfo).getArgs().get(1); + if (returnType.getKind() == ClassKind.ASYNC_RESULT) { + + } return isLegalNonCallableParam(returnType, allowAnyJavaType); } } diff --git a/src/test/java/io/vertx/test/codegen/ClassTest.java b/src/test/java/io/vertx/test/codegen/ClassTest.java index 0ee74adeb..2cfb9bf2d 100644 --- a/src/test/java/io/vertx/test/codegen/ClassTest.java +++ b/src/test/java/io/vertx/test/codegen/ClassTest.java @@ -9,6 +9,7 @@ import io.vertx.codegen.testmodel.TestGenEnum; import io.vertx.codegen.type.*; import io.vertx.core.AsyncResult; +import io.vertx.core.Future; import io.vertx.core.Handler; import io.vertx.core.json.JsonArray; import io.vertx.core.json.JsonObject; @@ -584,6 +585,84 @@ public void testValidFunctionParams() throws Exception { checkParam(params.get(10), "mapJsonArrayFunction", new TypeLiteral, Map>>(){}); } + @Test + public void testValidAsyncFunctionParams() throws Exception { + ClassModel model = new GeneratorHelper().generateClass(MethodWithValidAsyncFunctionParams.class); + + MethodInfo method = model.getMethods().get(0); + checkMethod(method, "methodWithFunctionParams", 18, "void", MethodKind.OTHER); + List params = method.getParams(); + checkParam(params.get(0), "byteFunction", new TypeLiteral>>() {}); + checkParam(params.get(1), "shortFunction", new TypeLiteral>>() {}); + checkParam(params.get(2), "intFunction", new TypeLiteral>>() {}); + checkParam(params.get(3), "longFunction", new TypeLiteral>>(){}); + checkParam(params.get(4), "floatFunction", new TypeLiteral>>(){}); + checkParam(params.get(5), "doubleFunction", new TypeLiteral>>(){}); + checkParam(params.get(6), "booleanFunction", new TypeLiteral>>(){}); + checkParam(params.get(7), "charFunction", new TypeLiteral>>(){}); + checkParam(params.get(8), "strFunction", new TypeLiteral>>(){}); + checkParam(params.get(9), "gen1Function", new TypeLiteral>>(){}); + checkParam(params.get(10), "gen2Function", new TypeLiteral>>(){}); + checkParam(params.get(11), "voidFunction", new TypeLiteral>>(){}); + checkParam(params.get(12), "throwableFunction", new TypeLiteral>>(){}); + checkParam(params.get(13), "dataObjectFunction", new TypeLiteral>>(){}); + checkParam(params.get(14), "enumFunction", new TypeLiteral>>(){}); + checkParam(params.get(15), "objectFunction", new TypeLiteral>>(){}); + checkParam(params.get(16), "genericFunction", new TypeLiteral>>(){}); + checkParam(params.get(17), "genericUserTypeFunction", new TypeLiteral, Future>>>(){}); + + method = model.getMethods().get(1); + checkMethod(method, "methodWithListFunctionParams", 14, "void", MethodKind.OTHER); + params = method.getParams(); + checkParam(params.get(0), "listByteFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(1), "listShortFunction", new TypeLiteral, Future>>>() {}); + checkParam(params.get(2), "listIntFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(3), "listLongFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(4), "listFloatFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(5), "listDoubleFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(6), "listBooleanFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(7), "listCharFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(8), "listStrFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(9), "listVertxGenFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(10), "listJsonObjectFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(11), "listJsonArrayFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(12), "listDataObjectFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(13), "listEnumFunction", new TypeLiteral, Future>>>(){}); + + method = model.getMethods().get(2); + checkMethod(method, "methodWithSetFunctionParams", 14, "void", MethodKind.OTHER); + params = method.getParams(); + checkParam(params.get(0), "setByteFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(1), "setShortFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(2), "setIntFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(3), "setLongFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(4), "setFloatFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(5), "setDoubleFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(6), "setBooleanFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(7), "setCharFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(8), "setStrFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(9), "setVertxGenFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(10), "setJsonObjectFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(11), "setJsonArrayFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(12), "setDataObjectFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(13), "setEnumFunction", new TypeLiteral, Future>>>(){}); + + method = model.getMethods().get(3); + checkMethod(method, "methodWithMapFunctionParams", 11, "void", MethodKind.OTHER); + params = method.getParams(); + checkParam(params.get(0), "mapByteFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(1), "mapShortFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(2), "mapIntFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(3), "mapLongFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(4), "mapFloatFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(5), "mapDoubleFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(6), "mapBooleanFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(7), "mapCharFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(8), "mapStrFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(9), "mapJsonObjectFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(10), "mapJsonArrayFunction", new TypeLiteral>, Map>>(){}); + } + @Test public void testMethodWithInvalidFunctionParams() throws Exception { assertGenInvalid(MethodWithInvalidFunctionParam1.class); diff --git a/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java new file mode 100644 index 000000000..33d66f231 --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014 Red Hat, Inc. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * The Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php + * + * You may elect to redistribute this code under either of these licenses. + */ + +package io.vertx.test.codegen.testapi; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.codegen.testmodel.TestDataObject; +import io.vertx.codegen.testmodel.TestEnum; +import io.vertx.core.Future; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * @author Tim Fox + */ +@VertxGen +public interface MethodWithValidAsyncFunctionParams { + + void methodWithFunctionParams(Function> byteFunction, Function> shortFunction, Function> intFunction, + Function> longFunction, Function> floatFunction, Function> doubleFunction, + Function> booleanFunction, Function> charFunction, Function> strFunction, + Function> gen1Function, Function> gen2Function, + Function> voidFunction, Function> throwableFunction, Function> dataObjectFunction, + Function> enumFunction, Function> objectFunction, Function> genericFunction, Function, Future>> genericUserTypeFunction); + + void methodWithListFunctionParams(Function, Future>> listByteFunction, Function, Future>> listShortFunction, Function, Future>> listIntFunction, + Function, Future>> listLongFunction, Function, Future>> listFloatFunction, Function, Future>> listDoubleFunction, + Function, Future>> listBooleanFunction, Function, Future>> listCharFunction, Function, Future>> listStrFunction, + Function, Future>> listVertxGenFunction, Function, Future>> listJsonObjectFunction, Function, Future>> listJsonArrayFunction, + Function, Future>> listDataObjectFunction, Function, Future>> listEnumFunction); + + void methodWithSetFunctionParams(Function, Future>> setByteFunction, Function, Future>> setShortFunction, Function, Future>> setIntFunction, + Function, Future>> setLongFunction, Function, Future>> setFloatFunction, Function, Future>> setDoubleFunction, + Function, Future>> setBooleanFunction, Function, Future>> setCharFunction, Function, Future>> setStrFunction, + Function, Future>> setVertxGenFunction, Function, Future>> setJsonObjectFunction, Function, Future>> setJsonArrayFunction, + Function, Future>> setDataObjectFunction, Function, Future>> setEnumFunction); + + void methodWithMapFunctionParams(Function>, Map> mapByteFunction, Function>, Map> mapShortFunction, Function>, Map> mapIntFunction, + Function>, Map> mapLongFunction, Function>, Map> mapFloatFunction, Function>, Map> mapDoubleFunction, + Function>, Map> mapBooleanFunction, Function>, Map> mapCharFunction, Function>, Map> mapStrFunction, + Function>, Map> mapJsonObjectFunction, Function>, Map> mapJsonArrayFunction); + +} From 11b0f7a51034b9db910e187b64fd92d1057c8578 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Mon, 3 Jun 2019 15:25:33 +0200 Subject: [PATCH 2/5] Implemented async lambdas rfc Signed-off-by: slinkydeveloper --- .../java/io/vertx/codegen/ClassModel.java | 11 +- .../java/io/vertx/codegen/MethodInfo.java | 11 +- .../testmodel/AsyncFunctionParamTCK.java | 68 ++++++++++++ .../testmodel/AsyncSupplierParamTCK.java | 67 ++++++++++++ .../java/io/vertx/test/codegen/ClassTest.java | 100 ++++++++++++++++-- .../MethodWithValidAsyncFunctionParams.java | 8 +- .../MethodWithValidAsyncSupplierParams.java | 61 +++++++++++ 7 files changed, 300 insertions(+), 26 deletions(-) create mode 100644 src/tck/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java create mode 100644 src/tck/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java create mode 100644 src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncSupplierParams.java diff --git a/src/main/java/io/vertx/codegen/ClassModel.java b/src/main/java/io/vertx/codegen/ClassModel.java index c07d35c2e..357a96592 100644 --- a/src/main/java/io/vertx/codegen/ClassModel.java +++ b/src/main/java/io/vertx/codegen/ClassModel.java @@ -482,10 +482,7 @@ private boolean isLegalFunctionType(TypeInfo typeInfo, boolean allowAnyJavaType) TypeInfo paramType = ((ParameterizedTypeInfo) typeInfo).getArgs().get(0); if (isLegalCallbackValueType(paramType, allowAnyJavaType) || paramType.getKind() == ClassKind.THROWABLE) { TypeInfo returnType = ((ParameterizedTypeInfo) typeInfo).getArgs().get(1); - if (returnType.getKind() == ClassKind.ASYNC_RESULT) { - - } - return isLegalNonCallableParam(returnType, allowAnyJavaType); + return "io.vertx.core.Future".equals(returnType.getRaw() != null ? returnType.getRaw().getName() : null) || isLegalNonCallableParam(returnType, allowAnyJavaType); } } return false; @@ -494,9 +491,9 @@ private boolean isLegalFunctionType(TypeInfo typeInfo, boolean allowAnyJavaType) private boolean isLegalHandlerType(TypeInfo type, boolean allowAnyJavaType) { if (type.getErased().getKind() == ClassKind.HANDLER) { TypeInfo eventType = ((ParameterizedTypeInfo) type).getArgs().get(0); - if (isLegalCallbackValueType(eventType, allowAnyJavaType) || eventType.getKind() == ClassKind.THROWABLE) { - return true; - } + return (eventType.getRaw() != null && "io.vertx.core.Future".equals(eventType.getRaw().getName())) || + isLegalCallbackValueType(eventType, allowAnyJavaType) || + eventType.getKind() == ClassKind.THROWABLE; } return false; } diff --git a/src/main/java/io/vertx/codegen/MethodInfo.java b/src/main/java/io/vertx/codegen/MethodInfo.java index 1aad15c52..fd9d0c954 100644 --- a/src/main/java/io/vertx/codegen/MethodInfo.java +++ b/src/main/java/io/vertx/codegen/MethodInfo.java @@ -87,10 +87,13 @@ public MethodKind getKind() { TypeInfo lastParamType = params.get(lastParamIndex).type; if (lastParamType.getKind() == ClassKind.HANDLER) { TypeInfo typeArg = ((ParameterizedTypeInfo) lastParamType).getArgs().get(0); - if (typeArg.getKind() == ClassKind.ASYNC_RESULT) { - return MethodKind.FUTURE; - } else { - return MethodKind.HANDLER; + // If the last param is a Handler> it should not be managed as a MethodKind.HANDLER + if (typeArg.getRaw() == null || !typeArg.getRaw().getName().equals("io.vertx.core.Future")) { + if (typeArg.getKind() == ClassKind.ASYNC_RESULT) { + return MethodKind.FUTURE; + } else { + return MethodKind.HANDLER; + } } } } diff --git a/src/tck/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java b/src/tck/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java new file mode 100644 index 000000000..b910cc603 --- /dev/null +++ b/src/tck/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java @@ -0,0 +1,68 @@ +package io.vertx.codegen.testmodel; + +import io.vertx.codegen.annotations.Nullable; +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Future; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * @author Francesco Guardiani + */ +@VertxGen +public interface AsyncFunctionParamTCK { + + List methodWithBasicParam( + Function> byteFunc, + Function> shortFunc, + Function> integerFunc, + Function> longFunc, + Function> floatFunc, + Function> doubleFunc, + Function> booleanFunc, + Function> charFunc, + Function> stringFunc + ); + + List methodWithJsonParam(Function> objectFunc, Function> arrayFunc); + + String methodWithVoidParam(Function> func); + String methodWithUserTypeParam(RefedInterface1 arg, Function> func); + String methodWithObjectParam(Object arg, Function> func); + String methodWithDataObjectParam(Function> func); + String methodWithEnumParam(Function> func); + String methodWithListParam(Function, Future> stringFunc); + String methodWithSetParam(Function, Future> func); + String methodWithMapParam(Function, Future> func); + + String methodWithGenericParam(T t, Function> func); + String methodWithGenericUserTypeParam(T t, Function, Future> func); + + String methodWithBasicReturn( + Function> byteFunc, + Function> shortFunc, + Function> integerFunc, + Function> longFunc, + Function> floatFunc, + Function> doubleFunc, + Function> booleanFunc, + Function> charFunc, + Function> stringFunc + ); + + String methodWithJsonReturn(Function> objectFunc, Function> arrayFunc); + String methodWithObjectReturn(Function> func); + String methodWithDataObjectReturn(Function> func); + String methodWithEnumReturn(Function> func); + String methodWithListReturn(Function>> func); + String methodWithSetReturn(Function>> func); + String methodWithMapReturn(Function>> func); + String methodWithGenericReturn(Function> func); + String methodWithGenericUserTypeReturn(Function, Future>> func); + +} diff --git a/src/tck/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java b/src/tck/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java new file mode 100644 index 000000000..4ec56db7f --- /dev/null +++ b/src/tck/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java @@ -0,0 +1,67 @@ +package io.vertx.codegen.testmodel; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +/** + * @author Francesco Guardiani + */ +@VertxGen +public interface AsyncSupplierParamTCK { + + List methodWithBasicParam( + Handler> byteFunc, + Handler> shortFunc, + Handler> integerFunc, + Handler> longFunc, + Handler> floatFunc, + Handler> doubleFunc, + Handler> booleanFunc, + Handler> charFunc, + Handler> stringFunc + ); + + List methodWithJsonParam(Handler> objectFunc, Handler> arrayFunc); + + String methodWithVoidParam(Handler> func); + String methodWithUserTypeParam(RefedInterface1 arg, Handler> func); + String methodWithObjectParam(Object arg, Handler> func); + String methodWithDataObjectParam(Handler> func); + String methodWithEnumParam(Handler> func); + String methodWithListParam(Handler> stringFunc); + String methodWithSetParam(Handler> func); + String methodWithMapParam(Handler>> func); + + String methodWithGenericParam(T t, Handler> func); + String methodWithGenericUserTypeParam(T t, Handler> func); + + String methodWithBasicReturn( + Handler> byteFunc, + Handler> shortFunc, + Handler> integerFunc, + Handler> longFunc, + Handler> floatFunc, + Handler> doubleFunc, + Handler> booleanFunc, + Handler> charFunc, + Handler> stringFunc + ); + + String methodWithJsonReturn(Handler> objectFunc, Handler> arrayFunc); + String methodWithDataObjectReturn(Handler> func); + String methodWithEnumReturn(Handler> func); + String methodWithListReturn(Handler>> func); + String methodWithSetReturn(Handler>> func); + String methodWithMapReturn(Handler>> func); + String methodWithGenericReturn(Handler> func); + String methodWithGenericUserTypeReturn(Handler>> func); + +} diff --git a/src/test/java/io/vertx/test/codegen/ClassTest.java b/src/test/java/io/vertx/test/codegen/ClassTest.java index 2cfb9bf2d..5a7605368 100644 --- a/src/test/java/io/vertx/test/codegen/ClassTest.java +++ b/src/test/java/io/vertx/test/codegen/ClassTest.java @@ -650,19 +650,97 @@ public void testValidAsyncFunctionParams() throws Exception { method = model.getMethods().get(3); checkMethod(method, "methodWithMapFunctionParams", 11, "void", MethodKind.OTHER); params = method.getParams(); - checkParam(params.get(0), "mapByteFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(1), "mapShortFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(2), "mapIntFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(3), "mapLongFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(4), "mapFloatFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(5), "mapDoubleFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(6), "mapBooleanFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(7), "mapCharFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(8), "mapStrFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(9), "mapJsonObjectFunction", new TypeLiteral>, Map>>(){}); - checkParam(params.get(10), "mapJsonArrayFunction", new TypeLiteral>, Map>>(){}); + checkParam(params.get(0), "mapByteFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(1), "mapShortFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(2), "mapIntFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(3), "mapLongFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(4), "mapFloatFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(5), "mapDoubleFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(6), "mapBooleanFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(7), "mapCharFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(8), "mapStrFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(9), "mapJsonObjectFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(10), "mapJsonArrayFunction", new TypeLiteral, Future>>>(){}); } + @Test + public void testValidAsyncSupplierParams() throws Exception { + ClassModel model = new GeneratorHelper().generateClass(MethodWithValidAsyncSupplierParams.class); + + MethodInfo method = model.getMethods().get(0); + checkMethod(method, "methodWithSupplierParams", 18, "void", MethodKind.OTHER); + List params = method.getParams(); + checkParam(params.get(0), "byteSupplier", new TypeLiteral>>() {}); + checkParam(params.get(1), "shortSupplier", new TypeLiteral>>() {}); + checkParam(params.get(2), "intSupplier", new TypeLiteral>>() {}); + checkParam(params.get(3), "longSupplier", new TypeLiteral>>(){}); + checkParam(params.get(4), "floatSupplier", new TypeLiteral>>(){}); + checkParam(params.get(5), "doubleSupplier", new TypeLiteral>>(){}); + checkParam(params.get(6), "booleanSupplier", new TypeLiteral>>(){}); + checkParam(params.get(7), "charSupplier", new TypeLiteral>>(){}); + checkParam(params.get(8), "strSupplier", new TypeLiteral>>(){}); + checkParam(params.get(9), "gen1Supplier", new TypeLiteral>>(){}); + checkParam(params.get(10), "gen2Supplier", new TypeLiteral>>(){}); + checkParam(params.get(11), "voidSupplier", new TypeLiteral>>(){}); + checkParam(params.get(12), "throwableSupplier", new TypeLiteral>>(){}); + checkParam(params.get(13), "dataObjectSupplier", new TypeLiteral>>(){}); + checkParam(params.get(14), "enumSupplier", new TypeLiteral>>(){}); + checkParam(params.get(15), "objectSupplier", new TypeLiteral>>(){}); + checkParam(params.get(16), "genericSupplier", new TypeLiteral>>(){}); + checkParam(params.get(17), "genericUserTypeSupplier", new TypeLiteral>>>(){}); + + method = model.getMethods().get(1); + checkMethod(method, "methodWithListSupplierParams", 14, "void", MethodKind.OTHER); + params = method.getParams(); + checkParam(params.get(0), "listByteSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(1), "listShortSupplier", new TypeLiteral>>>() {}); + checkParam(params.get(2), "listIntSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(3), "listLongSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(4), "listFloatSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(5), "listDoubleSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(6), "listBooleanSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(7), "listCharSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(8), "listStrSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(9), "listVertxGenSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(10), "listJsonObjectSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(11), "listJsonArraySupplier", new TypeLiteral>>>(){}); + checkParam(params.get(12), "listDataObjectSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(13), "listEnumSupplier", new TypeLiteral>>>(){}); + + method = model.getMethods().get(2); + checkMethod(method, "methodWithSetSupplierParams", 14, "void", MethodKind.OTHER); + params = method.getParams(); + checkParam(params.get(0), "setByteSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(1), "setShortSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(2), "setIntSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(3), "setLongSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(4), "setFloatSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(5), "setDoubleSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(6), "setBooleanSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(7), "setCharSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(8), "setStrSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(9), "setVertxGenSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(10), "setJsonObjectSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(11), "setJsonArraySupplier", new TypeLiteral>>>(){}); + checkParam(params.get(12), "setDataObjectSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(13), "setEnumSupplier", new TypeLiteral>>>(){}); + + method = model.getMethods().get(3); + checkMethod(method, "methodWithMapSupplierParams", 11, "void", MethodKind.OTHER); + params = method.getParams(); + checkParam(params.get(0), "mapByteSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(1), "mapShortSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(2), "mapIntSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(3), "mapLongSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(4), "mapFloatSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(5), "mapDoubleSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(6), "mapBooleanSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(7), "mapCharSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(8), "mapStrSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(9), "mapJsonObjectSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(10), "mapJsonArraySupplier", new TypeLiteral>>>(){}); + } + @Test public void testMethodWithInvalidFunctionParams() throws Exception { assertGenInvalid(MethodWithInvalidFunctionParam1.class); diff --git a/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java index 33d66f231..18426818b 100644 --- a/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java +++ b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java @@ -53,9 +53,9 @@ void methodWithSetFunctionParams(Function, Future>> setByteF Function, Future>> setVertxGenFunction, Function, Future>> setJsonObjectFunction, Function, Future>> setJsonArrayFunction, Function, Future>> setDataObjectFunction, Function, Future>> setEnumFunction); - void methodWithMapFunctionParams(Function>, Map> mapByteFunction, Function>, Map> mapShortFunction, Function>, Map> mapIntFunction, - Function>, Map> mapLongFunction, Function>, Map> mapFloatFunction, Function>, Map> mapDoubleFunction, - Function>, Map> mapBooleanFunction, Function>, Map> mapCharFunction, Function>, Map> mapStrFunction, - Function>, Map> mapJsonObjectFunction, Function>, Map> mapJsonArrayFunction); + void methodWithMapFunctionParams(Function, Future>> mapByteFunction, Function, Future>> mapShortFunction, Function, Future>> mapIntFunction, + Function, Future>> mapLongFunction, Function, Future>> mapFloatFunction, Function, Future>> mapDoubleFunction, + Function, Future>> mapBooleanFunction, Function, Future>> mapCharFunction, Function, Future>> mapStrFunction, + Function, Future>> mapJsonObjectFunction, Function, Future>> mapJsonArrayFunction); } diff --git a/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncSupplierParams.java b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncSupplierParams.java new file mode 100644 index 000000000..4888f77e0 --- /dev/null +++ b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncSupplierParams.java @@ -0,0 +1,61 @@ +/* + * Copyright 2014 Red Hat, Inc. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Apache License v2.0 which accompanies this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * The Apache License v2.0 is available at + * http://www.opensource.org/licenses/apache2.0.php + * + * You may elect to redistribute this code under either of these licenses. + */ + +package io.vertx.test.codegen.testapi; + +import io.vertx.codegen.annotations.VertxGen; +import io.vertx.codegen.testmodel.TestDataObject; +import io.vertx.codegen.testmodel.TestEnum; +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * @author Tim Fox + */ +@VertxGen +public interface MethodWithValidAsyncSupplierParams { + + void methodWithSupplierParams(Handler> byteSupplier, Handler> shortSupplier, Handler> intSupplier, + Handler> longSupplier, Handler> floatSupplier, Handler> doubleSupplier, + Handler> booleanSupplier, Handler> charSupplier, Handler> strSupplier, + Handler> gen1Supplier, Handler> gen2Supplier, + Handler> voidSupplier, Handler> throwableSupplier, Handler> dataObjectSupplier, + Handler> enumSupplier, Handler> objectSupplier, Handler> genericSupplier, Handler>> genericUserTypeSupplier); + + void methodWithListSupplierParams(Handler>> listByteSupplier, Handler>> listShortSupplier, Handler>> listIntSupplier, + Handler>> listLongSupplier, Handler>> listFloatSupplier, Handler>> listDoubleSupplier, + Handler>> listBooleanSupplier, Handler>> listCharSupplier, Handler>> listStrSupplier, + Handler>> listVertxGenSupplier, Handler>> listJsonObjectSupplier, Handler>> listJsonArraySupplier, + Handler>> listDataObjectSupplier, Handler>> listEnumSupplier); + + void methodWithSetSupplierParams(Handler>> setByteSupplier, Handler>> setShortSupplier, Handler>> setIntSupplier, + Handler>> setLongSupplier, Handler>> setFloatSupplier, Handler>> setDoubleSupplier, + Handler>> setBooleanSupplier, Handler>> setCharSupplier, Handler>> setStrSupplier, + Handler>> setVertxGenSupplier, Handler>> setJsonObjectSupplier, Handler>> setJsonArraySupplier, + Handler>> setDataObjectSupplier, Handler>> setEnumSupplier); + + void methodWithMapSupplierParams(Handler>> mapByteSupplier, Handler>> mapShortSupplier, Handler>> mapIntSupplier, + Handler>> mapLongSupplier, Handler>> mapFloatSupplier, Handler>> mapDoubleSupplier, + Handler>> mapBooleanSupplier, Handler>> mapCharSupplier, Handler>> mapStrSupplier, + Handler>> mapJsonObjectSupplier, Handler>> mapJsonArraySupplier); + +} From 6f1c02a8f74696a2a4fb1887a61e4890c54f5a9f Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Tue, 4 Jun 2019 10:49:05 +0200 Subject: [PATCH 3/5] Relaxed Handler and Function return type params Signed-off-by: slinkydeveloper --- .../java/io/vertx/codegen/ClassModel.java | 26 ++++++++++++++++--- src/main/java/io/vertx/core/Future.java | 3 +++ .../java/io/vertx/test/codegen/ClassTest.java | 24 +++++++---------- .../MethodWithValidAsyncFunctionParams.java | 4 +-- .../MethodWithValidAsyncSupplierParams.java | 4 +-- 5 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/vertx/codegen/ClassModel.java b/src/main/java/io/vertx/codegen/ClassModel.java index 357a96592..f765e49d6 100644 --- a/src/main/java/io/vertx/codegen/ClassModel.java +++ b/src/main/java/io/vertx/codegen/ClassModel.java @@ -35,6 +35,7 @@ import javax.lang.model.util.Types; import javax.tools.Diagnostic; import java.util.*; +import java.util.function.Predicate; import java.util.logging.Logger; import java.util.stream.Collectors; @@ -477,12 +478,29 @@ private boolean isLegalVertxGenInterface(TypeInfo type, boolean allowParameteriz return false; } + private boolean isLegalVertxGenInterface(TypeInfo type, boolean allowParameterized, Predicate p) { + if (type.getKind() == ClassKind.API) { + if (type.isParameterized()) { + ParameterizedTypeInfo parameterized = (ParameterizedTypeInfo) type; + return allowParameterized && + parameterized + .getArgs() + .stream() + .noneMatch(arg -> !p.test(arg) || arg.isNullable()); + } else { + return true; + } + } + return false; + } + private boolean isLegalFunctionType(TypeInfo typeInfo, boolean allowAnyJavaType) { if (typeInfo.getErased().getKind() == ClassKind.FUNCTION) { TypeInfo paramType = ((ParameterizedTypeInfo) typeInfo).getArgs().get(0); if (isLegalCallbackValueType(paramType, allowAnyJavaType) || paramType.getKind() == ClassKind.THROWABLE) { TypeInfo returnType = ((ParameterizedTypeInfo) typeInfo).getArgs().get(1); - return "io.vertx.core.Future".equals(returnType.getRaw() != null ? returnType.getRaw().getName() : null) || isLegalNonCallableParam(returnType, allowAnyJavaType); + return isLegalNonCallableReturnType(returnType, allowAnyJavaType) || + isLegalVertxGenInterface(returnType, true, t -> isLegalNonCallableReturnType(t, allowAnyJavaType)); } } return false; @@ -491,9 +509,9 @@ private boolean isLegalFunctionType(TypeInfo typeInfo, boolean allowAnyJavaType) private boolean isLegalHandlerType(TypeInfo type, boolean allowAnyJavaType) { if (type.getErased().getKind() == ClassKind.HANDLER) { TypeInfo eventType = ((ParameterizedTypeInfo) type).getArgs().get(0); - return (eventType.getRaw() != null && "io.vertx.core.Future".equals(eventType.getRaw().getName())) || - isLegalCallbackValueType(eventType, allowAnyJavaType) || - eventType.getKind() == ClassKind.THROWABLE; + return isLegalNonCallableReturnType(eventType, allowAnyJavaType) || + eventType.getKind() == ClassKind.VOID || + isLegalVertxGenInterface(eventType, true, t -> isLegalNonCallableReturnType(t, allowAnyJavaType)); } return false; } diff --git a/src/main/java/io/vertx/core/Future.java b/src/main/java/io/vertx/core/Future.java index b88058f00..b893d0130 100644 --- a/src/main/java/io/vertx/core/Future.java +++ b/src/main/java/io/vertx/core/Future.java @@ -1,8 +1,11 @@ package io.vertx.core; +import io.vertx.codegen.annotations.VertxGen; + /** * @author Julien Viet */ +@VertxGen public interface Future extends AsyncResult, Handler> { static Future succeededFuture(T result) { diff --git a/src/test/java/io/vertx/test/codegen/ClassTest.java b/src/test/java/io/vertx/test/codegen/ClassTest.java index 5a7605368..351968e1e 100644 --- a/src/test/java/io/vertx/test/codegen/ClassTest.java +++ b/src/test/java/io/vertx/test/codegen/ClassTest.java @@ -590,7 +590,7 @@ public void testValidAsyncFunctionParams() throws Exception { ClassModel model = new GeneratorHelper().generateClass(MethodWithValidAsyncFunctionParams.class); MethodInfo method = model.getMethods().get(0); - checkMethod(method, "methodWithFunctionParams", 18, "void", MethodKind.OTHER); + checkMethod(method, "methodWithFunctionParams", 16, "void", MethodKind.OTHER); List params = method.getParams(); checkParam(params.get(0), "byteFunction", new TypeLiteral>>() {}); checkParam(params.get(1), "shortFunction", new TypeLiteral>>() {}); @@ -604,12 +604,10 @@ public void testValidAsyncFunctionParams() throws Exception { checkParam(params.get(9), "gen1Function", new TypeLiteral>>(){}); checkParam(params.get(10), "gen2Function", new TypeLiteral>>(){}); checkParam(params.get(11), "voidFunction", new TypeLiteral>>(){}); - checkParam(params.get(12), "throwableFunction", new TypeLiteral>>(){}); - checkParam(params.get(13), "dataObjectFunction", new TypeLiteral>>(){}); - checkParam(params.get(14), "enumFunction", new TypeLiteral>>(){}); - checkParam(params.get(15), "objectFunction", new TypeLiteral>>(){}); - checkParam(params.get(16), "genericFunction", new TypeLiteral>>(){}); - checkParam(params.get(17), "genericUserTypeFunction", new TypeLiteral, Future>>>(){}); + checkParam(params.get(12), "dataObjectFunction", new TypeLiteral>>(){}); + checkParam(params.get(13), "enumFunction", new TypeLiteral>>(){}); + checkParam(params.get(14), "genericFunction", new TypeLiteral>>(){}); + checkParam(params.get(15), "genericUserTypeFunction", new TypeLiteral, Future>>>(){}); method = model.getMethods().get(1); checkMethod(method, "methodWithListFunctionParams", 14, "void", MethodKind.OTHER); @@ -668,7 +666,7 @@ public void testValidAsyncSupplierParams() throws Exception { ClassModel model = new GeneratorHelper().generateClass(MethodWithValidAsyncSupplierParams.class); MethodInfo method = model.getMethods().get(0); - checkMethod(method, "methodWithSupplierParams", 18, "void", MethodKind.OTHER); + checkMethod(method, "methodWithSupplierParams", 16, "void", MethodKind.OTHER); List params = method.getParams(); checkParam(params.get(0), "byteSupplier", new TypeLiteral>>() {}); checkParam(params.get(1), "shortSupplier", new TypeLiteral>>() {}); @@ -682,12 +680,10 @@ public void testValidAsyncSupplierParams() throws Exception { checkParam(params.get(9), "gen1Supplier", new TypeLiteral>>(){}); checkParam(params.get(10), "gen2Supplier", new TypeLiteral>>(){}); checkParam(params.get(11), "voidSupplier", new TypeLiteral>>(){}); - checkParam(params.get(12), "throwableSupplier", new TypeLiteral>>(){}); - checkParam(params.get(13), "dataObjectSupplier", new TypeLiteral>>(){}); - checkParam(params.get(14), "enumSupplier", new TypeLiteral>>(){}); - checkParam(params.get(15), "objectSupplier", new TypeLiteral>>(){}); - checkParam(params.get(16), "genericSupplier", new TypeLiteral>>(){}); - checkParam(params.get(17), "genericUserTypeSupplier", new TypeLiteral>>>(){}); + checkParam(params.get(12), "dataObjectSupplier", new TypeLiteral>>(){}); + checkParam(params.get(13), "enumSupplier", new TypeLiteral>>(){}); + checkParam(params.get(14), "genericSupplier", new TypeLiteral>>(){}); + checkParam(params.get(15), "genericUserTypeSupplier", new TypeLiteral>>>(){}); method = model.getMethods().get(1); checkMethod(method, "methodWithListSupplierParams", 14, "void", MethodKind.OTHER); diff --git a/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java index 18426818b..1d9f461c8 100644 --- a/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java +++ b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncFunctionParams.java @@ -38,8 +38,8 @@ void methodWithFunctionParams(Function> byteFunction, Fun Function> longFunction, Function> floatFunction, Function> doubleFunction, Function> booleanFunction, Function> charFunction, Function> strFunction, Function> gen1Function, Function> gen2Function, - Function> voidFunction, Function> throwableFunction, Function> dataObjectFunction, - Function> enumFunction, Function> objectFunction, Function> genericFunction, Function, Future>> genericUserTypeFunction); + Function> voidFunction, Function> dataObjectFunction, + Function> enumFunction, Function> genericFunction, Function, Future>> genericUserTypeFunction); void methodWithListFunctionParams(Function, Future>> listByteFunction, Function, Future>> listShortFunction, Function, Future>> listIntFunction, Function, Future>> listLongFunction, Function, Future>> listFloatFunction, Function, Future>> listDoubleFunction, diff --git a/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncSupplierParams.java b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncSupplierParams.java index 4888f77e0..80c91126c 100644 --- a/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncSupplierParams.java +++ b/src/test/java/io/vertx/test/codegen/testapi/MethodWithValidAsyncSupplierParams.java @@ -38,8 +38,8 @@ void methodWithSupplierParams(Handler> byteSupplier, Handler> longSupplier, Handler> floatSupplier, Handler> doubleSupplier, Handler> booleanSupplier, Handler> charSupplier, Handler> strSupplier, Handler> gen1Supplier, Handler> gen2Supplier, - Handler> voidSupplier, Handler> throwableSupplier, Handler> dataObjectSupplier, - Handler> enumSupplier, Handler> objectSupplier, Handler> genericSupplier, Handler>> genericUserTypeSupplier); + Handler> voidSupplier, Handler> dataObjectSupplier, + Handler> enumSupplier, Handler> genericSupplier, Handler>> genericUserTypeSupplier); void methodWithListSupplierParams(Handler>> listByteSupplier, Handler>> listShortSupplier, Handler>> listIntSupplier, Handler>> listLongSupplier, Handler>> listFloatSupplier, Handler>> listDoubleSupplier, From 1bd1fefcd58e6180bd64728e67b54c2ea99a8b7d Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Tue, 4 Jun 2019 10:53:58 +0200 Subject: [PATCH 4/5] Removed duplicated code isLefalVertxGenInterface Signed-off-by: slinkydeveloper --- src/main/java/io/vertx/codegen/ClassModel.java | 14 +------------- 1 file changed, 1 insertion(+), 13 deletions(-) diff --git a/src/main/java/io/vertx/codegen/ClassModel.java b/src/main/java/io/vertx/codegen/ClassModel.java index f765e49d6..04f882f94 100644 --- a/src/main/java/io/vertx/codegen/ClassModel.java +++ b/src/main/java/io/vertx/codegen/ClassModel.java @@ -463,19 +463,7 @@ private boolean isLegalVertxGenTypeArgument(TypeInfo arg) { } private boolean isLegalVertxGenInterface(TypeInfo type, boolean allowParameterized) { - if (type.getKind() == ClassKind.API) { - if (type.isParameterized()) { - ParameterizedTypeInfo parameterized = (ParameterizedTypeInfo) type; - return allowParameterized && - parameterized - .getArgs() - .stream() - .noneMatch(arg -> !isLegalVertxGenTypeArgument(arg) || arg.isNullable()); - } else { - return true; - } - } - return false; + return isLegalVertxGenInterface(type, allowParameterized, this::isLegalVertxGenTypeArgument); } private boolean isLegalVertxGenInterface(TypeInfo type, boolean allowParameterized, Predicate p) { From cd5aa78ba706c03258a9ae1193cbcbf3c3a936c3 Mon Sep 17 00:00:00 2001 From: slinkydeveloper Date: Tue, 4 Jun 2019 16:13:07 +0200 Subject: [PATCH 5/5] Removed illegal method in tck Signed-off-by: slinkydeveloper --- .../java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/tck/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java b/src/tck/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java index b910cc603..1325dc8b8 100644 --- a/src/tck/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java +++ b/src/tck/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java @@ -56,7 +56,6 @@ String methodWithBasicReturn( ); String methodWithJsonReturn(Function> objectFunc, Function> arrayFunc); - String methodWithObjectReturn(Function> func); String methodWithDataObjectReturn(Function> func); String methodWithEnumReturn(Function> func); String methodWithListReturn(Function>> func);