Copilot commented on code in PR #402:
URL: https://github.com/apache/commons-jexl/pull/402#discussion_r3313929846
##########
src/main/java/org/apache/commons/jexl3/JexlArithmetic.java:
##########
@@ -1005,6 +1007,37 @@ protected Object increment(final Object val, final int
incr) {
throw new ArithmeticException("Object "+(incr < 0?
"decrement":"increment")+":(" + val + ")");
}
+ /**
+ * Evaluates a supplier argument, eventually catching and logging any
JexlException.
+ *
+ * <p>Used primarily by {@link #empty(Object)} and {@link #size(Object)}
to guard argument evaluation.
+ * If evaluation succeeds, returns the supplier's result. If a {@link
JexlException} occurs, logs a
+ * warning and returns the supplier itself.</p>
+ *
+ * <p>This method is public to allow overriding. Implementations that
change the exception handling
+ * behavior must still return the supplier on failure to maintain the
contract.</p>
+ *
+ * @param logger the logger for warning messages; may be null
+ * @param arg the supplier providing the argument to evaluate
+ * @return the evaluated result on success, or the supplier itself on
JexlException
+ * @since 3.6.3
+ */
+ public Object evaluate(final Log logger, final Supplier<Object> arg) {
+ try {
+ return arg.get();
+ } catch (final JexlException e) {
+ if (logger != null && logger.isWarnEnabled()) {
+ Throwable t = e.getCause();
+ if (t == null) {
+ logger.warn(e.getMessage(), e.clean());
+ } else {
+ logger.warn(e.getMessage() + ", " + t.getMessage(),
JexlException.clean(t));
Review Comment:
In evaluate(...), the warn message is built as `e.getMessage() + ", " +
t.getMessage()`. If the cause message is null (common for NPE/IOOBE), this will
log an unhelpful trailing ", null" and can also duplicate information already
present in the Throwable passed to the logger. Consider logging
`e.getMessage()` as-is and relying on the Throwable argument for details (or
only appending the cause message when it’s non-null).
##########
src/main/java/org/apache/commons/jexl3/JexlArithmetic.java:
##########
@@ -1005,6 +1007,37 @@ protected Object increment(final Object val, final int
incr) {
throw new ArithmeticException("Object "+(incr < 0?
"decrement":"increment")+":(" + val + ")");
}
+ /**
+ * Evaluates a supplier argument, eventually catching and logging any
JexlException.
+ *
+ * <p>Used primarily by {@link #empty(Object)} and {@link #size(Object)}
to guard argument evaluation.
+ * If evaluation succeeds, returns the supplier's result. If a {@link
JexlException} occurs, logs a
+ * warning and returns the supplier itself.</p>
+ *
+ * <p>This method is public to allow overriding. Implementations that
change the exception handling
+ * behavior must still return the supplier on failure to maintain the
contract.</p>
+ *
+ * @param logger the logger for warning messages; may be null
+ * @param arg the supplier providing the argument to evaluate
+ * @return the evaluated result on success, or the supplier itself on
JexlException
+ * @since 3.6.3
+ */
+ public Object evaluate(final Log logger, final Supplier<Object> arg) {
+ try {
+ return arg.get();
+ } catch (final JexlException e) {
+ if (logger != null && logger.isWarnEnabled()) {
+ Throwable t = e.getCause();
+ if (t == null) {
+ logger.warn(e.getMessage(), e.clean());
+ } else {
+ logger.warn(e.getMessage() + ", " + t.getMessage(),
JexlException.clean(t));
+ }
+ }
+ }
+ return arg;
+ }
Review Comment:
evaluate(...) returns the Supplier instance as a sentinel on failure, and
Interpreter relies on an identity check (`value == eval`) to detect that case.
Since this method is public API, a caller could legitimately supply a Supplier
that returns itself, making success indistinguishable from failure, and
overrides could accidentally break the identity-based contract. Consider using
a dedicated sentinel value (e.g., a private/static constant) for the failure
case (and checking against that), or narrowing visibility (e.g., protected) if
it’s intended only for overriding in custom arithmetic implementations.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]