Skip to content

Commit

Permalink
Merge pull request #58 from kbss-cvut/development
Browse files Browse the repository at this point in the history
[0.13.1] Release
  • Loading branch information
ledsoft authored Jul 31, 2023
2 parents 1d680ed + 419c84c commit 77dcb10
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 12 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# JB4JSON-LD Changelog

## 0.13.1 - 2023-07-31
- Better handle class hierarchies when resolving property access.

## 0.13.0 - 2023-07-31
- Make `BeanAnnotationProcessor.getAncestors` public.
- **Breaking change:** Set Java 11 as minimum Java version.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cz.cvut.kbss.jsonld</groupId>
<artifactId>jb4jsonld</artifactId>
<version>0.13.0</version>
<version>0.13.1</version>
<name>JB4JSON-LD</name>
<description>Java Binding for JSON-LD allows serialization and deserialization of Java POJOs to/from JSON-LD.
This is the core implementation, which has to be integrated with Jackson, Jersey etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.function.BiPredicate;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class BeanAnnotationProcessor {

private static final String[] EMPTY_ARRAY = new String[0];
private static final Predicate<Field> ALWAYS_TRUE = field -> true;
private static final BiPredicate<Field, Class<?>> ALWAYS_TRUE = (f, cls) -> true;

private static PropertyAccessResolver propertyAccessResolver = new JsonLdPropertyAccessResolver();

Expand Down Expand Up @@ -214,12 +214,12 @@ public static List<Field> getSerializableFields(Object object) {
return getMarshallableFields(cls, propertyAccessResolver::isReadable);
}

private static List<Field> getMarshallableFields(Class<?> cls, Predicate<Field> filter) {
private static List<Field> getMarshallableFields(Class<?> cls, BiPredicate<Field, Class<?>> filter) {
final List<Class<?>> classes = getAncestors(cls);
final Set<Field> fields = new HashSet<>();
for (Class<?> c : classes) {
for (Field f : c.getDeclaredFields()) {
if (!isFieldTransient(f) && filter.test(f)) {
if (!isFieldTransient(f) && filter.test(f, cls)) {
fields.add(f);
}
}
Expand Down Expand Up @@ -416,7 +416,7 @@ public static String getAttributeIdentifier(Field field) {
}

public static Optional<Field> getIdentifierField(Class<?> cls) {
return getMarshallableFields(cls, f -> f.isAnnotationPresent(Id.class)).stream().findFirst();
return getMarshallableFields(cls, (f, c) -> f.isAnnotationPresent(Id.class)).stream().findFirst();
}

/**
Expand All @@ -431,7 +431,7 @@ public static Optional<Object> getInstanceIdentifier(Object instance) {
for (Class<?> cls : classes) {
for (Field f : cls.getDeclaredFields()) {
if (f.getDeclaredAnnotation(Id.class) != null) {
if (!f.isAccessible()) {
if (!f.canAccess(instance)) {
f.setAccessible(true);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private BeanClassProcessor() {
*/
public static Object getFieldValue(Field field, Object instance) {
Objects.requireNonNull(field);
if (!field.isAccessible()) {
if (!field.canAccess(instance)) {
field.setAccessible(true);
}
try {
Expand All @@ -58,7 +58,7 @@ public static Object getFieldValue(Field field, Object instance) {
*/
public static void setFieldValue(Field field, Object instance, Object value) {
Objects.requireNonNull(field);
if (!field.isAccessible()) {
if (!field.canAccess(instance)) {
field.setAccessible(true);
}
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
public class JsonLdPropertyAccessResolver implements PropertyAccessResolver {

@Override
public boolean isReadable(Field field) {
public boolean isReadable(Field field, Class<?> objectClass) {
Objects.requireNonNull(field);
final JsonLdProperty annotation = field.getAnnotation(JsonLdProperty.class);
return annotation == null || annotation.access() != JsonLdProperty.Access.WRITE_ONLY ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@ public interface PropertyAccessResolver {
* Resolves whether value of the specified field is readable for serialization.
*
* @param field Field to check
* @param objectClass Type of the object being serialized
* @return Whether the field is readable
*/
boolean isReadable(Field field);
boolean isReadable(Field field, Class<?> objectClass);

/**
* Resolves whether the specified field is writeable by deserialization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class JsonLdPropertyAccessResolverTest {
"types, true",
"id, true"})
void isReadable(String fieldName, boolean result) throws Exception {
assertEquals(result, sut.isReadable(TestClass.class.getDeclaredField(fieldName)));
assertEquals(result, sut.isReadable(TestClass.class.getDeclaredField(fieldName), TestClass.class));
}

@ParameterizedTest
Expand Down

0 comments on commit 77dcb10

Please sign in to comment.