Skip to content

Commit ff93479

Browse files
ajaaymchingor13
authored andcommitted
GenericData can now overload setters (#538)
* Fix #340 HttpHeaders won't let me put/set "accept" * fix case of null value
1 parent f0ca832 commit ff93479

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

google-http-client/src/main/java/com/google/api/client/util/FieldInfo.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@
1515
package com.google.api.client.util;
1616

1717
import java.lang.reflect.Field;
18+
import java.lang.reflect.InvocationTargetException;
19+
import java.lang.reflect.Method;
1820
import java.lang.reflect.Modifier;
1921
import java.lang.reflect.Type;
22+
import java.util.ArrayList;
23+
import java.util.List;
2024
import java.util.Map;
2125
import java.util.WeakHashMap;
2226

@@ -112,6 +116,9 @@ public static FieldInfo of(Field field) {
112116
/** Field. */
113117
private final Field field;
114118

119+
/** Setters Method for field */
120+
private final Method []setters;
121+
115122
/**
116123
* Data key name associated with the field for a non-enum-constant with a {@link Key} annotation,
117124
* or data key value associated with the enum constant with a {@link Value} annotation or {@code
@@ -127,6 +134,21 @@ public static FieldInfo of(Field field) {
127134
this.field = field;
128135
this.name = name == null ? null : name.intern();
129136
isPrimitive = Data.isPrimitive(getType());
137+
this.setters = settersMethodForField(field);
138+
}
139+
140+
/**
141+
* Creates list of setter methods for a field only in declaring class.
142+
*/
143+
private Method[] settersMethodForField(Field field) {
144+
List<Method> methods = new ArrayList<Method>();
145+
for (Method method : field.getDeclaringClass().getDeclaredMethods()) {
146+
if (method.getName().toLowerCase().equals("set" + field.getName().toLowerCase())
147+
&& method.getParameterTypes().length == 1) {
148+
methods.add(method);
149+
}
150+
}
151+
return methods.toArray(new Method[methods.size()]);
130152
}
131153

132154
/**
@@ -203,6 +225,20 @@ public Object getValue(Object obj) {
203225
* If the field is final, it checks that value being set is identical to the existing value.
204226
*/
205227
public void setValue(Object obj, Object value) {
228+
if (setters.length > 0) {
229+
for (Method method : setters) {
230+
if (value == null || method.getParameterTypes()[0].isAssignableFrom(value.getClass())) {
231+
try {
232+
method.invoke(obj, value);
233+
return;
234+
} catch (IllegalAccessException e) {
235+
// try to set field directly
236+
} catch (InvocationTargetException e) {
237+
// try to set field directly
238+
}
239+
}
240+
}
241+
}
206242
setFieldValue(field, obj, value);
207243
}
208244

google-http-client/src/test/java/com/google/api/client/util/GenericDataTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
package com.google.api.client.util;
1616

1717
import com.google.api.client.util.GenericData.Flags;
18+
import java.util.ArrayList;
1819
import java.util.EnumSet;
20+
import java.util.List;
1921
import junit.framework.Assert;
2022
import junit.framework.TestCase;
2123

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

3335
@Key("FieldA")
3436
public String fieldA;
37+
38+
@Key("FieldB")
39+
public List<String> fieldB;
40+
41+
public void setFieldB(String fieldB) {
42+
this.fieldB = Lists.newArrayList();
43+
this.fieldB.add(fieldB);
44+
}
45+
46+
public void setFieldB(List<String> fieldB) {
47+
this.fieldB = fieldB;
48+
}
3549
}
3650

3751

@@ -119,4 +133,14 @@ public void testRemoveIgnoreCase_unknownKey() {
119133
assertEquals(2, data.remove("TESTA"));
120134
assertEquals(null, data.remove("TESTA"));
121135
}
136+
137+
public void testPutShouldUseSetter() {
138+
MyData data = new MyData();
139+
data.put("fieldB", "value1");
140+
assertEquals("value1", data.fieldB.get(0));
141+
List<String> list = new ArrayList();
142+
list.add("value2");
143+
data.put("fieldB", list);
144+
assertEquals(list, data.fieldB);
145+
}
122146
}

0 commit comments

Comments
 (0)