You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2022/04/27 04:55:32 UTC

[isis] branch master updated (97ccfebdd2 -> d1ee66a618)

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


    from 97ccfebdd2 Bump vaadin.version from 23.0.6 to 23.0.7
     new 15e5f3f490 ISIS-2297: fix potential NPE with util Enums
     new d1ee66a618 ISIS-2297: guard decimal number parsing against overflow

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../java/org/apache/isis/applib/util/Enums.java    |  2 +-
 .../apache/isis/applib/value/semantics/Parser.java |  4 +-
 .../value/semantics/ValueSemanticsAbstract.java    | 14 +++--
 .../isis/commons/internal/primitives/_Doubles.java | 70 ++++++++++++++++++++++
 .../_ThreadSleep.java => primitives/_Floats.java}  | 45 ++++++++------
 .../valuesemantics/BigDecimalValueSemantics.java   |  3 +-
 .../valuesemantics/BigIntegerValueSemantics.java   |  3 +-
 .../valuesemantics/ByteValueSemantics.java         |  5 +-
 .../valuesemantics/DoubleValueSemantics.java       | 22 +++----
 .../valuesemantics/FloatValueSemantics.java        | 10 +---
 .../valuesemantics/IntValueSemantics.java          |  5 +-
 .../valuesemantics/LongValueSemantics.java         |  5 +-
 .../valuesemantics/ShortValueSemantics.java        |  5 +-
 13 files changed, 142 insertions(+), 51 deletions(-)
 create mode 100644 commons/src/main/java/org/apache/isis/commons/internal/primitives/_Doubles.java
 copy commons/src/main/java/org/apache/isis/commons/internal/{concurrent/_ThreadSleep.java => primitives/_Floats.java} (53%)


[isis] 02/02: ISIS-2297: guard decimal number parsing against overflow

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit d1ee66a61896579f71011d25ff856b82e7d57bfe
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Apr 27 06:55:25 2022 +0200

    ISIS-2297: guard decimal number parsing against overflow
    
    - also code quality: make null cases more explicit (value semantics)
---
 .../apache/isis/applib/value/semantics/Parser.java |  4 +-
 .../value/semantics/ValueSemanticsAbstract.java    | 14 +++--
 .../isis/commons/internal/primitives/_Doubles.java | 70 ++++++++++++++++++++++
 .../isis/commons/internal/primitives/_Floats.java  | 67 +++++++++++++++++++++
 .../valuesemantics/BigDecimalValueSemantics.java   |  3 +-
 .../valuesemantics/BigIntegerValueSemantics.java   |  3 +-
 .../valuesemantics/ByteValueSemantics.java         |  5 +-
 .../valuesemantics/DoubleValueSemantics.java       | 22 +++----
 .../valuesemantics/FloatValueSemantics.java        | 10 +---
 .../valuesemantics/IntValueSemantics.java          |  5 +-
 .../valuesemantics/LongValueSemantics.java         |  5 +-
 .../valuesemantics/ShortValueSemantics.java        |  5 +-
 12 files changed, 180 insertions(+), 33 deletions(-)

diff --git a/api/applib/src/main/java/org/apache/isis/applib/value/semantics/Parser.java b/api/applib/src/main/java/org/apache/isis/applib/value/semantics/Parser.java
index 760baefe64..4cea22359e 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/value/semantics/Parser.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/value/semantics/Parser.java
@@ -18,6 +18,8 @@
  */
 package org.apache.isis.applib.value.semantics;
 
+import org.springframework.lang.Nullable;
+
 import org.apache.isis.commons.internal.exceptions._Exceptions;
 
 /**
@@ -65,7 +67,6 @@ import org.apache.isis.commons.internal.exceptions._Exceptions;
  * the object reflectively.
  *
  * @see DefaultsProvider
- * @see EncoderDecoder
  * @see ValueSemanticsProvider
  * @since 1.x {@index}
  */
@@ -86,6 +87,7 @@ public interface Parser<T> {
     /**
      * Parses a string to an instance of the object. (bijective)
      */
+    @Nullable
     T parseTextRepresentation(ValueSemanticsProvider.Context context, String text);
 
     /**
diff --git a/api/applib/src/main/java/org/apache/isis/applib/value/semantics/ValueSemanticsAbstract.java b/api/applib/src/main/java/org/apache/isis/applib/value/semantics/ValueSemanticsAbstract.java
index 80fdf47390..09e7efad99 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/value/semantics/ValueSemanticsAbstract.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/value/semantics/ValueSemanticsAbstract.java
@@ -182,6 +182,7 @@ implements
      * this is typically overruled later by implementations of
      * {@link #configureDecimalFormat(org.apache.isis.applib.adapters.ValueSemanticsProvider.Context, DecimalFormat) configureDecimalFormat}
      */
+   @SuppressWarnings("javadoc")
    protected DecimalFormat getNumberFormat(final @Nullable ValueSemanticsProvider.Context context) {
         val format = (DecimalFormat)NumberFormat.getNumberInstance(getUserLocale(context).getNumberFormatLocale());
         // prime w/ 16 (64 bit IEEE 754 double has 15 decimal digits of precision)
@@ -189,26 +190,27 @@ implements
         return format;
     }
 
-    protected @Nullable BigInteger parseInteger(
+    protected Optional<BigInteger> parseInteger(
             final @Nullable ValueSemanticsProvider.Context context,
             final @Nullable String text) {
         val input = _Strings.blankToNullOrTrim(text);
         if(input==null) {
-            return null;
+            return Optional.empty();
         }
         try {
-            return parseDecimal(context, input).toBigIntegerExact();
+            return parseDecimal(context, input)
+            .map(BigDecimal::toBigIntegerExact);
         } catch (final NumberFormatException | ArithmeticException e) {
             throw new TextEntryParseException("Not an integer value " + text, e);
         }
     }
 
-    protected @Nullable BigDecimal parseDecimal(
+    protected Optional<BigDecimal> parseDecimal(
             final @Nullable ValueSemanticsProvider.Context context,
             final @Nullable String text) {
         val input = _Strings.blankToNullOrTrim(text);
         if(input==null) {
-            return null;
+            return Optional.empty();
         }
         val format = getNumberFormat(context);
         format.setParseBigDecimal(true);
@@ -230,7 +232,7 @@ implements
                         "No more than %d digits can be entered after the decimal separator, "
                         + "got %d in '%s'.", maxFractionDigits, number.scale(), input));
             }
-            return number;
+            return Optional.of(number);
         } catch (final NumberFormatException | ParseException e) {
             throw new TextEntryParseException(String.format(
                     "Not a decimal value '%s': %s", input, e.getMessage()),
diff --git a/commons/src/main/java/org/apache/isis/commons/internal/primitives/_Doubles.java b/commons/src/main/java/org/apache/isis/commons/internal/primitives/_Doubles.java
new file mode 100644
index 0000000000..02ae21859f
--- /dev/null
+++ b/commons/src/main/java/org/apache/isis/commons/internal/primitives/_Doubles.java
@@ -0,0 +1,70 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.commons.internal.primitives;
+
+import java.math.BigDecimal;
+import java.util.Optional;
+import java.util.OptionalDouble;
+
+import lombok.NonNull;
+import lombok.val;
+import lombok.experimental.UtilityClass;
+
+/**
+ * <h1>- internal use only -</h1>
+ * <p>
+ * Double Utility
+ * </p>
+ * <p>
+ * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
+ * These may be changed or removed without notice!
+ * </p>
+ *
+ * @since 2.0
+ */
+@UtilityClass
+public class _Doubles {
+
+    /**
+     * Ignores precision loss.
+     * @throws ArithmeticException
+     *      if {@code decimal} cannot not be approximated by {@link Double}.
+     * @apiNote specifically not using {@link OptionalDouble} as result type here,
+     * based on how this method is used from value semantics
+     */
+    public Optional<Double> convertToDouble(final @NonNull Optional<BigDecimal> decimal) {
+        return decimal
+                .map(_Doubles::convertToDouble);
+    }
+
+    /**
+     * Ignores precision loss.
+     * @throws ArithmeticException
+     *      if {@code decimal} cannot not be approximated by {@link Double}.
+     */
+    public double convertToDouble(final @NonNull BigDecimal decimal) {
+        val doubleValue = decimal.doubleValue();
+        // overflow detection
+        if(!Double.isInfinite(doubleValue)) {
+            throw new ArithmeticException("non finite BigDecimal to double conversion");
+        }
+        return doubleValue;
+    }
+
+}
diff --git a/commons/src/main/java/org/apache/isis/commons/internal/primitives/_Floats.java b/commons/src/main/java/org/apache/isis/commons/internal/primitives/_Floats.java
new file mode 100644
index 0000000000..432ff58257
--- /dev/null
+++ b/commons/src/main/java/org/apache/isis/commons/internal/primitives/_Floats.java
@@ -0,0 +1,67 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.commons.internal.primitives;
+
+import java.math.BigDecimal;
+import java.util.Optional;
+
+import lombok.NonNull;
+import lombok.val;
+import lombok.experimental.UtilityClass;
+
+/**
+ * <h1>- internal use only -</h1>
+ * <p>
+ * t Utility
+ * </p>
+ * <p>
+ * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
+ * These may be changed or removed without notice!
+ * </p>
+ *
+ * @since 2.0
+ */
+@UtilityClass
+public class _Floats {
+
+    /**
+     * Ignores precision loss.
+     * @throws ArithmeticException
+     *      if {@code decimal} cannot not be approximated by {@link Float}.
+     */
+    public Optional<Float> convertToFloat(final @NonNull Optional<BigDecimal> decimal) {
+        return decimal
+                .map(_Floats::convertToFloat);
+    }
+
+    /**
+     * Ignores precision loss.
+     * @throws ArithmeticException
+     *      if {@code decimal} cannot not be approximated by {@link Double}.
+     */
+    public float convertToFloat(final @NonNull BigDecimal decimal) {
+        val floatValue = decimal.floatValue();
+        // overflow detection
+        if(!Float.isInfinite(floatValue)) {
+            throw new ArithmeticException("non finite BigDecimal to float conversion");
+        }
+        return floatValue;
+    }
+
+}
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/BigDecimalValueSemantics.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/BigDecimalValueSemantics.java
index 55e954a9f4..8256c8b417 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/BigDecimalValueSemantics.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/BigDecimalValueSemantics.java
@@ -101,7 +101,8 @@ implements
 
     @Override
     public BigDecimal parseTextRepresentation(final ValueSemanticsProvider.Context context, final String text) {
-        return super.parseDecimal(context, text);
+        return super.parseDecimal(context, text)
+                .orElse(null);
     }
 
     @Override
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/BigIntegerValueSemantics.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/BigIntegerValueSemantics.java
index 3184ac25d1..032780b829 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/BigIntegerValueSemantics.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/BigIntegerValueSemantics.java
@@ -90,7 +90,8 @@ implements
 
     @Override
     public BigInteger parseTextRepresentation(final Context context, final String text) {
-        return super.parseInteger(context, text);
+        return super.parseInteger(context, text)
+                .orElse(null);
     }
 
     @Override
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/ByteValueSemantics.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/ByteValueSemantics.java
index 63e065bffd..6a987b2545 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/ByteValueSemantics.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/ByteValueSemantics.java
@@ -18,6 +18,7 @@
  */
 package org.apache.isis.core.metamodel.valuesemantics;
 
+import java.math.BigInteger;
 import java.util.function.UnaryOperator;
 
 import javax.inject.Named;
@@ -101,7 +102,9 @@ implements
             return null;
         }
         try {
-            return super.parseInteger(context, input).byteValueExact();
+            return super.parseInteger(context, input)
+                    .map(BigInteger::byteValueExact)
+                    .orElse(null);
         } catch (final NumberFormatException | ArithmeticException e) {
             throw new TextEntryParseException("Not a 8-bit signed integer " + input, e);
         }
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/DoubleValueSemantics.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/DoubleValueSemantics.java
index 6564d7c73b..ff4d495373 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/DoubleValueSemantics.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/DoubleValueSemantics.java
@@ -30,11 +30,10 @@ import org.apache.isis.applib.value.semantics.Renderer;
 import org.apache.isis.applib.value.semantics.ValueDecomposition;
 import org.apache.isis.applib.value.semantics.ValueSemanticsAbstract;
 import org.apache.isis.commons.collections.Can;
+import org.apache.isis.commons.internal.primitives._Doubles;
 import org.apache.isis.schema.common.v2.ValueType;
 import org.apache.isis.schema.common.v2.ValueWithTypeDto;
 
-import lombok.val;
-
 /**
  * due to auto-boxing also handles the primitive variant
  */
@@ -86,30 +85,27 @@ implements
 
     @Override
     public String parseableTextRepresentation(final Context context, final Double value) {
-        return value==null
-                ? null
-                : getNumberFormat(context)
-                    .format(value);
+        return value!=null
+                ? getNumberFormat(context)
+                    .format(value)
+                : null;
     }
 
     @Override
     public Double parseTextRepresentation(final Context context, final String text) {
-        //TODO at least overflow should be detected
-        val bigDec = super.parseDecimal(context, text);
-        return bigDec!=null
-                ? bigDec.doubleValue() // simply ignoring loss of precision or overflow
-                : null;
+        return _Doubles.convertToDouble(super.parseDecimal(context, text))
+                .orElse(null);
     }
 
     @Override
     public int typicalLength() {
-        //TODO research - legacy value, what motivates this number?
+        //XXX research - legacy value, what motivates this number?
         return 10;
     }
 
     @Override
     public int maxLength() {
-        //TODO research - legacy value, what motivates this number?
+        //XXX research - legacy value, what motivates this number?
         return 25;
     }
 
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/FloatValueSemantics.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/FloatValueSemantics.java
index 59866e9c9b..03ef5f1799 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/FloatValueSemantics.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/FloatValueSemantics.java
@@ -30,11 +30,10 @@ import org.apache.isis.applib.value.semantics.Renderer;
 import org.apache.isis.applib.value.semantics.ValueDecomposition;
 import org.apache.isis.applib.value.semantics.ValueSemanticsAbstract;
 import org.apache.isis.commons.collections.Can;
+import org.apache.isis.commons.internal.primitives._Floats;
 import org.apache.isis.schema.common.v2.ValueType;
 import org.apache.isis.schema.common.v2.ValueWithTypeDto;
 
-import lombok.val;
-
 /**
  * due to auto-boxing also handles the primitive variant
  */
@@ -94,11 +93,8 @@ implements
 
     @Override
     public Float parseTextRepresentation(final Context context, final String text) {
-        //TODO at least overflow should be detected
-        val bigDec = super.parseDecimal(context, text);
-        return bigDec!=null
-                ? bigDec.floatValue() // simply ignoring loss of precision or overflow
-                : null;
+        return _Floats.convertToFloat(super.parseDecimal(context, text))
+                .orElse(null);
     }
 
     @Override
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/IntValueSemantics.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/IntValueSemantics.java
index 76a9675f4f..8019e1236f 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/IntValueSemantics.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/IntValueSemantics.java
@@ -18,6 +18,7 @@
  */
 package org.apache.isis.core.metamodel.valuesemantics;
 
+import java.math.BigInteger;
 import java.util.function.UnaryOperator;
 
 import javax.inject.Named;
@@ -101,7 +102,9 @@ implements
             return null;
         }
         try {
-            return super.parseInteger(context, input).intValueExact();
+            return super.parseInteger(context, input)
+                    .map(BigInteger::intValueExact)
+                    .orElse(null);
         } catch (final NumberFormatException | ArithmeticException e) {
             throw new TextEntryParseException("Not a 32-bit signed integer " + input, e);
         }
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/LongValueSemantics.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/LongValueSemantics.java
index 3ad6a7bb67..747f5f7f15 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/LongValueSemantics.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/LongValueSemantics.java
@@ -18,6 +18,7 @@
  */
 package org.apache.isis.core.metamodel.valuesemantics;
 
+import java.math.BigInteger;
 import java.util.function.UnaryOperator;
 
 import javax.inject.Named;
@@ -101,7 +102,9 @@ implements
             return null;
         }
         try {
-            return super.parseInteger(context, input).longValueExact();
+            return super.parseInteger(context, input)
+                    .map(BigInteger::longValueExact)
+                    .orElse(null);
         } catch (final NumberFormatException | ArithmeticException e) {
             throw new TextEntryParseException("Not a 64-bit signed integer " + input, e);
         }
diff --git a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/ShortValueSemantics.java b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/ShortValueSemantics.java
index a9575acfc2..0b81b614af 100644
--- a/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/ShortValueSemantics.java
+++ b/core/metamodel/src/main/java/org/apache/isis/core/metamodel/valuesemantics/ShortValueSemantics.java
@@ -18,6 +18,7 @@
  */
 package org.apache.isis.core.metamodel.valuesemantics;
 
+import java.math.BigInteger;
 import java.util.function.UnaryOperator;
 
 import javax.inject.Named;
@@ -101,7 +102,9 @@ implements
             return null;
         }
         try {
-            return super.parseInteger(context, input).shortValueExact();
+            return super.parseInteger(context, input)
+                    .map(BigInteger::shortValueExact)
+                    .orElse(null);
         } catch (final NumberFormatException | ArithmeticException e) {
             throw new TextEntryParseException("Not a 16-bit signed integer " + input, e);
         }


[isis] 01/02: ISIS-2297: fix potential NPE with util Enums

Posted by ah...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 15e5f3f4906f5b58c31a0450909c4c372e0aaf51
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Apr 27 06:07:01 2022 +0200

    ISIS-2297: fix potential NPE with util Enums
---
 api/applib/src/main/java/org/apache/isis/applib/util/Enums.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/api/applib/src/main/java/org/apache/isis/applib/util/Enums.java b/api/applib/src/main/java/org/apache/isis/applib/util/Enums.java
index c01e21858c..c7aedc5844 100644
--- a/api/applib/src/main/java/org/apache/isis/applib/util/Enums.java
+++ b/api/applib/src/main/java/org/apache/isis/applib/util/Enums.java
@@ -91,7 +91,7 @@ public final class Enums {
             final @Nullable String text) {
         val input = _Strings.blankToNullOrTrim(text);
         if(input==null) {
-            return null;
+            return Optional.empty();
         }
         final T[] enumConstants = correspondingClass.getEnumConstants();
         for (final T enumConstant : enumConstants) {