users@javaserverfaces-spec-public.java.net

[jsr372-experts mirror] [jsr372-experts] Re: f:validateWholeBean copier strategy

From: Josh Juneau <juneau001_at_gmail.com>
Date: Fri, 11 Dec 2015 07:35:14 -0600

+1 with Arjan's suggestion in defaulting to all strategies in one with the
ability to override.

Josh Juneau
juneau001_at_gmail.com
http://jj-blogger.blogspot.com
https://www.apress.com/index.php/author/author/view/id/1866


On Thu, Dec 10, 2015 at 8:33 AM, arjan tijms <arjan.tijms_at_gmail.com> wrote:

> Hi,
>
> In the WholeBeanValidator class I encountered the following method:
>
> private Object
> copyObjectAndPopulateWithCandidateValues(ValueExpression beanVE,
> Object val,
> Map<String, Map<String, Object>> candidate) {
> // <editor-fold defaultstate="collapsed">
>
> // Populate the value copy with the validated values from the
> candidate
> Map<String, Object> propertiesToSet = new HashMap<>();
> for (Map.Entry<String, Map<String, Object>> cur :
> candidate.entrySet()) {
> propertiesToSet.put(cur.getKey(), cur.getValue().get("value"));
> }
> // Copy the value so that class-level validation can be performed
> // without corrupting the real value
> Object valCopy = null;
> try {
> NewInstanceCopier nic = new NewInstanceCopier();
> valCopy = nic.copy(val);
> } catch (IllegalStateException ise2) {
> }
> if (null == valCopy) {
> if (val instanceof Serializable) {
> try {
> SerializationCopier sc = new SerializationCopier();
> valCopy = sc.copy(val);
> } catch (IllegalStateException ise) {
> }
> } else if (val instanceof Cloneable) {
> try {
> CloneCopier cc = new CloneCopier();
> valCopy = cc.copy(val);
> } catch (IllegalStateException ise) {
> }
> } else {
> try {
> CopyCtorCopier ccc = new CopyCtorCopier();
> valCopy = ccc.copy(val);
> } catch (IllegalStateException ise) {
> }
> }
> }
>
> As it appears the copy strategy is hard coded here.
>
> In the original OmniFaces code I gave the user the option to set the
> strategy. The default is to try a number of strategies (as shown here), but
> alternatively the user can either choose an explicit existing strategy
> (e.g. CopyCtorCopier), or provide a custom strategy.
>
> This is needed in practice, since with the current implementation it's
> hard to make a bean that fails the NewInstanceCopier and still be a bean
> (actually, isn't this nearly impossible?).
>
> In other words, if the CopyCtorCopier would be more efficient since the
> bean had heavy weight dependencies, then this copier is quite hard now to
> actually reach.
>
> I would like to suggest to consider using the same or similar attribute
> that OmniFaces uses to set the copier, defaulting to the all strategies one.
>
> Kind regards,
> Arjan Tijms
>
>
>
>
>
>
>