diff --git a/praxiscore-api/src/main/java/org/praxislive/core/Component.java b/praxiscore-api/src/main/java/org/praxislive/core/Component.java index adfc71b2..c3fe8239 100644 --- a/praxiscore-api/src/main/java/org/praxislive/core/Component.java +++ b/praxiscore-api/src/main/java/org/praxislive/core/Component.java @@ -101,6 +101,10 @@ public interface Component { * component info, and property values, in that order. It may also add * custom annotations. *
+ * The component should delegate to + * {@link Container#getType(org.praxislive.core.Component)} to find its type + * rather than relying directly on {@link ComponentInfo#KEY_COMPONENT_TYPE}. + *
* The default implementation of this method does nothing. * * @param writer TreeWriter to write to diff --git a/praxiscore-api/src/main/java/org/praxislive/core/Container.java b/praxiscore-api/src/main/java/org/praxislive/core/Container.java index b72809de..d580d9e6 100644 --- a/praxiscore-api/src/main/java/org/praxislive/core/Container.java +++ b/praxiscore-api/src/main/java/org/praxislive/core/Container.java @@ -21,6 +21,7 @@ */ package org.praxislive.core; +import java.util.Optional; import java.util.stream.Stream; import org.praxislive.core.protocols.ContainerProtocol; @@ -79,4 +80,23 @@ public default void write(TreeWriter writer) { // no op } + /** + * Get the {@link ComponentType} of the provided child. The default + * implementation looks for {@link ComponentInfo#KEY_COMPONENT_TYPE} in the + * child's info. Container's may override to provide a more efficient or + * suitable result. + *
+ * The default implementation does not check if the provided component is
+ * actually a child of this container.
+ *
+ * @param child child component
+ * @return component type, or null if unavailable
+ */
+ public default ComponentType getType(Component child) {
+ return Optional.ofNullable(child.getInfo())
+ .map(info -> info.properties().get(ComponentInfo.KEY_COMPONENT_TYPE))
+ .flatMap(ComponentType::from)
+ .orElse(null);
+ }
+
}
diff --git a/praxiscore-base/src/main/java/org/praxislive/base/AbstractComponent.java b/praxiscore-base/src/main/java/org/praxislive/base/AbstractComponent.java
index 5d9107d2..6da6ac15 100644
--- a/praxiscore-base/src/main/java/org/praxislive/base/AbstractComponent.java
+++ b/praxiscore-base/src/main/java/org/praxislive/base/AbstractComponent.java
@@ -107,18 +107,23 @@ public void write(TreeWriter writer) {
}
protected final void writeTypeAndInfo(TreeWriter writer) {
- ComponentInfo info = getInfo();
- if (info == null) {
- return;
+ ComponentType type;
+ if (parent == null) {
+ // assume we're a root?!
+ type = Optional.ofNullable(getInfo())
+ .map(info -> info.properties().get(ComponentInfo.KEY_COMPONENT_TYPE))
+ .flatMap(ComponentType::from)
+ .orElse(null);
+ } else {
+ type = parent.getType(this);
}
- ComponentType type = Optional.ofNullable(
- info.properties().get(ComponentInfo.KEY_COMPONENT_TYPE))
- .flatMap(ComponentType::from)
- .orElse(null);
if (type != null) {
writer.writeType(type);
}
- writer.writeInfo(info);
+ ComponentInfo info = getInfo();
+ if (info != null) {
+ writer.writeInfo(info);
+ }
}
protected final void writeMeta(TreeWriter writer) {
diff --git a/praxiscore-base/src/main/java/org/praxislive/base/AbstractContainer.java b/praxiscore-base/src/main/java/org/praxislive/base/AbstractContainer.java
index f19d3b25..de9323c6 100644
--- a/praxiscore-base/src/main/java/org/praxislive/base/AbstractContainer.java
+++ b/praxiscore-base/src/main/java/org/praxislive/base/AbstractContainer.java
@@ -21,6 +21,7 @@
*/
package org.praxislive.base;
+import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
@@ -31,6 +32,7 @@
import org.praxislive.core.Call;
import org.praxislive.core.Component;
import org.praxislive.core.ComponentAddress;
+import org.praxislive.core.ComponentType;
import org.praxislive.core.Connection;
import org.praxislive.core.Container;
import org.praxislive.core.Control;
@@ -63,10 +65,12 @@ public abstract class AbstractContainer extends AbstractComponent implements Con
private final static System.Logger LOG = System.getLogger(AbstractContainer.class.getName());
private final Map