users@el-spec.java.net

[el-spec users] [jsr341-experts] Some thoughts on assignment operator

From: Kin-man Chung <kinman.chung_at_oracle.com>
Date: Thu, 22 Mar 2012 09:58:25 -0700

EL 3.0 introduces the assignment operator =. So instead of calling the
ValueExpression.setValue method to assign values to a bean or bean
property, the assignment operator can be used instead, like

     x = 5
     employee.name = "Mike Ball"

What's new here is the ability to define new variables with the
operator. My original idea was to use EL variables for the target of
the assignment. Suppose the "x" in "x=5" has not been defined (as a
bean resolvable by an ELResolver, or in a VariableMapper), it will be
defined as an EL variable with the expression "5". All seems well.

The problem I have now is when it is used with the semicolon operator, like

     x=5; x+1

When x is not defined, I am getting a 1 instead of a 6. The reason for
this surprising result is because EL variables are resolved and bound at
parse time, and not at evaluation time. This seems to be counter
intuitive, and in some way defeats the usefulness of the ; operator.

So instead, I am leaning towards creating locally a bean with the name
(same as the lhs of the assignment). We do have the concept of local
bean repository in StandardELContext, but not in ELContext. To get this
to work for a non stand-alone environment, like in a JSP page, we need
do this some other way. Since beans are managed with ELResolvers,
maybe we can add a method to it. So I am proposing the following.

class ELResolver {
     /**
       * Attempt to set the value of the given property object on the
given base object.
       * If the (base, property) pair is not currently resolved by this
ELResolver, it can
       * provide the assignment functionality so that a call to getValue
on the
       * (base, property) will be resolved to the given value.
       *
       * If this ELResolver cannot provide such assignment
functionality, it should
       * return without setting the propertyResolved property.
       *
       * To support the EL assignment operator, there should be one
ELResolver in
       * the ELResolver chain that supports the assignment. Otherwise, a
       * ProperNotWritbleException will be thrown.
       */
     public void assign(ELContext context, Object base, Object property);
}

For our use, base will be null, but there can be some interesting usages
for non-null bases. In a JSP page, one can implement such assignment in
a PageContext, enabling definitions of PageContext attributes in an EL
expression.

Thoughts and comments? I'll try to implement this to see if there are
issues.

--Kin-man

BTW, I've submitted a early draft of the spec to the JCP on Monday.
Hopefully it'll be posted next week..