Skip to content

Commit

Permalink
exclude static fields from POJO parsing in BeanParam
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
  • Loading branch information
senivam committed Mar 18, 2024
1 parent 984ba24 commit 214abdc
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand All @@ -43,6 +44,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Collector to retrieve parameters for setting up the HTTP request sent in the invoke method of WebResourceFactory
Expand Down Expand Up @@ -145,7 +147,7 @@ private void addBeanParameter(final Object beanParam)
throws IllegalAccessException, IntrospectionException, InvocationTargetException {
Class<?> beanClass = beanParam.getClass();
List<Field> fields = new ArrayList<>();
getAllFields(fields, beanClass);
getAllNonStaticFields(fields, beanClass);

for (final Field field : fields) {
Object value = null;
Expand Down Expand Up @@ -178,11 +180,15 @@ private void addBeanParameter(final Object beanParam)
}
}

private List<Field> getAllFields(List<Field> fields, Class<?> type) {
fields.addAll(Arrays.asList(type.getDeclaredFields()));
private List<Field> getAllNonStaticFields(List<Field> fields, Class<?> type) {

List<Field> nonStaticFields = Arrays.stream(type.getDeclaredFields())
.filter(field -> !Modifier.isStatic(field.getModifiers()))
.collect(Collectors.toList());
fields.addAll(nonStaticFields);

if (type.getSuperclass() != null) {
getAllFields(fields, type.getSuperclass());
getAllNonStaticFields(fields, type.getSuperclass());
}

return fields;
Expand Down

0 comments on commit 214abdc

Please sign in to comment.