From e1e744152a09db89d0a7afc3760c668f6466322b Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Fri, 11 Oct 2024 14:13:55 +0200 Subject: [PATCH] Fix overloading considering too many options, fix #348 --- .../target/generate/ASTToTargetAST.java | 20 ++++++++++++++++++- .../generate/StatementToTargetExpression.java | 2 +- src/test/java/TestComplete.java | 6 +++--- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index c30551cf..06e11131 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -34,6 +34,8 @@ public class ASTToTargetAST { protected List all; public Generics generics; + public List currentMethodOverloads; + final Map> userDefinedGenerics = new HashMap<>(); final Map> tphsInMethods = new HashMap<>(); private Method currentMethod; @@ -354,11 +356,16 @@ public class ASTToTargetAST { record MethodWithTphs(TargetMethod method, List args) {} + record Signature(TargetMethod.Signature java, TargetMethod.Signature tx, Generics generics) {} + private List convert(ClassOrInterface currentClass, Method method) { generics = all.getFirst(); List result = new ArrayList<>(); this.currentMethod = method; + List signatures = new ArrayList<>(); + HashMap> collectedGenerics = new HashMap<>(); + for (var s : all) { generics = s; var javaGenerics = this.generics.javaGenerics.generics(currentClass, method); @@ -382,8 +389,19 @@ public class ASTToTargetAST { var javaSignature = new TargetMethod.Signature(javaMethodGenerics, params, returnType); var txSignature = new TargetMethod.Signature(txMethodGenerics, txParams, convert(method.getReturnType(), this.generics.txGenerics)); - var newMethod = new TargetMethod(method.modifier, method.name, convert(method.block), javaSignature, txSignature); + signatures.add(new Signature(javaSignature, txSignature, generics)); + System.out.println(javaSignature); + var listOfGenerics = collectedGenerics.getOrDefault(javaSignature, new ArrayList<>()); + listOfGenerics.add(generics); + collectedGenerics.put(javaSignature, listOfGenerics); + } + + for (var signature : signatures) { + generics = signature.generics; + currentMethodOverloads = collectedGenerics.get(signature.java); + + var newMethod = new TargetMethod(method.modifier, method.name, convert(method.block), signature.java, signature.tx); var concreteParams = tphsInMethods.getOrDefault(method, new HashSet<>()).stream().map(sig -> new SignaturePairTarget(convert(sig.signature), convert(sig.parameter))).toList(); result.add(new MethodWithTphs(newMethod, concreteParams)); diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java index 60f71f7a..ba858f91 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -451,7 +451,7 @@ public class StatementToTargetExpression implements ASTVisitor { var oldGenerics = converter.generics; // Set the generics to matching result set - for (var generics : converter.all) { + for (var generics : converter.currentMethodOverloads) { var java = generics.javaGenerics(); var equals = true; for (var pair : l) { diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 74d7686f..915c3d80 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -863,13 +863,13 @@ public class TestComplete { var rctor = R.getDeclaredConstructor(Number.class); var instance = clazz.getDeclaredConstructor().newInstance(); - var m = clazz.getDeclaredMethod("m", R); + var m = clazz.getDeclaredMethod("m", R, Integer.class); var x = rctor.newInstance(10); var d = rctor.newInstance(20.0); - assertEquals(m.invoke(instance, x), 50); - assertEquals(m.invoke(instance, d), 40.0); + assertEquals(m.invoke(instance, x, 0), 50); + assertEquals(m.invoke(instance, d, 0), 40.0); } @Test