From e6eb5c1513140f41b0a75bd4238c2dcb155920f2 Mon Sep 17 00:00:00 2001 From: Bauke Scholtz Date: Sun, 2 May 2021 10:44:46 -0400 Subject: [PATCH] Revert #4808 due to being possible cause of failing TCK --- .../facelets/tag/jsf/ComponentSupport.java | 178 +++++++----------- .../faces/component/ComponentStateHelper.java | 15 -- .../javax/faces/component/UIComponent.java | 1 - .../faces/component/UIComponentBase.java | 27 +-- 4 files changed, 70 insertions(+), 151 deletions(-) diff --git a/impl/src/main/java/com/sun/faces/facelets/tag/jsf/ComponentSupport.java b/impl/src/main/java/com/sun/faces/facelets/tag/jsf/ComponentSupport.java index cb4644dd10..51de8e85c6 100644 --- a/impl/src/main/java/com/sun/faces/facelets/tag/jsf/ComponentSupport.java +++ b/impl/src/main/java/com/sun/faces/facelets/tag/jsf/ComponentSupport.java @@ -16,35 +16,35 @@ package com.sun.faces.facelets.tag.jsf; -import com.sun.faces.RIConstants; -import com.sun.faces.context.StateContext; -import com.sun.faces.facelets.tag.jsf.core.FacetHandler; import static com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter.PartialStateSaving; -import com.sun.faces.util.Util; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; import javax.faces.FacesException; import javax.faces.component.UIComponent; import javax.faces.component.UIPanel; import javax.faces.component.UIViewRoot; import javax.faces.context.FacesContext; +import javax.faces.event.PhaseId; import javax.faces.view.facelets.ComponentConfig; import javax.faces.view.facelets.ComponentHandler; import javax.faces.view.facelets.FaceletContext; +import javax.faces.view.facelets.Tag; import javax.faces.view.facelets.TagAttribute; import javax.faces.view.facelets.TagAttributeException; -import javax.faces.view.facelets.Tag; -import javax.faces.event.PhaseId; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; +import com.sun.faces.RIConstants; +import com.sun.faces.context.StateContext; +import com.sun.faces.facelets.tag.jsf.core.FacetHandler; +import com.sun.faces.util.Util; /** * @@ -55,7 +55,6 @@ public final class ComponentSupport { private final static String MARK_DELETED = "com.sun.faces.facelets.MARK_DELETED"; public final static String MARK_CREATED = "com.sun.faces.facelets.MARK_ID"; - private final static String MARK_ID_CACHE = "com.sun.faces.facelets.MARK_ID_CACHE"; // Expando boolean attribute used to identify parent components that have had // a dynamic child addition or removal. @@ -238,104 +237,65 @@ public static UIComponent findUIInstructionChildByTagId(FacesContext context, UI * @return the UI component */ public static UIComponent findChildByTagId(FacesContext context, UIComponent parent, String id) { - // fast path - get the child from the descendant mark id cache - return getDescendantMarkIdCache(parent).get(id); - } - - @SuppressWarnings("unchecked") - private static Map getDescendantMarkIdCache(UIComponent component) { - Map descendantMarkIdCache = (Map) component.getTransientStateHelper().getTransient(MARK_ID_CACHE); - - if (descendantMarkIdCache == null) { - descendantMarkIdCache = new HashMap(); - component.getTransientStateHelper().putTransient(MARK_ID_CACHE, descendantMarkIdCache); - } - - return descendantMarkIdCache; - } - - /** - * Adds the mark id of the specified {@link UIComponent} otherComponent to the mark id cache of this component, - * including all its descendant mark ids. Changes are propagated up the component tree. - */ - public static void addToDescendantMarkIdCache(UIComponent component, UIComponent otherComponent) { - String markId = (String) otherComponent.getAttributes().get(MARK_CREATED); - if (markId != null) { - addSingleDescendantMarkId(component, markId, otherComponent); - } - Map otherMarkIds = getDescendantMarkIdCache(otherComponent); - if (!otherMarkIds.isEmpty()) { - addAllDescendantMarkIds(component, otherMarkIds); - } - } - - /** - * Adds the specified markId and its corresponding {@link UIComponent} otherComponent - * to the mark id cache of this component. Changes are propagated up the component tree. - */ - private static void addSingleDescendantMarkId(UIComponent component, String markId, UIComponent otherComponent) { - getDescendantMarkIdCache(component).put(markId, otherComponent); - UIComponent parent = component.getParent(); - if (parent != null) { - addSingleDescendantMarkId(parent, markId, otherComponent); - } - } - - /** - * Adds all specified otherMarkIds to the mark id cache of this component. - * Changes are propagated up the component tree. - */ - private static void addAllDescendantMarkIds(UIComponent component, Map otherMarkIds) { - getDescendantMarkIdCache(component).putAll(otherMarkIds); - UIComponent parent = component.getParent(); - if (parent != null) { - addAllDescendantMarkIds(parent, otherMarkIds); - } - } - - /** - * Removes the mark id of the specified {@link UIComponent} otherComponent from the mark id cache of this component, - * including all its descendant mark ids. Changes are propagated up the component tree. - */ - public static void removeFromDescendantMarkIdCache(UIComponent component, UIComponent otherComponent) { - String markId = (String) otherComponent.getAttributes().get(MARK_CREATED); - if (markId != null) { - removeSingleDescendantMarkId(component, markId); + UIComponent c = null; + UIViewRoot root = context.getViewRoot(); + boolean hasDynamicComponents = (null != root && + root.getAttributes().containsKey(RIConstants.TREE_HAS_DYNAMIC_COMPONENTS)); + String cid = null; + List components; + String facetName = getFacetName(parent); + if (null != facetName) { + c = parent.getFacet(facetName); + // We will have a facet name, but no corresponding facet in the + // case of facets with composite components. In this case, + // we must do the brute force search. + if (null != c) { + cid = (String) c.getAttributes().get(MARK_CREATED); + if (id.equals(cid)) { + return c; + } + } } - Map otherMarkIds = getDescendantMarkIdCache(otherComponent); - if (!otherMarkIds.isEmpty()) { - removeAllDescendantMarkIds(component, otherMarkIds); + if (0 < parent.getFacetCount()) { + components = new ArrayList<>(); + components.addAll(parent.getFacets().values()); + components.addAll(parent.getChildren()); + } else { + components = parent.getChildren(); } - } - /** - * Removes the specified markId from the mark id cache of this component. - * Changes are propagated up the component tree. - */ - private static void removeSingleDescendantMarkId(UIComponent component, String markId) { - getDescendantMarkIdCache(component).remove(markId); - UIComponent parent = component.getParent(); - if (parent != null) { - removeSingleDescendantMarkId(parent, markId); + int len = components.size(); + for (int i = 0; i < len; i++) { + c = components.get(i); + cid = (String) c.getAttributes().get(MARK_CREATED); + if (id.equals(cid)) { + return c; + } + if (c instanceof UIPanel && c.getAttributes().containsKey(IMPLICIT_PANEL)) { + for (UIComponent c2 : c.getChildren()) { + cid = (String) c2.getAttributes().get(MARK_CREATED); + if (id.equals(cid)) { + return c2; + } + } + } + if (hasDynamicComponents) { + /* + * Make sure we look for the child recursively it might have moved + * into a different parent in the parent hierarchy. Note currently + * we are only looking down the tree. Maybe it would be better + * to use the VisitTree API instead. + */ + UIComponent foundChild = findChildByTagId(context, c, id); + if (foundChild != null) { + return foundChild; + } + } } - } - /** - * Removes all specified otherMarkIds from the mark id cache of this component. - * Changes are propagated up the component tree. - */ - private static void removeAllDescendantMarkIds(UIComponent component, Map otherMarkIds) { - Map descendantMarkIdCache = getDescendantMarkIdCache(component); - Iterator iterator = otherMarkIds.keySet().iterator(); - while (iterator.hasNext()) { - descendantMarkIdCache.remove(iterator.next()); - } - UIComponent parent = component.getParent(); - if (parent != null) { - removeAllDescendantMarkIds(parent, otherMarkIds); - } + return null; } - + /** * According to JSF 1.2 tag specs, this helper method will use the * TagAttribute passed in determining the Locale intended. diff --git a/impl/src/main/java/javax/faces/component/ComponentStateHelper.java b/impl/src/main/java/javax/faces/component/ComponentStateHelper.java index 0c8b0253bd..1e02dad348 100644 --- a/impl/src/main/java/javax/faces/component/ComponentStateHelper.java +++ b/impl/src/main/java/javax/faces/component/ComponentStateHelper.java @@ -16,11 +16,8 @@ package javax.faces.component; -import static com.sun.faces.facelets.tag.jsf.ComponentSupport.MARK_CREATED; -import static com.sun.faces.facelets.tag.jsf.ComponentSupport.addToDescendantMarkIdCache; import static com.sun.faces.util.Util.coalesce; import static com.sun.faces.util.Util.isEmpty; -import static javax.faces.component.UIComponent.PropertyKeys; import static javax.faces.component.UIComponentBase.restoreAttachedState; import static javax.faces.component.UIComponentBase.saveAttachedState; @@ -34,8 +31,6 @@ import javax.el.ValueExpression; import javax.faces.context.FacesContext; -import com.sun.faces.facelets.tag.jsf.ComponentSupport; - /** * A base implementation for maps which implement the PartialStateHolder and TransientStateHolder * interfaces. @@ -119,16 +114,6 @@ public Object remove(Serializable key) { public Object put(Serializable key, String mapKey, Object value) { initMap(key); - if (MARK_CREATED.equals(mapKey)) { - if (PropertyKeys.attributes.equals(key)) { - UIComponent parent = component.getParent(); - if (parent != null) { - // remember this component by its mark id - addToDescendantMarkIdCache(parent, component); - } - } - } - Object ret = null; if (component.initialStateMarked()) { Map dMap = (Map) deltaMap.get(key); diff --git a/impl/src/main/java/javax/faces/component/UIComponent.java b/impl/src/main/java/javax/faces/component/UIComponent.java index 00ef7e21ac..1666511f19 100644 --- a/impl/src/main/java/javax/faces/component/UIComponent.java +++ b/impl/src/main/java/javax/faces/component/UIComponent.java @@ -16,7 +16,6 @@ package javax.faces.component; -import static com.sun.faces.facelets.tag.jsf.ComponentSupport.MARK_CREATED; import static com.sun.faces.util.Util.isAnyNull; import static com.sun.faces.util.Util.isOneOf; import static java.util.Collections.emptyMap; diff --git a/impl/src/main/java/javax/faces/component/UIComponentBase.java b/impl/src/main/java/javax/faces/component/UIComponentBase.java index 7b0f218430..1df3e80811 100644 --- a/impl/src/main/java/javax/faces/component/UIComponentBase.java +++ b/impl/src/main/java/javax/faces/component/UIComponentBase.java @@ -16,8 +16,6 @@ package javax.faces.component; -import static com.sun.faces.facelets.tag.jsf.ComponentSupport.addToDescendantMarkIdCache; -import static com.sun.faces.facelets.tag.jsf.ComponentSupport.removeFromDescendantMarkIdCache; import static com.sun.faces.util.Util.isAllNull; import static com.sun.faces.util.Util.isAnyNull; import static com.sun.faces.util.Util.isEmpty; @@ -73,7 +71,6 @@ import javax.faces.event.ComponentSystemEventListener; import javax.faces.event.FacesEvent; import javax.faces.event.FacesListener; -import javax.faces.event.PhaseId; import javax.faces.event.PostAddToViewEvent; import javax.faces.event.PostValidateEvent; import javax.faces.event.PreRemoveFromViewEvent; @@ -85,7 +82,6 @@ import com.sun.faces.application.ValueBindingValueExpressionAdapter; import com.sun.faces.application.ValueExpressionValueBindingAdapter; -import com.sun.faces.facelets.tag.jsf.ComponentSupport; /** *

@@ -2256,12 +2252,10 @@ private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundE private static class ChildrenList extends ArrayList { private UIComponent component; - private FacesContext context; public ChildrenList(UIComponent component) { super(6); this.component = component; - this.context = component.getFacesContext(); } @Override @@ -2271,7 +2265,6 @@ public void add(int index, UIComponent element) { } else if ((index < 0) || (index > size())) { throw new IndexOutOfBoundsException(); } else { - addToDescendantMarkIdCache(component, element); eraseParent(element); super.add(index, element); element.setParent(component); @@ -2284,7 +2277,6 @@ public boolean add(UIComponent element) { if (element == null) { throw new NullPointerException(); } else { - addToDescendantMarkIdCache(component, element); eraseParent(element); boolean result = super.add(element); element.setParent(component); @@ -2332,9 +2324,6 @@ public void clear() { } for (int i = 0; i < n; i++) { UIComponent child = get(i); - if (!context.isReleased() && !context.getCurrentPhaseId().equals(PhaseId.RENDER_RESPONSE)) { - removeFromDescendantMarkIdCache(component, child); - } child.setParent(null); } super.clear(); @@ -2358,9 +2347,6 @@ public ListIterator listIterator(int index) { @Override public UIComponent remove(int index) { UIComponent child = get(index); - if (!context.isReleased() && !context.getCurrentPhaseId().equals(PhaseId.RENDER_RESPONSE)) { - removeFromDescendantMarkIdCache(component, child); - } child.setParent(null); super.remove(index); return (child); @@ -2372,9 +2358,7 @@ public boolean remove(Object elementObj) { if (element == null) { throw new NullPointerException(); } - if (!context.isReleased() && !context.getCurrentPhaseId().equals(PhaseId.RENDER_RESPONSE)) { - removeFromDescendantMarkIdCache(component, element); - } + if (super.indexOf(element) != -1) { element.setParent(null); } @@ -2416,10 +2400,8 @@ public UIComponent set(int index, UIComponent element) { } else if ((index < 0) || (index >= size())) { throw new IndexOutOfBoundsException(); } else { - addToDescendantMarkIdCache(component, element); eraseParent(element); UIComponent previous = get(index); - removeFromDescendantMarkIdCache(component, previous); super.set(index, element); previous.setParent(null); element.setParent(component); @@ -2580,12 +2562,10 @@ public void remove() { private static class FacetsMap extends HashMap { private UIComponent component; - private FacesContext context; public FacetsMap(UIComponent component) { super(3, 1.0f); this.component = component; - context = component.getFacesContext(); } @Override @@ -2618,10 +2598,8 @@ public UIComponent put(String key, UIComponent value) { } UIComponent previous = super.get(key); if (previous != null) { - removeFromDescendantMarkIdCache(component, previous); previous.setParent(null); } - addToDescendantMarkIdCache(component, value); eraseParent(value); UIComponent result = super.put(key, value); value.setParent(component); @@ -2643,9 +2621,6 @@ public void putAll(Map map) { public UIComponent remove(Object key) { UIComponent previous = get(key); if (previous != null) { - if (!context.isReleased() && !context.getCurrentPhaseId().equals(PhaseId.RENDER_RESPONSE)) { - removeFromDescendantMarkIdCache(component, previous); - } previous.setParent(null); } super.remove(key);