Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC Async lambda #258

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/main/java/io/vertx/codegen/ClassModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -462,14 +463,18 @@ private boolean isLegalVertxGenTypeArgument(TypeInfo arg) {
}

private boolean isLegalVertxGenInterface(TypeInfo type, boolean allowParameterized) {
return isLegalVertxGenInterface(type, allowParameterized, this::isLegalVertxGenTypeArgument);
}

private boolean isLegalVertxGenInterface(TypeInfo type, boolean allowParameterized, Predicate<TypeInfo> p) {
if (type.getKind() == ClassKind.API) {
if (type.isParameterized()) {
ParameterizedTypeInfo parameterized = (ParameterizedTypeInfo) type;
return allowParameterized &&
parameterized
.getArgs()
.stream()
.noneMatch(arg -> !isLegalVertxGenTypeArgument(arg) || arg.isNullable());
.noneMatch(arg -> !p.test(arg) || arg.isNullable());
} else {
return true;
}
Expand All @@ -482,7 +487,8 @@ 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);
return isLegalNonCallableParam(returnType, allowAnyJavaType);
return isLegalNonCallableReturnType(returnType, allowAnyJavaType) ||
isLegalVertxGenInterface(returnType, true, t -> isLegalNonCallableReturnType(t, allowAnyJavaType));
}
}
return false;
Expand All @@ -491,9 +497,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 isLegalNonCallableReturnType(eventType, allowAnyJavaType) ||
eventType.getKind() == ClassKind.VOID ||
isLegalVertxGenInterface(eventType, true, t -> isLegalNonCallableReturnType(t, allowAnyJavaType));
}
return false;
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/io/vertx/codegen/MethodInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Future<T>> 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;
}
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/io/vertx/core/Future.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package io.vertx.core;

import io.vertx.codegen.annotations.VertxGen;

/**
* @author <a href="mailto:[email protected]">Julien Viet</a>
*/
@VertxGen
public interface Future<T> extends AsyncResult<T>, Handler<AsyncResult<T>> {

static <T> Future<T> succeededFuture(T result) {
Expand Down
67 changes: 67 additions & 0 deletions src/tck/java/io/vertx/codegen/testmodel/AsyncFunctionParamTCK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 <a href="slinkydeveloper.com">Francesco Guardiani</a>
*/
@VertxGen
public interface AsyncFunctionParamTCK {

List<String> methodWithBasicParam(
Function<Byte, Future<String>> byteFunc,
Function<Short, Future<String>> shortFunc,
Function<Integer, Future<String>> integerFunc,
Function<Long, Future<String>> longFunc,
Function<Float, Future<String>> floatFunc,
Function<Double, Future<String>> doubleFunc,
Function<Boolean, Future<String>> booleanFunc,
Function<Character, Future<String>> charFunc,
Function<String, Future<String>> stringFunc
);

List<String> methodWithJsonParam(Function<JsonObject, Future<String>> objectFunc, Function<JsonArray, Future<String>> arrayFunc);

String methodWithVoidParam(Function<Void, Future<String>> func);
String methodWithUserTypeParam(RefedInterface1 arg, Function<RefedInterface1, Future<String>> func);
String methodWithObjectParam(Object arg, Function<Object, Future<String>> func);
String methodWithDataObjectParam(Function<TestDataObject, Future<String>> func);
String methodWithEnumParam(Function<TestEnum, Future<String>> func);
String methodWithListParam(Function<List<String>, Future<String>> stringFunc);
String methodWithSetParam(Function<Set<String>, Future<String>> func);
String methodWithMapParam(Function<Map<String, String>, Future<String>> func);

<T> String methodWithGenericParam(T t, Function<T, Future<String>> func);
<T> String methodWithGenericUserTypeParam(T t, Function<GenericRefedInterface<T>, Future<String>> func);

String methodWithBasicReturn(
Function<String, Future<Byte>> byteFunc,
Function<String, Future<Short>> shortFunc,
Function<String, Future<Integer>> integerFunc,
Function<String, Future<Long>> longFunc,
Function<String, Future<Float>> floatFunc,
Function<String, Future<Double>> doubleFunc,
Function<String, Future<Boolean>> booleanFunc,
Function<String, Future<Character>> charFunc,
Function<String, Future<String>> stringFunc
);

String methodWithJsonReturn(Function<String, Future<JsonObject>> objectFunc, Function<String, Future<JsonArray>> arrayFunc);
String methodWithDataObjectReturn(Function<String, Future<TestDataObject>> func);
String methodWithEnumReturn(Function<String, Future<TestEnum>> func);
String methodWithListReturn(Function<String, Future<List<String>>> func);
String methodWithSetReturn(Function<String, Future<Set<String>>> func);
String methodWithMapReturn(Function<String, Future<Map<String, String>>> func);
<T> String methodWithGenericReturn(Function<Integer, Future<T>> func);
<T> String methodWithGenericUserTypeReturn(Function<GenericRefedInterface<T>, Future<GenericRefedInterface<T>>> func);

}
67 changes: 67 additions & 0 deletions src/tck/java/io/vertx/codegen/testmodel/AsyncSupplierParamTCK.java
Original file line number Diff line number Diff line change
@@ -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 <a href="slinkydeveloper.com">Francesco Guardiani</a>
*/
@VertxGen
public interface AsyncSupplierParamTCK {

List<String> methodWithBasicParam(
Handler<Future<String>> byteFunc,
Handler<Future<String>> shortFunc,
Handler<Future<String>> integerFunc,
Handler<Future<String>> longFunc,
Handler<Future<String>> floatFunc,
Handler<Future<String>> doubleFunc,
Handler<Future<String>> booleanFunc,
Handler<Future<String>> charFunc,
Handler<Future<String>> stringFunc
);

List<String> methodWithJsonParam(Handler<Future<String>> objectFunc, Handler<Future<String>> arrayFunc);

String methodWithVoidParam(Handler<Future<String>> func);
String methodWithUserTypeParam(RefedInterface1 arg, Handler<Future<String>> func);
String methodWithObjectParam(Object arg, Handler<Future<String>> func);
String methodWithDataObjectParam(Handler<Future<String>> func);
String methodWithEnumParam(Handler<Future<String>> func);
String methodWithListParam(Handler<Future<String>> stringFunc);
String methodWithSetParam(Handler<Future<String>> func);
String methodWithMapParam(Handler<Future<Map<String, String>>> func);

<T> String methodWithGenericParam(T t, Handler<Future<String>> func);
<T> String methodWithGenericUserTypeParam(T t, Handler<Future<String>> func);

String methodWithBasicReturn(
Handler<Future<Byte>> byteFunc,
Handler<Future<Short>> shortFunc,
Handler<Future<Integer>> integerFunc,
Handler<Future<Long>> longFunc,
Handler<Future<Float>> floatFunc,
Handler<Future<Double>> doubleFunc,
Handler<Future<Boolean>> booleanFunc,
Handler<Future<Character>> charFunc,
Handler<Future<String>> stringFunc
);

String methodWithJsonReturn(Handler<Future<JsonObject>> objectFunc, Handler<Future<JsonArray>> arrayFunc);
String methodWithDataObjectReturn(Handler<Future<TestDataObject>> func);
String methodWithEnumReturn(Handler<Future<TestEnum>> func);
String methodWithListReturn(Handler<Future<List<String>>> func);
String methodWithSetReturn(Handler<Future<Set<String>>> func);
String methodWithMapReturn(Handler<Future<Map<String, String>>> func);
<T> String methodWithGenericReturn(Handler<Future<T>> func);
<T> String methodWithGenericUserTypeReturn(Handler<Future<GenericRefedInterface<T>>> func);

}
Loading