elharo commented on code in PR #2037: URL: https://github.com/apache/maven/pull/2037#discussion_r1912446057
########## api/maven-api-core/src/main/java/org/apache/maven/api/services/BaseRequest.java: ########## @@ -52,9 +51,16 @@ public static <T> T nonNull(T obj, String message) { return obj; } - protected static <T> Collection<T> unmodifiable(Collection<T> obj) { - return obj != null && !obj.isEmpty() - ? Collections.unmodifiableCollection(new ArrayList<>(obj)) - : Collections.emptyList(); + /** + * Creates a defensive immutable copy of a collection or returns an empty immutable list if the input is null or empty. + * This method is useful when creating immutable objects to ensure collection fields cannot be modified by external code. + * + * @param source the source collection to create an immutable copy from + * @param <T> the type of elements in the collection + * @return an unmodifiable view of the collection, or an empty list if the input is null or empty + * @throws NullPointerException if the collection contains null elements + */ + protected static <T> List<T> copyToImmutable(Collection<T> source) { + return source != null && !source.isEmpty() ? List.copyOf(source) : List.of(); } Review Comment: There are collections which allow nulls and collections which don't. Both are fine in their contexts. As written this method only copies the latter. It feels to me like this should be a generic method that can copy both types of lists, and the question of whether to allow nulls in the collection is made where the source list is created, not here. Since the return value is immutable we don't have to worry about someone adding a null to a list that didn't initially have it. Practically, you could use addAll with a List type that permits nulls instead of List.copyOf to enable this. Or if we make this method package protected, which we probably should anyway, then I think there are guaranteed no nulls in source, and also source is never null so that check can be removed too. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@maven.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org