From 124dea2e5811a2b6edb544f151ee4a7435fc4204 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Tue, 24 Oct 2023 12:05:19 +0200 Subject: [PATCH] Static blocks (might be needed for something) --- resources/bytecode/javFiles/Static.jav | 5 +++++ .../parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java | 6 +++++- src/test/java/TestComplete.java | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/resources/bytecode/javFiles/Static.jav b/resources/bytecode/javFiles/Static.jav index a0c00c25..98166abb 100644 --- a/resources/bytecode/javFiles/Static.jav +++ b/resources/bytecode/javFiles/Static.jav @@ -3,6 +3,11 @@ import java.lang.Integer; public class Static { static i = 20; + static { + var x = 30; + i = x; + } + static m() { return i; } diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java index d047e645..209b5ba9 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/SyntaxTreeGenerator.java @@ -296,7 +296,6 @@ public class SyntaxTreeGenerator { private void convert(ClassBodyDeclarationContext classBody, List fields, List constructors, List 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); } } diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 99619afc..7935960b 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -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); } }