Skip to content

Commit

Permalink
Handle class hierarchies when resolving property access.
Browse files Browse the repository at this point in the history
Fix post-JDK 8 Reflection API warnings.
  • Loading branch information
ledsoft committed Jul 31, 2023
1 parent 8528f91 commit 7b54511
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
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 7b54511

Please sign in to comment.