/** * Return all possible types that can be expected when an element of the given type is parsed. * If the type satisfies {@link #isEither(Type)}, a list of the corresponding type arguments is returned, * otherwise a list containg the type itself is returned. Type parameters are <em>not</em> resolved * by this method (use {@link #getElementTypes(TypeToken, Class)} to get resolved parameters). */ public static Collection<Type> getExpectedTypes(Type type) { Collection<Type> result = new ArrayList<>(); collectExpectedTypes(type, result); return result; }
/** * Return all possible types that can be expected when an element of the given type is parsed. * If the type satisfies {@link #isEither(Type)}, a list of the corresponding type arguments is returned, * otherwise a list containg the type itself is returned. Type parameters are <em>not</em> resolved * by this method (use {@link #getElementTypes(TypeToken, Class)} to get resolved parameters). */ public static Collection<Type> getExpectedTypes(Type type) { Collection<Type> result = new ArrayList<>(); collectExpectedTypes(type, result); return result; }
private static void collectExpectedTypes(Type type, Collection<Type> types) { if (isEither(type)) { if (type instanceof ParameterizedType) { for (Type typeArgument : ((ParameterizedType) type).getActualTypeArguments()) { collectExpectedTypes(typeArgument, types); } } if (type instanceof Class) { for (Type typeParameter : ((Class<?>) type).getTypeParameters()) { collectExpectedTypes(typeParameter, types); } } } else { types.add(type); } }
private static void collectExpectedTypes(Type type, Collection<Type> types) { if (isEither(type)) { if (type instanceof ParameterizedType) { for (Type typeArgument : ((ParameterizedType) type).getActualTypeArguments()) { collectExpectedTypes(typeArgument, types); } } if (type instanceof Class) { for (Type typeParameter : ((Class<?>) type).getTypeParameters()) { collectExpectedTypes(typeParameter, types); } } } else { types.add(type); } }