[errorprone] Transport: Suppress ModifyCollectionInEnhancedForLoop

CopyOnWriteArrayList is thread-safe.

See
https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/concurrent/CopyOnWriteArrayList.html
https://errorprone.info/bugpattern/ModifyCollectionInEnhancedForLoop

Change-Id: I97c411e7d171cb39a9c0676b076d48103db6ff88
This commit is contained in:
Matthias Sohn 2023-09-21 12:06:02 +02:00
parent 8302377d91
commit d5bcf199c7
1 changed files with 11 additions and 4 deletions

View File

@ -191,11 +191,13 @@ public static void register(TransportProtocol proto) {
* @param proto * @param proto
* the exact object previously given to register. * the exact object previously given to register.
*/ */
@SuppressWarnings("ModifyCollectionInEnhancedForLoop")
public static void unregister(TransportProtocol proto) { public static void unregister(TransportProtocol proto) {
for (WeakReference<TransportProtocol> ref : protocols) { for (WeakReference<TransportProtocol> ref : protocols) {
TransportProtocol refProto = ref.get(); TransportProtocol refProto = ref.get();
if (refProto == null || refProto == proto) if (refProto == null || refProto == proto) {
protocols.remove(ref); protocols.remove(ref);
}
} }
} }
@ -204,15 +206,17 @@ public static void unregister(TransportProtocol proto) {
* *
* @return an immutable copy of the currently registered protocols. * @return an immutable copy of the currently registered protocols.
*/ */
@SuppressWarnings("ModifyCollectionInEnhancedForLoop")
public static List<TransportProtocol> getTransportProtocols() { public static List<TransportProtocol> getTransportProtocols() {
int cnt = protocols.size(); int cnt = protocols.size();
List<TransportProtocol> res = new ArrayList<>(cnt); List<TransportProtocol> res = new ArrayList<>(cnt);
for (WeakReference<TransportProtocol> ref : protocols) { for (WeakReference<TransportProtocol> ref : protocols) {
TransportProtocol proto = ref.get(); TransportProtocol proto = ref.get();
if (proto != null) if (proto != null) {
res.add(proto); res.add(proto);
else } else {
protocols.remove(ref); protocols.remove(ref);
}
} }
return Collections.unmodifiableList(res); return Collections.unmodifiableList(res);
} }
@ -508,6 +512,7 @@ public static Transport open(Repository local, URIish uri)
* @throws org.eclipse.jgit.errors.TransportException * @throws org.eclipse.jgit.errors.TransportException
* the transport cannot open this URI. * the transport cannot open this URI.
*/ */
@SuppressWarnings("ModifyCollectionInEnhancedForLoop")
public static Transport open(Repository local, URIish uri, String remoteName) public static Transport open(Repository local, URIish uri, String remoteName)
throws NotSupportedException, TransportException { throws NotSupportedException, TransportException {
for (WeakReference<TransportProtocol> ref : protocols) { for (WeakReference<TransportProtocol> ref : protocols) {
@ -541,6 +546,7 @@ public static Transport open(Repository local, URIish uri, String remoteName)
* @throws org.eclipse.jgit.errors.TransportException * @throws org.eclipse.jgit.errors.TransportException
* if transport failed * if transport failed
*/ */
@SuppressWarnings("ModifyCollectionInEnhancedForLoop")
public static Transport open(URIish uri) throws NotSupportedException, TransportException { public static Transport open(URIish uri) throws NotSupportedException, TransportException {
for (WeakReference<TransportProtocol> ref : protocols) { for (WeakReference<TransportProtocol> ref : protocols) {
TransportProtocol proto = ref.get(); TransportProtocol proto = ref.get();
@ -549,8 +555,9 @@ public static Transport open(URIish uri) throws NotSupportedException, Transport
continue; continue;
} }
if (proto.canHandle(uri, null, null)) if (proto.canHandle(uri, null, null)) {
return proto.open(uri); return proto.open(uri);
}
} }
throw new NotSupportedException(MessageFormat.format(JGitText.get().URINotSupported, uri)); throw new NotSupportedException(MessageFormat.format(JGitText.get().URINotSupported, uri));