Compare commits

..

4 Commits

Author SHA1 Message Date
7b915df43d Exception type not gets resolved with classpath
Some checks failed
Build and Test with Maven / Build-and-test-with-Maven (push) Failing after 45s
2025-05-14 13:57:31 +02:00
ccfd9a65b8 hopefully fix classloader to scan classpath 2025-05-12 15:31:53 +02:00
9c675ee860 add basic implementation for try catch blocks. still need to resolve dependencies in classpath 2025-05-12 15:31:28 +02:00
15926e8e18 add syntax tree structure for try-catch-finally-blocks 2025-05-06 13:09:06 +02:00
29 changed files with 179 additions and 623 deletions

View File

@@ -15,7 +15,7 @@ jobs:
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '23'
java-version: '22'
cache: 'maven'
- name: Compile project
run: |

View File

@@ -539,7 +539,7 @@ catchClause
;
catchType
: qualifiedName ('|' qualifiedName)*
: typeType ('|' typeType)*
;
finallyBlock

View File

@@ -10,6 +10,7 @@ import de.dhbwstuttgart.target.tree.expression.*;
import de.dhbwstuttgart.target.tree.type.*;
import org.objectweb.asm.*;
import java.lang.annotation.Target;
import java.lang.invoke.*;
import java.lang.reflect.Modifier;
import java.util.*;
@@ -1158,11 +1159,48 @@ public class Codegen {
mv.visitLabel(end);
break;
}
default:
throw new CodeGenException("Unexpected value: " + expr);
case TargetPattern targetPattern:
break;
case TargetTryCatchFinally targetTryCatchFinally:
generateTryCatchFinally(targetTryCatchFinally, state);
}
}
private void generateTryCatchFinally(TargetTryCatchFinally tryCatchFinally, State state){
MethodVisitor mv = state.mv;
Label startTry = new Label();
Label endTry = new Label();
Label endOrFinally = new Label();
//Try
mv.visitLabel(startTry);
generate(state, tryCatchFinally.tryBlock());
mv.visitLabel(endTry);
mv.visitJumpInsn(GOTO, endOrFinally);
for (var catchClause : tryCatchFinally.catchClauses()){
Label startCatch = new Label();
Label endCatch = new Label();
mv.visitTryCatchBlock(startTry, endOrFinally, startCatch, "java/lang/Exception"); //ignore all exception types except the first
mv.visitLabel(startCatch);
if (!catchClause.identifier().isEmpty()) {
mv.visitFrame(F_SAME1, 0, null, 1, new Object[]{"java/lang/Exception"});
LocalVar excep = state.createVariable(catchClause.identifier(), catchClause.exceptionNames().getFirst());
mv.visitVarInsn(ASTORE, excep.index());
}
generate(state, catchClause.catchBlock());
mv.visitLabel(endCatch);
mv.visitJumpInsn(GOTO, endOrFinally);
}
if (tryCatchFinally.finallyBlock().isPresent()){
mv.visitLabel(endOrFinally);
generate(state, tryCatchFinally.finallyBlock().get());
}
else mv.visitLabel(endOrFinally);
}
private void generateForEach(TargetForEach forEach, State state) {
state.enterScope();
TargetVarDecl vd = (TargetVarDecl) forEach.vardecl();

View File

@@ -5,7 +5,6 @@ import de.dhbwstuttgart.bytecode.Codegen;
import de.dhbwstuttgart.environment.CompilationEnvironment;
import de.dhbwstuttgart.environment.DirectoryClassLoader;
import de.dhbwstuttgart.exceptions.DebugException;
import de.dhbwstuttgart.languageServerInterface.model.LanguageServerTransferObject;
import de.dhbwstuttgart.parser.JavaTXParser;
import de.dhbwstuttgart.parser.NullToken;
import de.dhbwstuttgart.parser.scope.GenericsRegistry;
@@ -98,7 +97,7 @@ public class JavaTXCompiler {
path.add(new File(System.getProperty("user.dir")));
}
if (outputPath != null) path.add(outputPath);
classLoader = new DirectoryClassLoader(path, ClassLoader.getSystemClassLoader());
classLoader = new DirectoryClassLoader(path, ClassLoader.getPlatformClassLoader());
environment = new CompilationEnvironment(sources, classLoader);
classPath = path;
this.outputPath = outputPath;
@@ -458,85 +457,6 @@ public class JavaTXCompiler {
return results.stream().map((unifyPairs -> new ResultSet(UnifyTypeFactory.convert(unifyPairs, Pair.generateTPHMap(cons))))).collect(Collectors.toList());
}
/**
* TEMPORARY - Only for Language Server Usage
*/
public LanguageServerTransferObject getResultSetAndAbstractSyntax(File file) throws IOException, ClassNotFoundException {
var sf = sourceFiles.get(file);
if(sf == null){
sf = sourceFiles.values().stream().findFirst().get();
}
Set<ClassOrInterface> 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<Pair> cons = getConstraints(file);
Set<Set<UnifyPair>> results = new HashSet<>();
try {
Writer logFile = new OutputStreamWriter(new NullOutputStream());
IFiniteClosure finiteClosure = UnifyTypeFactory.generateFC(allClasses.stream().toList(), logFile, classLoader, this);
ConstraintSet<UnifyPair> unifyCons = UnifyTypeFactory.convert(this, cons);
Function<UnifyPair, UnifyPair> 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;
};
unifyCons = unifyCons.map(distributeInnerVars);
TypeUnify unify = new TypeUnify();
Set<PlaceholderType> varianceTPHold;
Set<PlaceholderType> varianceTPH = new HashSet<>();
varianceTPH = varianceInheritanceConstraintSet(unifyCons);
List<Set<Constraint<UnifyPair>>> 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);
generateBytecode(sf, li.getResults());
return new LanguageServerTransferObject(li.getResults(), sf, ASTTypePrinter.print(sf), generatedGenerics);
}
/* UnifyResultModel End */
else {
Set<Set<UnifyPair>> result = unify.unifyOderConstraints(unifyCons.getUndConstraints(), oderConstraints, finiteClosure, logFile, log, new UnifyResultModel(cons, finiteClosure), usedTasks);
results.addAll(result);
results = results.stream().map(x -> {
Optional<Set<UnifyPair>> 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));
}
} catch (ClassNotFoundException e) {
}
generateBytecode(sf, results.stream().map((unifyPairs -> new ResultSet(UnifyTypeFactory.convert(unifyPairs, Pair.generateTPHMap(cons))))).collect(Collectors.toList()));
return new LanguageServerTransferObject(results.stream().map((unifyPairs -> new ResultSet(UnifyTypeFactory.convert(unifyPairs, Pair.generateTPHMap(cons))))).collect(Collectors.toList()), sf, ASTTypePrinter.print(sf), generatedGenerics);
}
/**
* Vererbt alle Variancen bei Paaren (a <. theta) oder (Theta <. a) wenn a eine Variance !=0 hat auf alle Typvariablen in Theta.
*

View File

@@ -16,7 +16,7 @@ public class DirectoryClassLoader extends URLClassLoader implements IByteArrayCl
// }
public DirectoryClassLoader(List<File> directory, java.lang.ClassLoader parent) {
super(directory.stream().map(DirectoryClassLoader::dirToURL).flatMap(List::stream).collect(Collectors.toList()).toArray(new URL[0]), parent.getParent());
super(directory.stream().map(DirectoryClassLoader::dirToURL).flatMap(List::stream).toArray(URL[]::new), parent);
}
private static URL[] generateURLArray(URL url) {

View File

@@ -1,277 +0,0 @@
package de.dhbwstuttgart.languageServerInterface;
import de.dhbwstuttgart.bytecode.Codegen;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.environment.ByteArrayClassLoader;
import de.dhbwstuttgart.environment.IByteArrayClassLoader;
import de.dhbwstuttgart.languageServerInterface.model.LanguageServerTransferObject;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
import de.dhbwstuttgart.target.generate.ASTToTargetAST;
import de.dhbwstuttgart.target.generate.GenericsResult;
import de.dhbwstuttgart.target.tree.TargetStructure;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* Implementation of an Interface for the Language-Server to get the Resultset and abstract Syntax.
*/
public class LanguageServerInterface {
public LanguageServerTransferObject getResultSetAndAbastractSyntax(String path, String resetNamesTo) throws IOException, URISyntaxException, ClassNotFoundException {
NameGenerator.resetTo(resetNamesTo);
return getResultSetAndAbstractSyntax(path);
}
public SourceFile getAst(String path, String resetNamesTo) throws IOException, URISyntaxException, ClassNotFoundException {
NameGenerator.resetTo(resetNamesTo);
return getAST(path);
}
/**
* returns the ResultSets, GenericResultSet and the AST
* You have to give the input as well as the path because of potential locks when the File is currently opened in an IDE.
* Example: file:///c:/test/main.jav -> file:///c:/test/out/main.class
*
* @param path the URI of the File. See Example.
* @throws IOException
* @throws ClassNotFoundException
* @throws URISyntaxException
*/
public LanguageServerTransferObject getResultSetAndAbstractSyntax(String path) throws IOException, ClassNotFoundException, URISyntaxException {
System.setOut(new PrintStream(OutputStream.nullOutputStream()));
URI uri = new URI(path);
ArrayList<String> pathWithoutName = new ArrayList<>(List.of(uri.getPath().split("/")));
pathWithoutName.remove(List.of(uri.getPath().split("/")).size() - 1);
String stringPathWithoutName = "";
for (String i : pathWithoutName) {
stringPathWithoutName += "/" + i;
}
try {
FileUtils.cleanDirectory(new File(stringPathWithoutName + "/out"));
} catch (Exception e) {
}
try {
(new File(stringPathWithoutName + "/out")).mkdirs();
} catch (Exception e) {
}
var test = getLanguageServerTransferObject(uri.getPath().split("/")[uri.getPath().split("/").length - 1], new ByteArrayClassLoader(), new File(stringPathWithoutName).getPath());
System.setOut(System.out);
return test;
// File oldTempSourcefile = new File(stringPathWithoutName + "/out/" + uri.getPath().split("/")[uri.getPath().split("/").length - 1].replace(".jav", ".tmp"));
// File oldTempOutFile = new File(stringPathWithoutName + "/out/" + uri.getPath().split("/")[uri.getPath().split("/").length - 1].replace(".jav", ".class"));
// Files.deleteIfExists(oldTempSourcefile.toPath());
// Files.deleteIfExists(oldTempOutFile.toPath());
// FileUtils.deleteDirectory(oldTempSourcefile);
//
// File tempSourcefile = new File(stringPathWithoutName + "/out/" + uri.getPath().split("/")[uri.getPath().split("/").length - 1].replace(".jav", ".tmp"));
// File tempOutFile = new File(stringPathWithoutName + "/out/");
// FileUtils.writeStringToFile(tempSourcefile, input, "UTF-8");
//
// //TODO: Enable for multiple Class-Files
// JavaTXCompiler tx = new JavaTXCompiler(new ArrayList<File>(List.of(tempSourcefile)), new ArrayList<File>(List.of((tempSourcefile))), tempOutFile);
// var result = tx.getResultSetAndAbstractSyntax(tempSourcefile);
// System.setOut(System.out);
// tempSourcefile.delete();
// tempOutFile.delete();
// return result;
}
/**
* returns the AST without calculating the result
* You have to give the input as well as the path because of potential locks when the File is currently opened in an IDE.
* Example: file:///c:/test/main.jav -> file:///c:/test/out/main.class
*
* @param path the URI of the File. See Example.
* @throws IOException
* @throws ClassNotFoundException
* @throws URISyntaxException
*/
public SourceFile getAST(String path) throws IOException, ClassNotFoundException, URISyntaxException {
System.setOut(new PrintStream(OutputStream.nullOutputStream()));
URI uri = new URI(path);
ArrayList<String> pathWithoutName = new ArrayList<>(List.of(uri.getPath().split("/")));
pathWithoutName.remove(List.of(uri.getPath().split("/")).size() - 1);
String stringPathWithoutName = "";
for (String i : pathWithoutName) {
stringPathWithoutName += "/" + i;
}
try {
FileUtils.cleanDirectory(new File(stringPathWithoutName + "/out"));
} catch (Exception e) {
}
try {
(new File(stringPathWithoutName + "/out")).mkdirs();
} catch (Exception e) {
}
var test = getAST(uri.getPath().split("/")[uri.getPath().split("/").length - 1], new File(stringPathWithoutName).getPath());
System.setOut(System.out);
return test;
}
private static void writeClassFile(String name, byte[] code, Path outputPath) throws IOException {
Files.createDirectories(outputPath);
Files.write(outputPath.resolve(name + ".class"), code);
}
public static Map<String, ? extends Class<?>> generateClassFiles(IByteArrayClassLoader classLoader, Path path, Path outputPath, String... files) throws IOException, ClassNotFoundException {
Files.createDirectories(outputPath);
var filenames = Arrays.stream(files).map(filename -> Path.of(path.toString(), filename).toFile()).toList();
var compiler = new JavaTXCompiler(filenames, List.of(path.toFile(), outputPath.toFile()), outputPath.toFile());
var result = new HashMap<String, Class<?>>();
for (var file : filenames) {
var resultSet = compiler.typeInference(file);
var sourceFile = compiler.sourceFiles.get(file);
var converter = new ASTToTargetAST(compiler, resultSet, sourceFile, classLoader);
var classes = compiler.sourceFiles.get(file).getClasses();
result.putAll(classes.stream().map(cli -> {
try {
return generateClass(converter.convert(cli), classLoader, converter, outputPath);
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}).collect(Collectors.toMap(Class::getName, Function.identity())));
converter.generateFunNTypes();
for (var entry : converter.auxiliaries.entrySet()) {
writeClassFile(entry.getKey(), entry.getValue(), outputPath);
}
}
for (var entry : compiler.loadedClasses.entrySet()) {
var name = entry.getKey().toString();
result.put(name, classLoader.loadClass(Path.of(entry.getValue().classFile().toURI())));
}
return result;
}
public static Class<?> generateClass(TargetStructure clazz, IByteArrayClassLoader classLoader, ASTToTargetAST converter, Path outputPath) throws IOException {
Codegen codegen = new Codegen(clazz, converter.compiler, converter);
var code = codegen.generate();
writeClassFile(clazz.qualifiedName().getClassName(), code, outputPath);
return classLoader.loadClass(code);
}
public static Map<String, ? extends Class<?>> generateClassFiles(String filename, IByteArrayClassLoader classLoader, String filePath) throws IOException, ClassNotFoundException {
var file = Path.of(filePath, filename).toFile();
var compiler = new JavaTXCompiler(List.of(file), List.of(file.getParentFile()), Path.of(filePath + "/out").toFile());
var resultSet = compiler.typeInference(file);
var sourceFile = compiler.sourceFiles.get(file);
var converter = new ASTToTargetAST(compiler, resultSet, sourceFile, classLoader);
var classes = compiler.sourceFiles.get(file).getClasses();
var result = classes.stream().map(cli -> {
try {
return generateClass(converter.convert(cli), classLoader, converter, Path.of(filePath + "/out/"));
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}).collect(Collectors.toMap(Class::getName, Function.identity()));
converter.generateFunNTypes();
for (var entry : converter.auxiliaries.entrySet()) {
writeClassFile(entry.getKey(), entry.getValue(), Path.of(filePath + "/out/"));
}
return result;
}
public static SourceFile getAST(String filename, String filePath) throws IOException, ClassNotFoundException {
var file = Path.of(filePath, filename).toFile();
var compiler = new JavaTXCompiler(List.of(file), List.of(file.getParentFile()), Path.of(filePath + "/out").toFile());
return compiler.sourceFiles.get(file);
}
public static LanguageServerTransferObject getLanguageServerTransferObject(String filename, IByteArrayClassLoader classLoader, String filePath) throws IOException, ClassNotFoundException {
var file = Path.of(filePath, filename).toFile();
var compiler = new JavaTXCompiler(List.of(file), List.of(file.getParentFile()), Path.of(filePath + "/out").toFile());
var resultSet = compiler.typeInference(file);
var sourceFile = compiler.sourceFiles.get(file);
var converter = new ASTToTargetAST(compiler, resultSet, sourceFile, classLoader);
compiler.generateBytecode();
converter.generateFunNTypes();
var ta = converter.javaGenerics();
var tb = converter.txGenerics();
Map<SourceFile, List<GenericsResult>> generics = new HashMap<>();
ArrayList<GenericsResult> genericsResults = new ArrayList<>();
genericsResults.addAll(ta);
genericsResults.addAll(tb);
generics.put(converter.compiler.sourceFiles.values().stream().findFirst().get(), ta);
var test = new LanguageServerTransferObject(resultSet, converter.compiler.sourceFiles.values().stream().findFirst().get(), "", generics);
return test;
}
/**
* generates Bytecode for the given Path of the File.
* The Generated Bytecode can be found in the same place as the path, except the File lies in an /out/ Directory
* Example: file:///c:/test/main.jav -> file:///c:/test/out/main.class
*
* @param uri the URI of the File. See Example.
* @throws IOException
* @throws ClassNotFoundException
* @throws URISyntaxException
*/
public void generateBytecode(URI uri) throws IOException, ClassNotFoundException, URISyntaxException {
System.setOut(new PrintStream(OutputStream.nullOutputStream()));
File inputDir = new File(uri.getPath());
File outFile = new File(uri.getPath() + "/out");
FileUtils.cleanDirectory(outFile);
String[] allowedEndings = {".jav"};
ArrayList<File> files = new ArrayList<>();
try (Stream<Path> stream = Files.walk(Paths.get(inputDir.toURI()))) {
stream.filter(Files::isRegularFile)
.forEach(el -> files.add(el.toFile()));
}
List<String> javFiles = files.stream().filter(el -> el.getName().split("\\.").length >= 2 && el.getName().split("\\.")[el.getName().split("\\.").length - 1].contains("jav")).map(el -> el.getPath().replace(inputDir.getPath(), "").substring(1)).toList();
//TODO: Link between Files
generateClassFiles(new ByteArrayClassLoader(), Path.of(inputDir.toURI()), Path.of(outFile.toURI()), javFiles.toArray(new String[0]));
System.setOut(System.out);
}
}

View File

@@ -1,40 +0,0 @@
package de.dhbwstuttgart.languageServerInterface;
import de.dhbwstuttgart.languageServerInterface.model.CustomParserErrorHandler;
import de.dhbwstuttgart.languageServerInterface.model.ParserError;
import de.dhbwstuttgart.parser.antlr.Java17Lexer;
import de.dhbwstuttgart.parser.antlr.Java17Parser;
import de.dhbwstuttgart.parser.antlr.Java17ParserBaseListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.ParseTreeWalker;
import java.util.List;
public class ParserInterface {
public List<ParserError> getParseErrors(String input){
CustomParserErrorHandler errorListener = new CustomParserErrorHandler();
CharStream charStream = CharStreams.fromString(input);
Java17Lexer lexer = new Java17Lexer(charStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);
Java17Parser parser = new Java17Parser(tokens);
parser.removeErrorListeners();
parser.addErrorListener(errorListener);
ParseTree tree = parser.sourceFile();
ParseTreeWalker walker = new ParseTreeWalker();
Java17ParserBaseListener listener = new Java17ParserBaseListener();
walker.walk(listener, tree);
return errorListener.getErrorMessages();
}
}

View File

@@ -1,47 +0,0 @@
package de.dhbwstuttgart.languageServerInterface.model;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.atn.ATNConfigSet;
import org.antlr.v4.runtime.dfa.DFA;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
public class CustomParserErrorHandler implements ANTLRErrorListener {
private final List<ParserError> errorMessages = new ArrayList<>();
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
int endCharPosition = charPositionInLine;
if (offendingSymbol instanceof Token) {
Token offendingToken = (Token) offendingSymbol;
endCharPosition = charPositionInLine + offendingToken.getText().length();
}
ParserError parserError = new ParserError(line, charPositionInLine, endCharPosition, msg);
errorMessages.add(parserError);
}
@Override
public void reportAmbiguity(Parser parser, DFA dfa, int i, int i1, boolean b, BitSet bitSet, ATNConfigSet atnConfigSet) {
}
@Override
public void reportAttemptingFullContext(Parser parser, DFA dfa, int i, int i1, BitSet bitSet, ATNConfigSet atnConfigSet) {
}
@Override
public void reportContextSensitivity(Parser parser, DFA dfa, int i, int i1, int i2, ATNConfigSet atnConfigSet) {
}
public List<ParserError> getErrorMessages() {
return errorMessages;
}
}

View File

@@ -1,31 +0,0 @@
package de.dhbwstuttgart.languageServerInterface.model;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.target.generate.GenericsResult;
import de.dhbwstuttgart.typeinference.result.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class LanguageServerTransferObject {
List<ResultSet> resultSets;
SourceFile Ast;
String printedAst;
Map<SourceFile, List<GenericsResult>> generatedGenerics = new HashMap<>();
public LanguageServerTransferObject(List<ResultSet> resultSets, SourceFile Ast, String printedAst, Map<SourceFile, List<GenericsResult>> generatedGenerics) {
this.resultSets = resultSets;
this.Ast = Ast;
this.printedAst = printedAst;
this.generatedGenerics = generatedGenerics;
}
public List<ResultSet> getResultSets() {return resultSets;}
public SourceFile getAst() {return Ast;}
public String getPrintedAst() {return printedAst;}
public Map<SourceFile, List<GenericsResult>> getGeneratedGenerics() {return generatedGenerics;}
}

View File

@@ -1,48 +0,0 @@
package de.dhbwstuttgart.languageServerInterface.model;
public class ParserError {
private int line;
private int charPositionInLine;
private int endCharPosition;
String msg;
public ParserError(int line, int charPositionInLine, int endCharPosition, String msg) {
this.line = line;
this.charPositionInLine = charPositionInLine;
this. endCharPosition = endCharPosition;
this.msg = msg;
}
public int getEndCharPosition() {
return endCharPosition;
}
public void setEndCharPosition(int endCharPosition) {
this.endCharPosition = endCharPosition;
}
public void setCharPositionInLine(int charPositionInLine) {
this.charPositionInLine = charPositionInLine;
}
public void setLine(int line) {
this.line = line;
}
public void setMsg(String msg) {
this.msg = msg;
}
public int getCharPositionInLine() {
return charPositionInLine;
}
public int getLine() {
return line;
}
public String getMsg() {
return msg;
}
}

View File

@@ -1,20 +0,0 @@
package de.dhbwstuttgart.languageServerInterface.model;
import com.google.common.reflect.TypeResolver;
import de.dhbwstuttgart.typeinference.unify.UnifyResultEvent;
import de.dhbwstuttgart.typeinference.unify.UnifyResultListener;
public class ResultSetListener implements UnifyResultListener {
TypeResolver typeResolver;
public ResultSetListener(TypeResolver typeResolver){
this.typeResolver = typeResolver;
}
@Override
public void onNewTypeResultFound(UnifyResultEvent evt) {
}
}

View File

@@ -1,12 +1,7 @@
package de.dhbwstuttgart.parser.SyntaxTreeGenerator;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -16,6 +11,7 @@ import de.dhbwstuttgart.syntaxtree.statement.*;
import de.dhbwstuttgart.syntaxtree.type.Void;
import de.dhbwstuttgart.target.tree.expression.TargetUnaryOp;
import de.dhbwstuttgart.target.generate.StatementToTargetExpression;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.exceptions.NotImplementedException;
@@ -638,8 +634,16 @@ public class StatementGenerator {
}
private Statement convert(Java17Parser.TrycatchblockContext stmt) {
// TODO
throw new NotImplementedException();
Block tryBlock = convert(stmt.block(), false);
List<CatchClause> catchClauses = stmt.catchClause().stream().
map(x ->
new CatchClause(x.catchType().typeType().stream().map(y -> TypeGenerator.convert(y, reg, generics)).toList(),
x.identifier().getText(),
convert(x.block(), false)
)).toList();
Optional<Block> finallyBlock = stmt.finallyBlock() != null ? Optional.of(convert(stmt.finallyBlock().block(), false)) : Optional.empty();
return new TryCatchFinally(tryBlock, catchClauses, finallyBlock, stmt.getStart());
}
private Statement convert(Java17Parser.TrycatchresourceContext stmt) {

View File

@@ -39,6 +39,11 @@ public class SyntacticSugar {
public void visit(LambdaExpression le) {
//PL 2024-04-09 Do nothing, as in a LambdaExpression a return could be
}
@Override
public void visit(TryCatchFinally tryCatchFinally) {
}
}
private static boolean hasReturn(Block block) {

View File

@@ -1,5 +1,6 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.statement.TryCatchFinally;
import de.dhbwstuttgart.syntaxtree.type.*;
public interface ASTVisitor extends StatementVisitor{
@@ -40,4 +41,6 @@ public interface ASTVisitor extends StatementVisitor{
void visit(GuardedPattern aGuardedPattern);
void visit(TryCatchFinally tryCatchFinally);
}

View File

@@ -0,0 +1,19 @@
package de.dhbwstuttgart.syntaxtree;
import de.dhbwstuttgart.syntaxtree.statement.Block;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import java.util.List;
public class CatchClause {
public List<RefTypeOrTPHOrWildcardOrGeneric> exceptionTypes;
public String identifier;
public Block catchBlock;
public CatchClause(List<RefTypeOrTPHOrWildcardOrGeneric> exceptionTypes, String identifier, Block catchBlock){
this.exceptionTypes = exceptionTypes;
this.identifier = identifier;
this.catchBlock = catchBlock;
}
}

View File

@@ -85,4 +85,6 @@ public interface StatementVisitor {
void visit(Throw aThrow);
void visit(Ternary ternary);
void visit(TryCatchFinally tryCatchFinally);
}

View File

@@ -14,10 +14,6 @@ public class NameGenerator {
strNextName = "A";
}
public static void resetTo(String name) {
strNextName = name;
}
/**
* Berechnet einen neuen, eindeutigen Namen f�r eine neue
* <code>TypePlaceholder</code>. <br>Author: J�rg B�uerle

View File

@@ -0,0 +1,30 @@
package de.dhbwstuttgart.syntaxtree.statement;
import de.dhbwstuttgart.syntaxtree.CatchClause;
import de.dhbwstuttgart.syntaxtree.StatementVisitor;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import org.antlr.v4.runtime.Token;
import de.dhbwstuttgart.syntaxtree.type.Void;
import java.util.List;
import java.util.Optional;
public class TryCatchFinally extends Statement{
public Block tryBlock;
public List<CatchClause> catchClauses;
public Optional<Block> finallyBlock;
public TryCatchFinally(Block tryBlock, List<CatchClause> catchClauses, Optional<Block> finallyBlock, Token offset){
super(new Void(offset), offset);
this.tryBlock = tryBlock;
this.catchClauses = catchClauses;
this.finallyBlock = finallyBlock;
}
@Override
public void accept(StatementVisitor visitor) {
visitor.visit(this);
}
}

View File

@@ -530,4 +530,9 @@ public class OutputGenerator implements ASTVisitor {
out.append(" with ");
aGuardedPattern.getCondition().accept(this);
}
@Override
public void visit(TryCatchFinally tryCatchFinally) {
out.append("");
}
}

View File

@@ -434,6 +434,11 @@ public abstract class GenerateGenerics {
whileStmt.loopBlock.accept(this);
}
@Override
public void visit(TryCatchFinally tryCatchFinally) {
}
@Override
public void visit(ArgumentList arglist) {
for (int i = 0; i < arglist.getArguments().size(); i++) {
@@ -536,6 +541,11 @@ public abstract class GenerateGenerics {
super.visit(methodCall);
typeVariables.addAll(findTypeVariables(methodCall.getType()));
}
@Override
public void visit(TryCatchFinally tryCatchFinally) {
}
});
}

View File

@@ -76,6 +76,11 @@ public class StatementToTargetExpression implements ASTVisitor {
localVariables.add(varDecl.getName());
}
@Override
public void visit(TryCatchFinally tryCatchFinally) {
}
@Override
public void visit(LambdaExpression lambda) {
} // Don't look at lambda expressions
@@ -591,4 +596,14 @@ public class StatementToTargetExpression implements ASTVisitor {
public void visit(GuardedPattern aGuardedPattern) {
result = new TargetGuard((TargetPattern) converter.convert(aGuardedPattern.getNestedPattern()), converter.convert(aGuardedPattern.getCondition()));
}
@Override
public void visit(TryCatchFinally tryCatchFinally) {
result = new TargetTryCatchFinally(
converter.convert(tryCatchFinally.tryBlock),
tryCatchFinally.catchClauses.stream().map(x -> new TargetCatchClause(x.exceptionTypes.stream().map(converter::convert).toList(), x.identifier, converter.convert(x.catchBlock))).toList(),
tryCatchFinally.finallyBlock.map(converter::convert)
);
}
}

View File

@@ -0,0 +1,8 @@
package de.dhbwstuttgart.target.tree.expression;
import de.dhbwstuttgart.target.tree.type.TargetType;
import java.util.List;
public record TargetCatchClause(List<TargetType> exceptionNames, String identifier, TargetBlock catchBlock) {
}

View File

@@ -3,7 +3,7 @@ package de.dhbwstuttgart.target.tree.expression;
import de.dhbwstuttgart.target.tree.type.*;
public sealed interface TargetExpression
permits TargetBinaryOp, TargetBlock, TargetBreak, TargetCast, TargetClassName, TargetContinue, TargetDo, TargetFieldVar, TargetFor, TargetForEach, TargetIf, TargetInstanceOf, TargetLambdaExpression, TargetLiteral, TargetLocalVar, TargetPattern, TargetReturn, TargetStatementExpression, TargetSuper, TargetSwitch, TargetTernary, TargetThis, TargetThrow, TargetUnaryOp, TargetVarDecl, TargetWhile, TargetYield {
permits TargetBinaryOp, TargetBlock, TargetBreak, TargetCast, TargetClassName, TargetContinue, TargetDo, TargetFieldVar, TargetFor, TargetForEach, TargetIf, TargetInstanceOf, TargetLambdaExpression, TargetLiteral, TargetLocalVar, TargetPattern, TargetReturn, TargetStatementExpression, TargetSuper, TargetSwitch, TargetTernary, TargetThis, TargetThrow, TargetTryCatchFinally, TargetUnaryOp, TargetVarDecl, TargetWhile, TargetYield {
default TargetType type() {
return null;

View File

@@ -0,0 +1,7 @@
package de.dhbwstuttgart.target.tree.expression;
import java.util.List;
import java.util.Optional;
public record TargetTryCatchFinally(TargetBlock tryBlock, List<TargetCatchClause> catchClauses, Optional<TargetBlock> finallyBlock) implements TargetExpression{
}

View File

@@ -2,6 +2,7 @@ package de.dhbwstuttgart.typedeployment;
import de.dhbwstuttgart.syntaxtree.*;
import de.dhbwstuttgart.syntaxtree.statement.LambdaExpression;
import de.dhbwstuttgart.syntaxtree.statement.TryCatchFinally;
import de.dhbwstuttgart.syntaxtree.type.TypePlaceholder;
import de.dhbwstuttgart.target.generate.GenericsResult;
import de.dhbwstuttgart.target.generate.GenericsResultSet;
@@ -32,6 +33,11 @@ public class TypeInsertPlacer extends AbstractASTWalker {
TypeInsertPlacerClass cl = new TypeInsertPlacerClass(classOrInterface, withResults, genericsResult);
this.inserts.addAll(cl.inserts);
}
@Override
public void visit(TryCatchFinally tryCatchFinally) {
}
}
class TypeInsertPlacerClass extends AbstractASTWalker{
@@ -63,6 +69,11 @@ class TypeInsertPlacerClass extends AbstractASTWalker{
super.visit(method);
}
@Override
public void visit(TryCatchFinally tryCatchFinally) {
}
@Override
public void visit(Field field) {
if(field.getType() instanceof TypePlaceholder){

View File

@@ -500,6 +500,12 @@ public class TYPEStmt implements StatementVisitor {
constraintsSet.addUndConstraint(new Pair(ternary.iffalse.getType(), ternary.getType(), PairOperator.SMALLERDOT));
}
@Override
public void visit(TryCatchFinally tryCatchFinally) {
tryCatchFinally.tryBlock.accept(this);
tryCatchFinally.catchClauses.forEach(c -> c.catchBlock.accept(this));
}
@Override
public void visit(Return returnExpr) {
returnExpr.retexpr.accept(this);

View File

@@ -835,7 +835,6 @@ public class TestComplete {
var instance = clazz.getDeclaredConstructor().newInstance();
}
@Ignore("Not implemented")
@Test
public void testStringSwitch() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "SwitchString.jav");
@@ -857,7 +856,6 @@ public class TestComplete {
var instance = clazz.getDeclaredConstructor().newInstance();
}
@Ignore("Not implemented")
@Test
public void testOverloadPattern() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "OverloadPattern.jav");
@@ -883,7 +881,6 @@ public class TestComplete {
assertEquals(m3.invoke(instance, 10), 10);
}
@Ignore("Not implemented")
@Test
public void testOverloadNestedPattern() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "OverloadNestedPattern.jav");
@@ -905,7 +902,7 @@ public class TestComplete {
assertEquals(m.invoke(instance, r1, r1), 3);
}
@Ignore("Not implemented")
//@Ignore("Not implemented")
@Test
public void testPatternMatchingHaskellStyle() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PatternMatchingHaskellStyle.jav");
@@ -928,7 +925,6 @@ public class TestComplete {
}
@Ignore("Not implemented")
@Test
public void testPatternMatchingListAppend() throws Exception {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "PatternMatchingListAppend.jav");

View File

@@ -1,51 +0,0 @@
package languageServerInterfaceTest;
import de.dhbwstuttgart.core.ConsoleInterface;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.syntaxtree.factory.NameGenerator;
import de.dhbwstuttgart.syntaxtree.visual.ASTPrinter;
import org.junit.Ignore;
import org.junit.Test;
import de.dhbwstuttgart.languageServerInterface.LanguageServerInterface;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
public class LangaugeServerInterfaceTest {
@Test
@Ignore
public void consoleInterfaceTest() throws IOException, ClassNotFoundException, URISyntaxException {
LanguageServerInterface languageServerInterface = new LanguageServerInterface();
var resp = languageServerInterface.getResultSetAndAbstractSyntax("/home/ruben/code/JavaCompilerCore/src/test/java/languageServerInterfaceTest/test.jav");
System.out.println("\n-----------------------------------------\n");
System.out.println(ASTPrinter.print(resp.getAst()));
System.out.println("\n-----------------------------------------\n");
LanguageServerInterface languageServerInterface2 = new LanguageServerInterface();
var ast = languageServerInterface2.getAst("/home/ruben/code/JavaCompilerCore/src/test/java/languageServerInterfaceTest/test.jav", "N");
System.out.println("\n-----------------------------------------\n");
System.out.println(ASTPrinter.print(ast));
System.out.println("\n-----------------------------------------\n");
System.out.println("");
}
@Test
@Ignore
public void testBytecodeGen() throws IOException, ClassNotFoundException, URISyntaxException {
//TODO: Ordner und Datei löschen wenn sie bereits existieren
LanguageServerInterface languageServerInterface = new LanguageServerInterface();
languageServerInterface.generateBytecode(new URI("c%3A/Users/ruben/Neuer%20Ordner%20%282%29/LSP-Vortrag/images/"));
}
}

View File

@@ -1,5 +0,0 @@
public class t{
public mofus(){
return 1;
}
}