Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/6"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>
48 changes: 32 additions & 16 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,26 +30,26 @@
- 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.
- Added `Methods.Unchecked.getAllDeclared(Class, Predicate)` for direct retrieval and filtering of declared methods.
- 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.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -81,6 +81,6 @@ Maven: add this dependency to your `pom.xml`
<dependency>
<groupId>io.github.raduking</groupId>
<artifactId>morphix-all</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
</dependency>
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<groupId>io.github.raduking</groupId>
<artifactId>morphix-all</artifactId>
<version>1.1.1</version>
<version>1.1.2</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
38 changes: 4 additions & 34 deletions src/main/java/org/morphix/convert/strategy/FinderPathStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -44,41 +40,15 @@ public <T> ExtendedField find(final T source, final List<ExtendedField> 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 <T> 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 <T> 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);
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/morphix/lang/JavaArrays.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,26 @@ static <T> T[] empty(final Class<T> 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.
* <p>
* Will not throw a {@link NullPointerException} when the array is {@code null}, or an
* {@link ArrayIndexOutOfBoundsException} when the index is out of range.
*
* @param <T> 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> T safeGet(final T[] array, final int index) {
if (null == array || index < 0) {
return null;
}
if (index >= array.length) {
return null;
}
return array[index];
}
}
32 changes: 32 additions & 0 deletions src/main/java/org/morphix/lang/JavaObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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}.
* <p>
* 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 <T> 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> 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));
}
}
24 changes: 24 additions & 0 deletions src/main/java/org/morphix/lang/collections/Lists.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,30 @@ static <T> boolean isNotEmpty(final List<T> 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.
* <p>
* Will not throw a {@link NullPointerException} when the list is {@code null}, or an {@link IndexOutOfBoundsException}
* when the index is out of range.
*
* @param <T> 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> T safeGet(final List<T> 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.
* <ul>
Expand Down
Loading