/**
   * Resolve current method generic return type to a {@link GenericMetadataSupport}.
   *
   * @param method Method to resolve the return type.
   * @return {@link GenericMetadataSupport} representing this generic return type.
   */
  public GenericMetadataSupport resolveGenericReturnType(Method method) {
    Type genericReturnType = method.getGenericReturnType();
    // logger.log("Method '" + method.toGenericString() + "' has return type : " +
    // genericReturnType.getClass().getInterfaces()[0].getSimpleName() + " : " + genericReturnType);

    if (genericReturnType instanceof Class) {
      return new NotGenericReturnTypeSupport(genericReturnType);
    }
    if (genericReturnType instanceof ParameterizedType) {
      return new ParameterizedReturnType(
          this, method.getTypeParameters(), (ParameterizedType) method.getGenericReturnType());
    }
    if (genericReturnType instanceof TypeVariable) {
      return new TypeVariableReturnType(
          this, method.getTypeParameters(), (TypeVariable) genericReturnType);
    }

    throw new MockitoException(
        "Ouch, it shouldn't happen, type '"
            + genericReturnType.getClass().getCanonicalName()
            + "' on method : '"
            + method.toGenericString()
            + "' is not supported : "
            + genericReturnType);
  }
Exemplo n.º 2
0
 public static Class<?> determineGenericsType(Type type) {
   Class<?> result = null;
   if (type != null && ParameterizedType.class.isAssignableFrom(type.getClass())) {
     Type genericType = ((ParameterizedType) type).getActualTypeArguments()[0];
     if (genericType != null) {
       result = (Class<?>) genericType;
     }
   }
   return result;
 }
  /**
   * Create an new instance of {@link GenericMetadataSupport} inferred from a {@link Type}.
   *
   * <p>At the moment <code>type</code> can only be a {@link Class} or a {@link ParameterizedType},
   * otherwise it'll throw a {@link MockitoException}.
   *
   * @param type The class from which the {@link GenericMetadataSupport} should be built.
   * @return The new {@link GenericMetadataSupport}.
   * @throws MockitoException Raised if type is not a {@link Class} or a {@link ParameterizedType}.
   */
  public static GenericMetadataSupport inferFrom(Type type) {
    Checks.checkNotNull(type, "type");
    if (type instanceof Class) {
      return new FromClassGenericMetadataSupport((Class<?>) type);
    }
    if (type instanceof ParameterizedType) {
      return new FromParameterizedTypeGenericMetadataSupport((ParameterizedType) type);
    }

    throw new MockitoException(
        "Type meta-data for this Type ("
            + type.getClass().getCanonicalName()
            + ") is not supported : "
            + type);
  }
Exemplo n.º 4
0
 public TypeList(Type t) {
   myclass = t.getClass();
   instances = new SafeArrayList<Type>(Type.class);
   hash = new Hashtable<String, Type>();
 }