Skip to content

Commit

Permalink
incorporated code review feedback. Added a test to ensure JsonAdapter…
Browse files Browse the repository at this point in the history
… validation doesn't carry side-effects to other fields
  • Loading branch information
inder123 committed Aug 1, 2014
1 parent e0b3539 commit 5aea8e9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
1 change: 0 additions & 1 deletion gson/src/main/java/com/google/gson/JsonArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public void addAll(JsonArray array) {
* Element can be null.
* @param index index of the element to replace
* @param element element to be stored at the specified position
* @return
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException if the specified index is outside the array bounds
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
* }
* }
* </pre>
* The above annotation will ensure UserJsonAdapter2 supersedes UserJsonAdapter for the user
* field of the Gadget class.
* The above annotation will ensure UserJsonAdapter2 takes precedence over UserJsonAdapter
* for the user field of the Gadget class.
*
* @since 2.3
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,21 @@ private static final class UserJsonAdapter2 extends TypeAdapter<User> {
return new User(in.nextString());
}
}

public void testJsonAdapterInvokedOnlyForAnnotatedFields() {
Gson gson = new Gson();
String json = "{'part1':'name','part2':{'name':'name2'}}";
GadgetWithTwoParts gadget = gson.fromJson(json, GadgetWithTwoParts.class);
assertEquals("partJsonAdapter", gadget.part1.name);
assertEquals("name2", gadget.part2.name);
}

private static final class GadgetWithTwoParts {
@JsonAdapter(PartJsonAdapter.class) final Part part1;
final Part part2; // Doesn't have the JsonAdapter annotation
@SuppressWarnings("unused") GadgetWithTwoParts(Part part1, Part part2) {
this.part1 = part1;
this.part2 = part2;
}
}
}
21 changes: 8 additions & 13 deletions gson/src/test/java/com/google/gson/internal/GsonTypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

public final class GsonTypesTest extends TestCase {

public void testNewParameterizedTypeWithoutOwner() {
public void testNewParameterizedTypeWithoutOwner() throws Exception {
// List<A>. List is a top-level class
Type type = $Gson$Types.newParameterizedTypeWithOwner(null, List.class, A.class);
assertEquals(A.class, getFirstTypeArgument(type));
Expand All @@ -45,7 +45,7 @@ final class D {
assertEquals(D.class, getFirstTypeArgument(type));
}

public void testGetFirstTypeArgument() {
public void testGetFirstTypeArgument() throws Exception {
assertNull(getFirstTypeArgument(A.class));

Type type = $Gson$Types.newParameterizedTypeWithOwner(null, A.class, B.class, C.class);
Expand All @@ -63,16 +63,11 @@ private static final class C {
* Given a parameterized type A&lt;B,C&gt;, returns B. If the specified type is not
* a generic type, returns null.
*/
public static Type getFirstTypeArgument(Type type) {
try {
if (!(type instanceof ParameterizedType)) return null;
ParameterizedType ptype = (ParameterizedType) type;
Type[] actualTypeArguments = ptype.getActualTypeArguments();
if (actualTypeArguments.length == 0) return null;
return $Gson$Types.canonicalize(actualTypeArguments[0]);
} catch (Exception e) {
return null;
}
public static Type getFirstTypeArgument(Type type) throws Exception {
if (!(type instanceof ParameterizedType)) return null;
ParameterizedType ptype = (ParameterizedType) type;
Type[] actualTypeArguments = ptype.getActualTypeArguments();
if (actualTypeArguments.length == 0) return null;
return $Gson$Types.canonicalize(actualTypeArguments[0]);
}

}

0 comments on commit 5aea8e9

Please sign in to comment.