From e88d4428c59491d18074529775c8ce87071cacfb Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 5 Feb 2024 15:24:06 +0100 Subject: [PATCH] Fix constructor overloads getting picked with the wrong arity --- resources/bytecode/javFiles/OLConstructor.jav | 17 +++++++++++++++++ .../typeinference/typeAlgo/TYPEStmt.java | 4 +++- src/test/java/TestComplete.java | 8 ++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 resources/bytecode/javFiles/OLConstructor.jav diff --git a/resources/bytecode/javFiles/OLConstructor.jav b/resources/bytecode/javFiles/OLConstructor.jav new file mode 100644 index 00000000..815c80be --- /dev/null +++ b/resources/bytecode/javFiles/OLConstructor.jav @@ -0,0 +1,17 @@ +import java.lang.Integer; + +public class Parent { + public Integer x; + + public Parent(Integer a) { + this.x = a; + } + + public Parent() {} +} + +class Child extends Parent { + Child() { + super(3); + } +} diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 440bee30..c49eceaf 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -548,7 +548,9 @@ public class TYPEStmt implements StatementVisitor { for (var clazz : info.getAvailableClasses()) { if (clazz.getClassName().equals(info.getCurrentClass().getSuperClass().getName())) { for (var ctor : clazz.getConstructors()) { - var assumption = new MethodAssumption(null, new Void(new NullToken()), convertParams(ctor.getParameterList(), info), createTypeScope(clazz, ctor), ctor.isInherited); + var params = convertParams(ctor.getParameterList(), info); + if (params.size() != superCall.arglist.getArguments().size()) continue; + var assumption = new MethodAssumption(null, new Void(new NullToken()), params, createTypeScope(clazz, ctor), ctor.isInherited); GenericsResolver resolver = getResolverInstance(); Set> oneMethodConstraints = generateConstraint(superCall, assumption, info, resolver); diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 56dc7aa5..cd396155 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -825,4 +825,12 @@ public class TestComplete { assertNull(clazz.getDeclaredMethod("m").invoke(instance)); } + + @Test + public void testOLConstructor() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "OLConstructor.jav"); + var clazz = classFiles.get("Child"); + var instance = clazz.getDeclaredConstructor().newInstance(); + assertEquals(clazz.getSuperclass().getDeclaredField("x").get(instance), 3); + } }