From a3b4ea9b1cc58805be862c379ccc67f426a8f402 Mon Sep 17 00:00:00 2001 From: JanUlrich Date: Sat, 14 Dec 2019 17:50:23 +0100 Subject: [PATCH] [Bytecde] Generate new directory if given output path doesnt exist yet --- .../java/de/dhbwstuttgart/core/JavaTXCompiler.java | 4 +++- src/test/java/packages/Bytecode.java | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java index 3c234175..b1bb04ca 100644 --- a/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java +++ b/src/main/java/de/dhbwstuttgart/core/JavaTXCompiler.java @@ -793,7 +793,9 @@ public class JavaTXCompiler { System.out.println("generating " + name + ".class file ..."); // output = new FileOutputStream(new File(System.getProperty("user.dir") + // "/testBytecode/generatedBC/" +name+".class")); - output = new FileOutputStream(new File(path + File.separator + name.getClassName() + ".class")); + File outputFile = new File(path + File.separator + name.getClassName() + ".class"); + outputFile.getParentFile().mkdirs(); + output = new FileOutputStream(outputFile); output.write(bytecode); output.close(); System.out.println(name + ".class file generated"); diff --git a/src/test/java/packages/Bytecode.java b/src/test/java/packages/Bytecode.java index 1f7854c1..ece46ac8 100644 --- a/src/test/java/packages/Bytecode.java +++ b/src/test/java/packages/Bytecode.java @@ -22,4 +22,17 @@ public class Bytecode extends TestCase { f = new File(rootDirectory + "TestClass.class"); assertTrue(f.exists()); } + + @Test + public void testSetPackageNameInBytecodeAndOutputFolder() throws IOException, ClassNotFoundException { + JavaTXCompiler compiler = new JavaTXCompiler(new File(rootDirectory+"packageNameTest.jav")); + compiler.typeInference(); + File f = new File(rootDirectory + "output/de/test/TestClass.class"); + if(f.exists() && !f.isDirectory()) { + f.delete(); + } + compiler.generateBytecode(rootDirectory + "output/"); + f = new File(rootDirectory + "output/de/test/TestClass.class"); + assertTrue(f.exists()); + } }