Skip to content

Commit

Permalink
Merge pull request square#1728 from square/jw/test-exposed-util-behavior
Browse files Browse the repository at this point in the history
Cleanup and test exposed utility behavior.
  • Loading branch information
swankjesse committed Apr 8, 2016
2 parents 9de61e0 + b51d128 commit 2839881
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 20 deletions.
40 changes: 20 additions & 20 deletions retrofit/src/main/java/retrofit2/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,41 +36,41 @@ private Utils() {
// No instances.
}

public static Class<?> getRawType(Type type) {
static Class<?> getRawType(Type type) {
if (type == null) throw new NullPointerException("type == null");

if (type instanceof Class<?>) {
// Type is a normal class.
return (Class<?>) type;

} else if (type instanceof ParameterizedType) {
}
if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;

// I'm not exactly sure why getRawType() returns Type instead of Class. Neal isn't either but
// suspects some pathological case related to nested classes exists.
Type rawType = parameterizedType.getRawType();
if (!(rawType instanceof Class)) throw new IllegalArgumentException();
return (Class<?>) rawType;

} else if (type instanceof GenericArrayType) {
}
if (type instanceof GenericArrayType) {
Type componentType = ((GenericArrayType) type).getGenericComponentType();
return Array.newInstance(getRawType(componentType), 0).getClass();

} else if (type instanceof TypeVariable) {
}
if (type instanceof TypeVariable) {
// We could use the variable's bounds, but that won't work if there are multiple. Having a raw
// type that's more general than necessary is okay.
return Object.class;

} else if (type instanceof WildcardType) {
}
if (type instanceof WildcardType) {
return getRawType(((WildcardType) type).getUpperBounds()[0]);

} else {
String className = type == null ? "null" : type.getClass().getName();
throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + className);
}

throw new IllegalArgumentException("Expected a Class, ParameterizedType, or "
+ "GenericArrayType, but <" + type + "> is of type " + type.getClass().getName());
}

/** Returns true if {@code a} and {@code b} are equal. */
public static boolean equals(Type a, Type b) {
static boolean equals(Type a, Type b) {
if (a == b) {
return true; // Also handles (a == null && b == null).

Expand Down Expand Up @@ -162,7 +162,7 @@ static int hashCodeOrZero(Object o) {
return o != null ? o.hashCode() : 0;
}

public static String typeToString(Type type) {
static String typeToString(Type type) {
return type instanceof Class ? ((Class<?>) type).getName() : type.toString();
}

Expand All @@ -173,13 +173,13 @@ public static String typeToString(Type type) {
*
* @param supertype a superclass of, or interface implemented by, this.
*/
public static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype) {
static Type getSupertype(Type context, Class<?> contextRawType, Class<?> supertype) {
if (!supertype.isAssignableFrom(contextRawType)) throw new IllegalArgumentException();
return resolve(context, contextRawType,
getGenericSupertype(context, contextRawType, supertype));
}

public static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
static Type resolve(Type context, Class<?> contextRawType, Type toResolve) {
// This implementation is made a little more complicated in an attempt to avoid object-creation.
while (true) {
if (toResolve instanceof TypeVariable) {
Expand Down Expand Up @@ -318,9 +318,9 @@ static <T> void validateServiceInterface(Class<T> service) {

static Type getParameterUpperBound(int index, ParameterizedType type) {
Type[] types = type.getActualTypeArguments();
if (types.length <= index) {
if (index < 0 || index >= types.length) {
throw new IllegalArgumentException(
"Expected at least " + index + " type argument(s) but got: " + Arrays.toString(types));
"Index " + index + " not in range [0," + types.length + ") for " + type);
}
Type paramType = types[index];
if (paramType instanceof WildcardType) {
Expand Down
96 changes: 96 additions & 0 deletions retrofit/src/test/java/retrofit2/CallAdapterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (C) 2016 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package retrofit2;

import com.google.common.reflect.TypeToken;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
import static retrofit2.CallAdapter.Factory.getParameterUpperBound;
import static retrofit2.CallAdapter.Factory.getRawType;

public final class CallAdapterTest {
@Test public void parameterizedTypeInvalidIndex() {
ParameterizedType listOfString = (ParameterizedType) new TypeToken<List<String>>() {}.getType();
try {
getParameterUpperBound(-1, listOfString);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Index -1 not in range [0,1) for java.util.List<java.lang.String>");
}
try {
getParameterUpperBound(1, listOfString);
fail();
} catch (IllegalArgumentException e) {
assertThat(e).hasMessage("Index 1 not in range [0,1) for java.util.List<java.lang.String>");
}
}

@Test public void parameterizedTypes() {
ParameterizedType one = (ParameterizedType) new TypeToken<List<String>>() {}.getType();
assertThat(getParameterUpperBound(0, one)).isSameAs(String.class);

ParameterizedType two = (ParameterizedType) new TypeToken<Map<String, String>>() {}.getType();
assertThat(getParameterUpperBound(0, two)).isSameAs(String.class);
assertThat(getParameterUpperBound(1, two)).isSameAs(String.class);

ParameterizedType wild = (ParameterizedType) new TypeToken<List<? extends CharSequence>>() {
}.getType();
assertThat(getParameterUpperBound(0, wild)).isSameAs(CharSequence.class);
}

@Test public void rawTypeThrowsOnNull() {
try {
getRawType(null);
fail();
} catch (NullPointerException e) {
assertThat(e).hasMessage("type == null");
}
}

@Test public void rawTypes() throws NoSuchMethodException {
assertThat(getRawType(String.class)).isSameAs(String.class);

Type listOfString = new TypeToken<List<String>>() {}.getType();
assertThat(getRawType(listOfString)).isSameAs(List.class);

Type stringArray = new TypeToken<String[]>() {}.getType();
assertThat(getRawType(stringArray)).isSameAs(String[].class);

Type wild = ((ParameterizedType) new TypeToken<List<? extends CharSequence>>() {
}.getType()).getActualTypeArguments()[0];
assertThat(getRawType(wild)).isSameAs(CharSequence.class);

Type wildParam = ((ParameterizedType) new TypeToken<List<? extends List<String>>>() {
}.getType()).getActualTypeArguments()[0];
assertThat(getRawType(wildParam)).isSameAs(List.class);

Type typeVar = A.class.getDeclaredMethod("method").getGenericReturnType();
assertThat(getRawType(typeVar)).isSameAs(Object.class);
}

@SuppressWarnings("unused") // Used reflectively.
static class A<T> {
T method() {
return null;
}
}
}

0 comments on commit 2839881

Please sign in to comment.