From c2ee12397f59b126791bd80600888fbc78f0fc7a Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Fri, 22 Mar 2024 14:54:23 +0100 Subject: [PATCH] Work on issue --- .../de/dhbwstuttgart/core/JavaTXCompiler.java | 4 +-- .../syntaxtree/factory/UnifyTypeFactory.java | 17 ++++++------ .../unify/interfaces/IFiniteClosure.java | 4 +++ .../unify/model/FiniteClosure.java | 26 +++++++++++++++++-- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index edfb5f4d..15ae40f4 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -288,7 +288,7 @@ public class JavaTXCompiler { // urm.addUnifyResultListener(resultListener); try { logFile = logFile == null ? new FileWriter(new File("log_" + sourceFiles.keySet().iterator().next().getName())) : logFile; - IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, getClassLoader()); + IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, getClassLoader(), this); System.out.println(finiteClosure); urm = new UnifyResultModel(cons, finiteClosure); urm.addUnifyResultListener(resultListener); @@ -426,7 +426,7 @@ public class JavaTXCompiler { var logFolder = new File(System.getProperty("user.dir") + "/logFiles/"); if (log) logFolder.mkdirs(); Writer logFile = log ? new FileWriter(new File(logFolder, "log_" + sourceFiles.keySet().iterator().next().getName())) : new OutputStreamWriter(new NullOutputStream()); - IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, classLoader); + IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses, logFile, classLoader, this); System.out.println(finiteClosure); ConstraintSet unifyCons = UnifyTypeFactory.convert(cons); System.out.println("xxx1"); diff --git a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java index 4037d2f4..1b802a3f 100644 --- a/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java +++ b/src/main/java/de/dhbwstuttgart/syntaxtree/factory/UnifyTypeFactory.java @@ -6,6 +6,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.exceptions.NotImplementedException; import de.dhbwstuttgart.parser.NullToken; import de.dhbwstuttgart.parser.SourceLoc; @@ -29,7 +30,7 @@ public class UnifyTypeFactory { private static ArrayList PLACEHOLDERS = new ArrayList<>(); - public static FiniteClosure generateFC(List fromClasses, Writer logFile, ClassLoader classLoader) throws ClassNotFoundException { + public static FiniteClosure generateFC(List fromClasses, Writer logFile, ClassLoader classLoader, JavaTXCompiler compiler) throws ClassNotFoundException { /* Die transitive Hülle muss funktionieren. Man darf schreiben List extends AL @@ -40,7 +41,7 @@ public class UnifyTypeFactory { Generell dürfen sie immer die gleichen Namen haben. TODO: die transitive Hülle bilden */ - return new FiniteClosure(FCGenerator.toUnifyFC(fromClasses, classLoader), logFile); + return new FiniteClosure(FCGenerator.toUnifyFC(fromClasses, classLoader), logFile, compiler); } public static UnifyPair generateSmallerPair(UnifyType tl, UnifyType tr, SourceLoc location){ @@ -64,15 +65,15 @@ public class UnifyTypeFactory { * ASTType -> UnifyType */ public static UnifyType convert(RefTypeOrTPHOrWildcardOrGeneric t, Boolean innerType){ - if(t instanceof GenericRefType){ + if (t instanceof GenericRefType){ return UnifyTypeFactory.convert((GenericRefType)t, innerType); - }else if(t instanceof TypePlaceholder){ + } else if (t instanceof TypePlaceholder){ return UnifyTypeFactory.convert((TypePlaceholder)t, innerType); - }else if(t instanceof ExtendsWildcardType){ + } else if (t instanceof ExtendsWildcardType){ return UnifyTypeFactory.convert((ExtendsWildcardType)t, innerType); - }else if(t instanceof SuperWildcardType){ - return UnifyTypeFactory.convert((SuperWildcardType)t, innerType); - }else if(t instanceof RefType){ + } else if (t instanceof SuperWildcardType) { + return UnifyTypeFactory.convert((SuperWildcardType) t, innerType); + } else if (t instanceof RefType){ return UnifyTypeFactory.convert((RefType)t, innerType); } //Es wurde versucht ein Typ umzuwandeln, welcher noch nicht von der Factory abgedeckt ist diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java index dea9e53a..d87aedab 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/interfaces/IFiniteClosure.java @@ -1,5 +1,6 @@ package de.dhbwstuttgart.typeinference.unify.interfaces; +import java.util.List; import java.util.Optional; import java.util.Set; @@ -73,5 +74,8 @@ public interface IFiniteClosure { public Set getChildren(UnifyType t); public Set getAllTypesByName(String typeName); + public boolean isFunctionalInterface(ReferenceType t); + public List getFunctionalInterfaceTypeArguments(ReferenceType t); + public int compare(UnifyType rhsType, UnifyType rhsType2, PairOperator pairop); } diff --git a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java index f4c8ab24..1c1d065b 100644 --- a/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java +++ b/src/main/java/de/dhbwstuttgart/typeinference/unify/model/FiniteClosure.java @@ -3,6 +3,7 @@ package de.dhbwstuttgart.typeinference.unify.model; import java.io.FileWriter; import java.io.IOException; import java.io.Writer; +import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -20,7 +21,10 @@ import com.google.common.collect.Ordering; //PL 18-02-05/18-04-05 Unifier durch Matcher ersetzt //muss greater noch ersetzt werden ja erledigt 18--04-05 +import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.parser.SourceLoc; +import de.dhbwstuttgart.parser.scope.JavaClassName; +import de.dhbwstuttgart.syntaxtree.factory.UnifyTypeFactory; import de.dhbwstuttgart.typeinference.unify.MartelliMontanariUnify; import de.dhbwstuttgart.typeinference.unify.Match; @@ -37,6 +41,8 @@ import org.antlr.v4.runtime.Token; public class FiniteClosure //extends Ordering //entfernt PL 2018-12-11 implements IFiniteClosure { + final JavaTXCompiler compiler; + Writer logFile; static Boolean log = false; public void setLogTrue() { @@ -71,7 +77,8 @@ implements IFiniteClosure { /** * Creates a new instance using the inheritance tree defined in the pairs. */ - public FiniteClosure(Set pairs, Writer logFile) { + public FiniteClosure(Set pairs, Writer logFile, JavaTXCompiler compiler) { + this.compiler = compiler; this.logFile = logFile; this.pairs = new HashSet<>(pairs); inheritanceGraph = new HashMap>(); @@ -596,7 +603,22 @@ implements IFiniteClosure { return new HashSet<>(); return strInheritanceGraph.get(typeName).stream().map(x -> x.getContent()).collect(Collectors.toCollection(HashSet::new)); } - + + @Override + public boolean isFunctionalInterface(ReferenceType t) { + return compiler.getClass(new JavaClassName(t.getName())).isFunctionalInterface(); + } + + @Override + public List getFunctionalInterfaceTypeArguments(ReferenceType t) { + var clazz = compiler.getClass(new JavaClassName(t.getName())); + var intfMethod = clazz.getMethods().stream().filter(m -> Modifier.isAbstract(m.modifier)).findFirst().orElseThrow(); + var args = new ArrayList(); + args.add(UnifyTypeFactory.convert(intfMethod.getReturnType(), false)); + intfMethod.getParameterList().getFormalparalist().forEach(param -> args.add(UnifyTypeFactory.convert(param.getType(), false))); + return args; + } + @Override public Optional getLeftHandedType(String typeName) { if(!strInheritanceGraph.containsKey(typeName))