Skip to content

Commit

Permalink
api: Deprecate Component#detectCycle
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Mar 8, 2021
1 parent 64ac1ba commit 28042b8
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 45 deletions.
10 changes: 3 additions & 7 deletions api/src/main/java/net/kyori/adventure/text/Component.java
Original file line number Diff line number Diff line change
Expand Up @@ -1297,8 +1297,10 @@ default boolean contains(final @NonNull Component that) {
* Prevents a cycle between this component and the provided component.
*
* @param that the other component
* @deprecated for removal since 4.7.0, with no replacement - this method is not necessary due to the fact {@code Component}s are immutable
* @since 4.0.0
*/
@Deprecated
default void detectCycle(final @NonNull Component that) {
if(that.contains(this)) {
throw new IllegalStateException("Component cycle detected between " + this + " and " + that);
Expand Down Expand Up @@ -1598,13 +1600,7 @@ default boolean hasDecoration(final @NonNull TextDecoration decoration) {
*/
@Contract(pure = true)
default @NonNull Component hoverEvent(final @Nullable HoverEventSource<?> source) {
final HoverEvent<?> event = HoverEventSource.unbox(source);
if(event != null) {
if(event.action().type().isAssignableFrom(Component.class)) {
this.detectCycle((Component) event.value()); // detect cycle before modifying
}
}
return this.style(this.style().hoverEvent(event));
return this.style(this.style().hoverEvent(source));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public interface ScopedComponent<C extends Component> extends Component {
@SuppressWarnings("unchecked")
default @NonNull C append(final @NonNull Component component) {
if(component == Component.empty()) return (C) this;
this.detectCycle(component); // detect cycle before modifying
final List<Component> oldChildren = this.children();
return this.children(AbstractComponent.addOne(oldChildren, component));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
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.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

abstract class AbstractComponentTest<C extends BuildableComponent<C, B> & ScopedComponent<C>, B extends ComponentBuilder<C, B>> {
abstract B builder();
Expand All @@ -72,15 +70,6 @@ void testChildren() {
assertThat(c1.children()).containsExactly(child).inOrder();
}

@Test
void testCycleSelf() {
assertThrows(IllegalStateException.class, () -> {
final Component component = this.buildOne();
component.append(component);
fail("A component was added to itself");
});
}

@Test
void testDecorations() {
Component component = this.buildOne();
Expand Down Expand Up @@ -336,32 +325,6 @@ void testBasicEquals() {
.testEquals();
}

@Test
void testCycleHoverRoot() {
assertThrows(IllegalStateException.class, () -> {
final Component hoverComponent = Component.text("hover");
final Component component = this.builder()
.hoverEvent(HoverEvent.showText(hoverComponent))
.build();
// component's hover event value is hoverComponent, we should not be able to add it
hoverComponent.append(component);
fail("A component was added to itself");
});
}

@Test
void testCycleHoverChild() {
assertThrows(IllegalStateException.class, () -> {
final Component hoverComponent = Component.text("hover child");
final Component component = this.builder()
.hoverEvent(HoverEvent.showText(Component.text("hover").append(hoverComponent)))
.build();
// component's hover event value contains hoverComponent, we should not be able to add it
hoverComponent.append(component);
fail("A component was added to itself");
});
}

// -----------------
// ---- Builder ----
// -----------------
Expand Down

0 comments on commit 28042b8

Please sign in to comment.