Composite extends Component { public T getContent() { if (content == null) { try { if (Boolean.TRUE.equals(contentIsInitializing.get())) { throw new IllegalStateException( "The content is not yet initialized. " + "Detected direct or indirect call to 'getContent' from 'initContent'. " + "You may not call any framework method on a '" + Composite.class.getSimpleName() + "' instance before 'initContent' has completed initializing the component."); } contentIsInitializing.set(true); T newContent = initContent(); if (newContent == null) { throw new IllegalStateException( "initContent returned null instead of a component"); } setContent(newContent); } finally { contentIsInitializing.set(false); } } return content; } protected  Composite(); T getContent(); @Override Element getElement(); @Override Stream<Component> getChildren();  }
@Test public void automaticCompositeContentType() { class CompositeWithGenericType extends Composite<TestComponent> { } CompositeWithGenericType instance = new CompositeWithGenericType(); assertEquals(TestComponent.class, instance.getContent().getClass()); }
@Test(expected = IllegalStateException.class) public void compositeContentTypeWithVariableTypeParameter() { class CompositeWithVariableType<C extends Component> extends Composite<C> { } CompositeWithVariableType<TestComponent> composite = new CompositeWithVariableType<>(); composite.getContent(); }
@Test public void compositeContentTypeWithSpecifiedType() { class CompositeWithCustomComponent extends Composite<CustomComponent<List<String>>> { } CompositeWithCustomComponent composite = new CompositeWithCustomComponent(); assertEquals(CustomComponent.class, composite.getContent().getClass()); }
@Test(expected = IllegalStateException.class) public void compositeContentTypeWithTypeVariable() { class CompositeWithComposite extends Composite<CompositeWithVariableType<TestComponent>> { } CompositeWithComposite composite = new CompositeWithComposite(); composite.getContent(); }
@Test(expected = IllegalStateException.class) public void rawContentType() { @SuppressWarnings("rawtypes") class CompositeWithRawType extends Composite { } CompositeWithRawType composite = new CompositeWithRawType(); composite.getContent(); }
@Test(expected = IllegalArgumentException.class) public void noDefaultConstructor() { class NoDefaultConstructor extends Composite<Text> { } NoDefaultConstructor composite = new NoDefaultConstructor(); composite.getContent(); }
@Test public void compositeHierarchy() { class Class1<T extends Component> extends Composite<T> { } class Class2<T, V extends Component> extends Class1<V> { } class Class3<E extends Component> extends Class2<String, E> { } class Class4<A, B extends Component, C> extends Class3<B> { } class ComplexClass extends Class4<String, UI, Boolean> { } ComplexClass composite = new ComplexClass(); assertEquals(UI.class, composite.getContent().getClass()); }