From 65a71ebe0c2a56e17023859915f32a784adc4bc1 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Fri, 8 Mar 2024 14:00:52 +0100 Subject: [PATCH] Add test for #285 and partially fix it --- resources/bytecode/javFiles/Bug285.jav | 15 +++++++++++++++ .../SyntaxTreeGenerator/StatementGenerator.java | 6 ++++-- src/test/java/TestComplete.java | 7 +++++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 resources/bytecode/javFiles/Bug285.jav diff --git a/resources/bytecode/javFiles/Bug285.jav b/resources/bytecode/javFiles/Bug285.jav new file mode 100644 index 00000000..e6b0696c --- /dev/null +++ b/resources/bytecode/javFiles/Bug285.jav @@ -0,0 +1,15 @@ +import java.util.Optional; +import java.lang.Integer; + +class StaticClass { + static StaticClass barbar() { + return new StaticClass(); + } +} + +public class Bug285 { + void foo() { + Optional opt = Optional.empty(); + StaticClass b = StaticClass.barbar(); + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 000526b3..1875a312 100644 --- a/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/main/java/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -13,6 +13,7 @@ import de.dhbwstuttgart.core.JavaTXCompiler; import de.dhbwstuttgart.syntaxtree.*; import de.dhbwstuttgart.syntaxtree.statement.*; import de.dhbwstuttgart.syntaxtree.type.Void; +import de.dhbwstuttgart.target.tree.expression.TargetClassName; import org.antlr.v4.runtime.Token; import de.dhbwstuttgart.exceptions.NotImplementedException; @@ -769,10 +770,11 @@ public class StatementGenerator { if (localVars.get(expression) != null) { return new LocalVar(expression, localVars.get(expression), offset); } else if (fields.get(expression) != null) {// PL 2018-11-01 fields eingefuegt, damit die fields immer die - // gleiche TPH bekommen + // gleiche TPH bekommen var field = fields.get(expression); return new FieldVar(new This(offset), Modifier.isStatic(field.modifiers()), expression, fields.get(expression).type(), offset); - + } else if (reg.contains(expression)) { + return new StaticClassName(reg.getName(expression), offset); } else { // lokale Variable wurde ohne "var"-Keyword deklariert und direkt mit Wert versehen localVars.put(expression, TypePlaceholder.fresh(offset)); diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index 307f04cd..45d3cdb6 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -876,4 +876,11 @@ public class TestComplete { var clazz = classFiles.get("Bug112"); var instance = clazz.getDeclaredConstructor().newInstance(); } + + @Test + public void testBug285() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "Bug285.jav"); + var clazz = classFiles.get("Bug285"); + var instance = clazz.getDeclaredConstructor().newInstance(); + } }