Skip to content

Commit b1d5d74

Browse files
committed
[BAEL-8962] own review
1 parent c7ffa66 commit b1d5d74

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

Diff for: core-java-modules/core-java-compiler/src/main/java/com/baeldung/compilerApi/JavaCompilerApiDemo.java

+19-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.baeldung.compilerApi;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import java.nio.file.Files;
47
import java.nio.file.Path;
58
import java.nio.file.Paths;
@@ -8,15 +11,15 @@
811
* Demonstration of the JavaCompilerUtil class.
912
*/
1013
public class JavaCompilerApiDemo {
14+
private static final Logger logger = LoggerFactory.getLogger(JavaCompilerApiDemo.class);
1115

1216
public static void main(String[] args) {
1317
try {
1418
// Create output directory for compiled classes
1519
Path outputDir = Paths.get("compiled-classes");
1620

17-
// Initialize the compiler utility
1821
JavaCompilerUtils compilerUtil = new JavaCompilerUtils(outputDir);
19-
System.out.println("Java compiler initialized with output directory: " + outputDir.toAbsolutePath());
22+
logger.debug("Java compiler initialized with output directory: {}", outputDir.toAbsolutePath());
2023

2124
// Example 1: Compile from string
2225
compileFromStringExample(compilerUtil);
@@ -25,13 +28,12 @@ public static void main(String[] args) {
2528
compileFromFileExample(compilerUtil);
2629

2730
} catch (Exception e) {
28-
System.err.println("Error in compiler demo: ");
29-
e.printStackTrace();
31+
logger.error("Compilation failed {}", e.getMessage(), e);
3032
}
3133
}
3234

3335
private static void compileFromStringExample(JavaCompilerUtils compilerUtil) throws Exception {
34-
System.out.println("\n--- Example 1: Compile from String ---");
36+
logger.debug("\n--- Example 1: Compile from String ---");
3537

3638
// Define a simple class
3739
String className = "HelloWorld";
@@ -45,20 +47,20 @@ private static void compileFromStringExample(JavaCompilerUtils compilerUtil) thr
4547
boolean success = compilerUtil.compileFromString(className, sourceCode);
4648

4749
if (success) {
48-
System.out.println("Compilation successful!");
49-
System.out.println("Running the compiled class:");
50+
logger.debug("Compilation successful!");
51+
logger.debug("Running the compiled class:");
5052

5153
// Run the compiled class
52-
System.out.println("----- Output from HelloWorld -----");
54+
logger.debug("----- Output from HelloWorld -----");
5355
compilerUtil.runClass(className, "arg1", "arg2");
54-
System.out.println("---------------------------------");
56+
logger.debug("---------------------------------");
5557
} else {
56-
System.out.println("Compilation failed.");
58+
logger.error("Compilation failed.");
5759
}
5860
}
5961

6062
private static void compileFromFileExample(JavaCompilerUtils compilerUtil) throws Exception {
61-
System.out.println("\n--- Example 2: Compile from File ---");
63+
logger.debug("\n--- Example 2: Compile from File ---");
6264

6365
// Create a temporary Java file
6466
Path tempFile = Paths.get("Calculator.java");
@@ -84,26 +86,26 @@ private static void compileFromFileExample(JavaCompilerUtils compilerUtil) throw
8486
"}";
8587
Files.write(tempFile, sourceCode.getBytes());
8688

87-
System.out.println("Created temporary Java file: " + tempFile);
89+
logger.debug("Created temporary Java file: {}", tempFile);
8890

8991
// Compile the file
9092
boolean success = compilerUtil.compileFile(tempFile);
9193

9294
if (success) {
93-
System.out.println("Compilation successful!");
94-
System.out.println("Running the compiled class:");
95+
logger.debug("Compilation successful!");
96+
logger.debug("Running the compiled class:");
9597

9698
// Run the compiled class
97-
System.out.println("----- Output from Calculator -----");
99+
logger.debug("----- Output from Calculator -----");
98100
compilerUtil.runClass("Calculator", "5", "7");
99-
System.out.println("----------------------------------");
101+
logger.debug("----------------------------------");
100102
} else {
101103
System.out.println("Compilation failed.");
102104
}
103105

104106
// Clean up the temporary file
105107
Files.delete(tempFile);
106-
System.out.println("Deleted temporary file: " + tempFile);
108+
logger.debug("Deleted temporary file: {}", tempFile);
107109
}
108110

109111
}

Diff for: core-java-modules/core-java-compiler/src/main/java/com/baeldung/compilerApi/JavaCompilerUtils.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.baeldung.compilerApi;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
36
import javax.tools.*;
47
import java.io.*;
58
import java.net.URL;
@@ -19,6 +22,8 @@ public class JavaCompilerUtils {
1922
private final StandardJavaFileManager standardFileManager;
2023
private final Path outputDirectory;
2124

25+
private static final Logger logger = LoggerFactory.getLogger(JavaCompilerUtils.class);
26+
2227
/**
2328
* Constructs a new JavaCompilerUtil instance.
2429
*
@@ -60,7 +65,7 @@ public boolean compileFile(Path sourceFile) {
6065
standardFileManager.getJavaFileObjectsFromFiles(Collections.singletonList(sourceFile.toFile()));
6166
return compile(compilationUnits);
6267
} catch (Exception e) {
63-
e.printStackTrace();
68+
logger.error("Compilation failed: ", e);
6469
return false;
6570
}
6671
}
@@ -100,7 +105,7 @@ private boolean compile(Iterable<? extends JavaFileObject> compilationUnits) {
100105

101106
// Print compilation diagnostics
102107
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics.getDiagnostics()) {
103-
System.out.println(diagnostic.getMessage(null));
108+
logger.debug(diagnostic.getMessage(null));
104109
}
105110

106111
return success;

0 commit comments

Comments
 (0)