From 3a9468a174974694ffa0c5740abffc7513310aaf Mon Sep 17 00:00:00 2001 From: Ruben Date: Sun, 8 Dec 2024 14:32:42 +0100 Subject: [PATCH] feat: add Method to get Result-Set and SyntaxTree instead of two different Methods. --- .../de/dhbw/compiler/core/JavaTXCompiler.java | 87 +++++++++++++++++++ .../LanguageServerInterface.java | 34 +------- .../LanguageServerTransferObject.java | 21 +++++ .../main/java/de/dhbw/helper/TypeFinder.java | 2 +- .../src/test/java/CompilerInterfaceTest.java | 18 ++-- 5 files changed, 118 insertions(+), 44 deletions(-) create mode 100644 LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerTransferObject.java diff --git a/LanguageServer/src/main/java/de/dhbw/compiler/core/JavaTXCompiler.java b/LanguageServer/src/main/java/de/dhbw/compiler/core/JavaTXCompiler.java index 107115b..a10650b 100644 --- a/LanguageServer/src/main/java/de/dhbw/compiler/core/JavaTXCompiler.java +++ b/LanguageServer/src/main/java/de/dhbw/compiler/core/JavaTXCompiler.java @@ -5,6 +5,7 @@ import de.dhbw.compiler.bytecode.Codegen; import de.dhbw.compiler.environment.CompilationEnvironment; import de.dhbw.compiler.environment.DirectoryClassLoader; import de.dhbw.compiler.exceptions.DebugException; +import de.dhbw.compiler.languageServerInterface.LanguageServerTransferObject; import de.dhbw.compiler.parser.JavaTXParser; import de.dhbw.compiler.parser.NullToken; import de.dhbw.compiler.parser.scope.GenericsRegistry; @@ -349,6 +350,92 @@ public class JavaTXCompiler { return urm; } + /** + * TEMPORARY - Only for Language Server Usage + */ + public LanguageServerTransferObject getResultSetAndAbstractSyntax(File file) throws IOException, ClassNotFoundException { + var sf = sourceFiles.get(file); + Set allClasses = new HashSet<>(); + allClasses.addAll(getAvailableClasses(sf)); + allClasses.addAll(sf.getClasses()); + var newClasses = CompilationEnvironment.loadDefaultPackageClasses(sf.getPkgName(), file, this).stream().map(ASTFactory::createClass).collect(Collectors.toSet()); + for (var clazz : newClasses) { + // Don't load classes that get recompiled + if (sf.getClasses().stream().anyMatch(nf -> nf.getClassName().equals(clazz.getClassName()))) + continue; + if (allClasses.stream().noneMatch(old -> old.getClassName().equals(clazz.getClassName()))) + allClasses.add(clazz); + } + + final ConstraintSet cons = getConstraints(file); + Set> results = new HashSet<>(); + try { + 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.stream().toList(), logFile, classLoader, this); + ConstraintSet unifyCons = UnifyTypeFactory.convert(this, cons); + Function distributeInnerVars = x -> { + UnifyType lhs, rhs; + if (((lhs = x.getLhsType()) instanceof PlaceholderType) && ((rhs = x.getRhsType()) instanceof PlaceholderType) && (((PlaceholderType) lhs).isInnerType() || ((PlaceholderType) rhs).isInnerType())) { + ((PlaceholderType) lhs).setInnerType(true); + ((PlaceholderType) rhs).setInnerType(true); + } + return x; + + }; + + logFile.write("Unify:" + unifyCons.toString()); + unifyCons = unifyCons.map(distributeInnerVars); + logFile.write("\nUnify_distributeInnerVars: " + unifyCons.toString()); + TypeUnify unify = new TypeUnify(); + logFile.write("FC:\\" + finiteClosure.toString() + "\n"); + logFile.write(ASTTypePrinter.print(sf)); + logFile.flush(); + Set varianceTPHold; + Set varianceTPH = new HashSet<>(); + varianceTPH = varianceInheritanceConstraintSet(unifyCons); + + List>> oderConstraints = unifyCons.getOderConstraints(); + + if (resultmodel) { + /* UnifyResultModel Anfang */ + UnifyResultModel urm = new UnifyResultModel(cons, finiteClosure); + UnifyResultListenerImpl li = new UnifyResultListenerImpl(); + urm.addUnifyResultListener(li); + unify.unifyParallel(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, urm, usedTasks); + logFile.write("RES_FINAL: " + li.getResults().toString() + "\n"); + logFile.flush(); + return new LanguageServerTransferObject(li.getResults(), sf, ASTTypePrinter.print(sf)); + } + /* UnifyResultModel End */ + else { + Set> result = unify.unifyOderConstraints(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, new UnifyResultModel(cons, finiteClosure), usedTasks); + logFile.write("RES: " + result.toString() + "\n"); + logFile.flush(); + results.addAll(result); + + results = results.stream().map(x -> { + Optional> res = new RuleSet().subst(x.stream().map(y -> { + if (y.getPairOp() == PairOperator.SMALLERDOTWC) + y.setPairOp(PairOperator.EQUALSDOT); + return y; // alle Paare a <.? b erden durch a =. b ersetzt + }).collect(Collectors.toCollection(HashSet::new))); + if (res.isPresent()) {// wenn subst ein Erg liefert wurde was veraendert + return new TypeUnifyTask().applyTypeUnificationRules(res.get(), finiteClosure); + } else + return x; // wenn nichts veraendert wurde wird x zurueckgegeben + }).collect(Collectors.toCollection(HashSet::new)); + logFile.write("RES_FINAL: " + results.toString() + "\n"); + logFile.flush(); + logFile.write("PLACEHOLDERS: " + PlaceholderType.EXISTING_PLACEHOLDERS); + logFile.flush(); + } + } catch (IOException | ClassNotFoundException e) { + } + return new LanguageServerTransferObject(results.stream().map((unifyPairs -> new ResultSet(UnifyTypeFactory.convert(unifyPairs, Pair.generateTPHMap(cons))))).collect(Collectors.toList()), sf, ASTTypePrinter.print(sf)); + } + public List typeInference(File file) throws ClassNotFoundException, IOException { var sf = sourceFiles.get(file); Set allClasses = new HashSet<>();// environment.getAllAvailableClasses(); diff --git a/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerInterface.java b/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerInterface.java index 2abaacb..f270a4c 100644 --- a/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerInterface.java +++ b/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerInterface.java @@ -19,7 +19,7 @@ public class LanguageServerInterface { /** * get final Result Set * */ - public List getResultSet(String input) throws IOException, ClassNotFoundException { + public LanguageServerTransferObject getResultSetAndAbstractSyntax(String input) throws IOException, ClassNotFoundException { File tempSourcefile = File.createTempFile("pattern", ".java"); tempSourcefile.deleteOnExit(); @@ -28,36 +28,6 @@ public class LanguageServerInterface { out.close(); JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile); - return tx.typeInference(tempSourcefile); - } - - /** - * get abstract Syntax - */ - public SourceFile getAbstractSyntax(String input) throws IOException, ClassNotFoundException { - File tempSourcefile = File.createTempFile("pattern", ".java"); - tempSourcefile.deleteOnExit(); - - BufferedWriter out = new BufferedWriter(new FileWriter(tempSourcefile)); - out.write(input); - out.close(); - - JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile); - return tx.sourceFiles.get(tempSourcefile); - } - - /** - * get abstract Syntax as String - */ - public String getAbstractSyntaxAsString(String input) throws IOException, ClassNotFoundException { - File tempSourcefile = File.createTempFile("pattern", ".java"); - tempSourcefile.deleteOnExit(); - - BufferedWriter out = new BufferedWriter(new FileWriter(tempSourcefile)); - out.write(input); - out.close(); - - JavaTXCompiler tx = new JavaTXCompiler(tempSourcefile); - return ASTTypePrinter.print(tx.sourceFiles.get(tempSourcefile)); + return tx.getResultSetAndAbstractSyntax(tempSourcefile); } } diff --git a/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerTransferObject.java b/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerTransferObject.java new file mode 100644 index 0000000..803943c --- /dev/null +++ b/LanguageServer/src/main/java/de/dhbw/compiler/languageServerInterface/LanguageServerTransferObject.java @@ -0,0 +1,21 @@ +package de.dhbw.compiler.languageServerInterface; + +import de.dhbw.compiler.syntaxtree.SourceFile; +import de.dhbw.compiler.typeinference.result.ResultSet; + +import java.util.List; + +public class LanguageServerTransferObject { + List resultSets; + SourceFile Ast; + String printedAst; + public LanguageServerTransferObject(List resultSets, SourceFile Ast, String printedAst) { + this.resultSets = resultSets; + this.Ast = Ast; + this.printedAst = printedAst; + } + + public List getResultSets() {return resultSets;} + public SourceFile getAst() {return Ast;} + public String getPrintedAst() {return printedAst;} +} diff --git a/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java b/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java index 2a3864f..30dcbe1 100644 --- a/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java +++ b/LanguageServer/src/main/java/de/dhbw/helper/TypeFinder.java @@ -32,7 +32,7 @@ public class TypeFinder { * */ public String findAvailableTypes(int line, int character, String currentTextDocument) throws IOException, ClassNotFoundException { - var resultSet = languageServer.getResultSet(currentTextDocument); + var resultSet = languageServer.getResultSetAndAbstractSyntax(currentTextDocument); return ""; } } \ No newline at end of file diff --git a/LanguageServer/src/test/java/CompilerInterfaceTest.java b/LanguageServer/src/test/java/CompilerInterfaceTest.java index 384114a..b9d3ce3 100644 --- a/LanguageServer/src/test/java/CompilerInterfaceTest.java +++ b/LanguageServer/src/test/java/CompilerInterfaceTest.java @@ -9,20 +9,16 @@ public class CompilerInterfaceTest { @Test public void testAbstractSyntaxAsString() throws IOException, ClassNotFoundException { LanguageServerInterface languageServer = new LanguageServerInterface(); - var abstractSyntaxString = languageServer.getAbstractSyntaxAsString("public class test{\n" + + var res = languageServer.getResultSetAndAbstractSyntax("import java.lang.Integer; public class test{\n" + " \n" + - " public main(int test){\n" + - "return test;\n" + - " }\n" + - "}"); - var resultSet = languageServer.getResultSet("public class test{\n" + - " \n" + - " public main(int test){\n" + - "return test;\n" + + " public main( test){\n" + + " Integer i = test; " + + " return i;\n" + " }\n" + "}"); - System.out.println(resultSet.toString()); - System.out.println(abstractSyntaxString); + System.out.println("TEST OUTPUT:"); + System.out.println(res.getResultSets().toString()); + System.out.println(res.getPrintedAst()); } }