Skip to content
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

Use Java 11 methods where possible #6700

Merged
merged 14 commits into from Jun 30, 2022
3 changes: 2 additions & 1 deletion bom/pom.xml
Expand Up @@ -40,7 +40,8 @@ THE SOFTWARE.
<properties>
<asm.version>9.3</asm.version>
<slf4jVersion>1.7.36</slf4jVersion>
<stapler.version>1685.v3b_5035c4ce05</stapler.version>
<!-- TODO https://github.com/jenkinsci/stapler/pull/377 -->
<stapler.version>1701.va_9519c27c7c2</stapler.version>
Copy link
Member Author

Choose a reason for hiding this comment

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

<groovy.version>2.4.21</groovy.version>
</properties>

Expand Down
5 changes: 2 additions & 3 deletions cli/src/main/java/hudson/cli/CLI.java
Expand Up @@ -47,7 +47,6 @@
import java.security.KeyPair;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -267,7 +266,7 @@ public boolean verify(String s, SSLSession sslSession) {
}

if (args.isEmpty())
args = Collections.singletonList("help"); // default to help
args = List.of("help"); // default to help

if (mode == null) {
mode = Mode.HTTP;
Expand Down Expand Up @@ -346,7 +345,7 @@ class Authenticator extends ClientEndpointConfig.Configurator {
@Override
public void beforeRequest(Map<String, List<String>> headers) {
if (factory.authorization != null) {
headers.put("Authorization", Collections.singletonList(factory.authorization));
headers.put("Authorization", List.of(factory.authorization));
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions cli/src/main/java/hudson/cli/SSHCLI.java
Expand Up @@ -35,7 +35,6 @@
import java.net.URLConnection;
import java.security.KeyPair;
import java.security.PublicKey;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
Expand Down Expand Up @@ -114,7 +113,7 @@ public boolean verifyServerKey(ClientSession clientSession, SocketAddress remote
WaitableFuture wf = channel.open();
wf.await();

Set<ClientChannelEvent> waitMask = channel.waitFor(Collections.singletonList(ClientChannelEvent.CLOSED), 0L);
Set<ClientChannelEvent> waitMask = channel.waitFor(List.of(ClientChannelEvent.CLOSED), 0L);

if (waitMask.contains(ClientChannelEvent.TIMEOUT)) {
throw new SocketTimeoutException("Failed to retrieve command result in time: " + command);
Expand Down
5 changes: 3 additions & 2 deletions core/src/main/java/hudson/ClassicPluginStrategy.java
Expand Up @@ -50,6 +50,7 @@
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
Expand Down Expand Up @@ -338,7 +339,7 @@ public <T> List<ExtensionComponent<T>> findComponents(Class<T> type, Hudson huds
List<ExtensionFinder> finders;
if (type == ExtensionFinder.class) {
// Avoid infinite recursion of using ExtensionFinders to find ExtensionFinders
finders = Collections.singletonList(new ExtensionFinder.Sezpoz());
finders = List.of(new ExtensionFinder.Sezpoz());
} else {
finders = hudson.getExtensionList(ExtensionFinder.class);
}
Expand Down Expand Up @@ -629,7 +630,7 @@ protected List<PluginWrapper> getEdges(PluginWrapper pw) {
for (Dependency d : dependencies) {
PluginWrapper p = pluginManager.getPlugin(d.shortName);
if (p != null && p.isActive())
cgd.run(Collections.singleton(p));
cgd.run(Set.of(p));
}
} catch (CycleDetectedException e) {
throw new AssertionError(e); // such error should have been reported earlier
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/Functions.java
Expand Up @@ -1945,7 +1945,7 @@ public boolean hyperlinkMatchesCurrentPage(String href) throws UnsupportedEncodi
}

public <T> List<T> singletonList(T t) {
return Collections.singletonList(t);
return List.of(t);
Copy link
Member

Choose a reason for hiding this comment

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

}

/**
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/PluginManager.java
Expand Up @@ -930,7 +930,7 @@ public void dynamicLoad(File arc, boolean removeExisting, @CheckForNull List<Plu
if (batch != null) {
batch.add(p);
} else {
start(Collections.singletonList(p));
start(List.of(p));
}

} catch (Exception e) {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/PluginWrapper.java
Expand Up @@ -222,7 +222,7 @@ public boolean hasDerivedDependencyErrors() {
* The core can depend on a plugin if it is bundled. Sometimes it's the only thing that
* depends on the plugin e.g. UI support library bundle plugin.
*/
private static Set<String> CORE_ONLY_DEPENDANT = Collections.singleton("jenkins-core");
private static Set<String> CORE_ONLY_DEPENDANT = Set.of("jenkins-core");

/**
* Set the list of components that depend on this plugin.
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/StructuredForm.java
Expand Up @@ -67,7 +67,7 @@ public static List<JSONObject> toList(JSONObject parent, String propertyName) {
if (v == null)
return Collections.emptyList();
if (v instanceof JSONObject)
return Collections.singletonList((JSONObject) v);
return List.of((JSONObject) v);
if (v instanceof JSONArray)
return (List) v;

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/WebAppMain.java
Expand Up @@ -140,7 +140,7 @@ public void contextInitialized(ServletContextEvent event) {
// Nicer console log formatting when using mvn jetty:run.
if (Main.isDevelopmentMode && System.getProperty("java.util.logging.config.file") == null) {
try {
Formatter formatter = (Formatter) Class.forName("io.jenkins.lib.support_log_formatter.SupportLogFormatter").newInstance();
Formatter formatter = (Formatter) Class.forName("io.jenkins.lib.support_log_formatter.SupportLogFormatter").getDeclaredConstructor().newInstance();
for (Handler h : Logger.getLogger("").getHandlers()) {
if (h instanceof ConsoleHandler) {
((ConsoleHandler) h).setFormatter(formatter);
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/AbstractBuild.java
Expand Up @@ -925,7 +925,7 @@ public boolean tearDown(AbstractBuild build, BuildListener listener) throws IOEx
@Override
@NonNull public List<ChangeLogSet<? extends ChangeLogSet.Entry>> getChangeSets() {
ChangeLogSet<? extends ChangeLogSet.Entry> cs = getChangeSet();
return cs.isEmptySet() ? Collections.emptyList() : Collections.singletonList(cs);
return cs.isEmptySet() ? Collections.emptyList() : List.of(cs);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/model/Actionable.java
Expand Up @@ -31,6 +31,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -227,7 +228,7 @@ public boolean removeAction(@Nullable Action a) {
return false;
}
// CopyOnWriteArrayList does not support Iterator.remove, so need to do it this way:
return getActions().removeAll(Collections.singleton(a));
return getActions().removeAll(Set.of(a));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/hudson/model/Descriptor.java
Expand Up @@ -59,13 +59,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -863,7 +863,7 @@ Permission getRequiredGlobalConfigPagePermission() {
}

private String getViewPage(Class<?> clazz, String pageName, String defaultValue) {
return getViewPage(clazz, Collections.singleton(pageName), defaultValue);
return getViewPage(clazz, Set.of(pageName), defaultValue);
}

private String getViewPage(Class<?> clazz, Collection<String> pageNames, String defaultValue) {
Expand Down Expand Up @@ -978,7 +978,7 @@ public void doHelp(StaplerRequest req, StaplerResponse rsp) throws IOException,
rsp.setContentType("text/html;charset=UTF-8");
try (InputStream in = url.openStream()) {
String literal = IOUtils.toString(in, StandardCharsets.UTF_8);
rsp.getWriter().println(Util.replaceMacro(literal, Collections.singletonMap("rootURL", req.getContextPath())));
rsp.getWriter().println(Util.replaceMacro(literal, Map.of("rootURL", req.getContextPath())));
}
return;
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/model/DirectoryBrowserSupport.java
Expand Up @@ -39,7 +39,6 @@
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -764,7 +763,7 @@ private static List<List<Path>> buildChildPaths(VirtualFile cur, Locale locale)
for (VirtualFile f : files) {
Path p = new Path(Util.rawEncode(f.getName()), f.getName(), f.isDirectory(), f.length(), f.canRead(), f.lastModified());
if (!f.isDirectory()) {
r.add(Collections.singletonList(p));
r.add(List.of(p));
} else {
// find all empty intermediate directory
List<Path> l = new ArrayList<>();
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/JDK.java
Expand Up @@ -197,7 +197,7 @@ public List<? extends ToolInstaller> getDefaultInstallers() {
Class<? extends ToolInstaller> jdkInstallerClass = Jenkins.get().getPluginManager()
.uberClassLoader.loadClass("hudson.tools.JDKInstaller").asSubclass(ToolInstaller.class);
Constructor<? extends ToolInstaller> constructor = jdkInstallerClass.getConstructor(String.class, boolean.class);
return Collections.singletonList(constructor.newInstance(null, false));
return List.of(constructor.newInstance(null, false));
} catch (ClassNotFoundException e) {
return Collections.emptyList();
} catch (Exception e) {
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/model/Job.java
Expand Up @@ -81,6 +81,7 @@
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -505,7 +506,7 @@ public void suggest(String token, List<SearchItem> result) {

@Override
public Collection<? extends Job> getAllJobs() {
return Collections.<Job>singleton(this);
return Set.of(this);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/model/JobProperty.java
Expand Up @@ -36,6 +36,7 @@
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import jenkins.model.Jenkins;
import jenkins.model.OptionalJobProperty;
import net.sf.json.JSONObject;
Expand Down Expand Up @@ -134,7 +135,7 @@ public Collection<? extends Action> getJobActions(J job) {
// delegate to getJobAction (singular) for backward compatible behavior
Action a = getJobAction(job);
if (a == null) return Collections.emptyList();
return Collections.singletonList(a);
return List.of(a);
}

//
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/ListView.java
Expand Up @@ -143,7 +143,7 @@ protected Object readResolve() {
includePattern = Pattern.compile(includeRegex);
} catch (PatternSyntaxException x) {
includeRegex = null;
OldDataMonitor.report(this, Collections.singleton(x));
OldDataMonitor.report(this, Set.of(x));
}
}
synchronized (this) {
Expand Down
Expand Up @@ -39,8 +39,8 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
Expand Down Expand Up @@ -112,7 +112,7 @@ public List<String> getParameterDefinitionNames() {
@NonNull
@Override
public Collection<Action> getJobActions(Job<?, ?> job) {
return Collections.singleton(this);
return Set.of(this);
}

@Deprecated
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/Queue.java
Expand Up @@ -1987,7 +1987,7 @@ default boolean isConcurrentBuild() {
* @since 1.377
*/
default Collection<? extends SubTask> getSubTasks() {
return Collections.singleton(this);
return Set.of(this);
}

/**
Expand Down
5 changes: 1 addition & 4 deletions core/src/main/java/hudson/model/Slave.java
Expand Up @@ -59,10 +59,7 @@
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -744,5 +741,5 @@ private Object readResolve() {
/**
* Provides a collection of file names, which are accessible via /jnlpJars link.
*/
private static final Set<String> ALLOWED_JNLPJARS_FILES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("agent.jar", "slave.jar", "remoting.jar", "jenkins-cli.jar", "hudson-cli.jar")));
private static final Set<String> ALLOWED_JNLPJARS_FILES = Set.of("agent.jar", "slave.jar", "remoting.jar", "jenkins-cli.jar", "hudson-cli.jar");
}
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/User.java
Expand Up @@ -441,7 +441,7 @@ private LegitimateButUnknownUserDetails(String username) throws IllegalArgumentE
super(
username, "",
true, true, true, true,
Collections.singleton(SecurityRealm.AUTHENTICATED_AUTHORITY2)
Set.of(SecurityRealm.AUTHENTICATED_AUTHORITY2)
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/model/labels/LabelAtom.java
Expand Up @@ -170,7 +170,7 @@ public <V, P> V accept(LabelVisitor<V, P> visitor, P param) {

@Override
public Set<LabelAtom> listAtoms() {
return Collections.singleton(this);
return Set.of(this);
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/hudson/scm/EditType.java
Expand Up @@ -24,8 +24,6 @@

package hudson.scm;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.kohsuke.stapler.export.CustomExportedBean;

Expand Down Expand Up @@ -60,5 +58,5 @@ public String toExportedObject() {
public static final EditType EDIT = new EditType("edit", "The file was modified");
public static final EditType DELETE = new EditType("delete", "The file was removed");

public static final List<EditType> ALL = Collections.unmodifiableList(Arrays.asList(ADD, EDIT, DELETE));
public static final List<EditType> ALL = List.of(ADD, EDIT, DELETE);
}
Expand Up @@ -56,6 +56,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -586,7 +587,7 @@ public User getUser(String id) {
}

// TODO
private static final Collection<? extends GrantedAuthority> TEST_AUTHORITY = Collections.singleton(AUTHENTICATED_AUTHORITY2);
private static final Collection<? extends GrantedAuthority> TEST_AUTHORITY = Set.of(AUTHENTICATED_AUTHORITY2);

public static final class SignupInfo {
public String username, password1, password2, fullname, email, captcha;
Expand Down
Expand Up @@ -28,7 +28,7 @@
import hudson.Extension;
import hudson.model.Descriptor;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;
import jenkins.model.Jenkins;
import org.acegisecurity.acls.sid.GrantedAuthoritySid;
import org.jenkinsci.Symbol;
Expand All @@ -55,7 +55,7 @@ public ACL getRootACL() {

@Override
public Collection<String> getGroups() {
return Collections.singleton("admin");
return Set.of("admin");
}

@Extension @Symbol("legacy")
Expand Down
4 changes: 1 addition & 3 deletions core/src/main/java/hudson/security/Permission.java
Expand Up @@ -27,10 +27,8 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.model.Hudson;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
Expand Down Expand Up @@ -150,7 +148,7 @@ public Permission(@NonNull PermissionGroup group, @NonNull String name,
this.description = description;
this.impliedBy = impliedBy;
this.enabled = enable;
this.scopes = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(scopes)));
this.scopes = Set.of(scopes);
this.id = owner.getName() + '.' + name;

group.add(this);
Expand Down