From e6321ff8bcc029afec7248a4cb86c1ad8c329649 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Thu, 21 Mar 2024 17:40:42 +0100 Subject: [PATCH] Fix gtvs being strange, #301 --- resources/bytecode/javFiles/Bug301.jav | 4 +++ .../de/dhbwstuttgart/core/JavaTXCompiler.java | 27 ++++++++++--------- .../syntaxtree/ClassOrInterface.java | 1 - .../de/dhbwstuttgart/syntaxtree/Method.java | 14 ++++++++++ .../TypeInferenceBlockInformation.java | 5 +++- src/test/java/TestComplete.java | 8 ++++++ 6 files changed, 45 insertions(+), 14 deletions(-) create mode 100644 resources/bytecode/javFiles/Bug301.jav diff --git a/resources/bytecode/javFiles/Bug301.jav b/resources/bytecode/javFiles/Bug301.jav new file mode 100644 index 00000000..30a6f2bb --- /dev/null +++ b/resources/bytecode/javFiles/Bug301.jav @@ -0,0 +1,4 @@ +import java.util.HashSet; + +public class Bug301 extends HashSet { +} diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index fb56e8c5..edfb5f4d 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -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 getConstraints() throws ClassNotFoundException, IOException { Set 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 source : sourceFiles.entrySet()) { var importedClasses = new ArrayList(); @@ -184,17 +187,17 @@ public class JavaTXCompiler { while (paraIt.hasNext()) { gtvs.put(tvarVarIt.next().getName(), paraIt.next()); } - Iterator 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(); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java b/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java index a8777386..108c813a 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/ClassOrInterface.java @@ -128,7 +128,6 @@ public class ClassOrInterface extends SyntaxTreeNode implements TypeScope { public List getMethods() { return this.methods; } - /* * public RefType getType() { return generateTypeOfClass(this.getClassName(), this.getGenerics(), this.getOffset()); } */ diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java index 904e4c8e..97b18bcd 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/Method.java @@ -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); + } } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java b/src/main/java/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java index 94460472..6b9f54a5 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/assumptions/TypeInferenceBlockInformation.java @@ -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; diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 50049a90..f6e49b70 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -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(); + } }