From 837317c84cc921c0ba045c5ea151b41d7d98d0e8 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Fri, 27 Oct 2023 15:28:46 +0200 Subject: [PATCH] Make System.out.println work --- resources/bytecode/javFiles/HelloWorld.jav | 9 +++++++++ src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java | 4 ++++ src/test/java/TestComplete.java | 8 ++++++++ 3 files changed, 21 insertions(+) create mode 100644 resources/bytecode/javFiles/HelloWorld.jav diff --git a/resources/bytecode/javFiles/HelloWorld.jav b/resources/bytecode/javFiles/HelloWorld.jav new file mode 100644 index 00000000..ff3df087 --- /dev/null +++ b/resources/bytecode/javFiles/HelloWorld.jav @@ -0,0 +1,9 @@ +import java.lang.System; +import java.lang.String; +import java.io.PrintStream; + +public class HelloWorld { + static hello() { + System.out.println("Hello World!"); + } +} \ No newline at end of file diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 9a36f871..c337dcd0 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -119,6 +119,10 @@ public class JavaTXCompiler { if (clazz.getClassName().equals(name)) return clazz; } } + try { + var clazz = classLoader.loadClass(name.toString()); + return ASTFactory.createClass(clazz); + } catch (ClassNotFoundException ignored) {} return null; } diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index e4e5849b..3b7386ec 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -754,4 +754,12 @@ public class TestComplete { var m = clazz.getDeclaredMethod("m"); assertEquals(m.invoke(instance), 5); } + + @Test + public void testHelloWorld() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "HelloWorld.jav"); + var clazz = classFiles.get("HelloWorld"); + var hello = clazz.getDeclaredMethod("hello"); + hello.invoke(null); + } }