Fix gtvs being strange, #301

This commit is contained in:
Daniel Holle 2024-03-21 17:40:42 +01:00
parent 786e0a7a23
commit e6321ff8bc
6 changed files with 45 additions and 14 deletions

View File

@ -0,0 +1,4 @@
import java.util.HashSet;
public class Bug301<A> extends HashSet<A> {
}

View File

@ -57,6 +57,7 @@ import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.lang.reflect.Modifier;
import java.sql.Array;
import java.util.*;
import java.util.Map.Entry;
@ -130,8 +131,10 @@ public class JavaTXCompiler {
public ConstraintSet<Pair> getConstraints() throws ClassNotFoundException, IOException {
Set<ClassOrInterface> allClasses = new HashSet<>();// environment.getAllAvailableClasses();
ClassOrInterface objectClass = ASTFactory.createClass(classLoader.loadClass(new JavaClassName("java.lang.Object").toString()));
ClassOrInterface objectClass = ASTFactory.createClass(Object.class);
var recordClass = ASTFactory.createClass(Record.class);
allClasses.add(objectClass);
allClasses.add(recordClass);
// Alle Importierten Klassen in allen geparsten Sourcefiles kommen ins FC
for (Entry<File, SourceFile> source : sourceFiles.entrySet()) {
var importedClasses = new ArrayList<ClassOrInterface>();
@ -184,17 +187,17 @@ public class JavaTXCompiler {
while (paraIt.hasNext()) {
gtvs.put(tvarVarIt.next().getName(), paraIt.next());
}
Iterator<Method> methodIt = superclass.getMethods().iterator();
// TODO: PL 2020-05-06: Hier müssen ueberschriebene Methoden noch rausgefiltert
// werden
while (methodIt.hasNext()) {
Method m = methodIt.next();
ParameterList newParaList = new ParameterList(m.getParameterList().getFormalparalist().stream().map(fp -> fp.withType(fp.getType().acceptTV(new TypeExchanger(gtvs)))).collect(Collectors.toCollection(ArrayList::new)), m.getParameterList().getOffset());
cl.getMethods().add(new Method(m.modifier, m.name, m.getReturnType().acceptTV(new TypeExchanger(gtvs)), newParaList, m.block,
// new GenericDeclarationList(newGenericsList,
// ((GenericDeclarationList)m.getGenerics()).getOffset()),
(GenericDeclarationList) m.getGenerics(), m.getOffset(), true));
}
for (Method m : superclass.getMethods()) {
if (m.isInherited || Modifier.isStatic(m.modifier)) continue;
// TODO: Add gtvs from method itself
ParameterList newParaList = new ParameterList(m.getParameterList().getFormalparalist().stream().map(fp -> fp.withType(fp.getType().acceptTV(new TypeExchanger(gtvs)))).collect(Collectors.toCollection(ArrayList::new)), m.getParameterList().getOffset());
cl.getMethods().add(new Method(m.modifier, m.name, m.getReturnType().acceptTV(new TypeExchanger(gtvs)), newParaList, m.block,
// new GenericDeclarationList(newGenericsList,
// ((GenericDeclarationList)m.getGenerics()).getOffset()),
(GenericDeclarationList) m.getGenerics(), m.getOffset(), true));
}
}
cl.setMethodsAdded();

View File

@ -128,7 +128,6 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope {
public List<Method> getMethods() {
return this.methods;
}
/*
* public RefType getType() { return generateTypeOfClass(this.getClassName(), this.getGenerics(), this.getOffset()); }
*/

View File

@ -1,6 +1,7 @@
package de.dhbwstuttgart.syntaxtree;
import java.util.ArrayList;
import java.util.Objects;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
@ -93,4 +94,17 @@ public class Method extends SyntaxTreeNode implements IItemWithOffset, TypeScope
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Method method = (Method) o;
return Objects.equals(name, method.name) && Objects.equals(parameterlist, method.parameterlist) && Objects.equals(returnType, method.returnType);
}
@Override
public int hashCode() {
return Objects.hash(name, parameterlist, returnType);
}
}

View File

@ -2,6 +2,8 @@ package de.dhbwstuttgart.typeinference.assumptions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import de.dhbwstuttgart.bytecode.CodeGenException;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.syntaxtree.ClassOrInterface;
import de.dhbwstuttgart.syntaxtree.GenericTypeVar;
import de.dhbwstuttgart.syntaxtree.Method;
@ -33,10 +35,11 @@ public class TypeInferenceBlockInformation extends TypeInferenceInformation {
public ClassOrInterface getSuperClass() {
for (var clazz : getAvailableClasses()) {
System.out.println(currentClass.getSuperClass().getName());
if (clazz.getClassName().equals(currentClass.getSuperClass().getName()))
return clazz;
}
return null;
throw new DebugException("Class has no superclass!");
}
public TypeScope getCurrentTypeScope() {
return methodContext;

View File

@ -980,6 +980,7 @@ public class TestComplete {
var clazz = classFiles.get("Bug298");
var instance = clazz.getDeclaredConstructor().newInstance();
}
@Test
public void testBug300() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug300.jav");
@ -987,4 +988,11 @@ public class TestComplete {
var instance = clazz.getDeclaredConstructor().newInstance();
assertEquals(clazz.getDeclaredMethod("m").invoke(instance), "Base");
}
@Test
public void testBug301() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug301.jav");
var clazz = classFiles.get("Bug301");
var instance = clazz.getDeclaredConstructor().newInstance();
}
}