Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exclude static fields from POJO parsing in BeanParam #5566

Merged
merged 1 commit into from
Mar 19, 2024
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
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;
}
}
Loading