-
Notifications
You must be signed in to change notification settings - Fork 111
GH-130: Fix AutoCloseables to work with @Nullable structures #1017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jbonofre
merged 1 commit into
apache:main
from
axreldable:ARROW-130-checker-framework-autocloseables-annotations
Feb 17, 2026
+311
−18
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,9 @@ | |
| import java.util.Collections; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.stream.Stream; | ||
| import java.util.stream.StreamSupport; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| /** Utilities for AutoCloseable classes. */ | ||
| public final class AutoCloseables { | ||
|
|
@@ -33,7 +35,8 @@ private AutoCloseables() {} | |
| * Returns a new {@link AutoCloseable} that calls {@link #close(Iterable)} on <code>autoCloseables | ||
| * </code> when close is called. | ||
| */ | ||
| public static AutoCloseable all(final Collection<? extends AutoCloseable> autoCloseables) { | ||
| public static AutoCloseable all( | ||
| final @Nullable Collection<? extends @Nullable AutoCloseable> autoCloseables) { | ||
| return new AutoCloseable() { | ||
| @Override | ||
| public void close() throws Exception { | ||
|
|
@@ -48,7 +51,10 @@ public void close() throws Exception { | |
| * @param t the throwable to add suppressed exception to | ||
| * @param autoCloseables the closeables to close | ||
| */ | ||
| public static void close(Throwable t, AutoCloseable... autoCloseables) { | ||
| public static void close(Throwable t, @Nullable AutoCloseable... autoCloseables) { | ||
| if (autoCloseables == null) { | ||
| return; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That should not happen, but it seems you have a case ;)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, they are saying it's not overly cautious. |
||
| } | ||
| close(t, Arrays.asList(autoCloseables)); | ||
| } | ||
|
|
||
|
|
@@ -58,7 +64,8 @@ public static void close(Throwable t, AutoCloseable... autoCloseables) { | |
| * @param t the throwable to add suppressed exception to | ||
| * @param autoCloseables the closeables to close | ||
| */ | ||
| public static void close(Throwable t, Iterable<? extends AutoCloseable> autoCloseables) { | ||
| public static void close( | ||
| Throwable t, @Nullable Iterable<? extends @Nullable AutoCloseable> autoCloseables) { | ||
| try { | ||
| close(autoCloseables); | ||
| } catch (Exception e) { | ||
|
|
@@ -71,7 +78,10 @@ public static void close(Throwable t, Iterable<? extends AutoCloseable> autoClos | |
| * | ||
| * @param autoCloseables the closeables to close | ||
| */ | ||
| public static void close(AutoCloseable... autoCloseables) throws Exception { | ||
| public static void close(@Nullable AutoCloseable... autoCloseables) throws Exception { | ||
| if (autoCloseables == null) { | ||
| return; | ||
| } | ||
| close(Arrays.asList(autoCloseables)); | ||
| } | ||
|
|
||
|
|
@@ -80,7 +90,8 @@ public static void close(AutoCloseable... autoCloseables) throws Exception { | |
| * | ||
| * @param ac the closeables to close | ||
| */ | ||
| public static void close(Iterable<? extends AutoCloseable> ac) throws Exception { | ||
| public static void close(@Nullable Iterable<? extends @Nullable AutoCloseable> ac) | ||
| throws Exception { | ||
| // this method can be called on a single object if it implements Iterable<AutoCloseable> | ||
| // like for example VectorContainer make sure we handle that properly | ||
| if (ac == null) { | ||
|
|
@@ -111,12 +122,17 @@ public static void close(Iterable<? extends AutoCloseable> ac) throws Exception | |
|
|
||
| /** Calls {@link #close(Iterable)} on the flattened list of closeables. */ | ||
| @SafeVarargs | ||
| public static void close(Iterable<? extends AutoCloseable>... closeables) throws Exception { | ||
| public static void close(@Nullable Iterable<? extends @Nullable AutoCloseable>... closeables) | ||
| throws Exception { | ||
| if (closeables == null) { | ||
| return; | ||
| } | ||
| close(flatten(closeables)); | ||
| } | ||
|
|
||
| @SafeVarargs | ||
| private static Iterable<AutoCloseable> flatten(Iterable<? extends AutoCloseable>... closeables) { | ||
| private static Iterable<AutoCloseable> flatten( | ||
| Iterable<? extends @Nullable AutoCloseable>... closeables) { | ||
| return new Iterable<AutoCloseable>() { | ||
| // Cast from Iterable<? extends AutoCloseable> to Iterable<AutoCloseable> is safe in this | ||
| // context | ||
|
|
@@ -127,16 +143,18 @@ public Iterator<AutoCloseable> iterator() { | |
| return Arrays.stream(closeables) | ||
| .flatMap( | ||
| (Iterable<? extends AutoCloseable> i) -> | ||
| StreamSupport.stream( | ||
| ((Iterable<AutoCloseable>) i).spliterator(), /* parallel= */ false)) | ||
| i == null | ||
| ? Stream.empty() | ||
| : StreamSupport.stream( | ||
| ((Iterable<AutoCloseable>) i).spliterator(), /* parallel= */ false)) | ||
| .iterator(); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| /** Converts <code>ac</code> to a {@link Iterable} filtering out any null values. */ | ||
| public static Iterable<AutoCloseable> iter(AutoCloseable... ac) { | ||
| if (ac.length == 0) { | ||
| public static Iterable<AutoCloseable> iter(@Nullable AutoCloseable... ac) { | ||
| if (ac == null || ac.length == 0) { | ||
| return Collections.emptyList(); | ||
| } else { | ||
| final List<AutoCloseable> nonNullAc = new ArrayList<>(); | ||
|
|
@@ -153,10 +171,11 @@ public static Iterable<AutoCloseable> iter(AutoCloseable... ac) { | |
| public static class RollbackCloseable implements AutoCloseable { | ||
|
|
||
| private boolean commit = false; | ||
| private List<AutoCloseable> closeables; | ||
| private final List<AutoCloseable> closeables; | ||
|
|
||
| public RollbackCloseable(AutoCloseable... closeables) { | ||
| this.closeables = new ArrayList<>(Arrays.asList(closeables)); | ||
| public RollbackCloseable(@Nullable AutoCloseable... closeables) { | ||
| this.closeables = | ||
| closeables == null ? new ArrayList<>() : new ArrayList<>(Arrays.asList(closeables)); | ||
| } | ||
|
|
||
| public <T extends AutoCloseable> T add(T t) { | ||
|
|
@@ -165,12 +184,18 @@ public <T extends AutoCloseable> T add(T t) { | |
| } | ||
|
|
||
| /** Add all of <code>list</code> to the rollback list. */ | ||
| public void addAll(AutoCloseable... list) { | ||
| public void addAll(@Nullable AutoCloseable... list) { | ||
| if (list == null) { | ||
| return; | ||
| } | ||
| closeables.addAll(Arrays.asList(list)); | ||
| } | ||
|
|
||
| /** Add all of <code>list</code> to the rollback list. */ | ||
| public void addAll(Iterable<? extends AutoCloseable> list) { | ||
| public void addAll(@Nullable Iterable<? extends @Nullable AutoCloseable> list) { | ||
| if (list == null) { | ||
| return; | ||
| } | ||
| for (AutoCloseable ac : list) { | ||
| closeables.add(ac); | ||
| } | ||
|
|
@@ -189,7 +214,7 @@ public void close() throws Exception { | |
| } | ||
|
|
||
| /** Creates an {@link RollbackCloseable} from the given closeables. */ | ||
| public static RollbackCloseable rollbackable(AutoCloseable... closeables) { | ||
| public static RollbackCloseable rollbackable(@Nullable AutoCloseable... closeables) { | ||
| return new RollbackCloseable(closeables); | ||
| } | ||
|
|
||
|
|
@@ -203,7 +228,7 @@ public static RollbackCloseable rollbackable(AutoCloseable... closeables) { | |
| * @throws RuntimeException if an Exception occurs; the Exception is wrapped by the | ||
| * RuntimeException | ||
| */ | ||
| public static void closeNoChecked(final AutoCloseable autoCloseable) { | ||
| public static void closeNoChecked(final @Nullable AutoCloseable autoCloseable) { | ||
| if (autoCloseable != null) { | ||
| try { | ||
| autoCloseable.close(); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have problem to add
@Nullablehere.