This is an automated email from the ASF dual-hosted git repository.
gyeongtae pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zeppelin.git
The following commit(s) were added to refs/heads/master by this push:
new f45587db39 [ZEPPELIN-6299] Refactor StaticRepl for readability,
correctness, and modern Java usage
f45587db39 is described below
commit f45587db39531b885343122f83ecb20f16bd7f0e
Author: eunhwa99 <[email protected]>
AuthorDate: Fri Sep 5 14:40:13 2025 +0900
[ZEPPELIN-6299] Refactor StaticRepl for readability, correctness, and
modern Java usage
### What is this PR for?
This PR refactors the `StaticRepl` class to improve readability,
maintainability, and align with modern Java best practices.
There are no functional changes.
- Generic type refinement with diamond operator in loop: Adds type safety
by explicitly specifying the generic type. Prevents unchecked warnings and
makes the code clearer to readers and tools.
- Replace `Arrays.asList` to `List.of` : List.of (Java 9+) produces an
immutable list, which is safer and better communicates the intent that the list
will not be modified. It avoids side effects of Arrays.asList, which is
fixed-size and backed by the original array.
- Minor typos fixed: Corrected spelling mistakes and adjusted comments.
- Removes redundant boolean comparison to follow standard Java coding
conventions.
- Replace index-based loop with enhanced for-each to eliminate boilerplate
index management, improves readability, and reduces chances of off-by-one
errors. It makes the intent ("iterate all elements") clearer.
- Variable declaration moved closer to usage to improve code locality and
readability by reducing variable scope. This makes the code easier to maintain
and follow.
### What type of PR is it?
Refactoring
### Todos
* [ ] - Task
### What is the Jira issue?
* [ZEPPELIN-6299](https://issues.apache.org/jira/browse/ZEPPELIN-6299)
### How should this be tested?
* Run existing unit tests to confirm behavior remains unchanged.
### Screenshots (if appropriate)
### Questions:
* Does the license files need to update? No
* Is there breaking changes for older versions? No
* Does this needs documentation? No
Closes #5048 from eunhwa99/ZEPPELIN-6299.
Signed-off-by: ParkGyeongTae <[email protected]>
---
.../java/org/apache/zeppelin/java/StaticRepl.java | 30 ++++++++++------------
1 file changed, 14 insertions(+), 16 deletions(-)
diff --git a/java/src/main/java/org/apache/zeppelin/java/StaticRepl.java
b/java/src/main/java/org/apache/zeppelin/java/StaticRepl.java
index 9506ed5d6d..410020f15e 100644
--- a/java/src/main/java/org/apache/zeppelin/java/StaticRepl.java
+++ b/java/src/main/java/org/apache/zeppelin/java/StaticRepl.java
@@ -19,6 +19,7 @@ package org.apache.zeppelin.java;
import com.thoughtworks.qdox.JavaProjectBuilder;
import com.thoughtworks.qdox.model.JavaClass;
+import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -38,21 +39,17 @@ import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
-import java.util.Arrays;
import java.util.List;
/**
- * StaticRepl for compling the java code in memory
+ * StaticRepl for compiling the java code in memory
*/
public class StaticRepl {
private static final Logger LOGGER =
LoggerFactory.getLogger(StaticRepl.class);
public static String execute(String generatedClassName, String code) throws
Exception {
- JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
- DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<JavaFileObject>();
-
- // Java parasing
+ // Java parsing
JavaProjectBuilder builder = new JavaProjectBuilder();
JavaSource src = builder.addSource(new StringReader(code));
@@ -61,24 +58,23 @@ public class StaticRepl {
String mainClassName = null;
// Searching for class containing Main method
- for (int i = 0; i < classes.size(); i++) {
+ for (JavaClass javaClass : classes) {
boolean hasMain = false;
- for (int j = 0; j < classes.get(i).getMethods().size(); j++) {
- if (classes.get(i).getMethods().get(j).getName().equals("main") &&
classes.get(i)
- .getMethods().get(j).isStatic()) {
- mainClassName = classes.get(i).getName();
+ for (JavaMethod method : javaClass.getMethods()) {
+ if (method.getName().equals("main") && method.isStatic()) {
+ mainClassName = javaClass.getName();
hasMain = true;
break;
}
}
- if (hasMain == true) {
+ if (hasMain) {
break;
}
}
- // if there isn't Main method, will retuen error
+ // if there isn't Main method, will return error
if (mainClassName == null) {
LOGGER.error("Exception for Main method", "There isn't any class "
+ "containing static main method.");
@@ -88,8 +84,8 @@ public class StaticRepl {
// replace name of class containing Main method with generated name
code = code.replace(mainClassName, generatedClassName);
- JavaFileObject file = new JavaSourceFromString(generatedClassName,
code.toString());
- Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(file);
+ JavaFileObject file = new JavaSourceFromString(generatedClassName, code);
+ Iterable<? extends JavaFileObject> compilationUnits = List.of(file);
ByteArrayOutputStream baosOut = new ByteArrayOutputStream();
ByteArrayOutputStream baosErr = new ByteArrayOutputStream();
@@ -104,6 +100,8 @@ public class StaticRepl {
System.setOut(newOut);
System.setErr(newErr);
+ JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+ DiagnosticCollector<JavaFileObject> diagnostics = new
DiagnosticCollector<>();
CompilationTask task = compiler.getTask(null, null, diagnostics, null,
null, compilationUnits);
// executing the compilation process
@@ -111,7 +109,7 @@ public class StaticRepl {
// if success is false will get error
if (!success) {
- for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
+ for (Diagnostic<? extends JavaFileObject> diagnostic :
diagnostics.getDiagnostics()) {
if (diagnostic.getLineNumber() == -1) {
continue;
}