Skip to content

Conversation

@bengbengbalabalabeng
Copy link

Purpose of the pull request

Fixed: #665

Avoid UnsupportedOperationException when using immutable list (such as: Arrays.asListList.of or Collections.unmodifiableList ...) in dynamic header

What's changed?

  • Modified AbstractParameterBuilder#head to always convert head into a mutable deep copy, preventing UnsupportedOperationException with immutable lists.
  • Added unit tests (ImmutableListHeadDataTest).

Checklist

  • I have read the Contributor Guide.
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

This prevents UnsupportedOperationException when using immutable lists such as Arrays.asList、List.of or Collections.unmodifiableList ...

Assertions.assertEquals(1, list.size());
Map<Integer, String> data = list.get(0);
Assertions.assertEquals("字符串0", data.get(0));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is best to use English.


import java.io.File;
import java.text.ParseException;
import java.util.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using import *

- Use English to describe
- Avoid use imports *
@bengbengbalabalabeng
Copy link
Author

Thanks for the review. I’ve updated the test code as suggested.

@delei delei added the PR: first-time contributor first-time contributor label Nov 1, 2025
@bengbengbalabalabeng
Copy link
Author

Concern on the eager to new, any potential performance impact here?

And meanwhile, head.stream().map(ArrayList::new).collect(Collectors.toList()) is not the deep copy.

I considered another approach in #665:

In cn.idev.excel.metadata.property.ExcelHeadProperty#ExcelHeadProperty constructor.

  • Wrap head.get(i) with ArrayList<String>.
  • But since the constructor may run multiple times during debugging, this could lead to repeated wrapping.

So I decided not to adopt that solution.

Copy link
Member

@alaahong alaahong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concern on the eager to new, any potential performance impact here?

And meanwhile, head.stream().map(ArrayList::new).collect(Collectors.toList()) is not the deep copy.

if (null == head || head.isEmpty()) {
return head;
}
return head.stream().map(ArrayList::new).collect(Collectors.toList());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not directly new ArrayList as no any map or reduce required?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’ve replaced the previous stream implementation with a simple for loop to create the copy. I also updated the method description: changed "fully mutable deep copy ..." to "fully mutable of head list" which more accurately reflects the actual behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

PR: first-time contributor first-time contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] An UnsupportedOperationException occurred when setting the dynamic header

3 participants