// Copyright 2012 Square, Inc.
package retrofit.http;

/**
 * Helper methods for dealing with generic types via reflection.
 *
 * @author Jake Wharton (jw@squareup.com)
 */
import java.lang.reflect.Type;

class Types {
  /**
   * Returns the generic supertype for {@code supertype}. For example, given a class {@code
   * IntegerSet}, the result for when supertype is {@code Set.class} is {@code Set<Integer>} and the
   * result when the supertype is {@code Collection.class} is {@code Collection<Integer>}.
   * <p/>
   * NOTE: This method is copied from Guice's {@code MoreTypes} class.
   */
  static Type getGenericSupertype(Type context, Class<?> rawType, Class<?> toResolve) {
    if (toResolve == rawType) {
      return context;
    }

    // we skip searching through interfaces if unknown is an interface
    if (toResolve.isInterface()) {
      Class<?>[] interfaces = rawType.getInterfaces();
      for (int i = 0, length = interfaces.length; i < length; i++) {
        if (interfaces[i] == toResolve) {
          return rawType.getGenericInterfaces()[i];
        } else if (toResolve.isAssignableFrom(interfaces[i])) {
          return getGenericSupertype(rawType.getGenericInterfaces()[i], interfaces[i], toResolve);
        }
      }
    }

    // check our supertypes
    if (!rawType.isInterface()) {
      while (rawType != Object.class) {
        Class<?> rawSupertype = rawType.getSuperclass();
        if (rawSupertype == toResolve) {
          return rawType.getGenericSuperclass();
        } else if (toResolve.isAssignableFrom(rawSupertype)) {
          return getGenericSupertype(rawType.getGenericSuperclass(), rawSupertype, toResolve);
        }
        rawType = rawSupertype;
      }
    }

    // we can't resolve this further
    return toResolve;
  }
}
@@ -12,16 +12,16 @@
 t 20
-12 Squar
+08 Googl
 e, I
@@ -48,21 +48,50 @@
 .http;%0A%0A
-/**%0A 
+import java.lang.reflect.Type;%0A%0A/*
 * Helper
@@ -148,90 +148,57 @@
 tion
-.%0A *%0A * @author Jake Wharton (jw@squareup.com)%0A */%0Aimport java.lang.reflect.Type;%0A
+ copied from Guice's %7B@code MoreTypes%7D class.. */
 %0Acla
@@ -502,89 +502,8 @@
 %3E%7D.%0A
-   * %3Cp/%3E%0A   * NOTE: This method is copied from Guice's %7B@code MoreTypes%7D class.%0A
    *

Update copyright, fix javadoc.