Java Class.getGenericSuperclass Examples

Java Class.getGenericSuperclass - 30 examples found. These are the top rated real world Java examples of Class.getGenericSuperclass extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  /**
   * 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>}.
   */
  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;
  }
  private Object searchForBaseClass(
      Class<?> clazz, AutoBindSingleton annotation, Set<Object> usedSet) {
    if (clazz == null) {
      return null;
    }

    if (clazz.equals(annotation.value())) {
      return clazz;
    }

    if (!usedSet.add(clazz)) {
      return null;
    }

    for (Type type : clazz.getGenericInterfaces()) {
      if (MoreTypes.getRawType(type).equals(annotation.value())) {
        return type;
      }
    }

    if (clazz.getGenericSuperclass() != null) {
      if (MoreTypes.getRawType(clazz.getGenericSuperclass()).equals(annotation.value())) {
        return clazz.getGenericSuperclass();
      }
    }

    for (Class<?> interfaceClass : clazz.getInterfaces()) {
      Object foundBindingClass = searchForBaseClass(interfaceClass, annotation, usedSet);
      if (foundBindingClass != null) {
        return foundBindingClass;
      }
    }

    return searchForBaseClass(clazz.getSuperclass(), annotation, usedSet);
  }
Example #3
0
 public static <T> Class<T> getGenericType(Class<?> klass, int index) {
   Type genericSuperclass = klass.getGenericSuperclass();
   if (genericSuperclass instanceof ParameterizedType)
     return (Class<T>)
         ((ParameterizedType) klass.getGenericSuperclass()).getActualTypeArguments()[index];
   if (genericSuperclass.equals(Object.class)) return null;
   return getGenericType(klass.getSuperclass(), index);
 }
 private List<String> GetExtends(Class<?> cls) {
   List<String> list = new ArrayList<>();
   if (cls.getGenericSuperclass() != null) {
     String name = cls.getGenericSuperclass().getTypeName();
     list.add(name);
   }
   return list;
 }
Example #5
0
  /**
   * Method to resolve a TypeVariable to its most <a
   * href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#112582">reifiable</a>
   * form.
   *
   * <p>How to resolve a TypeVariable:<br>
   * All of the TypeVariables defined by a generic class will be given a Type by any class that
   * extends it. The Type given may or may not be reifiable; it may be another TypeVariable for
   * instance.
   *
   * <p>Consider <br>
   * <i>class Pair&gt;A,B> { A getA(){...}; ...}</i><br>
   * <i>class StringLongPair extends Pair&gt;String, Long> { }</i><br>
   * To resolve the actual return type of Pair.getA() you must first resolve the TypeVariable "A".
   * We can do that by first finding the index of "A" in the Pair.class.getTypeParameters() array of
   * TypeVariables.
   *
   * <p>To get to the Type provided by StringLongPair you access the generics information by calling
   * StringLongPair.class.getGenericSuperclass; this will be a ParameterizedType. ParameterizedType
   * gives you access to the actual type arguments provided to Pair by StringLongPair. The array is
   * in the same order as the array in Pair.class.getTypeParameters so you can use the index we
   * discovered earlier to extract the Type; String.class.
   *
   * <p>When extracting Types we only have to consider the superclass hierarchy and not the
   * interfaces implemented by the class. When a class implements a generic interface it must
   * provide types for the interface and any generic methods implemented from the interface will be
   * re-defined by the class with its generic type variables.
   *
   * @param typeVariable - the type variable to resolve.
   * @param containingType - the shallowest class in the class hierarchy (furthest from Object)
   *     where typeVariable is defined.
   * @return a Type that has had all possible TypeVariables resolved that have been defined between
   *     the type variable declaration and the containingType.
   */
  private static Type resolve(TypeVariable typeVariable, Type containingType) {
    // The generic declaration is either a Class, Method or Constructor
    final GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();

    if (!(genericDeclaration instanceof Class)) {
      // It's a method or constructor. The best we can do here is try to resolve the bounds
      // e.g. <T extends E> T getT(T param){} where E is defined by the class.
      final Type bounds0 = typeVariable.getBounds()[0];
      return resolve(bounds0, containingType);
    }

    final Class typeVariableOwner = (Class) genericDeclaration;

    // find the typeOwner in the containingType's hierarchy
    final LinkedList<Type> stack = new LinkedList<Type>();

    // If you pass a List<Long> as the containingType then the TypeVariable is going to be resolved
    // by the
    // containingType and not the super class.
    if (containingType instanceof ParameterizedType) {
      stack.add(containingType);
    }

    Class theClass = asClass(containingType);
    Type genericSuperclass = theClass.getGenericSuperclass();
    while (genericSuperclass != null
        && // true for interfaces with no superclass
        !theClass.equals(Object.class)
        && !theClass.equals(typeVariableOwner)) {
      stack.addFirst(genericSuperclass);
      theClass = asClass(genericSuperclass);
      genericSuperclass = theClass.getGenericSuperclass();
    }

    int i = getTypeVariableIndex(typeVariable);
    Type resolved = typeVariable;
    for (Type t : stack) {
      if (t instanceof ParameterizedType) {
        resolved = ((ParameterizedType) t).getActualTypeArguments()[i];
        if (resolved instanceof Class) return resolved;
        if (resolved instanceof TypeVariable) {
          // Need to look at the next class in the hierarchy
          i = getTypeVariableIndex((TypeVariable) resolved);
          continue;
        }
        return resolve(resolved, containingType);
      }
    }

    // the only way we get here is if resolved is still a TypeVariable, otherwise an
    // exception is thrown or a value is returned.
    return ((TypeVariable) resolved).getBounds()[0];
  }
 static Class<?> getTypeFromXmlAdapter(XmlJavaTypeAdapter xjta) {
   if (xjta != null) {
     Class<?> c2 = xjta.value();
     Type sp = c2.getGenericSuperclass();
     while (!XmlAdapter.class.equals(c2) && c2 != null) {
       sp = c2.getGenericSuperclass();
       c2 = c2.getSuperclass();
     }
     if (sp instanceof ParameterizedType) {
       return (Class<?>) ((ParameterizedType) sp).getActualTypeArguments()[0];
     }
   }
   return null;
 }