From 14ba59fa8858126dbe0c3e645a84c7de3bf2361a Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Mon, 19 May 2014 17:34:25 +0000 Subject: [PATCH] code review fixes. Moved getFirstArgument() method from $Gson$Types to GsonTypesTest. --- .../com/google/gson/internal/$Gson$Types.java | 16 ----------- .../gson/internal/LinkedHashTreeMap.java | 4 +-- .../google/gson/internal/LinkedTreeMap.java | 2 +- .../google/gson/internal/GsonTypesTest.java | 28 +++++++++++++++---- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/gson/src/main/java/com/google/gson/internal/$Gson$Types.java b/gson/src/main/java/com/google/gson/internal/$Gson$Types.java index 4adfe166..9365be74 100644 --- a/gson/src/main/java/com/google/gson/internal/$Gson$Types.java +++ b/gson/src/main/java/com/google/gson/internal/$Gson$Types.java @@ -391,22 +391,6 @@ public final class $Gson$Types { } } - /** - * Given a parameterized type A<B,C>, 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 canonicalize(actualTypeArguments[0]); - } catch (Exception e) { - return null; - } - } - static Type resolveTypeVariable(Type context, Class contextRawType, TypeVariable unknown) { Class declaredByRaw = declaringClassOf(unknown); diff --git a/gson/src/main/java/com/google/gson/internal/LinkedHashTreeMap.java b/gson/src/main/java/com/google/gson/internal/LinkedHashTreeMap.java index 098cb016..e251ec2f 100644 --- a/gson/src/main/java/com/google/gson/internal/LinkedHashTreeMap.java +++ b/gson/src/main/java/com/google/gson/internal/LinkedHashTreeMap.java @@ -788,7 +788,7 @@ public final class LinkedHashTreeMap extends AbstractMap implements } } - class EntrySet extends AbstractSet> { + final class EntrySet extends AbstractSet> { @Override public int size() { return size; } @@ -823,7 +823,7 @@ public final class LinkedHashTreeMap extends AbstractMap implements } } - class KeySet extends AbstractSet { + final class KeySet extends AbstractSet { @Override public int size() { return size; } diff --git a/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java b/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java index 578795a7..c2c84802 100644 --- a/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java +++ b/gson/src/main/java/com/google/gson/internal/LinkedTreeMap.java @@ -589,7 +589,7 @@ public final class LinkedTreeMap extends AbstractMap implements Seri } } - class KeySet extends AbstractSet { + final class KeySet extends AbstractSet { @Override public int size() { return size; } diff --git a/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java b/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java index 8087909a..e448746b 100644 --- a/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java +++ b/gson/src/test/java/com/google/gson/internal/GsonTypesTest.java @@ -16,6 +16,7 @@ package com.google.gson.internal; +import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.List; @@ -26,11 +27,11 @@ public final class GsonTypesTest extends TestCase { public void testNewParameterizedTypeWithoutOwner() { // List. List is a top-level class Type type = $Gson$Types.newParameterizedTypeWithOwner(null, List.class, A.class); - assertEquals(A.class, $Gson$Types.getFirstTypeArgument(type)); + assertEquals(A.class, getFirstTypeArgument(type)); // A. A is a static inner class. type = $Gson$Types.newParameterizedTypeWithOwner(null, A.class, B.class); - assertEquals(B.class, $Gson$Types.getFirstTypeArgument(type)); + assertEquals(B.class, getFirstTypeArgument(type)); final class D { } @@ -41,14 +42,14 @@ public final class GsonTypesTest extends TestCase { // A is allowed. type = $Gson$Types.newParameterizedTypeWithOwner(null, A.class, D.class); - assertEquals(D.class, $Gson$Types.getFirstTypeArgument(type)); + assertEquals(D.class, getFirstTypeArgument(type)); } public void testGetFirstTypeArgument() { - assertNull($Gson$Types.getFirstTypeArgument(A.class)); + assertNull(getFirstTypeArgument(A.class)); Type type = $Gson$Types.newParameterizedTypeWithOwner(null, A.class, B.class, C.class); - assertEquals(B.class, $Gson$Types.getFirstTypeArgument(type)); + assertEquals(B.class, getFirstTypeArgument(type)); } private static final class A { @@ -57,4 +58,21 @@ public final class GsonTypesTest extends TestCase { } private static final class C { } + + /** + * Given a parameterized type A<B,C>, 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; + } + } + }