8298380: Clean up redundant array length checks in JDK code base

Reviewed-by: dholmes, amenkov, serb, vtewari
This commit is contained in:
Sergey Tsypanov 2022-12-09 12:50:55 +00:00 committed by Julian Waters
parent 33d955ad6e
commit e3c6cf8eaf
8 changed files with 37 additions and 51 deletions

View file

@ -226,15 +226,13 @@ class LinuxWatchService
} }
// no modifiers supported at this time // no modifiers supported at this time
if (modifiers.length > 0) { for (WatchEvent.Modifier modifier : modifiers) {
for (WatchEvent.Modifier modifier: modifiers) { if (modifier == null)
if (modifier == null) return new NullPointerException();
return new NullPointerException(); if (!ExtendedOptions.SENSITIVITY_HIGH.matches(modifier) &&
if (!ExtendedOptions.SENSITIVITY_HIGH.matches(modifier) && !ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier) &&
!ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier) && !ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) {
!ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) { return new UnsupportedOperationException("Modifier not supported");
return new UnsupportedOperationException("Modifier not supported");
}
} }
} }

View file

@ -411,13 +411,11 @@ public abstract class FileSystemProvider {
public InputStream newInputStream(Path path, OpenOption... options) public InputStream newInputStream(Path path, OpenOption... options)
throws IOException throws IOException
{ {
if (options.length > 0) { for (OpenOption opt : options) {
for (OpenOption opt: options) { // All OpenOption values except for APPEND and WRITE are allowed
// All OpenOption values except for APPEND and WRITE are allowed if (opt == StandardOpenOption.APPEND ||
if (opt == StandardOpenOption.APPEND || opt == StandardOpenOption.WRITE)
opt == StandardOpenOption.WRITE) throw new UnsupportedOperationException("'" + opt + "' not allowed");
throw new UnsupportedOperationException("'" + opt + "' not allowed");
}
} }
ReadableByteChannel rbc = Files.newByteChannel(path, options); ReadableByteChannel rbc = Files.newByteChannel(path, options);
if (rbc instanceof FileChannelImpl) { if (rbc instanceof FileChannelImpl) {

View file

@ -627,11 +627,9 @@ final class JrtPath implements Path {
} }
final InputStream newInputStream(OpenOption... options) throws IOException { final InputStream newInputStream(OpenOption... options) throws IOException {
if (options.length > 0) { for (OpenOption opt : options) {
for (OpenOption opt : options) { if (opt != READ) {
if (opt != READ) { throw new UnsupportedOperationException("'" + opt + "' not allowed");
throw new UnsupportedOperationException("'" + opt + "' not allowed");
}
} }
} }
return jrtfs.newInputStream(this); return jrtfs.newInputStream(this);

View file

@ -119,20 +119,18 @@ class PollingWatchService
// Extended modifiers may be used to specify the sensitivity level // Extended modifiers may be used to specify the sensitivity level
int sensitivity = DEFAULT_POLLING_INTERVAL; int sensitivity = DEFAULT_POLLING_INTERVAL;
if (modifiers.length > 0) { for (WatchEvent.Modifier modifier : modifiers) {
for (WatchEvent.Modifier modifier: modifiers) { if (modifier == null)
if (modifier == null) throw new NullPointerException();
throw new NullPointerException();
if (ExtendedOptions.SENSITIVITY_HIGH.matches(modifier)) { if (ExtendedOptions.SENSITIVITY_HIGH.matches(modifier)) {
sensitivity = ExtendedOptions.SENSITIVITY_HIGH.parameter(); sensitivity = ExtendedOptions.SENSITIVITY_HIGH.parameter();
} else if (ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier)) { } else if (ExtendedOptions.SENSITIVITY_MEDIUM.matches(modifier)) {
sensitivity = ExtendedOptions.SENSITIVITY_MEDIUM.parameter(); sensitivity = ExtendedOptions.SENSITIVITY_MEDIUM.parameter();
} else if (ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) { } else if (ExtendedOptions.SENSITIVITY_LOW.matches(modifier)) {
sensitivity = ExtendedOptions.SENSITIVITY_LOW.parameter(); sensitivity = ExtendedOptions.SENSITIVITY_LOW.parameter();
} else { } else {
throw new UnsupportedOperationException("Modifier not supported"); throw new UnsupportedOperationException("Modifier not supported");
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -653,10 +653,8 @@ public class AquaFileChooserUI extends FileChooserUI {
int selectableCount = 0; int selectableCount = 0;
// Double-check that all the list selections are valid for this mode // Double-check that all the list selections are valid for this mode
// Directories can be selected in the list regardless of mode // Directories can be selected in the list regardless of mode
if (rows.length > 0) { for (final int element : rows) {
for (final int element : rows) { if (isSelectableForMode(chooser, (File) fFileList.getValueAt(element, 0))) selectableCount++;
if (isSelectableForMode(chooser, (File)fFileList.getValueAt(element, 0))) selectableCount++;
}
} }
if (selectableCount > 0) { if (selectableCount > 0) {
final File[] files = new File[selectableCount]; final File[] files = new File[selectableCount];

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2011, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -89,10 +89,8 @@ public final class Assumptions implements Iterable<Assumptions.Assumption> {
public void recordTo(Assumptions target) { public void recordTo(Assumptions target) {
assert canRecordTo(target); assert canRecordTo(target);
if (assumptions.length > 0) { for (Assumption assumption : assumptions) {
for (Assumption assumption : assumptions) { target.record(assumption);
target.record(assumption);
}
} }
} }

View file

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
@ -649,7 +649,7 @@ public class XTree extends JTree {
Messages.NOTIFICATIONS, null); Messages.NOTIFICATIONS, null);
notifications.setUserObject(notificationsUO); notifications.setUserObject(notificationsUO);
node.insert(notifications, childIndex++); node.insert(notifications, childIndex++);
if (ni != null && ni.length > 0) { if (ni != null) {
for (MBeanNotificationInfo mbni : ni) { for (MBeanNotificationInfo mbni : ni) {
DefaultMutableTreeNode notification = DefaultMutableTreeNode notification =
new DefaultMutableTreeNode(); new DefaultMutableTreeNode();

View file

@ -744,11 +744,9 @@ final class ZipPath implements Path {
InputStream newInputStream(OpenOption... options) throws IOException InputStream newInputStream(OpenOption... options) throws IOException
{ {
if (options.length > 0) { for (OpenOption opt : options) {
for (OpenOption opt : options) { if (opt != READ)
if (opt != READ) throw new UnsupportedOperationException("'" + opt + "' not allowed");
throw new UnsupportedOperationException("'" + opt + "' not allowed");
}
} }
return zfs.newInputStream(getResolvedPath()); return zfs.newInputStream(getResolvedPath());
} }