From 43da2ffbdc1f1e7c87878db279893abd9acd8d99 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Mon, 25 Mar 2024 14:49:18 +0100 Subject: [PATCH] Fix #302 --- resources/bytecode/javFiles/Bug302.jav | 11 +++++++++++ .../SyntaxTreeGenerator/StatementGenerator.java | 1 + .../dhbwstuttgart/target/generate/ASTToTargetAST.java | 5 +++++ .../target/generate/StatementToTargetExpression.java | 5 ++++- .../target/tree/expression/TargetNew.java | 4 ++-- .../typeinference/typeAlgo/TYPEStmt.java | 9 ++++++++- src/test/java/TestComplete.java | 7 +++++++ 7 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 resources/bytecode/javFiles/Bug302.jav diff --git a/resources/bytecode/javFiles/Bug302.jav b/resources/bytecode/javFiles/Bug302.jav new file mode 100644 index 00000000..baeb8a8a --- /dev/null +++ b/resources/bytecode/javFiles/Bug302.jav @@ -0,0 +1,11 @@ +import java.util.ArrayList; +import java.util.List; +import java.lang.Integer; + +public class Bug302 { + public Bug302(List a){} + + public static m() { + new Bug302(new ArrayList()); + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index a4ca0246..829ba9e6 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -280,6 +280,7 @@ public class StatementGenerator { if (!Objects.isNull(creator.classCreatorRest())) { ArgumentList args = convertArguments(creator.classCreatorRest().arguments().expressionList()); ArrayList argTypes = args.getArguments().stream().map(x -> TypePlaceholder.fresh(creator.getStart())).collect(Collectors.toCollection(ArrayList::new)); + argTypes.add(TypePlaceholder.fresh(creator.getStart())); // return type Statement ret = new NewClass(newclass, args, null, argTypes, creator.getStart()); ret.setStatement(); return ret; diff --git a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java index 3578ab02..3db8957a 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/ASTToTargetAST.java @@ -6,6 +6,7 @@ import de.dhbwstuttgart.environment.ByteArrayClassLoader; import de.dhbwstuttgart.environment.IByteArrayClassLoader; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; +import de.dhbwstuttgart.parser.scope.JavaClassName; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.Record; import de.dhbwstuttgart.syntaxtree.factory.ASTFactory; @@ -87,6 +88,10 @@ public class ASTToTargetAST { return method; } + Optional findConstructor(ClassOrInterface owner, List argumentList) { + return owner.getConstructors().stream().filter(c -> parameterEquals(c.getParameterList(), argumentList)).findFirst(); + } + boolean parameterEquals(ParameterList parameterList, List arguments) { var pars = parameterList.getFormalparalist(); if (pars.size() != arguments.size()) diff --git a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java index 3014ae46..c719c2c2 100644 --- a/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java +++ b/src/main/java/de/dhbwstuttgart/target/generate/StatementToTargetExpression.java @@ -224,7 +224,10 @@ public class StatementToTargetExpression implements ASTVisitor { @Override public void visit(NewClass newClass) { - result = new TargetNew(new TargetRefType(newClass.name), newClass.getArgumentList().getArguments().stream().map(converter::convert).toList()); + var receiverName = new JavaClassName(newClass.name); + var ctor = converter.findConstructor(converter.compiler.getClass(receiverName), newClass.signatureArguments().stream().map(converter::convert).toList()); + var signature = ctor.orElseThrow().getParameterList().getFormalparalist().stream().map(e -> converter.convert(e.getType())).toList(); + result = new TargetNew(new TargetRefType(newClass.name), signature, newClass.getArgumentList().getArguments().stream().map(converter::convert).toList()); } @Override diff --git a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java index e9d4ce8c..0ce5c7a8 100644 --- a/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java +++ b/src/main/java/de/dhbwstuttgart/target/tree/expression/TargetNew.java @@ -5,8 +5,8 @@ import de.dhbwstuttgart.target.tree.type.TargetType; import java.util.List; -public record TargetNew(TargetType type, List params) implements TargetStatementExpression { +public record TargetNew(TargetType type, List signature, List params) implements TargetStatementExpression { public String getDescriptor() { - return TargetMethod.getDescriptor(null, params.stream().map(TargetExpression::type).toArray(TargetType[]::new)); + return TargetMethod.getDescriptor(null, signature.toArray(TargetType[]::new)); } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java index 35626c69..f4df7a50 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/typeAlgo/TYPEStmt.java @@ -742,7 +742,8 @@ public class TYPEStmt implements StatementVisitor { if (cl.getClassName().equals(ofType.getName())) { for (Method m : cl.getConstructors()) { if (m.getParameterList().getFormalparalist().size() == argList.getArguments().size()) { - ret.add(new MethodAssumption(cl, cl.generateTypeOfThisClass(), convertParams(m.getParameterList(), info), createTypeScope(cl, m), m.isInherited)); + var params = convertParams(m.getParameterList(), info); + ret.add(new MethodAssumption(cl, cl.generateTypeOfThisClass(), params, createTypeScope(cl, m), m.isInherited)); } } } @@ -757,6 +758,12 @@ public class TYPEStmt implements StatementVisitor { methodConstraint.add(new Pair(assumption.getReturnType(resolver), forConstructor.getType(), PairOperator.SMALLERDOT, loc(forConstructor.getOffset()))); // WELCHEN SINN MACHT DIESER CONSTRAINT??? methodConstraint.addAll(generateParameterConstraints(forConstructor, assumption, info, resolver)); + + Set methodSignatureConstraint = generatemethodSignatureConstraint(forConstructor, assumption, info, resolver); + methodConstraint.setmethodSignatureConstraint(methodSignatureConstraint); + + methodConstraint.addAll(methodSignatureConstraint); + return methodConstraint; } diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 6000fcec..a9191989 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -996,6 +996,13 @@ public class TestComplete { var instance = clazz.getDeclaredConstructor().newInstance(); } + @Test + public void testBug302() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug302.jav"); + var clazz = classFiles.get("Bug302"); + clazz.getDeclaredMethod("m").invoke(null); + } + @Test public void testBug306() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug306.jav");