Resolve generic JavaFX binding targets and verify them in IntelliJ
This commit is contained in:
+40
-55
@@ -31,27 +31,23 @@ public final class BindingDiscovery {
|
||||
|
||||
private record TargetType(Class<?> type, boolean writable, Class<?> listElement) {}
|
||||
|
||||
// Invariant element signatures for explicit JavaFX list properties. Keep raw, wildcard
|
||||
// and unresolvable generic declarations in the existing unverified collection path.
|
||||
private static Class<?> listElement(Method method) {
|
||||
if (!(method.getGenericReturnType() instanceof ParameterizedType p)
|
||||
|| p.getActualTypeArguments().length != 1) return null;
|
||||
Type value = p.getActualTypeArguments()[0];
|
||||
if (p.getRawType() == javafx.beans.property.ListProperty.class
|
||||
|| p.getRawType() == javafx.beans.property.ReadOnlyListProperty.class)
|
||||
return value instanceof Class<?> element ? element : null;
|
||||
if ((p.getRawType() == javafx.beans.property.ObjectProperty.class
|
||||
|| p.getRawType() == javafx.beans.property.ReadOnlyObjectProperty.class)
|
||||
&& value instanceof ParameterizedType list
|
||||
&& list.getRawType() == javafx.collections.ObservableList.class
|
||||
&& list.getActualTypeArguments()[0] instanceof Class<?> element) return element;
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean knownListPair(String path, TargetType target, String direction) {
|
||||
return direction.equals("view")
|
||||
&& listElements.containsKey(path)
|
||||
&& target.listElement() != null;
|
||||
&& target.listElement() != null
|
||||
&& target.type() != null
|
||||
&& target.type().isAssignableFrom(paths.get(path));
|
||||
}
|
||||
|
||||
private static boolean disjointCollectionTypes(Class<?> source, Class<?> target) {
|
||||
if (source == null || target == null || source == Object.class || target == Object.class)
|
||||
return false;
|
||||
return (Collection.class.isAssignableFrom(source)
|
||||
|| Map.class.isAssignableFrom(source)
|
||||
|| Collection.class.isAssignableFrom(target)
|
||||
|| Map.class.isAssignableFrom(target))
|
||||
&& !source.isAssignableFrom(target)
|
||||
&& !target.isAssignableFrom(source);
|
||||
}
|
||||
|
||||
private final Set<String> actionHandlers = new HashSet<>();
|
||||
@@ -352,6 +348,7 @@ public final class BindingDiscovery {
|
||||
if (b.length > 512 * 1024) throw new IOException("Component FXML exceeds limit");
|
||||
componentModel = model(parse(new String(b, java.nio.charset.StandardCharsets.UTF_8)));
|
||||
}
|
||||
var fieldTypes = GenericPropertyType.controlBindings(owner.controller, id, control);
|
||||
for (Method m : control.getMethods()) {
|
||||
if (Modifier.isStatic(m.getModifiers())
|
||||
|| m.getParameterCount() != 0
|
||||
@@ -360,10 +357,12 @@ public final class BindingDiscovery {
|
||||
String property = m.getName().substring(0, m.getName().length() - 8);
|
||||
if (property.isEmpty()) continue;
|
||||
Class<?> getter = valueType(control, property);
|
||||
Class<?> value = propertyType(m.getGenericReturnType(), Map.of());
|
||||
var signature = GenericPropertyType.of(control, m, fieldTypes);
|
||||
Class<?> value = signature.value();
|
||||
if (value == Number.class && getter != null && Number.class.isAssignableFrom(box(getter)))
|
||||
value = getter;
|
||||
else if (value != null && getter != null && box(value) != box(getter)) value = null;
|
||||
else if (value != null && getter != null && !box(getter).isAssignableFrom(box(value)))
|
||||
value = null;
|
||||
boolean constrained = false;
|
||||
if (property.equals("model") && !componentModel.isEmpty()) {
|
||||
value = load(componentModel);
|
||||
@@ -371,7 +370,9 @@ public final class BindingDiscovery {
|
||||
}
|
||||
var target =
|
||||
new TargetType(
|
||||
value, Property.class.isAssignableFrom(m.getReturnType()), listElement(m));
|
||||
value,
|
||||
Property.class.isAssignableFrom(m.getReturnType()),
|
||||
signature.listElement());
|
||||
targetTypes.put(id + "." + property, target);
|
||||
String status, reason;
|
||||
if (!Property.class.isAssignableFrom(m.getReturnType())) {
|
||||
@@ -386,7 +387,8 @@ public final class BindingDiscovery {
|
||||
|| value == Object.class) {
|
||||
status = "UNRESOLVED";
|
||||
reason =
|
||||
"Declared types are insufficient; rebuild with specific signatures/Model metadata.";
|
||||
"Declared types are insufficient. Declare the fx:id field with its concrete generic"
|
||||
+ " type in the FXML controller and rebuild.";
|
||||
} else if (knownListPair(selected, target, direction)) {
|
||||
boolean matches = listElements.get(selected) == target.listElement();
|
||||
status = matches ? "COMPATIBLE" : "INCOMPATIBLE";
|
||||
@@ -394,14 +396,17 @@ public final class BindingDiscovery {
|
||||
matches
|
||||
? "Matching observable list element type: " + target.listElement().getName()
|
||||
: "Observable list element types differ.";
|
||||
} else if (disjointCollectionTypes(source, value)) {
|
||||
status = "INCOMPATIBLE";
|
||||
reason = "Collection and scalar types cannot be directly bound.";
|
||||
} else if (Collection.class.isAssignableFrom(source)
|
||||
|| Collection.class.isAssignableFrom(value)
|
||||
|| Map.class.isAssignableFrom(source)
|
||||
|| Map.class.isAssignableFrom(value)) {
|
||||
status = "UNRESOLVED";
|
||||
reason =
|
||||
"Collection variance requires further metadata; use Repeat/Table for Model"
|
||||
+ " collections.";
|
||||
"Unresolved collection element types. Declare concrete element types on the model"
|
||||
+ " and the fx:id controller field.";
|
||||
} else {
|
||||
Class<?> a = box(source), b = box(value);
|
||||
boolean view = b.isAssignableFrom(a), back = a.isAssignableFrom(b);
|
||||
@@ -551,13 +556,15 @@ public final class BindingDiscovery {
|
||||
|| m.isSynthetic()) continue;
|
||||
String name = m.getName().substring(0, m.getName().length() - 8);
|
||||
if (name.isEmpty() || paths.containsKey(name)) continue;
|
||||
Class<?> element = listElement(m);
|
||||
var signature = GenericPropertyType.of(vm, m, Map.of());
|
||||
Class<?> element = signature.listElement();
|
||||
if (element != null) listElements.put(name, element);
|
||||
Class<?> value = propertyType(m.getGenericReturnType(), Map.of());
|
||||
Class<?> value = signature.value();
|
||||
Class<?> getter = valueType(vm, name);
|
||||
if (value == Number.class && getter != null && Number.class.isAssignableFrom(box(getter)))
|
||||
value = getter;
|
||||
else if (value != null && getter != null && box(value) != box(getter)) value = null;
|
||||
else if (value != null && getter != null && !box(getter).isAssignableFrom(box(value)))
|
||||
value = null;
|
||||
boolean readOnly = !Property.class.isAssignableFrom(m.getReturnType());
|
||||
if (readOnly) readOnlySources.add(name);
|
||||
paths.put(name, value == null ? Object.class : value);
|
||||
@@ -743,6 +750,9 @@ public final class BindingDiscovery {
|
||||
severity = "ERROR";
|
||||
message = "Observable list element types differ.";
|
||||
}
|
||||
} else if (disjointCollectionTypes(source, target.type())) {
|
||||
severity = "ERROR";
|
||||
message = "Collection and scalar types cannot be directly bound.";
|
||||
} else if (target.type() == null
|
||||
|| source == Object.class
|
||||
|| target.type() == Object.class
|
||||
@@ -751,7 +761,9 @@ public final class BindingDiscovery {
|
||||
|| Map.class.isAssignableFrom(source)
|
||||
|| Map.class.isAssignableFrom(target.type())) {
|
||||
severity = "WARNING";
|
||||
message = "Value types require additional metadata";
|
||||
message =
|
||||
"Value types require additional metadata. Declare concrete types on the model and the"
|
||||
+ " fx:id controller field.";
|
||||
} else {
|
||||
Class<?> a = box(source), b = box(target.type());
|
||||
if ((!direction.equals("model") && !b.isAssignableFrom(a))
|
||||
@@ -808,33 +820,6 @@ public final class BindingDiscovery {
|
||||
return candidates.iterator().next();
|
||||
}
|
||||
|
||||
static Class<?> propertyType(Type type, Map<TypeVariable<?>, Type> inherited) {
|
||||
Class<?> raw;
|
||||
var bindings = new HashMap<>(inherited);
|
||||
if (type instanceof ParameterizedType p && p.getRawType() instanceof Class<?> c) {
|
||||
raw = c;
|
||||
for (int i = 0; i < c.getTypeParameters().length; i++) {
|
||||
Type argument = p.getActualTypeArguments()[i];
|
||||
if (argument instanceof TypeVariable<?> v) argument = bindings.getOrDefault(v, v);
|
||||
bindings.put(c.getTypeParameters()[i], argument);
|
||||
}
|
||||
} else if (type instanceof Class<?> c) raw = c;
|
||||
else return null;
|
||||
if (raw == ObservableValue.class) {
|
||||
Type value = bindings.get(raw.getTypeParameters()[0]);
|
||||
return value instanceof Class<?> c
|
||||
? c
|
||||
: value instanceof ParameterizedType p && p.getRawType() instanceof Class<?> c ? c : null;
|
||||
}
|
||||
for (Type parent : raw.getGenericInterfaces()) {
|
||||
Class<?> value = propertyType(parent, bindings);
|
||||
if (value != null) return value;
|
||||
}
|
||||
return raw.getGenericSuperclass() == null
|
||||
? null
|
||||
: propertyType(raw.getGenericSuperclass(), bindings);
|
||||
}
|
||||
|
||||
private static Class<?> valueType(Class<?> control, String name) {
|
||||
String suffix = Character.toUpperCase(name.charAt(0)) + name.substring(1);
|
||||
for (String prefix : List.of("get", "is"))
|
||||
|
||||
+84
@@ -0,0 +1,84 @@
|
||||
package de.neitzel.fxdesigner.target;
|
||||
|
||||
import java.lang.reflect.*;
|
||||
import java.util.*;
|
||||
import javafx.beans.value.ObservableValue;
|
||||
import javafx.collections.ObservableList;
|
||||
import javafx.fxml.FXML;
|
||||
|
||||
/** Resolves JavaFX accessor signatures without constructing a controller or control. */
|
||||
record GenericPropertyType(Class<?> value, Class<?> listElement) {
|
||||
static Map<TypeVariable<?>, Type> controlBindings(
|
||||
Class<?> controller, String id, Class<?> control) {
|
||||
for (Class<?> declaring = controller;
|
||||
declaring != null;
|
||||
declaring = declaring.getSuperclass()) {
|
||||
try {
|
||||
Field field = declaring.getDeclaredField(id);
|
||||
int modifiers = field.getModifiers();
|
||||
if (Modifier.isStatic(modifiers)
|
||||
|| Modifier.isFinal(modifiers)
|
||||
|| !(Modifier.isPublic(modifiers) || field.isAnnotationPresent(FXML.class))
|
||||
|| !field.getType().isAssignableFrom(control)) continue;
|
||||
var inherited = bindings(controller, declaring, Map.of());
|
||||
return bindArguments(field.getGenericType(), inherited);
|
||||
} catch (NoSuchFieldException ignored) {
|
||||
// FXMLLoader also looks for inherited controller fields.
|
||||
}
|
||||
}
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
static GenericPropertyType of(
|
||||
Class<?> receiver, Method accessor, Map<TypeVariable<?>, Type> fields) {
|
||||
var scope = bindings(receiver, accessor.getDeclaringClass(), fields);
|
||||
var valueScope = bindings(accessor.getGenericReturnType(), ObservableValue.class, scope);
|
||||
Type type = resolve(ObservableValue.class.getTypeParameters()[0], valueScope);
|
||||
Class<?> raw = raw(type);
|
||||
Class<?> element = null;
|
||||
if (raw != null && ObservableList.class.isAssignableFrom(raw)) {
|
||||
var listScope = bindings(type, ObservableList.class, valueScope);
|
||||
var item = resolve(ObservableList.class.getTypeParameters()[0], listScope);
|
||||
if (item instanceof Class<?> exact) element = exact;
|
||||
}
|
||||
return new GenericPropertyType(raw, element);
|
||||
}
|
||||
|
||||
private static Class<?> raw(Type type) {
|
||||
if (type instanceof ParameterizedType p) return (Class<?>) p.getRawType();
|
||||
return type instanceof Class<?> c ? c : null;
|
||||
}
|
||||
|
||||
private static Type resolve(Type type, Map<TypeVariable<?>, Type> scope) {
|
||||
while (type instanceof TypeVariable<?> v && scope.containsKey(v)) type = scope.get(v);
|
||||
return type;
|
||||
}
|
||||
|
||||
private static Map<TypeVariable<?>, Type> bindArguments(
|
||||
Type type, Map<TypeVariable<?>, Type> scope) {
|
||||
if (!(type instanceof ParameterizedType p)) return scope;
|
||||
var result = new HashMap<>(scope);
|
||||
var vars = raw(type).getTypeParameters();
|
||||
var args = p.getActualTypeArguments();
|
||||
for (int i = 0; i < vars.length; i++) {
|
||||
Type argument = resolve(args[i], scope);
|
||||
// A field can type an ancestor (ComboBoxBase<String>) while the FXML declares
|
||||
// a raw subclass (ComboBox). An unbound subclass variable adds no information.
|
||||
if (argument instanceof TypeVariable<?> && scope.containsKey(vars[i])) continue;
|
||||
if (!vars[i].equals(argument)) result.put(vars[i], argument);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Map<TypeVariable<?>, Type> bindings(
|
||||
Type type, Class<?> target, Map<TypeVariable<?>, Type> scope) {
|
||||
var raw = raw(type);
|
||||
if (raw == null || !target.isAssignableFrom(raw)) return scope;
|
||||
var local = bindArguments(type, scope);
|
||||
if (raw == target) return local;
|
||||
for (Type parent : raw.getGenericInterfaces()) {
|
||||
if (target.isAssignableFrom(raw(parent))) return bindings(parent, target, local);
|
||||
}
|
||||
return bindings(raw.getGenericSuperclass(), target, local);
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -222,7 +222,7 @@ final class RepeatDiscovery {
|
||||
if (!Property.class.isAssignableFrom(accessor.getReturnType())
|
||||
|| !ControllerMetadata.accessible(accessor.getDeclaringClass(), true))
|
||||
throw new IllegalArgumentException("Component needs an accessible writable modelProperty");
|
||||
Class<?> property = BindingDiscovery.propertyType(accessor.getGenericReturnType(), Map.of());
|
||||
Class<?> property = GenericPropertyType.of(type, accessor, Map.of()).value();
|
||||
Document doc = resource(type);
|
||||
Element root = doc.getDocumentElement();
|
||||
if (!"http://javafx.com/fxml/1".equals(root.getNamespaceURI())
|
||||
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
package de.neitzel.fxdesigner.target;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.*;
|
||||
import javafx.fxml.FXML;
|
||||
import javafx.scene.control.ComboBox;
|
||||
import javafx.scene.control.ComboBoxBase;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class GenericPropertyTypeTest {
|
||||
static class Base<T> {
|
||||
@FXML private ComboBox<T> inherited;
|
||||
@FXML private ComboBoxBase<Long> baseControl;
|
||||
@FXML private ComboBox<? extends Number> bounded;
|
||||
@FXML private ComboBox<List<String>> nested;
|
||||
private ComboBox<String> uninjected;
|
||||
@FXML private List<String> wrongControl;
|
||||
}
|
||||
|
||||
static class Controller extends Base<Integer> {}
|
||||
|
||||
private GenericPropertyType signature(String id, String property) throws Exception {
|
||||
var field = GenericPropertyType.controlBindings(Controller.class, id, ComboBox.class);
|
||||
return GenericPropertyType.of(
|
||||
ComboBox.class, ComboBox.class.getMethod(property + "Property"), field);
|
||||
}
|
||||
|
||||
@Test
|
||||
void resolvesInheritedControllerAndControlArgumentsWithoutStartingJavaFx() throws Exception {
|
||||
assertEquals(Integer.class, signature("inherited", "value").value());
|
||||
assertEquals(Integer.class, signature("inherited", "items").listElement());
|
||||
assertEquals(Long.class, signature("baseControl", "value").value());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unknownOrIncompatibleFieldsDoNotCertifyListElementTypes() throws Exception {
|
||||
for (String id : List.of("bounded", "nested", "uninjected", "missing", "wrongControl"))
|
||||
assertNull(signature(id, "items").listElement(), id);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user