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 be68619a7e Issue #8986 : Fix the connection toolbar on a Neo4j 
execution location (#8987)
be68619a7e is described below

commit be68619a7eb2c65e34fc28ec0de8a28da158f6e7
Author: Matt Casters <[email protected]>
AuthorDate: Sat Oct 17 16:26:15 2026 +0200

    Issue #8986 : Fix the connection toolbar on a Neo4j execution location 
(#8987)
    
    A graph connection line is a MetaSelectionLine subclass. The toolbar
    looked the plugin up as MetaSelectionLine, missed the subclass, and
    tried a constructor that class does not have. The edit icon never
    loaded, and clicks could not see methods declared on the base class.
---
 .../MetaSelectionLineSubclassToolbarTest.java      | 173 +++++++++++++++++++++
 .../org/apache/hop/ui/core/gui/BaseGuiWidgets.java |  33 +++-
 .../hop/ui/core/widget/MetaSelectionLine.java      |   6 +-
 .../core/gui/BaseGuiWidgetsStaticListenerTest.java |  42 +++++
 4 files changed, 247 insertions(+), 7 deletions(-)

diff --git 
a/rcp/src/test/java/org/apache/hop/ui/core/widget/MetaSelectionLineSubclassToolbarTest.java
 
b/rcp/src/test/java/org/apache/hop/ui/core/widget/MetaSelectionLineSubclassToolbarTest.java
new file mode 100644
index 0000000000..a3f54717c2
--- /dev/null
+++ 
b/rcp/src/test/java/org/apache/hop/ui/core/widget/MetaSelectionLineSubclassToolbarTest.java
@@ -0,0 +1,173 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hop.ui.core.widget;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assumptions.assumeFalse;
+
+import java.awt.GraphicsEnvironment;
+import java.lang.reflect.Method;
+import org.apache.hop.core.HopEnvironment;
+import org.apache.hop.core.gui.plugin.GuiRegistry;
+import org.apache.hop.core.gui.plugin.toolbar.GuiToolbarElement;
+import org.apache.hop.core.variables.Variables;
+import org.apache.hop.execution.ExecutionInfoLocation;
+import org.apache.hop.metadata.serializer.memory.MemoryMetadataProvider;
+import org.apache.hop.ui.core.PropsUi;
+import org.apache.hop.ui.core.gui.GuiResource;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Event;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.swt.widgets.ToolBar;
+import org.eclipse.swt.widgets.ToolItem;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+/**
+ * A graph connection line is a {@link MetaSelectionLine} subclass. The edit 
icon and the toolbar
+ * buttons are declared on the base class, so they have to resolve the 
subclass instance (issue
+ * #8986).
+ *
+ * <p>Lives in hop-ui-rcp because constructing the widget needs the desktop 
look-and-feel
+ * implementations.
+ */
+@Tag("uitest")
+class MetaSelectionLineSubclassToolbarTest {
+
+  private static final String EDIT_TOOLTIP = "Edit this metadata element";
+
+  private Display display;
+  private Shell shell;
+
+  @BeforeAll
+  static void registerToolbar() {
+    GuiRegistry registry = GuiRegistry.getInstance();
+    for (Method method : MetaSelectionLine.class.getDeclaredMethods()) {
+      GuiToolbarElement element = 
method.getAnnotation(GuiToolbarElement.class);
+      if (element != null && registry.findGuiToolbarItem(element.root(), 
element.id()) == null) {
+        registry.addGuiToolbarElement(
+            MetaSelectionLine.class.getName(),
+            element,
+            method,
+            MetaSelectionLine.class.getClassLoader());
+      }
+    }
+  }
+
+  @BeforeEach
+  void openShell() throws Exception {
+    assumeFalse(GraphicsEnvironment.isHeadless(), "No display available; 
skipping SWT test.");
+    HopEnvironment.init();
+    display = Display.getDefault();
+    PropsUi.getInstance();
+    GuiResource.getInstance();
+    shell = new Shell(display, SWT.NONE);
+  }
+
+  @AfterEach
+  void disposeShell() {
+    if (shell != null && !shell.isDisposed()) {
+      shell.dispose();
+    }
+  }
+
+  @Test
+  void subclassShowsTheEditIconAndEditsItself() {
+    MemoryMetadataProvider provider = new MemoryMetadataProvider();
+    ConnectionSelectionLine line =
+        new ConnectionSelectionLine(
+            shell, provider, "Graph database connection", "Connection 
tooltip");
+    line.setText("neo4j");
+
+    ToolItem edit = itemWithTooltip(line, EDIT_TOOLTIP);
+    assertNotNull(edit.getImage(), "edit button image");
+    edit.notifyListeners(SWT.Selection, new Event());
+    assertTrue(line.edited, "Edit calls the connection line, not a new 
MetaSelectionLine");
+  }
+
+  @Test
+  void selectionLineStillShowsTheEditIcon() {
+    MetaSelectionLine<ExecutionInfoLocation> line =
+        new MetaSelectionLine<>(
+            Variables.getADefaultVariableSpace(),
+            new MemoryMetadataProvider(),
+            ExecutionInfoLocation.class,
+            shell,
+            SWT.NONE,
+            "Location",
+            "Location tooltip");
+
+    ToolItem edit = itemWithTooltip(line, EDIT_TOOLTIP);
+    assertNotNull(edit.getImage(), "edit button image");
+  }
+
+  private static ToolItem itemWithTooltip(MetaSelectionLine<?> line, String 
tooltip) {
+    ToolBar toolBar = null;
+    for (Control child : line.getChildren()) {
+      if (child instanceof ToolBar bar) {
+        toolBar = bar;
+        break;
+      }
+    }
+    assertNotNull(toolBar, "metadata selection line toolbar");
+    StringBuilder seen = new StringBuilder();
+    for (ToolItem item : toolBar.getItems()) {
+      String tip = item.getToolTipText();
+      if (tip != null) {
+        seen.append(tip).append("; ");
+        if (tip.startsWith(tooltip)) {
+          return item;
+        }
+      }
+    }
+    fail("No toolbar item with tooltip '" + tooltip + "'. Saw: " + seen);
+    return null;
+  }
+
+  /** Stands in for {@code NeoConnectionSelectionLine} without the Neo4j 
plugin. */
+  private static final class ConnectionSelectionLine
+      extends MetaSelectionLine<ExecutionInfoLocation> {
+
+    private boolean edited;
+
+    private ConnectionSelectionLine(
+        Shell shell, MemoryMetadataProvider provider, String label, String 
tooltip) {
+      super(
+          Variables.getADefaultVariableSpace(),
+          provider,
+          ExecutionInfoLocation.class,
+          shell,
+          SWT.NONE,
+          label,
+          tooltip);
+    }
+
+    @Override
+    protected boolean editMetadata() {
+      edited = true;
+      return true;
+    }
+  }
+}
diff --git a/ui/src/main/java/org/apache/hop/ui/core/gui/BaseGuiWidgets.java 
b/ui/src/main/java/org/apache/hop/ui/core/gui/BaseGuiWidgets.java
index a6e8ab4018..199548cd47 100644
--- a/ui/src/main/java/org/apache/hop/ui/core/gui/BaseGuiWidgets.java
+++ b/ui/src/main/java/org/apache/hop/ui/core/gui/BaseGuiWidgets.java
@@ -271,15 +271,19 @@ public class BaseGuiWidgets {
         Object guiPluginInstance =
             findGuiPluginInstance(getHopGuiId(), classLoader, 
listenerClassName, instanceId);
         // Prefer a method that accepts the SWT Event (e.g. to read modifier 
keys such as Shift).
-        try {
-          Method withEvent =
-              
guiPluginInstance.getClass().getDeclaredMethod(listenerMethodName, Event.class);
+        // The method may be declared on a superclass: a Neo4j connection line 
is a
+        // MetaSelectionLine, and getDeclaredMethod does not walk that 
hierarchy.
+        Method withEvent =
+            findDeclaredMethod(guiPluginInstance.getClass(), 
listenerMethodName, Event.class);
+        if (withEvent != null) {
           withEvent.invoke(guiPluginInstance, e);
           return;
-        } catch (NoSuchMethodException noEventMethod) {
-          // Fall back to the standard no-argument method.
         }
-        Method listenerMethod = 
guiPluginInstance.getClass().getDeclaredMethod(listenerMethodName);
+        Method listenerMethod =
+            findDeclaredMethod(guiPluginInstance.getClass(), 
listenerMethodName);
+        if (listenerMethod == null) {
+          throw new NoSuchMethodException(listenerMethodName);
+        }
         listenerMethod.invoke(guiPluginInstance);
 
       } catch (Exception exception) {
@@ -295,6 +299,23 @@ public class BaseGuiWidgets {
     };
   }
 
+  /**
+   * The first method with this name and parameter types, starting at {@code 
type} and walking
+   * superclasses. {@code Class.getDeclaredMethod} only looks at one class, so 
a toolbar listener
+   * declared on a base widget is invisible when the registered object is a 
subclass. Returns null
+   * when neither this class nor a superclass declares it.
+   */
+  static Method findDeclaredMethod(Class<?> type, String name, Class<?>... 
parameterTypes) {
+    for (Class<?> current = type; current != null; current = 
current.getSuperclass()) {
+      try {
+        return current.getDeclaredMethod(name, parameterTypes);
+      } catch (NoSuchMethodException e) {
+        // Declared further up, or not at all.
+      }
+    }
+    return null;
+  }
+
   /**
    * Find a public static method with a single parameter assignable from 
{@code argumentType}.
    * Prefers the most specific matching parameter type when more than one 
candidate exists.
diff --git 
a/ui/src/main/java/org/apache/hop/ui/core/widget/MetaSelectionLine.java 
b/ui/src/main/java/org/apache/hop/ui/core/widget/MetaSelectionLine.java
index 73aa413e04..0e2504ca6a 100644
--- a/ui/src/main/java/org/apache/hop/ui/core/widget/MetaSelectionLine.java
+++ b/ui/src/main/java/org/apache/hop/ui/core/widget/MetaSelectionLine.java
@@ -238,7 +238,11 @@ public class MetaSelectionLine<T extends IHopMetadata> 
extends Composite {
     // Add more toolbar items from plugins.
     //
     GuiToolbarWidgets toolbarWidgets = new GuiToolbarWidgets();
-    toolbarWidgets.registerGuiPluginObject(this);
+    // Register under MetaSelectionLine so the edit icon and the toolbar 
listeners resolve this
+    // instance for every subclass (NeoConnectionSelectionLine, 
CypherConnectionSelectionLine, …).
+    // The toolbar elements are declared on this class, and a lookup by the 
runtime class name
+    // misses them and then tries to construct a MetaSelectionLine with no 
arguments.
+    toolbarWidgets.registerGuiPluginObject(MetaSelectionLine.class.getName(), 
this);
     toolbarWidgets.createToolbarWidgets(toolBarContainer, 
GUI_PLUGIN_TOOLBAR_PARENT_ID);
     if (!isCreatingNewElements()) {
       toolbarWidgets.enableToolbarItem(TOOLBAR_ITEM_NEW, false);
diff --git 
a/ui/src/test/java/org/apache/hop/ui/core/gui/BaseGuiWidgetsStaticListenerTest.java
 
b/ui/src/test/java/org/apache/hop/ui/core/gui/BaseGuiWidgetsStaticListenerTest.java
index 5c4c55c57c..9ffd12b1dd 100644
--- 
a/ui/src/test/java/org/apache/hop/ui/core/gui/BaseGuiWidgetsStaticListenerTest.java
+++ 
b/ui/src/test/java/org/apache/hop/ui/core/gui/BaseGuiWidgetsStaticListenerTest.java
@@ -88,4 +88,46 @@ class BaseGuiWidgetsStaticListenerTest {
             ListenerHost.class, "ignored", ConcreteEditor.class);
     assertNull(method);
   }
+
+  /** Toolbar methods live on the base widget. A subclass still has to find 
them. */
+  public static class SelectionLine {
+    public void edit() {}
+
+    public void edit(String event) {}
+  }
+
+  public static class ConnectionSelectionLine extends SelectionLine {}
+
+  public static class OverridingSelectionLine extends SelectionLine {
+    @Override
+    public void edit() {}
+  }
+
+  @Test
+  void findsListenerMethodDeclaredOnASuperclass() throws Exception {
+    Method method = 
BaseGuiWidgets.findDeclaredMethod(ConnectionSelectionLine.class, "edit");
+    assertNotNull(method);
+    assertEquals(SelectionLine.class, method.getDeclaringClass());
+    assertEquals(0, method.getParameterCount());
+  }
+
+  @Test
+  void prefersTheListenerMethodDeclaredOnTheInstanceClass() {
+    Method method = 
BaseGuiWidgets.findDeclaredMethod(OverridingSelectionLine.class, "edit");
+    assertNotNull(method);
+    assertEquals(OverridingSelectionLine.class, method.getDeclaringClass());
+  }
+
+  @Test
+  void findsTheEventListenerBeforeSearchingFurther() {
+    Method method =
+        BaseGuiWidgets.findDeclaredMethod(ConnectionSelectionLine.class, 
"edit", String.class);
+    assertNotNull(method);
+    assertEquals(String.class, method.getParameterTypes()[0]);
+  }
+
+  @Test
+  void returnsNullWhenNoListenerMethodExists() {
+    
assertNull(BaseGuiWidgets.findDeclaredMethod(ConnectionSelectionLine.class, 
"missing"));
+  }
 }

Reply via email to