Skip to content

Commit

Permalink
Check name conflicts of functions in Funqy
Browse files Browse the repository at this point in the history
Signed-off-by: Matej Vasek <[email protected]>
  • Loading branch information
matejvasek authored and patriot1burke committed May 18, 2023
1 parent 24cbc44 commit da92ba4
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.funqy.test;

import io.quarkus.funqy.Funq;

public class NameConflict {

@Funq
public String function(String s) {
return s.toUpperCase();
}

@Funq
public int function(int i) {
return i * 2;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.funqy.test;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.*;
import java.util.logging.Level;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class NameConflictTest {
@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot(jar -> jar.addClass(NameConflict.class))
.setLogRecordPredicate(logRecord -> logRecord.getLevel().intValue() >= Level.WARNING.intValue() &&
logRecord.getLoggerName().startsWith("io.quarkus.funqy"))
.assertLogRecords(logRecords -> {
boolean match = logRecords
.stream()
.anyMatch(logRecord -> logRecord.getMessage().contains("Name conflict"));
if (!match) {
fail("Log does not contain message about name conflict.");
}
});

@Test
void test() {
assertTrue(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,34 @@
import java.util.HashMap;
import java.util.Map;

import org.jboss.logging.Logger;

public class FunctionRegistry {
protected Map<String, FunctionInvoker> functions = new HashMap<>();

private static final Logger log = Logger.getLogger(FunctionRegistry.class);

public void register(Class clz, String methodName, String descriptor, String functionName) {
String methodDescription = clz.getName() + "/" + methodName + descriptor;

FunctionInvoker fi = functions.get(functionName);
if (fi != null) {
String otherMethodDescription = fi.targetClass.getName() + "/" + fi.method.getName()
+ getMethodDescriptor(fi.method);
String msg = String.format("Name conflict: the name \"%s\" is shared by \"%s\" and \"%s\"." +
" Consider using @Func(\"name-here\") annotation parameter to distinguish them.",
functionName, methodDescription, otherMethodDescription);
log.warn(msg);
}

for (Method m : clz.getMethods()) {
if (m.getName().equals(methodName) && descriptor.equals(getMethodDescriptor(m))) {
functions.put(functionName, new FunctionInvoker(functionName, clz, m));
return;
}
}

throw new RuntimeException("Method \"" + methodDescription + "\" not found.");
}

public FunctionInvoker matchInvoker(String name) {
Expand Down

0 comments on commit da92ba4

Please sign in to comment.