Skip to content
Merged
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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
## Release Notes

`1.1.3`

- Added `ExtendedField.valueNotNull()` to check whether the field value is not `null`, usable as a predicate for `ExtendedFields.findAllNonStatic`.
- Added `JavaObjects.isArray(Object)` to check whether an object is an array, returning `false` for `null`.
- Added `JavaObjects.isNotArray(Object)` to check whether an object is not an array, returning `true` for `null`.
- Upgraded parent to `4.1.3`.

---

`1.1.2`

- Added `FieldPathParser` for JSON Path-style field path parsing and resolution (properties, indices, and map keys).
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.2`
Current release `1.1.3`

### Documentation

Expand Down Expand Up @@ -88,6 +88,6 @@ Maven: add this dependency to your `pom.xml`
<dependency>
<groupId>io.github.raduking</groupId>
<artifactId>morphix-all</artifactId>
<version>1.1.2</version>
<version>1.1.3</version>
</dependency>
```
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>4.1.2</version>
<version>4.1.3</version>
<relativePath />
</parent>

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

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/org/morphix/lang/JavaObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static boolean isEmpty(final Object o) {
case Collection<?> collection -> collection.isEmpty();
case Map<?, ?> map -> map.isEmpty();
case Optional<?> optional -> optional.isEmpty();
case Object object when object.getClass().isArray() -> Array.getLength(o) == 0;
case Object object when isArray(object) -> Array.getLength(o) == 0;
default -> false;
};
}
Expand Down Expand Up @@ -125,4 +125,25 @@ public static <T> T getAt(final Object obj, final int index) {
}
return cast(Array.get(obj, index));
}

/**
* Returns true if the given object is an array, false otherwise.
*
* @param obj object to check
* @return true if the given object is an array, false otherwise
*/
public static boolean isArray(final Object obj) {
Class<?> cls = Classes.getFrom(obj);
return Classes.isArray(cls);
}

/**
* Returns true if the given object is not an array, false otherwise.
*
* @param obj object to check
* @return true if the given object is not an array, false otherwise
*/
public static boolean isNotArray(final Object obj) {
return !isArray(obj);
}
}
10 changes: 10 additions & 0 deletions src/main/java/org/morphix/reflection/ExtendedField.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ public boolean typeMeets(final Predicate<Type> predicate) {
return predicate.test(getType());
}

/**
* Returns true if the field value is not {@code null}, false otherwise. Can be used as a predicate in
* {@link ExtendedFields#findAllNonStatic(Object, Predicate)}.
*
* @return true if the field value is not {@code null}, false otherwise
*/
public boolean valueNotNull() {
return null != getFieldValue();
}

/**
* Returns true if the annotation is present on the extended field.
*
Expand Down
106 changes: 106 additions & 0 deletions src/test/java/org/morphix/lang/JavaObjectsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,112 @@ void shouldReturnTrueWhenOneObjectIsNull() {
}
}

@Nested
class IsArrayTests {

@Test
void shouldReturnFalseOnNull() {
boolean result = JavaObjects.isArray(null);

assertFalse(result);
}

@Test
void shouldReturnFalseOnNonArray() {
boolean result = JavaObjects.isArray(BUBU);

assertFalse(result);
}

@Test
void shouldReturnFalseOnList() {
boolean result = JavaObjects.isArray(List.of(BUBU));

assertFalse(result);
}

@Test
void shouldReturnTrueOnObjectArray() {
boolean result = JavaObjects.isArray(new String[] { BUBU });

assertTrue(result);
}

@Test
void shouldReturnTrueOnEmptyArray() {
boolean result = JavaObjects.isArray(new Object[] { });

assertTrue(result);
}

@Test
void shouldReturnTrueOnPrimitiveArray() {
boolean result = JavaObjects.isArray(new int[] { 1 });

assertTrue(result);
}

@Test
void shouldReturnTrueOnMultiDimensionalArray() {
boolean result = JavaObjects.isArray(new int[][] { { 1 } });

assertTrue(result);
}
}

@Nested
class IsNotArrayTests {

@Test
void shouldReturnTrueOnNull() {
boolean result = JavaObjects.isNotArray(null);

assertTrue(result);
}

@Test
void shouldReturnTrueOnNonArray() {
boolean result = JavaObjects.isNotArray(BUBU);

assertTrue(result);
}

@Test
void shouldReturnTrueOnList() {
boolean result = JavaObjects.isNotArray(List.of(BUBU));

assertTrue(result);
}

@Test
void shouldReturnFalseOnObjectArray() {
boolean result = JavaObjects.isNotArray(new String[] { BUBU });

assertFalse(result);
}

@Test
void shouldReturnFalseOnEmptyArray() {
boolean result = JavaObjects.isNotArray(new Object[] { });

assertFalse(result);
}

@Test
void shouldReturnFalseOnPrimitiveArray() {
boolean result = JavaObjects.isNotArray(new int[] { 1 });

assertFalse(result);
}

@Test
void shouldReturnFalseOnMultiDimensionalArray() {
boolean result = JavaObjects.isNotArray(new int[][] { { 1 } });

assertFalse(result);
}
}

@Nested
class GetAtTests {

Expand Down
44 changes: 40 additions & 4 deletions src/test/java/org/morphix/reflection/ExtendedFieldTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -48,11 +50,12 @@ public static class A {
}

@Test
void shouldBuildStringWithToString() throws Exception {
void shouldBuildStringWithToString() {
A a = new A();
a.l = TEST_LONG;

ExtendedField fop = ExtendedField.of(A.class.getDeclaredField("l"), a);
Field field = Fields.getOneDeclared(A.class, "l");
ExtendedField fop = ExtendedField.of(field, a);
String result = fop.toString();

assertThat(result, equalTo("Field: l" + EOL
Expand All @@ -73,11 +76,12 @@ void shouldBuildStringWithToStringWithNoField() {
}

@Test
void shouldBuildStringWithToStringWithNoObject() throws Exception {
void shouldBuildStringWithToStringWithNoObject() {
A a = new A();
a.l = TEST_LONG;

ExtendedField fop = ExtendedField.of(A.class.getDeclaredField("l"));
Field field = Fields.getOneDeclared(A.class, "l");
ExtendedField fop = ExtendedField.of(field);
String result = fop.toString();

assertThat(result, equalTo("Field: l" + EOL
Expand Down Expand Up @@ -188,6 +192,38 @@ void shouldReturnAnnotationFromGetterMethod() {
assertThat(annotation, equalTo(method.getAnnotation(Deprecated.class)));
}

@Test
void shouldReturnTrueOnValueNotNullWhenFieldValueIsNotNull() {
A a = new A();
a.l = TEST_LONG;
Field field = Fields.getOneDeclared(A.class, "l");
ExtendedField ef = ExtendedField.of(field, a);

boolean result = ef.valueNotNull();

assertTrue(result);
}

@Test
void shouldReturnFalseOnValueNotNullWhenFieldValueIsNull() {
A a = new A();
Field field = Fields.getOneDeclared(A.class, "l");
ExtendedField ef = ExtendedField.of(field, a);

boolean result = ef.valueNotNull();

assertFalse(result);
}

@Test
void shouldReturnFalseOnValueNotNullWhenThereIsNoFieldOrObject() {
ExtendedField ef = ExtendedField.of(null);

boolean result = ef.valueNotNull();

assertFalse(result);
}

@Test
void shouldThrowExceptionWhenTryingToInstantiate() {
UnsupportedOperationException e = Tests.verifyDefaultConstructorThrows(ExtendedField.Default.class);
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/org/morphix/reflection/ExtendedFieldsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ExtendedFieldsTest {
private static final String WITH_GETTER = "withGetter";
private static final String COMPUTED = "computed";
private static final String BOXED = "boxed";
private static final String SET = "set";
private static final String ITEM = "item";
private static final String BASE_MAPPING = "baseMapping";
private static final String DERIVED_MAPPING = "derivedMapping";
Expand Down Expand Up @@ -156,6 +157,13 @@ void shouldNotReturnStaticGetterAsAProperty() {
assertThat(ExtendedFields.findAllNonStatic(new WithOnlyStaticGetter()), equalTo(List.of()));
}

@Test
void shouldReturnOnlyFieldsWithNotNullValueWhenUsingValueNotNullAsPredicate() {
List<ExtendedField> fields = ExtendedFields.findAllNonStatic(new WithNullableFields(), ExtendedField::valueNotNull);

assertThat(namesOf(fields), contains(SET));
}

private static List<String> namesOf(final List<ExtendedField> fields) {
return fields.stream().map(ExtendedField::getName).toList();
}
Expand Down Expand Up @@ -280,6 +288,13 @@ public static Long getValue() {
}
}

public static class WithNullableFields {

Long set = 1L;

Long unset;
}

public static class WithOnlyStaticGetter {

public static Long getValue() {
Expand Down