Static blocks (might be needed for something)

This commit is contained in:
Daniel Holle 2023-10-24 12:05:19 +02:00
parent eaef00ff54
commit 124dea2e58
3 changed files with 11 additions and 2 deletions

View File

@ -3,6 +3,11 @@ import java.lang.Integer;
public class Static {
static i = 20;
static {
var x = 30;
i = x;
}
static m() {
return i;
}

View File

@ -296,7 +296,6 @@ public class SyntaxTreeGenerator {
private void convert(ClassBodyDeclarationContext classBody, List<Field> fields, List<Constructor> constructors, List<Method> methods, JavaClassName name, RefType superClass, GenericsRegistry generics) {
MemberdeclContext member;
// Statement-Blöcke und "leere Zeilen" (;) werden noch nicht berücksichtigt
if (classBody instanceof MemberdeclContext) {
member = (MemberdeclContext) classBody;
Integer membermodifiers = 0;
@ -327,6 +326,11 @@ public class SyntaxTreeGenerator {
default:
break;
}
} else if (classBody instanceof Java17Parser.ClassblockContext ctx && ctx.STATIC() != null) {
// Static blocks
var stmtgen = new StatementGenerator(superClass, compiler, reg, generics, this.fields, new HashMap<>());
var block = stmtgen.convert(((Java17Parser.ClassblockContext) classBody).block(), false);
staticFieldInitializations.addAll(block.statements);
}
}

View File

@ -725,6 +725,6 @@ public class TestComplete {
var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Static.jav");
var clazz = classFiles.get("Static");
var m = clazz.getDeclaredMethod("m");
assertEquals(m.invoke(null), 20);
assertEquals(m.invoke(null), 30);
}
}