sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 02/02: Catch some exceptions related to number conversions. Either rethrow them or log them. Note: the decision to require Expression.evaluate(Object, Class) to return null in case of error is questionable. We should revisit that policy when we will review the org.opengis.filter interfaces.
Date Mon, 10 Jun 2019 15:01:34 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 4610d2eac246c991eb65e69d8ff39929aca88f8b
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Mon Jun 10 15:52:47 2019 +0200

    Catch some exceptions related to number conversions. Either rethrow them or log them.
    Note: the decision to require Expression.evaluate(Object, Class) to return null in case
of error is questionable.
    We should revisit that policy when we will review the org.opengis.filter interfaces.
---
 .../main/java/org/apache/sis/filter/ArithmeticFunction.java   | 11 +++++++++--
 .../src/main/java/org/apache/sis/filter/LeafExpression.java   |  4 ++--
 .../sis-feature/src/main/java/org/apache/sis/filter/Node.java |  5 +++--
 .../org/apache/sis/internal/converter/NumberConverter.java    |  7 ++++++-
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/filter/ArithmeticFunction.java
b/core/sis-feature/src/main/java/org/apache/sis/filter/ArithmeticFunction.java
index 598ba82..ebacd88 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/ArithmeticFunction.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/ArithmeticFunction.java
@@ -21,6 +21,7 @@ import java.math.BigInteger;
 import org.apache.sis.util.Numbers;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.internal.feature.FeatureExpression;
+import org.apache.sis.internal.util.Numerics;
 import org.apache.sis.math.Fraction;
 
 // Branch-dependent imports
@@ -94,14 +95,20 @@ abstract class ArithmeticFunction extends BinaryFunction implements BinaryExpres
     @SuppressWarnings("unchecked")
     public final <T> T evaluate(final Object feature, final Class<T> target)
{
         ArgumentChecks.ensureNonNull("target", target);
-        if (Number.class.isAssignableFrom(target)) {
+        if (Number.class.isAssignableFrom(target)) try {
             final Number left = (Number) expression1.evaluate(feature, target);
             if (left != null) {
                 final Number right = (Number) expression2.evaluate(feature, target);
                 if (right != null) {
-                    return (T) Numbers.cast(apply(left, right), (Class<? extends Number>)
target);
+                    final Number result = apply(left, right);
+                    final Number casted = Numbers.cast(result, (Class<? extends Number>)
target);
+                    if (Numerics.equals(result.doubleValue(), casted.doubleValue())) {
+                        return (T) casted;
+                    }
                 }
             }
+        } catch (ArithmeticException e) {
+            warning(e);
         }
         return null;
     }
diff --git a/core/sis-feature/src/main/java/org/apache/sis/filter/LeafExpression.java b/core/sis-feature/src/main/java/org/apache/sis/filter/LeafExpression.java
index 42aec25..84027cf 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/LeafExpression.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/LeafExpression.java
@@ -80,7 +80,7 @@ abstract class LeafExpression extends Node implements Expression, FeatureExpress
         try {
             return ObjectConverters.convert(value, target);
         } catch (UnconvertibleObjectException e) {
-            warning("evaluate", e);
+            warning(e);
             return null;                    // As per method contract.
         }
     }
@@ -137,7 +137,7 @@ abstract class LeafExpression extends Node implements Expression, FeatureExpress
             if (candidate instanceof Feature) try {
                 return ((Feature) candidate).getPropertyValue(name);
             } catch (PropertyNotFoundException ex) {
-                warning("evaluate", ex);
+                warning(ex);
                 // Null will be returned below.
             } else if (candidate instanceof Map<?,?>) {
                 return ((Map<?,?>) candidate).get(name);
diff --git a/core/sis-feature/src/main/java/org/apache/sis/filter/Node.java b/core/sis-feature/src/main/java/org/apache/sis/filter/Node.java
index f999f9a..1b9d552 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/Node.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/Node.java
@@ -162,10 +162,11 @@ abstract class Node implements Serializable {
 
     /**
      * Reports that an operation failed because of the given exception.
+     * This method assumes that the warning occurred in an {@code evaluate(…)} method.
      *
      * @todo Consider defining a {@code Context} class providing, among other information,
listeners where to report warnings.
      */
-    final void warning(final String caller, final Exception e) {
-        Logging.recoverableException(Logging.getLogger(Loggers.FILTER), getClass(), caller,
e);
+    final void warning(final Exception e) {
+        Logging.recoverableException(Logging.getLogger(Loggers.FILTER), getClass(), "evaluate",
e);
     }
 }
diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/converter/NumberConverter.java
b/core/sis-utility/src/main/java/org/apache/sis/internal/converter/NumberConverter.java
index 023c6dc..6d4d7b8 100644
--- a/core/sis-utility/src/main/java/org/apache/sis/internal/converter/NumberConverter.java
+++ b/core/sis-utility/src/main/java/org/apache/sis/internal/converter/NumberConverter.java
@@ -114,7 +114,12 @@ final class NumberConverter<S extends Number, T extends Number>
extends SystemCo
     @Override
     public T apply(final S source) {
         final double sourceValue = source.doubleValue();
-        T target = Numbers.cast(source, targetClass);
+        T target;
+        try {
+            target = Numbers.cast(source, targetClass);
+        } catch (IllegalArgumentException e) {
+            throw new UnconvertibleObjectException(formatErrorMessage(source), e);
+        }
         final double targetValue = target.doubleValue();
         if (Double.doubleToLongBits(targetValue) != Double.doubleToLongBits(sourceValue))
{
             /*


Mime
View raw message