You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by "gnodet (via GitHub)" <gi...@apache.org> on 2025/01/10 17:09:00 UTC

[PR] Rename BaseRequest#unmodifiable to copyToImmutable and add doc [maven]

gnodet opened a new pull request, #2037:
URL: https://github.com/apache/maven/pull/2037

   (no comment)


-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rename BaseRequest#unmodifiable to copyToImmutable and add doc [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #2037:
URL: https://github.com/apache/maven/pull/2037#discussion_r1912070489


##########
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

Review Comment:
   It's just a side effect of using `List.copyOf()`.  The behavior thus surfaces to this helper methods and the javadoc mentions it.



-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [MNG-8484] Replace BaseRequest#unmodifiable with List.copyOf() [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet merged PR #2037:
URL: https://github.com/apache/maven/pull/2037


-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rename BaseRequest#unmodifiable to copyToImmutable and add doc [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #2037:
URL: https://github.com/apache/maven/pull/2037#discussion_r1912529432


##########
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:
   You're right.  But then, I'll go further and just remove the method and use `List.copyOf(xxx)` in the 5 places where this method is actually used.



-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rename BaseRequest#unmodifiable to copyToImmutable and add doc [maven]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
elharo commented on code in PR #2037:
URL: https://github.com/apache/maven/pull/2037#discussion_r1912014544


##########
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

Review Comment:
   Why do we do this? In general, lists with null elements are OK, though they might be wrong in a specific case. However if null elements are wrong in the specific case I'd expect the source list to already have checked that. 



-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rename BaseRequest#unmodifiable to copyToImmutable and add doc [maven]

Posted by "gnodet (via GitHub)" <gi...@apache.org>.
gnodet commented on code in PR #2037:
URL: https://github.com/apache/maven/pull/2037#discussion_r1912120793


##########
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

Review Comment:
   In general, non null elements in list are a burden. Unless there's a specific semantic associated with a null value, I'd rather forbid those.  For example if you want to resolve a list of artifacts, passing null values makes absolutely no sense to me.



-- 
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Rename BaseRequest#unmodifiable to copyToImmutable and add doc [maven]

Posted by "elharo (via GitHub)" <gi...@apache.org>.
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-unsubscribe@maven.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org