private Object coerceToExpectedType()

in src/com/facebook/buck/rules/ParamInfo.java [269:316]


  private Object coerceToExpectedType(BuildRuleResolver ruleResolver, Object value) {
    if (BuildRule.class.isAssignableFrom(type)) {
      BuildTarget target = asBuildTarget(value);
      return ruleResolver.get(target);
    }

    if (BuildTarget.class.isAssignableFrom(type)) {
      return asBuildTarget(value);
    }

    // All paths should be relative to the base path.
    if (Path.class.isAssignableFrom(type)) {
      return asNormalizedPath(value);
    }

    if (SourcePath.class.isAssignableFrom(type)) {
      BuildTarget target = asBuildTarget(value);
      if (target != null) {
        return new BuildTargetSourcePath(target);
      }
      Path path = asNormalizedPath(value);
      return new FileSourcePath(pathRelativeToProjectRoot.relativize(path).toString());
    }

    if (value instanceof Number) {
      Number num = (Number) value;
      if (Double.class.equals(type)) {
        return num.doubleValue();
      } else if (Integer.class.equals(type)) {
        return num.intValue();
      } else if (Float.class.equals(type)) {
        return num.floatValue();
      } else if (Long.class.equals(type)) {
        return num.longValue();  // not strictly necessary, but included for completeness.
      } else if (Short.class.equals(type)) {
        return num.shortValue();
      }
    }

    // We're going to cheat and let the JVM take the strain of converting between primitive and
    // object wrapper types, but it has a habit of coercing to a String that should be avoided.
    if (String.class.equals(type) && !String.class.equals(value.getClass())) {
      throw new IllegalArgumentException(
          String.format("Unable to convert '%s' to %s", value, type));
    }

    return value;
  }