// This is safe because of the type checking @SuppressWarnings("unchecked") private SkylarkNestedSet( Order order, SkylarkType contentType, Object item, Location loc, List<Object> items, List<NestedSet<Object>> transitiveItems) throws EvalException { // Adding the item if (item instanceof SkylarkNestedSet) { SkylarkNestedSet nestedSet = (SkylarkNestedSet) item; if (!nestedSet.isEmpty()) { contentType = checkType(contentType, nestedSet.contentType, loc); transitiveItems.add((NestedSet<Object>) nestedSet.set); } } else if (item instanceof SkylarkList) { // TODO(bazel-team): we should check ImmutableList here but it screws up genrule at line 43 for (Object object : (SkylarkList) item) { contentType = checkType(contentType, SkylarkType.of(object.getClass()), loc); checkImmutable(object, loc); items.add(object); } } else { throw new EvalException( loc, String.format("cannot add value of type '%s' to a set", EvalUtils.getDataTypeName(item))); } this.contentType = Preconditions.checkNotNull(contentType, "type cannot be null"); // Initializing the real nested set NestedSetBuilder<Object> builder = new NestedSetBuilder<>(order); builder.addAll(items); try { for (NestedSet<Object> nestedSet : transitiveItems) { builder.addTransitive(nestedSet); } } catch (IllegalStateException e) { throw new EvalException(loc, e.getMessage()); } this.set = builder.build(); this.items = ImmutableList.copyOf(items); this.transitiveItems = ImmutableList.copyOf(transitiveItems); }
/** * A not type safe constructor for SkylarkNestedSet, specifying type as a Java class. It's * discouraged to use it unless type generic safety is guaranteed from the caller side. */ public SkylarkNestedSet(Class<?> contentType, NestedSet<?> set) { this(SkylarkType.of(contentType), set); }
/** Returns a type safe SkylarkNestedSet. Use this instead of the constructor if possible. */ public static <T> SkylarkNestedSet of(Class<T> contentType, NestedSet<T> set) { return of(SkylarkType.of(contentType), set); }