Skip to content

Commit 7e98ebd

Browse files
authored
Support Properties subclasses in GsonTypes.getMapKeyAndValueTypes (#2758)
1 parent ec5a5e4 commit 7e98ebd

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

gson/src/main/java/com/google/gson/internal/$Gson$Types.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,8 @@ public static Type[] getMapKeyAndValueTypes(Type context, Class<?> contextRawTyp
328328
* class should extend Hashtable<String, String>, but it's declared to
329329
* extend Hashtable<Object, Object>.
330330
*/
331-
if (context == Properties.class) {
332-
return new Type[] {String.class, String.class}; // TODO: test subclasses of Properties!
331+
if (Properties.class.isAssignableFrom(contextRawType)) {
332+
return new Type[] {String.class, String.class};
333333
}
334334

335335
Type mapType = getSupertype(context, contextRawType, Map.class);

gson/src/test/java/com/google/gson/internal/GsonTypesTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.lang.reflect.ParameterizedType;
2525
import java.lang.reflect.Type;
2626
import java.util.List;
27+
import java.util.Properties;
2728
import org.junit.Test;
2829

2930
@SuppressWarnings("ClassNamedLikeTypeParameter") // for dummy classes A, B, ...
@@ -133,4 +134,17 @@ public <T> T method() {
133134
return null;
134135
}
135136
}
137+
138+
@Test
139+
public void testGetMapKeyAndValueTypesForPropertiesSubclass() throws Exception {
140+
class CustomProperties extends Properties {
141+
private static final long serialVersionUID = 4112578634029874840L;
142+
}
143+
144+
Type[] types =
145+
$Gson$Types.getMapKeyAndValueTypes(CustomProperties.class, CustomProperties.class);
146+
147+
assertThat(types[0]).isEqualTo(String.class);
148+
assertThat(types[1]).isEqualTo(String.class);
149+
}
136150
}

0 commit comments

Comments
 (0)