You are viewing a plain text version of this content. The canonical link for it is here.
Posted to taglibs-user@tomcat.apache.org by "David M. Karr" <dm...@earthlink.net> on 2002/10/13 20:05:38 UTC

ExpressionUtil.evalNotNull doesn't return NAE on null value

I'm wondering about the following block of code, the entire body of
"ExpressionUtil.evalNotNull()":

-----------
        if (expression != null) {
            Object r = ExpressionEvaluatorManager.evaluate(
                attributeName, expression, expectedType, tag, pageContext);
            if (r == null)
                throw new NullAttributeException(tagName, attributeName);
	    return r;
        } else
	    return null;
-----------

In particular, I see that the NullAttributeException is thrown if the
expression string was not null, but the resulting value was null.  However, if
the expression string was null, it just returns null.  Shawn, could you explain
the motivation for this?  I'm wondering why it doesn't throw the NAE if the
expression string was null.

-- 
===================================================================
David M. Karr          ; Java/J2EE/XML/Unix/C++
dmkarr@earthlink.net


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ExpressionUtil.evalNotNull doesn't return NAE on null value

Posted by "David M. Karr" <dm...@earthlink.net>.
>>>>> "Shawn" == Shawn Bayern <ba...@essentially.net> writes:

    Shawn> On 13 Oct 2002, David M. Karr wrote:
    >> In particular, I see that the NullAttributeException is thrown if the
    >> expression string was not null, but the resulting value was null.  
    >> However, if the expression string was null, it just returns null.  
    >> Shawn, could you explain the motivation for this?  I'm wondering why
    >> it doesn't throw the NAE if the expression string was null.

    Shawn> In some cases, there's a difference between an attribute not being
    Shawn> specified (which might be allowed) and evaluating to null (which might
    Shawn> need to be reported as an error).  Including this check in this particular
    Shawn> utility method is arbitrary, but it allowed us to factor out logic
    Shawn> conveniently.

    Shawn> For instance, 'escapeXml' in <c:out> is one such attribute.  For this
    Shawn> attribute, we can simply write

    Shawn>   boolean escapeXml = true;
    Shawn>   Boolean escape = ((Boolean) ExpressionUtil.evalNotNull(
    Shawn>     "out", "escapeXml", escapeXml_, Boolean.class, this, pageContext));
    Shawn>   if (escape != null)
    Shawn>     escapeXml = escape.booleanValue();

    Shawn> and let the NullAttributeException propagate.

    Shawn> Of course, we could have simply checked for this outside the method, too.  
    Shawn> Some of this code grew organically, changing slightly rather than being
    Shawn> completely rewritten as the spec was updated.  While I don't have a
    Shawn> problem with the current evalNotNull() method, there are certainly other
    Shawn> ways we could have organized it.  For instance, we could have used
    Shawn> type-specific methods, with 'allowNull' as a boolean attribute.  But
    Shawn> "evaluate but do not produce null" was a common enough case that the
    Shawn> method seems useful.

    Shawn> Did that answer your question?

Well enough, thanks.

-- 
===================================================================
David M. Karr          ; Java/J2EE/XML/Unix/C++
dmkarr@earthlink.net


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: ExpressionUtil.evalNotNull doesn't return NAE on null value

Posted by Shawn Bayern <ba...@essentially.net>.
On 13 Oct 2002, David M. Karr wrote:

> In particular, I see that the NullAttributeException is thrown if the
> expression string was not null, but the resulting value was null.  
> However, if the expression string was null, it just returns null.  
> Shawn, could you explain the motivation for this?  I'm wondering why
> it doesn't throw the NAE if the expression string was null.

In some cases, there's a difference between an attribute not being
specified (which might be allowed) and evaluating to null (which might
need to be reported as an error).  Including this check in this particular
utility method is arbitrary, but it allowed us to factor out logic
conveniently.

For instance, 'escapeXml' in <c:out> is one such attribute.  For this
attribute, we can simply write

  boolean escapeXml = true;
  Boolean escape = ((Boolean) ExpressionUtil.evalNotNull(
    "out", "escapeXml", escapeXml_, Boolean.class, this, pageContext));
  if (escape != null)
    escapeXml = escape.booleanValue();

and let the NullAttributeException propagate.

Of course, we could have simply checked for this outside the method, too.  
Some of this code grew organically, changing slightly rather than being
completely rewritten as the spec was updated.  While I don't have a
problem with the current evalNotNull() method, there are certainly other
ways we could have organized it.  For instance, we could have used
type-specific methods, with 'allowNull' as a boolean attribute.  But
"evaluate but do not produce null" was a common enough case that the
method seems useful.

Did that answer your question?

Shawn


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>