Skip to content

Commit e084d39

Browse files
authored
fix: Make findAncestor return Object for backward compatibility (#14242)
Related-to vaadin/classic-components-internal#164
1 parent 0afd496 commit e084d39

2 files changed

Lines changed: 19 additions & 22 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/component/Component.java‎

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -702,28 +702,27 @@ public void scrollIntoView() {
702702
}
703703

704704
/**
705-
* Traverses the component tree up and returns the first component that
706-
* matches the given type.
705+
* Traverses the component tree up and returns the first ancestor component
706+
* that matches the given type.
707707
*
708708
* @param componentType
709-
* the class of the component to search for
710-
* @return the component as Optional or empty Optional if a parent of given
711-
* type is not found
709+
* the class of the ancestor component to search for
710+
* @return The first ancestor that can be assigned to the given class. Null
711+
* if no ancestor with the correct type could be found.
712712
* @param <T>
713-
* the type of the component to return
714-
*/
715-
@SuppressWarnings("unchecked")
716-
public <T> Optional<T> findAncestor(Class<T> componentType) {
717-
Optional<Component> parent = getParent();
718-
while (parent.isPresent()) {
719-
Component component = parent.get();
720-
if (componentType.isAssignableFrom(component.getClass())) {
721-
return Optional.of((T) component);
713+
* the type of the ancestor component to return
714+
*/
715+
public <T> T findAncestor(Class<T> componentType) {
716+
Optional<Component> optionalParent = getParent();
717+
while (optionalParent.isPresent()) {
718+
Component parent = optionalParent.get();
719+
if (componentType.isAssignableFrom(parent.getClass())) {
720+
return componentType.cast(parent);
722721
} else {
723-
parent = component.getParent();
722+
optionalParent = parent.getParent();
724723
}
725724
}
726-
return Optional.empty();
725+
return null;
727726
}
728727

729728
}

‎flow-server/src/test/java/com/vaadin/flow/component/ComponentTest.java‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,12 +1672,10 @@ public void findAncestorTest() {
16721672
ui.add(componentContainer);
16731673

16741674
Assert.assertEquals(componentContainer,
1675-
component.findAncestor(TestComponentContainer.class).get());
1676-
Assert.assertEquals(ui, component.findAncestor(UI.class).get());
1677-
Assert.assertEquals(ui,
1678-
component.findAncestor(PollNotifier.class).get());
1679-
Assert.assertFalse(
1680-
component.findAncestor(TestButton.class).isPresent());
1675+
component.findAncestor(TestComponentContainer.class));
1676+
Assert.assertEquals(ui, component.findAncestor(UI.class));
1677+
Assert.assertEquals(ui, component.findAncestor(PollNotifier.class));
1678+
Assert.assertNull(component.findAncestor(TestButton.class));
16811679

16821680
}
16831681

0 commit comments

Comments
 (0)