private static ValueTypeInfo computeTypeInfo()

in src/com/facebook/buck/rules/modern/impl/ValueTypeInfoFactory.java [115:206]


  private static ValueTypeInfo<?> computeTypeInfo(Type type) {
    Preconditions.checkArgument(!(type instanceof TypeVariable));
    if (type instanceof WildcardType) {
      WildcardType wildcardType = (WildcardType) type;
      Type[] upperBounds = wildcardType.getUpperBounds();
      Preconditions.checkState(upperBounds.length == 1);
      type = upperBounds[0];
    }
    Preconditions.checkArgument(!(type instanceof WildcardType));

    if (isSimpleType(type)) {
      return ValueTypeInfos.forSimpleType(type);
    } else if (type instanceof Class) {
      Class<?> rawClass = Primitives.wrap((Class<?>) type);
      if (rawClass.equals(Path.class)) {
        throw new IllegalArgumentException(
            "Buildables should not have Path references. Use SourcePath or OutputPath instead");
      }

      if (rawClass.isEnum()) {
        @SuppressWarnings({"unchecked", "rawtypes"})
        EnumValueTypeInfo enumValueTypeInfo = new EnumValueTypeInfo(rawClass);
        return enumValueTypeInfo;
      } else if (SourcePath.class.isAssignableFrom(rawClass)) {
        return SourcePathValueTypeInfo.INSTANCE;
      } else if (rawClass.equals(OutputPath.class) || rawClass.equals(PublicOutputPath.class)) {
        return ValueTypeInfos.OutputPathValueTypeInfo.INSTANCE;
      } else if (NonHashableSourcePathContainer.class.isAssignableFrom(rawClass)) {
        return new NonHashableSourcePathContainerValueTypeInfo();
      } else if (BuildTarget.class.isAssignableFrom(rawClass)) {
        return BuildTargetTypeInfo.INSTANCE;
      } else if (UnconfiguredBuildTarget.class.isAssignableFrom(rawClass)) {
        return UnconfiguredBuildTargetTypeInfo.INSTANCE;
      } else if (TargetConfiguration.class.isAssignableFrom(rawClass)) {
        return TargetConfigurationTypeInfo.INSTANCE;
      } else if (Pattern.class.isAssignableFrom(rawClass)) {
        return PatternValueTypeInfo.INSTANCE;
      } else if (Toolchain.class.isAssignableFrom(rawClass)) {
        @SuppressWarnings("unchecked")
        Class<? extends Toolchain> asToolchain = (Class<? extends Toolchain>) rawClass;
        return new ToolchainTypeInfo<>(asToolchain);
      } else if (AddsToRuleKey.class.isAssignableFrom(rawClass)) {
        return DynamicTypeInfo.INSTANCE;
      } else if (HashCode.class.isAssignableFrom(rawClass)) {
        return HashCodeValueTypeInfo.INSTANCE;
      } else if (OutputLabel.class.isAssignableFrom(rawClass)) {
        return OutputLabelValueTypeInfo.INSTANCE;
      }
    } else if (type instanceof ParameterizedType) {
      // This is a parameterized type where one of the parameters requires special handling (i.e.
      // it has input/output path/data).
      ParameterizedType parameterizedType = (ParameterizedType) type;
      Type rawType = parameterizedType.getRawType();
      Preconditions.checkState(rawType instanceof Class<?>);
      Class<?> rawClass = (Class<?>) rawType;

      Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments();
      if (rawClass.equals(Either.class)) {
        Preconditions.checkState(typeArguments.length == 2);
        return new EitherValueTypeInfo<>(forType(typeArguments[0]), forType(typeArguments[1]));
      } else if (rawClass.equals(Pair.class)) {
        // TODO(cjhopman): handle Pair
        throw new UnsupportedOperationException();
      } else if (Supplier.class.isAssignableFrom(rawClass)) {
        Preconditions.checkState(typeArguments.length == 1);
        return new SupplierValueTypeInfo<>(forType(typeArguments[0]));
      } else if (rawClass.equals(ImmutableList.class)) {
        Preconditions.checkState(typeArguments.length == 1);
        return new ImmutableListValueTypeInfo<>(forType(typeArguments[0]));
      } else if (rawClass.equals(ImmutableSortedSet.class)) {
        Preconditions.checkState(typeArguments.length == 1);
        return new ImmutableSortedSetValueTypeInfo<>(forType(typeArguments[0]));
      } else if (rawClass.equals(ImmutableSortedMap.class)) {
        Preconditions.checkState(typeArguments.length == 2);
        return new ImmutableSortedMapValueTypeInfo<>(
            forType(typeArguments[0]), forType(typeArguments[1]));
      } else if (rawClass.equals(ImmutableSet.class)) {
        Preconditions.checkState(typeArguments.length == 1);
        return new ImmutableSetValueTypeInfo<>(forType(typeArguments[0]));
      } else if (rawClass.equals(ImmutableMap.class)) {
        Preconditions.checkState(typeArguments.length == 2);
        return new ImmutableMapValueTypeInfo<>(
            forType(typeArguments[0]), forType(typeArguments[1]));
      } else if (rawClass.equals(Optional.class)) {
        Preconditions.checkState(typeArguments.length == 1);
        return new OptionalValueTypeInfo<>(forType(typeArguments[0]));
      } else if (AddsToRuleKey.class.isAssignableFrom(rawClass)) {
        return DynamicTypeInfo.INSTANCE;
      }
    }
    throw new IllegalArgumentException("Cannot create ValueTypeInfo for type: " + type);
  }