Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix #340 HttpHeaders won't let me put/set "accept"
  • Loading branch information
ajaaym committed Dec 7, 2018
commit 3554b7b628b5b2c6ac4074a0a7f044dee0ced12c
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,18 @@ 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 (method.getParameterTypes()[0].isAssignableFrom(value.getClass())) {
try {
method.invoke(obj, value);
return;
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
}
}
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);
}
}