Edit control properties and verify controller event handlers in IntelliJ

This commit is contained in:
2026-11-25 08:23:40 +01:00
parent ceb32166ff
commit 22ff06ea75
14 changed files with 699 additions and 16 deletions
@@ -483,7 +483,8 @@ public final class BindingDiscovery {
.filter(e -> commandTarget(e.getValue()))
.map(Map.Entry::getKey)
.sorted()
.toList());
.toList(),
owner.actionEventHandlers());
}
private List<String> commandPaths() {
@@ -117,6 +117,50 @@ final class ControllerMetadata {
}
}
/** FXMLLoader gives an event-taking overload precedence over a parameterless handler. */
Optional<List<String>> actionEventHandlers() {
if (unknownController && controller == null) return Optional.empty();
var names = new TreeSet<String>();
var effectiveEvents = new HashMap<String, Boolean>();
for (Class<?> type = controller;
type != null
&& !type.getPackageName().startsWith("java.")
&& !type.getPackageName().startsWith("javafx.");
type = type.getSuperclass()) {
var declaredEvents = new HashMap<String, Boolean>();
for (var method : type.getDeclaredMethods()) {
int modifiers = method.getModifiers();
boolean publicAccess =
Modifier.isPublic(modifiers) && Modifier.isPublic(type.getModifiers());
if (Modifier.isStatic(modifiers)
|| Modifier.isNative(modifiers)
|| method.isSynthetic()
|| method.isBridge()
|| (!publicAccess && !method.isAnnotationPresent(javafx.fxml.FXML.class))
|| method.getName().equals("initialize")) continue;
Module module = type.getModule(), fxml = javafx.fxml.FXMLLoader.class.getModule();
if (!module.isOpen(type.getPackageName(), fxml)
&& !(publicAccess && module.isExported(type.getPackageName(), fxml))) continue;
var parameters = method.getParameterTypes();
if (parameters.length == 0) names.add("#" + method.getName());
if (parameters.length == 1 && javafx.event.Event.class.isAssignableFrom(parameters[0])) {
declaredEvents.merge(
"#" + method.getName(),
parameters[0].isAssignableFrom(javafx.event.ActionEvent.class),
Boolean::logicalAnd);
}
}
// FXMLLoader indexes EVENT methods by name, with the subclass taking precedence.
declaredEvents.forEach(effectiveEvents::putIfAbsent);
}
effectiveEvents.forEach(
(name, acceptsAction) -> {
if (acceptsAction) names.add(name);
else names.remove(name);
});
return Optional.of(List.copyOf(names));
}
List<Computed> methods(Class<?> target) {
if (controller == null) return List.of();
var result = new TreeMap<String, Computed>();
@@ -0,0 +1,61 @@
package de.neitzel.fxdesigner.target;
import static org.junit.jupiter.api.Assertions.*;
import de.neitzel.fxdesigner.protocol.BindingMetadata;
import java.util.List;
import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.fxml.FXML;
import javafx.scene.input.KeyEvent;
import org.junit.jupiter.api.Test;
class ActionEventHandlersTest {
public static class Parent {
@FXML
private void inherited(Event event) {}
public void replaced(KeyEvent event) {}
private void hidden() {}
}
public static class Controller extends Parent {
@FXML
private void submit(ActionEvent event) {}
public void replaced(ActionEvent event) {}
public void clear() {}
public void mixed() {}
public void mixed(KeyEvent event) {}
public void keys(KeyEvent event) {}
public static void helper() {}
public void initialize() {}
Controller() {
throw new AssertionError("Discovery constructed a controller");
}
}
@Test
void reportsOnlyAccessibleSignaturesCallableForActionEvents() throws Exception {
var xml =
"<?import javafx.scene.control.TextField?><TextField xmlns:fx=\"http://javafx.com/fxml/1\""
+ " fx:controller=\""
+ Controller.class.getName()
+ "\"/>";
var result = BindingDiscovery.inspect(xml, "", "view", getClass().getClassLoader());
var wire = result.encode();
assertEquals(
List.of("#clear", "#inherited", "#replaced", "#submit"),
BindingMetadata.decode(wire).actionEventHandlers().orElseThrow());
wire.remove("actionEvents");
assertTrue(BindingMetadata.decode(wire).actionEventHandlers().isEmpty());
}
}