diff --git a/.classpath b/.classpath index dd3c948b..59b1c7f8 100644 --- a/.classpath +++ b/.classpath @@ -36,5 +36,6 @@ + diff --git a/CHANGELOG.md b/CHANGELOG.md index 3de9619c..0b6652e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,26 @@ ## Release Notes +`1.1.2` + +- Added `FieldPathParser` for JSON Path-style field path parsing and resolution (properties, indices, and map keys). +- Changed `Fields.Safe.getByPath` to resolve JSON Path-style expressions including array, list, and map selectors. +- Changed `Fields.Safe.getByPaths(Object, String)` to split alternate paths on commas outside bracket segments. +- Added `Fields.Unchecked.getByPath(Object, String)` for path lookup without argument validation. +- Added `Fields.Unchecked.getByPaths(Object, String)` for comma-separated path lookup without argument validation. +- Added `Fields.Unchecked.getByPaths(Object, String...)` for ordered path lookup without argument validation. +- Changed `Fields.IgnoreAccess.getByPaths(Object, String...)` to reject `null` arrays and skip `null` path entries. +- Changed `Fields.Safe.getByPaths(Object, String...)` to return `null` for `null` arrays and skip `null` path entries. +- Added `JavaObjects.getAt(Object, int)` to read a zero-based index from an array or list, returning `null` when the value cannot be read. +- Added `JavaArrays.safeGet(T[], int)` to return an array element at an index, or `null` when the array is `null` or the index is out of range. +- Added `Lists.safeGet(List, int)` to return a list element at an index, or `null` when the list is `null`, empty, or the index is out of range. + +--- + `1.1.1` -- Kept `Fields.IgnoreAccess.get(Object, String)` throwing `ReflectionException` for null input while using unchecked lookup for valid input. -- Changed `Fields.Unchecked.getOneDeclaredInHierarchy(Class, String)` to throw `NullPointerException` for a null class. -- Changed `Methods.Unchecked.getOneDeclaredInHierarchy(String, Class, Class...)` to throw `NullPointerException` for a null class. +- Kept `Fields.IgnoreAccess.get(Object, String)` throwing `ReflectionException` for `null` input while using unchecked lookup for valid input. +- Changed `Fields.Unchecked.getOneDeclaredInHierarchy(Class, String)` to throw `NullPointerException` for a `null` class. +- Changed `Methods.Unchecked.getOneDeclaredInHierarchy(String, Class, Class...)` to throw `NullPointerException` for a `null` class. - Fixed `Fields.Safe.getByPath(Object, String)` returning a field value for paths ending in a dot. - Added `Fields.Unchecked.getOneDeclared(Class, String)` for direct field lookup without parameter validation. - Added `Fields.Unchecked.getOneDeclaredInHierarchy(Class, String)` for direct field lookup throughout a class hierarchy. @@ -14,15 +30,15 @@ - Added `Fields.Unchecked.getAllDeclaredInHierarchy(Class)` for direct hierarchy field retrieval without parameter validation. - Added `Fields.Unchecked.getAllDeclaredInHierarchy(Class, Predicate)` for direct filtered hierarchy field retrieval without parameter validation. - Added `Fields.Unchecked.forEachMatching(Field[], Predicate, Consumer)` to pass matching declared fields to a consumer without allocating a result list. -- Added `Fields.Safe.getAllDeclared(Class)` for null-safe retrieval of a class's declared fields. -- Added `Fields.Safe.getAllDeclared(Object)` for null-safe retrieval of declared fields from an object's class. -- Added `Fields.Safe.getAllDeclared(Class, Predicate)` for null-safe retrieval and filtering of declared fields. -- Added `Fields.Safe.getAllDeclared(Object, Predicate)` for null-safe retrieval and filtering of declared fields from an object's class. -- Added `Fields.Safe.filter(List, Predicate)` for null-safe filtering of a field list. -- Added `Fields.Safe.getAllDeclaredInHierarchy(Class)` for null-safe hierarchy field retrieval. -- Added `Fields.Safe.getAllDeclaredInHierarchy(Class, Predicate)` for null-safe filtered hierarchy field retrieval. -- Added `Fields.Safe.getAllDeclaredInHierarchy(Object)` for null-safe hierarchy field retrieval from an object's class. -- Added `Fields.Safe.getAllDeclaredInHierarchy(Object, Predicate)` for null-safe filtered hierarchy field retrieval from an object's class. +- Added `Fields.Safe.getAllDeclared(Class)` for `null`-safe retrieval of a class's declared fields. +- Added `Fields.Safe.getAllDeclared(Object)` for `null`-safe retrieval of declared fields from an object's class. +- Added `Fields.Safe.getAllDeclared(Class, Predicate)` for `null`-safe retrieval and filtering of declared fields. +- Added `Fields.Safe.getAllDeclared(Object, Predicate)` for `null`-safe retrieval and filtering of declared fields from an object's class. +- Added `Fields.Safe.filter(List, Predicate)` for `null`-safe filtering of a field list. +- Added `Fields.Safe.getAllDeclaredInHierarchy(Class)` for `null`-safe hierarchy field retrieval. +- Added `Fields.Safe.getAllDeclaredInHierarchy(Class, Predicate)` for `null`-safe filtered hierarchy field retrieval. +- Added `Fields.Safe.getAllDeclaredInHierarchy(Object)` for `null`-safe hierarchy field retrieval from an object's class. +- Added `Fields.Safe.getAllDeclaredInHierarchy(Object, Predicate)` for `null`-safe filtered hierarchy field retrieval from an object's class. - Added `Methods.Unchecked.getOneDeclared(String, Class, Class...)` for direct method lookup without parameter validation. - Added `Methods.Unchecked.getOneDeclaredInHierarchy(String, Class, Class...)` for direct method lookup throughout a class hierarchy. - Added `Methods.Unchecked.getAllDeclared(Class)` for direct retrieval of a class's declared methods. @@ -30,10 +46,10 @@ - Added `Methods.Unchecked.getAllDeclaredInHierarchy(Class)` for direct superclass-chain method retrieval without parameter validation. - Added `Methods.Unchecked.getAllDeclaredInHierarchy(Class, Predicate)` for direct filtered superclass-chain method retrieval without parameter validation. - Added `Methods.Unchecked.forEachMatching(Method[], Predicate, Consumer)` to pass matching declared methods to a consumer without allocating a result list. -- Added `Methods.Safe.getAllDeclared(Class)` for null-safe retrieval of a class's declared methods. -- Added `Methods.Safe.getAllDeclared(Class, Predicate)` for null-safe retrieval and filtering of declared methods. -- Added `Methods.Safe.getAllDeclaredInHierarchy(Class)` for null-safe superclass-chain method retrieval. -- Added `Methods.Safe.getAllDeclaredInHierarchy(Class, Predicate)` for null-safe filtered superclass-chain method retrieval. +- Added `Methods.Safe.getAllDeclared(Class)` for `null`-safe retrieval of a class's declared methods. +- Added `Methods.Safe.getAllDeclared(Class, Predicate)` for `null`-safe retrieval and filtering of declared methods. +- Added `Methods.Safe.getAllDeclaredInHierarchy(Class)` for `null`-safe superclass-chain method retrieval. +- Added `Methods.Safe.getAllDeclaredInHierarchy(Class, Predicate)` for `null`-safe filtered superclass-chain method retrieval. - Changed `Methods.getAllDeclared(Class, Predicate)` to return matching methods in declaration order. - Added `Classes.isArray(Class)` to check whether a class represents an array type. - Added `Classes.isNotArray(Class)` to check whether a class does not represent an array type. diff --git a/README.md b/README.md index 56475b57..fd69d313 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ A library that exposes general Java utility and reflection methods and a package ### Releases -Current release `1.1.1` +Current release `1.1.2` ### Documentation @@ -81,6 +81,6 @@ Maven: add this dependency to your `pom.xml` io.github.raduking morphix-all - 1.1.1 + 1.1.2 ``` diff --git a/pom.xml b/pom.xml index 4928ccc3..08cdd4ba 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ io.github.raduking morphix-all - 1.1.1 + 1.1.2 jar ${project.groupId}:${project.artifactId} diff --git a/src/main/java/org/morphix/convert/strategy/FinderPathStrategy.java b/src/main/java/org/morphix/convert/strategy/FinderPathStrategy.java index 2c7b428f..95a90ece 100644 --- a/src/main/java/org/morphix/convert/strategy/FinderPathStrategy.java +++ b/src/main/java/org/morphix/convert/strategy/FinderPathStrategy.java @@ -12,14 +12,10 @@ */ package org.morphix.convert.strategy; -import static org.morphix.reflection.ExtendedField.of; -import static org.morphix.reflection.JavaModifier.STATIC; - -import java.lang.reflect.Field; import java.util.List; import org.morphix.reflection.ExtendedField; -import org.morphix.reflection.Fields; +import org.morphix.reflection.FieldPathParser; /** * Finds the field in the source by path. @@ -44,41 +40,15 @@ public ExtendedField find(final T source, final List fields, } /** - * Returns the field and the object it corresponds to given its path. + * Returns the first field and its owning object from comma-separated alternative paths. * * @param object type * * @param obj object on which to search - * @param path path to the wanted field (object) + * @param path alternative paths to the wanted field * @return field object pair */ public static ExtendedField getConverterFieldByPathInHierarchy(final T obj, final String path) { - String[] fieldNames = path.split("\\."); - - Field resultField = null; - Object resultObject = obj; - Class currentClass = resultObject.getClass(); - - for (String fieldName : fieldNames) { - if (null != resultField && null != resultObject) { - resultObject = Fields.IgnoreAccess.get(resultObject, resultField); - } - resultField = Fields.getOneDeclaredInHierarchy(currentClass, fieldName); - if (null != resultField && STATIC.isPresentOn(resultField)) { - resultField = null; - } - if (null != resultField) { - currentClass = resultField.getType(); - } else { - resultObject = null; - } - if (null == resultField) { - break; - } - } - if (null == resultObject) { - return ExtendedField.EMPTY; - } - return of(resultField, resultObject); + return FieldPathParser.resolveFirstField(obj, path); } } diff --git a/src/main/java/org/morphix/lang/JavaArrays.java b/src/main/java/org/morphix/lang/JavaArrays.java index 4776a013..38e87641 100644 --- a/src/main/java/org/morphix/lang/JavaArrays.java +++ b/src/main/java/org/morphix/lang/JavaArrays.java @@ -145,4 +145,26 @@ static T[] empty(final Class componentType) { } return JavaObjects.cast(Array.newInstance(componentType, 0)); } + + /** + * Returns the element at the given index, or {@code null} if the array is {@code null} or the index is out of range. + *

+ * Will not throw a {@link NullPointerException} when the array is {@code null}, or an + * {@link ArrayIndexOutOfBoundsException} when the index is out of range. + * + * @param the component type of the array + * + * @param array the array to read, may be {@code null} + * @param index zero-based element index + * @return the element at {@code index}, or {@code null} when the array is {@code null} or the index is out of range + */ + static T safeGet(final T[] array, final int index) { + if (null == array || index < 0) { + return null; + } + if (index >= array.length) { + return null; + } + return array[index]; + } } diff --git a/src/main/java/org/morphix/lang/JavaObjects.java b/src/main/java/org/morphix/lang/JavaObjects.java index 9f769a58..068566f3 100644 --- a/src/main/java/org/morphix/lang/JavaObjects.java +++ b/src/main/java/org/morphix/lang/JavaObjects.java @@ -14,10 +14,13 @@ import java.lang.reflect.Array; import java.util.Collection; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; +import org.morphix.lang.collections.Lists; +import org.morphix.reflection.Classes; import org.morphix.reflection.Constructors; /** @@ -93,4 +96,33 @@ public static boolean isNotEmpty(final Object o) { public static boolean notEquals(final Object o1, final Object o2) { return !Objects.equals(o1, o2); } + + /** + * Returns the element at a zero-based index in an array or {@link List}. + *

+ * Returns {@code null} when {@code obj} is {@code null}, is neither a list nor an array, or the index is negative or + * out of range. Primitive array elements are boxed. + * + * @param element type + * + * @param obj array or list to read, may be {@code null} + * @param index zero-based element index + * @return the element at {@code index}, or {@code null} when it cannot be read + */ + public static T getAt(final Object obj, final int index) { + if (null == obj || index < 0) { + return null; + } + if (obj instanceof List list) { + return cast(Lists.safeGet(list, index)); + } + if (Classes.isNotArray(obj.getClass())) { + return null; + } + int length = Array.getLength(obj); + if (length == 0 || index >= length) { + return null; + } + return cast(Array.get(obj, index)); + } } diff --git a/src/main/java/org/morphix/lang/collections/Lists.java b/src/main/java/org/morphix/lang/collections/Lists.java index 24cf42dc..5fa76516 100644 --- a/src/main/java/org/morphix/lang/collections/Lists.java +++ b/src/main/java/org/morphix/lang/collections/Lists.java @@ -188,6 +188,30 @@ static boolean isNotEmpty(final List list) { return !isEmpty(list); } + /** + * Returns the element at the given index, or {@code null} if the list is {@code null}, empty, or the index is out of + * range. + *

+ * Will not throw a {@link NullPointerException} when the list is {@code null}, or an {@link IndexOutOfBoundsException} + * when the index is out of range. + * + * @param element type + * + * @param list the list to read, may be {@code null} + * @param index zero-based element index + * @return the element at {@code index}, or {@code null} when the list is {@code null}, empty, or the index is out of + * range + */ + static T safeGet(final List list, final int index) { + if (isEmpty(list)) { + return null; + } + if (index < 0 || index >= list.size()) { + return null; + } + return list.get(index); + } + /** * Merges two sorted lists returning a new mutable sorted list. *