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 <[email protected]>
  • Loading branch information
senivam committed Mar 19, 2024
1 parent 984ba24 commit 90d0e7d
Show file tree
Hide file tree
Showing 2 changed files with 30 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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ public class MyBeanParamWithPrivateField {
@QueryParam("privateFieldParam")
private String privateFieldParam;

private static String privateStaticField;

static String staticField;

public MyBeanParamWithPrivateField() {}

public String getPrivateFieldParam() {
Expand All @@ -36,4 +40,20 @@ public String getPrivateFieldParam() {
public void setPrivateFieldParam(String privateFieldParam) {
this.privateFieldParam = privateFieldParam;
}

public static String getPrivateStaticField() {
return privateStaticField;
}

public static void setPrivateStaticField(String privateStaticField) {
MyBeanParamWithPrivateField.privateStaticField = privateStaticField;
}

public static String getStaticField() {
return staticField;
}

public static void setStaticField(String staticField) {
MyBeanParamWithPrivateField.staticField = staticField;
}
}

0 comments on commit 90d0e7d

Please sign in to comment.