From a983e27b3ad93a1219083831bc546801180d98eb Mon Sep 17 00:00:00 2001
From: pizzi80
Date: Sat, 17 Dec 2022 18:46:25 +0100
Subject: [PATCH] BUG inside restoreState: Object[].length is 2! Generics
Cosmetics
Signed-off-by: pizzi80
---
.../tag/composite/BehaviorHolderWrapper.java | 2 +-
.../faces/component/StateHolderSaver.java | 13 +-
.../jakarta/faces/component/UIComponent.java | 2 +-
.../faces/component/UIComponentBase.java | 138 ++++++++----------
.../java/jakarta/faces/render/RenderKit.java | 4 +-
.../com/sun/faces/mock/MockRenderKit.java | 17 +--
6 files changed, 77 insertions(+), 99 deletions(-)
diff --git a/impl/src/main/java/com/sun/faces/facelets/tag/composite/BehaviorHolderWrapper.java b/impl/src/main/java/com/sun/faces/facelets/tag/composite/BehaviorHolderWrapper.java
index a0f89fe76d..4dc817e297 100644
--- a/impl/src/main/java/com/sun/faces/facelets/tag/composite/BehaviorHolderWrapper.java
+++ b/impl/src/main/java/com/sun/faces/facelets/tag/composite/BehaviorHolderWrapper.java
@@ -413,7 +413,7 @@ protected FacesListener[] getFacesListeners(Class clazz) {
}
@Override
- protected Renderer getRenderer(FacesContext context) {
+ protected Renderer extends UIComponent> getRenderer(FacesContext context) {
return null;
}
diff --git a/impl/src/main/java/jakarta/faces/component/StateHolderSaver.java b/impl/src/main/java/jakarta/faces/component/StateHolderSaver.java
index f30aa39f47..ee07d34db9 100644
--- a/impl/src/main/java/jakarta/faces/component/StateHolderSaver.java
+++ b/impl/src/main/java/jakarta/faces/component/StateHolderSaver.java
@@ -17,6 +17,7 @@
package jakarta.faces.component;
import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
import jakarta.faces.context.FacesContext;
@@ -100,7 +101,7 @@ public StateHolderSaver(FacesContext context, Object toSave) {
public Object restore(FacesContext context) throws IllegalStateException {
Object result = null;
- Class toRestoreClass;
+ Class> toRestoreClass;
// if the Object to save implemented Serializable but not
// StateHolder
@@ -124,11 +125,11 @@ public Object restore(FacesContext context) throws IllegalStateException {
if (null != toRestoreClass) {
try {
- result = toRestoreClass.newInstance();
- } catch (InstantiationException e) {
+ result = toRestoreClass.getDeclaredConstructor().newInstance();
+ } catch (InstantiationException | IllegalAccessException | NoSuchMethodException e) {
throw new IllegalStateException(e);
- } catch (IllegalAccessException a) {
- throw new IllegalStateException(a);
+ } catch (InvocationTargetException e) {
+ throw new RuntimeException(e);
}
}
@@ -141,7 +142,7 @@ public Object restore(FacesContext context) throws IllegalStateException {
return result;
}
- private static Class loadClass(String name, Object fallbackClass) throws ClassNotFoundException {
+ private static Class> loadClass(String name, Object fallbackClass) throws ClassNotFoundException {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
if (loader == null) {
loader = fallbackClass.getClass().getClassLoader();
diff --git a/impl/src/main/java/jakarta/faces/component/UIComponent.java b/impl/src/main/java/jakarta/faces/component/UIComponent.java
index 6697ac0608..d60f03ff9e 100644
--- a/impl/src/main/java/jakarta/faces/component/UIComponent.java
+++ b/impl/src/main/java/jakarta/faces/component/UIComponent.java
@@ -1981,7 +1981,7 @@ public void processEvent(ComponentSystemEvent event) throws AbortProcessingExcep
* @param context {@link FacesContext} for the current request
* @return the renderer, or null
.
*/
- protected abstract Renderer getRenderer(FacesContext context);
+ protected abstract Renderer extends UIComponent> getRenderer(FacesContext context);
// --------------------------------------------------------- Package Private
diff --git a/impl/src/main/java/jakarta/faces/component/UIComponentBase.java b/impl/src/main/java/jakarta/faces/component/UIComponentBase.java
index 80ef190176..a8115ff32f 100644
--- a/impl/src/main/java/jakarta/faces/component/UIComponentBase.java
+++ b/impl/src/main/java/jakarta/faces/component/UIComponentBase.java
@@ -41,20 +41,8 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
-import java.util.AbstractCollection;
-import java.util.AbstractMap;
-import java.util.AbstractSet;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ListIterator;
-import java.util.Map;
+import java.util.*;
import java.util.Map.Entry;
-import java.util.NoSuchElementException;
-import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.logging.Level;
@@ -73,7 +61,6 @@
import jakarta.faces.event.ComponentSystemEventListener;
import jakarta.faces.event.FacesEvent;
import jakarta.faces.event.FacesListener;
-import jakarta.faces.event.PhaseId;
import jakarta.faces.event.PostAddToViewEvent;
import jakarta.faces.event.PostValidateEvent;
import jakarta.faces.event.PreRemoveFromViewEvent;
@@ -102,7 +89,7 @@ public abstract class UIComponentBase extends UIComponent {
// -------------------------------------------------------------- Attributes
- private static Logger LOGGER = Logger.getLogger("jakarta.faces.component", "jakarta.faces.LogStrings");
+ private static final Logger LOGGER = Logger.getLogger("jakarta.faces.component", "jakarta.faces.LogStrings");
private static final String ADDED = UIComponentBase.class.getName() + ".ADDED";
@@ -131,7 +118,7 @@ public abstract class UIComponentBase extends UIComponent {
* An EMPTY_OBJECT_ARRAY argument list to be passed to reflection methods.
*
*/
- private static final Object EMPTY_OBJECT_ARRAY[] = new Object[0];
+ private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
/**
*
@@ -201,7 +188,7 @@ public Map getPassThroughAttributes(boolean create) {
Map passThroughAttributes = (Map) this.getStateHelper().get(PropertyKeys.passThroughAttributes);
if (passThroughAttributes == null && create) {
- passThroughAttributes = new PassThroughAttributesMap<>();
+ passThroughAttributes = new PassThroughAttributesMap();
getStateHelper().put(PropertyKeys.passThroughAttributes, passThroughAttributes);
}
@@ -239,7 +226,7 @@ public String getClientId(FacesContext context) {
}
// Allow the renderer to convert the clientId
- Renderer renderer = getRenderer(context);
+ Renderer> renderer = getRenderer(context);
if (renderer != null) {
clientId = renderer.convertClientId(context, clientId);
}
@@ -263,7 +250,7 @@ public void setId(String id) {
// if the current ID is not null, and the passed
// argument is the same, no need to validate it
// as it has already been validated.
- if (this.id == null || !this.id.equals(id)) {
+ if (!Objects.equals(this.id, id)) {
validateId(id);
this.id = id;
}
@@ -306,7 +293,7 @@ public void setParent(UIComponent parent) {
@Override
public boolean isRendered() {
- return Boolean.valueOf(getStateHelper().eval(PropertyKeys.rendered, TRUE).toString());
+ return Boolean.parseBoolean(getStateHelper().eval(PropertyKeys.rendered, TRUE).toString());
}
@Override
@@ -495,7 +482,7 @@ public void decode(FacesContext context) {
String rendererType = getRendererType();
if (rendererType != null) {
- Renderer renderer = getRenderer(context);
+ Renderer renderer = (Renderer) getRenderer(context);
if (renderer != null) {
renderer.decode(context, this);
} else {
@@ -1038,9 +1025,9 @@ protected FacesContext getFacesContext() {
}
@Override
- protected Renderer getRenderer(FacesContext context) {
+ protected Renderer extends UIComponent> getRenderer(FacesContext context) {
- Renderer renderer = null;
+ Renderer> renderer = null;
String rendererType = getRendererType();
if (rendererType != null) {
@@ -1194,25 +1181,19 @@ public Object saveState(FacesContext context) {
@Override
public void restoreState(FacesContext context, Object state) {
-
- if (context == null) {
- throw new NullPointerException();
- }
-
- if (state == null) {
- return;
- }
+ Objects.requireNonNull(context); // NPE
+ if (state == null) return; // skip restore
Object[] values = (Object[]) state;
- if (values[0] != null) {
+ if ( values.length > 0 && values[0] != null) {
if (listeners == null) {
listeners = new AttachedObjectListHolder<>();
}
listeners.restoreState(context, values[0]);
}
- if (values[1] != null) {
+ if (values.length > 1 && values[1] != null) {
Map, List> restoredListeners = restoreSystemEventListeners(context, values[1]);
if (listenersByEventClass != null) {
listenersByEventClass.putAll(restoredListeners);
@@ -1221,15 +1202,15 @@ public void restoreState(FacesContext context, Object state) {
}
}
- if (values[2] != null) {
+ if (values.length > 2 && values[2] != null) {
behaviors = restoreBehaviorsState(context, values[2]);
}
- if (values[3] != null) {
+ if (values.length > 3 && values[3] != null) {
bindings = restoreBindingsState(context, values[3]);
}
- if (values[4] != null) {
+ if (values.length > 4 && values[4] != null) {
getStateHelper().restoreState(context, values[4]);
}
@@ -1293,7 +1274,7 @@ public static Object saveAttachedState(FacesContext context, Object attachedObje
}
Object result;
- Class mapOrCollectionClass = attachedObject.getClass();
+ Class> mapOrCollectionClass = attachedObject.getClass();
boolean newWillSucceed = true;
// first, test for newability of the class.
try {
@@ -1307,7 +1288,7 @@ public static Object saveAttachedState(FacesContext context, Object attachedObje
}
if (newWillSucceed && attachedObject instanceof Collection) {
- Collection attachedCollection = (Collection) attachedObject;
+ Collection> attachedCollection = (Collection>) attachedObject;
List resultList = new ArrayList<>(attachedCollection.size() + 1);
resultList.add(new StateHolderSaver(context, mapOrCollectionClass));
for (Object item : attachedCollection) {
@@ -1375,16 +1356,18 @@ public static Object restoreAttachedState(FacesContext context, Object stateObj)
if (stateObj instanceof List) {
List stateList = (List) stateObj;
StateHolderSaver collectionSaver = stateList.get(0);
- Class mapOrCollection = (Class) collectionSaver.restore(context);
+ Class> mapOrCollection = (Class>) collectionSaver.restore(context);
if (Collection.class.isAssignableFrom(mapOrCollection)) {
Collection