This is an automated email from the ASF dual-hosted git repository.

bamaer pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git


The following commit(s) were added to refs/heads/main by this push:
     new 778d8ce275 SessionDisplay answers "no display" when SWT can't give 
one, instead of letting an Error escape, fixes #9570 (#9572)
778d8ce275 is described below

commit 778d8ce275a21d2893a07616ba69a4ede7558623
Author: Hans Van Akelyen <[email protected]>
AuthorDate: Wed Nov 18 20:16:43 2026 +0100

    SessionDisplay answers "no display" when SWT can't give one, instead of 
letting an Error escape, fixes #9570 (#9572)
---
 .../org/apache/hop/ui/hopgui/SessionDisplay.java   | 24 ++++++++++
 .../apache/hop/ui/hopgui/SessionDisplayTest.java   | 52 ++++++++++++++++++++++
 2 files changed, 76 insertions(+)

diff --git a/ui/src/main/java/org/apache/hop/ui/hopgui/SessionDisplay.java 
b/ui/src/main/java/org/apache/hop/ui/hopgui/SessionDisplay.java
index e2f0deb912..f253cd7c46 100644
--- a/ui/src/main/java/org/apache/hop/ui/hopgui/SessionDisplay.java
+++ b/ui/src/main/java/org/apache/hop/ui/hopgui/SessionDisplay.java
@@ -34,6 +34,12 @@ import org.eclipse.swt.widgets.Display;
  * following #7896 / #7897).
  *
  * <p>A thread whose session died is a thread with no display, which is what 
these methods answer.
+ *
+ * <p>The same goes for a JVM where SWT can't give a display at all: SWT isn't 
on the classpath or
+ * its native libraries don't load ({@code NoClassDefFoundError}, {@code 
UnsatisfiedLinkError}), or
+ * there is no display server ({@code SWTError: No more handles}). Those are 
errors, not runtime
+ * exceptions, and they mean "no display" too. Only the JVM's own errors 
({@link
+ * VirtualMachineError}, such as running out of memory) still propagate.
  */
 public class SessionDisplay {
 
@@ -52,6 +58,8 @@ public class SessionDisplay {
       return Display.getCurrent();
     } catch (RuntimeException e) {
       return null;
+    } catch (Error e) {
+      return noDisplay(e);
     }
   }
 
@@ -72,6 +80,22 @@ public class SessionDisplay {
       return Display.getDefault();
     } catch (RuntimeException e) {
       return null;
+    } catch (Error e) {
+      return noDisplay(e);
+    }
+  }
+
+  /**
+   * SWT can't give a display in this JVM. {@code SWTError} isn't named in a 
catch on purpose:
+   * matching it would need the SWT class to load, which is one of the cases 
handled here.
+   *
+   * @param error What asking for the display threw
+   * @return null, there is no display
+   */
+  private static Display noDisplay(Error error) {
+    if (error instanceof VirtualMachineError virtualMachineError) {
+      throw virtualMachineError;
     }
+    return null;
   }
 }
diff --git a/ui/src/test/java/org/apache/hop/ui/hopgui/SessionDisplayTest.java 
b/ui/src/test/java/org/apache/hop/ui/hopgui/SessionDisplayTest.java
index 79ef812863..84290a62cb 100644
--- a/ui/src/test/java/org/apache/hop/ui/hopgui/SessionDisplayTest.java
+++ b/ui/src/test/java/org/apache/hop/ui/hopgui/SessionDisplayTest.java
@@ -19,9 +19,12 @@ package org.apache.hop.ui.hopgui;
 
 import static org.junit.jupiter.api.Assertions.assertNull;
 import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.mockStatic;
 
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.SWTError;
 import org.eclipse.swt.widgets.Display;
 import org.junit.jupiter.api.DisplayName;
 import org.junit.jupiter.api.Test;
@@ -85,4 +88,53 @@ class SessionDisplayTest {
       assertSame(defaultDisplay, SessionDisplay.currentOrDefault());
     }
   }
+
+  @Test
+  @DisplayName("without SWT on the classpath there is no display, rather than 
an error")
+  void currentAnswersNullWithoutSwt() {
+    try (MockedStatic<Display> display = mockStatic(Display.class)) {
+      display
+          .when(Display::getCurrent)
+          .thenThrow(new 
NoClassDefFoundError("org/eclipse/swt/widgets/Display"));
+
+      assertNull(SessionDisplay.current());
+    }
+  }
+
+  @Test
+  @DisplayName("when the SWT native libraries don't load there is no display")
+  void currentAnswersNullWhenTheNativesDontLoad() {
+    try (MockedStatic<Display> display = mockStatic(Display.class)) {
+      display
+          .when(Display::getCurrent)
+          .thenThrow(new UnsatisfiedLinkError("no swt-gtk in 
java.library.path"));
+      display.when(Display::getDefault).thenThrow(new UnsatisfiedLinkError("no 
swt-gtk"));
+
+      assertNull(SessionDisplay.current());
+      assertNull(SessionDisplay.currentOrDefault());
+    }
+  }
+
+  @Test
+  @DisplayName("without a display server the default display isn't there 
either")
+  void currentOrDefaultAnswersNullWithoutADisplayServer() {
+    try (MockedStatic<Display> display = mockStatic(Display.class)) {
+      display.when(Display::getCurrent).thenReturn(null);
+      display
+          .when(Display::getDefault)
+          .thenThrow(new SWTError(SWT.ERROR_NO_HANDLES, "gtk_init_check() 
failed"));
+
+      assertNull(SessionDisplay.currentOrDefault());
+    }
+  }
+
+  @Test
+  @DisplayName("the JVM's own errors are not taken for a missing display")
+  void virtualMachineErrorsStillPropagate() {
+    try (MockedStatic<Display> display = mockStatic(Display.class)) {
+      display.when(Display::getCurrent).thenThrow(new OutOfMemoryError("Java 
heap space"));
+
+      assertThrows(OutOfMemoryError.class, SessionDisplay::current);
+    }
+  }
 }

Reply via email to