Fix #302
All checks were successful
Build and Test with Maven / Build-and-test-with-Maven (push) Successful in 2m47s

This commit is contained in:
Daniel Holle 2024-03-25 14:49:18 +01:00
parent 9472b5c86f
commit 43da2ffbdc
7 changed files with 38 additions and 4 deletions

View File

@ -0,0 +1,11 @@
import java.util.ArrayList;
import java.util.List;
import java.lang.Integer;
public class Bug302 {
public Bug302(List<Integer> a){}
public static m() {
new Bug302(new ArrayList<Integer>());
}
}

View File

@ -280,6 +280,7 @@ public class StatementGenerator {
if (!Objects.isNull(creator.classCreatorRest())) {
ArgumentList args = convertArguments(creator.classCreatorRest().arguments().expressionList());
ArrayList<RefTypeOrTPHOrWildcardOrGeneric> 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;

View File

@ -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<Constructor> findConstructor(ClassOrInterface owner, List<TargetType> argumentList) {
return owner.getConstructors().stream().filter(c -> parameterEquals(c.getParameterList(), argumentList)).findFirst();
}
boolean parameterEquals(ParameterList parameterList, List<TargetType> arguments) {
var pars = parameterList.getFormalparalist();
if (pars.size() != arguments.size())

View File

@ -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

View File

@ -5,8 +5,8 @@ import de.dhbwstuttgart.target.tree.type.TargetType;
import java.util.List;
public record TargetNew(TargetType type, List<TargetExpression> params) implements TargetStatementExpression {
public record TargetNew(TargetType type, List<TargetType> signature, List<TargetExpression> 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));
}
}

View File

@ -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<Pair> methodSignatureConstraint = generatemethodSignatureConstraint(forConstructor, assumption, info, resolver);
methodConstraint.setmethodSignatureConstraint(methodSignatureConstraint);
methodConstraint.addAll(methodSignatureConstraint);
return methodConstraint;
}

View File

@ -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");