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
18 changed files with 177 additions and 14 deletions

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

@@ -97,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;

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,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

@@ -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);