[go: nahoru, domu]

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

Httpheaders put use setter if defined #538

Merged
merged 2 commits into from
Dec 10, 2018
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 @@ -15,8 +15,12 @@
package com.google.api.client.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;

Expand Down Expand Up @@ -112,6 +116,9 @@ public static FieldInfo of(Field field) {
/** Field. */
private final Field field;

/** Setters Method for field */
private final Method []setters;

/**
* Data key name associated with the field for a non-enum-constant with a {@link Key} annotation,
* or data key value associated with the enum constant with a {@link Value} annotation or {@code
Expand All @@ -127,6 +134,21 @@ public static FieldInfo of(Field field) {
this.field = field;
this.name = name == null ? null : name.intern();
isPrimitive = Data.isPrimitive(getType());
this.setters = settersMethodForField(field);
}

/**
* Creates list of setter methods for a field only in declaring class.
*/
private Method[] settersMethodForField(Field field) {
List<Method> methods = new ArrayList<Method>();
for (Method method : field.getDeclaringClass().getDeclaredMethods()) {
if (method.getName().toLowerCase().equals("set" + field.getName().toLowerCase())
&& method.getParameterTypes().length == 1) {
methods.add(method);
}
}
return methods.toArray(new Method[methods.size()]);
}

/**
Expand Down Expand Up @@ -203,6 +225,20 @@ public Object getValue(Object obj) {
* If the field is final, it checks that value being set is identical to the existing value.
*/
public void setValue(Object obj, Object value) {
if (setters.length > 0) {
for (Method method : setters) {
if (value == null || method.getParameterTypes()[0].isAssignableFrom(value.getClass())) {
try {
method.invoke(obj, value);
return;
} catch (IllegalAccessException e) {
// try to set field directly
} catch (InvocationTargetException e) {
// try to set field directly
}
}
}
}
setFieldValue(field, obj, value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package com.google.api.client.util;

import com.google.api.client.util.GenericData.Flags;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import junit.framework.Assert;
import junit.framework.TestCase;

Expand All @@ -32,6 +34,18 @@ public MyData() {

@Key("FieldA")
public String fieldA;

@Key("FieldB")
public List<String> fieldB;

public void setFieldB(String fieldB) {
this.fieldB = Lists.newArrayList();
this.fieldB.add(fieldB);
}

public void setFieldB(List<String> fieldB) {
this.fieldB = fieldB;
}
}


Expand Down Expand Up @@ -119,4 +133,14 @@ public void testRemoveIgnoreCase_unknownKey() {
assertEquals(2, data.remove("TESTA"));
assertEquals(null, data.remove("TESTA"));
}

public void testPutShouldUseSetter() {
MyData data = new MyData();
data.put("fieldB", "value1");
assertEquals("value1", data.fieldB.get(0));
List<String> list = new ArrayList();
list.add("value2");
data.put("fieldB", list);
assertEquals(list, data.fieldB);
}
}