Composite.java (vaadin-flow-4.0.5) | : | Composite.java (vaadin-flow-4.0.6) | ||
---|---|---|---|---|
skipping to change at line 18 | skipping to change at line 18 | |||
* http://www.apache.org/licenses/LICENSE-2.0 | * http://www.apache.org/licenses/LICENSE-2.0 | |||
* | * | |||
* Unless required by applicable law or agreed to in writing, software | * Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||
* License for the specific language governing permissions and limitations under | * License for the specific language governing permissions and limitations under | |||
* the License. | * the License. | |||
*/ | */ | |||
package com.vaadin.flow.component; | package com.vaadin.flow.component; | |||
import java.io.IOException; | ||||
import java.io.ObjectInputStream; | ||||
import java.lang.reflect.ParameterizedType; | import java.lang.reflect.ParameterizedType; | |||
import java.lang.reflect.Type; | import java.lang.reflect.Type; | |||
import java.lang.reflect.TypeVariable; | import java.lang.reflect.TypeVariable; | |||
import java.util.stream.Stream; | import java.util.stream.Stream; | |||
import com.googlecode.gentyref.GenericTypeReflector; | import com.googlecode.gentyref.GenericTypeReflector; | |||
import com.vaadin.flow.dom.Element; | import com.vaadin.flow.dom.Element; | |||
import com.vaadin.flow.dom.ElementUtil; | import com.vaadin.flow.dom.ElementUtil; | |||
import com.vaadin.flow.internal.ReflectTools; | import com.vaadin.flow.internal.ReflectTools; | |||
skipping to change at line 55 | skipping to change at line 53 | |||
* element to the {@link Element} tree. | * element to the {@link Element} tree. | |||
* | * | |||
* @author Vaadin Ltd | * @author Vaadin Ltd | |||
* @since 1.0 | * @since 1.0 | |||
* @param <T> | * @param <T> | |||
* the type of the content | * the type of the content | |||
*/ | */ | |||
public abstract class Composite<T extends Component> extends Component { | public abstract class Composite<T extends Component> extends Component { | |||
private T content; | private T content; | |||
private transient ThreadLocal<Boolean> contentIsInitializing = new ThreadLoc | // Will deserialize as the default value for boolean, i.e. false | |||
al<>(); | private transient boolean contentIsInitializing = false; | |||
/** | /** | |||
* Creates a new composite. | * Creates a new composite. | |||
* <p> | * <p> | |||
* To define your own composite, extend this class and implement | * To define your own composite, extend this class and implement | |||
* {@link #initContent()}. | * {@link #initContent()}. | |||
*/ | */ | |||
protected Composite() { | protected Composite() { | |||
super(null); | super(null); | |||
} | } | |||
skipping to change at line 121 | skipping to change at line 120 | |||
/** | /** | |||
* Gets the content of the composite, i.e. the component the composite is | * Gets the content of the composite, i.e. the component the composite is | |||
* wrapping. | * wrapping. | |||
* | * | |||
* @return the content for the composite, never {@code null} | * @return the content for the composite, never {@code null} | |||
*/ | */ | |||
public T getContent() { | public T getContent() { | |||
if (content == null) { | if (content == null) { | |||
try { | try { | |||
if (Boolean.TRUE.equals(contentIsInitializing.get())) { | if (contentIsInitializing) { | |||
throw new IllegalStateException( | throw new IllegalStateException( | |||
"The content is not yet initialized. " | "The content is not yet initialized. " | |||
+ "Detected direct or indirect call to 'getC ontent' from 'initContent'. " | + "Detected direct or indirect call to 'getC ontent' from 'initContent'. " | |||
+ "You may not call any framework method on a '" | + "You may not call any framework method on a '" | |||
+ Composite.class.getSimpleName() | + Composite.class.getSimpleName() | |||
+ "' instance before 'initContent' has compl eted initializing the component."); | + "' instance before 'initContent' has compl eted initializing the component."); | |||
} | } | |||
contentIsInitializing.set(true); | contentIsInitializing = true; | |||
T newContent = initContent(); | T newContent = initContent(); | |||
if (newContent == null) { | if (newContent == null) { | |||
throw new IllegalStateException( | throw new IllegalStateException( | |||
"initContent returned null instead of a component"); | "initContent returned null instead of a component"); | |||
} | } | |||
setContent(newContent); | setContent(newContent); | |||
} finally { | } finally { | |||
contentIsInitializing.set(false); | contentIsInitializing = false; | |||
} | } | |||
} | } | |||
return content; | return content; | |||
} | } | |||
/** | /** | |||
* Sets the content for this composite and attaches it to the element. | * Sets the content for this composite and attaches it to the element. | |||
* <p> | * <p> | |||
* This method must only be called once. | * This method must only be called once. | |||
* | * | |||
skipping to change at line 189 | skipping to change at line 188 | |||
* <p> | * <p> | |||
* A composite always has one child component, returned by | * A composite always has one child component, returned by | |||
* {@link #initContent()}. | * {@link #initContent()}. | |||
* | * | |||
* @return the child component of this composite | * @return the child component of this composite | |||
*/ | */ | |||
@Override | @Override | |||
public Stream<Component> getChildren() { | public Stream<Component> getChildren() { | |||
return Stream.of(getContent()); | return Stream.of(getContent()); | |||
} | } | |||
private void readObject(ObjectInputStream stream) | ||||
throws ClassNotFoundException, IOException { | ||||
stream.defaultReadObject(); | ||||
contentIsInitializing = new ThreadLocal<>(); | ||||
} | ||||
} | } | |||
End of changes. 6 change blocks. | ||||
13 lines changed or deleted | 5 lines changed or added |