From 24cbc44d3b8768f054b4f4f4f5c3336002001f81 Mon Sep 17 00:00:00 2001 From: Matej Vasek Date: Wed, 3 May 2023 18:28:03 +0200 Subject: [PATCH] Enable Funqy function with same name Signed-off-by: Matej Vasek --- .../io/quarkus/funqy/test/Overloading.java | 17 ++++++++ .../quarkus/funqy/test/OverloadingTest.java | 31 +++++++++++++++ .../funqy/deployment/FunctionBuildItem.java | 8 +++- .../deployment/FunctionScannerBuildStep.java | 7 ++-- .../funqy/runtime/FunctionRecorder.java | 8 ++-- .../funqy/runtime/FunctionRegistry.java | 39 ++++++++++++++++++- 6 files changed, 100 insertions(+), 10 deletions(-) create mode 100644 extensions/funqy/funqy-knative-events/deployment/src/test/java/io/quarkus/funqy/test/Overloading.java create mode 100644 extensions/funqy/funqy-knative-events/deployment/src/test/java/io/quarkus/funqy/test/OverloadingTest.java diff --git a/extensions/funqy/funqy-knative-events/deployment/src/test/java/io/quarkus/funqy/test/Overloading.java b/extensions/funqy/funqy-knative-events/deployment/src/test/java/io/quarkus/funqy/test/Overloading.java new file mode 100644 index 0000000000000..5de19324730ce --- /dev/null +++ b/extensions/funqy/funqy-knative-events/deployment/src/test/java/io/quarkus/funqy/test/Overloading.java @@ -0,0 +1,17 @@ +package io.quarkus.funqy.test; + +import io.quarkus.funqy.Funq; + +public class Overloading { + + @Funq("intfun") + public int function(int i) { + return i * 2; + } + + @Funq("strfun") + public String function(String s) { + return s.toUpperCase(); + } + +} diff --git a/extensions/funqy/funqy-knative-events/deployment/src/test/java/io/quarkus/funqy/test/OverloadingTest.java b/extensions/funqy/funqy-knative-events/deployment/src/test/java/io/quarkus/funqy/test/OverloadingTest.java new file mode 100644 index 0000000000000..32ddc5f3c9cb7 --- /dev/null +++ b/extensions/funqy/funqy-knative-events/deployment/src/test/java/io/quarkus/funqy/test/OverloadingTest.java @@ -0,0 +1,31 @@ +package io.quarkus.funqy.test; + +import static org.hamcrest.Matchers.equalTo; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class OverloadingTest { + @RegisterExtension + static QuarkusUnitTest test = new QuarkusUnitTest() + .withApplicationRoot((jar) -> jar + .addClasses(Overloading.class)); + + @Test + public void testMapping() { + RestAssured.given().contentType("application/json") + .body("\"a\"") + .post("/strfun") + .then().statusCode(200) + .body(equalTo("\"A\"")); + + RestAssured.given().contentType("application/json") + .body("10") + .post("/intfun") + .then().statusCode(200) + .body(equalTo("20")); + } +} diff --git a/extensions/funqy/funqy-server-common/deployment/src/main/java/io/quarkus/funqy/deployment/FunctionBuildItem.java b/extensions/funqy/funqy-server-common/deployment/src/main/java/io/quarkus/funqy/deployment/FunctionBuildItem.java index f16210ec2b85b..0f78c90b994c8 100644 --- a/extensions/funqy/funqy-server-common/deployment/src/main/java/io/quarkus/funqy/deployment/FunctionBuildItem.java +++ b/extensions/funqy/funqy-server-common/deployment/src/main/java/io/quarkus/funqy/deployment/FunctionBuildItem.java @@ -5,11 +5,13 @@ public final class FunctionBuildItem extends MultiBuildItem { protected String className; protected String methodName; + protected String descriptor; protected String functionName; - public FunctionBuildItem(String className, String methodName, String functionName) { + public FunctionBuildItem(String className, String methodName, String descriptor, String functionName) { this.className = className; this.methodName = methodName; + this.descriptor = descriptor; this.functionName = functionName; } @@ -21,6 +23,10 @@ public String getMethodName() { return methodName; } + public String getDescriptor() { + return descriptor; + } + public String getFunctionName() { return functionName; } diff --git a/extensions/funqy/funqy-server-common/deployment/src/main/java/io/quarkus/funqy/deployment/FunctionScannerBuildStep.java b/extensions/funqy/funqy-server-common/deployment/src/main/java/io/quarkus/funqy/deployment/FunctionScannerBuildStep.java index 344c3de220edd..a95f30c426fff 100644 --- a/extensions/funqy/funqy-server-common/deployment/src/main/java/io/quarkus/funqy/deployment/FunctionScannerBuildStep.java +++ b/extensions/funqy/funqy-server-common/deployment/src/main/java/io/quarkus/funqy/deployment/FunctionScannerBuildStep.java @@ -76,7 +76,7 @@ public void scanFunctions(BeanArchiveIndexBuildItem beanArchiveIndexBuildItem, } if (functionName != null && functionName.isEmpty()) functionName = null; - functions.produce(new FunctionBuildItem(className, methodName, functionName)); + functions.produce(new FunctionBuildItem(className, methodName, method.descriptor(), functionName)); String source = FunctionScannerBuildStep.class.getSimpleName() + " > " + method.declaringClass() + "[" + method + "]"; @@ -167,10 +167,11 @@ public FunctionInitializedBuildItem staticInit(FunctionRecorder recorder, List functions = new HashMap<>(); - public void register(Class clz, String methodName, String functionName) { + public void register(Class clz, String methodName, String descriptor, String functionName) { for (Method m : clz.getMethods()) { - if (m.getName().equals(methodName)) { + if (m.getName().equals(methodName) && descriptor.equals(getMethodDescriptor(m))) { functions.put(functionName, new FunctionInvoker(functionName, clz, m)); } } @@ -23,4 +23,39 @@ public FunctionInvoker matchInvoker(String name) { public Collection invokers() { return functions.values(); } + + private static String getDescriptorForClass(final Class c) { + if (c.isPrimitive()) { + if (c == byte.class) + return "B"; + if (c == char.class) + return "C"; + if (c == double.class) + return "D"; + if (c == float.class) + return "F"; + if (c == int.class) + return "I"; + if (c == long.class) + return "J"; + if (c == short.class) + return "S"; + if (c == boolean.class) + return "Z"; + if (c == void.class) + return "V"; + throw new RuntimeException("Unrecognized primitive " + c); + } + if (c.isArray()) + return c.getName().replace('.', '/'); + return ('L' + c.getName() + ';').replace('.', '/'); + } + + private static String getMethodDescriptor(Method m) { + String s = "("; + for (final Class c : m.getParameterTypes()) + s += getDescriptorForClass(c); + s += ')'; + return s + getDescriptorForClass(m.getReturnType()); + } }