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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 94b8b5fa2b Reduce a little bit the needs to use reflection for getting 
code list values.
94b8b5fa2b is described below

commit 94b8b5fa2ba29721e3010d67947b3a6ab9fd6c76
Author: Martin Desruisseaux <[email protected]>
AuthorDate: Tue Apr 14 11:54:09 2026 +0200

    Reduce a little bit the needs to use reflection for getting code list 
values.
---
 .../org/apache/sis/util/collection/CodeListSet.java   | 19 +++++++++++++++++--
 .../apache/sis/util/internal/shared/CodeLists.java    |  4 ++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git 
a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/collection/CodeListSet.java
 
b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/collection/CodeListSet.java
index 6a8325fff4..4221818af5 100644
--- 
a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/collection/CodeListSet.java
+++ 
b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/collection/CodeListSet.java
@@ -175,9 +175,24 @@ public class CodeListSet<E extends CodeList<E>> extends 
AbstractSet<E>
      */
     private E valueOf(final int ordinal) {
         E[] array = codes;
-        if (array == null || ordinal >= array.length) {
-            codes = array = POOL.unique(CodeLists.values(elementType));
+        if (array != null) {
+            if (ordinal < array.length) {
+                return array[ordinal];
+            }
+            // Prefer `CodeList.family()` rather than 
`CodeLists.values(Class)`.
+            for (int i = array.length; --i >= 0;) {
+                final E code = array[i];
+                if (code.getClass() == elementType) {
+                    array = code.family();
+                    break;
+                }
+            }
+        }
+        if (array == null) {
+            // Fragile fallback: it uses reflection and relies on a convention.
+            array = CodeLists.values(elementType);
         }
+        codes = array = POOL.unique(array);
         return array[ordinal];
     }
 
diff --git 
a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/internal/shared/CodeLists.java
 
b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/internal/shared/CodeLists.java
index 262525d49a..3989d5d35b 100644
--- 
a/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/internal/shared/CodeLists.java
+++ 
b/endorsed/src/org.apache.sis.util/main/org/apache/sis/util/internal/shared/CodeLists.java
@@ -163,6 +163,10 @@ public final class CodeLists {
      * This method delegates to the public static {@code values()} method.
      * If that method is not found, an empty list is returned.
      *
+     * <p>Note that this method is fragile: is uses reflection and relies on a 
convention
+     * (expected contract of {@code values()}) which is not enforced by the 
Java language.
+     * Callers should prefer {@link CodeList#family()} as much as possible.</p>
+     *
      * @param  <T>       the compile-time type given as the {@code codeType} 
parameter.
      * @param  codeType  the type of code list or enumeration.
      * @return the list of values for the given code list or enumeration, or 
an empty array if none.

Reply via email to