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 language features where possible #531

Merged
merged 1 commit into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import com.gargoylesoftware.htmlunit.WebClientUtil;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
Expand Down Expand Up @@ -70,7 +68,7 @@ public static Page click(HtmlElement element) throws IOException {
*/
public static boolean hasClassName(HtmlElement element, String className) {
String classAttribute = element.getAttribute("class");
Set<String> classes = new HashSet<>(Arrays.asList(classAttribute.split(" ")));
Set<String> classes = Set.of(classAttribute.split(" "));
return classes.contains(className);
}
}
48 changes: 14 additions & 34 deletions src/main/java/hudson/cli/CLICommandInvoker.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@
import java.io.InputStream;
import java.io.PrintStream;
import java.io.UncheckedIOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Set;
Expand Down Expand Up @@ -74,9 +71,9 @@ public class CLICommandInvoker {
private SecurityContext originalSecurityContext = null;

private InputStream stdin;
private List<String> args = Collections.emptyList();
private List<String> args = List.of();
@Deprecated
private List<Permission> permissions = Collections.emptyList();
private List<Permission> permissions = List.of();
private Locale locale = Locale.ENGLISH;

public CLICommandInvoker(final JenkinsRule rule, final CLICommand command) {
Expand All @@ -99,8 +96,7 @@ public CLICommandInvoker(final JenkinsRule rule, final String command) {
*/
@Deprecated
public CLICommandInvoker authorizedTo(final Permission... permissions) {

this.permissions = Arrays.asList(permissions);
this.permissions = List.of(permissions);
return this;
}

Expand All @@ -126,8 +122,7 @@ public CLICommandInvoker withStdin(final InputStream stdin) {
}

public CLICommandInvoker withArgs(final String... args) {

this.args = Arrays.asList(args);
this.args = List.of(args);
return this;
}

Expand All @@ -153,18 +148,13 @@ public Result invoke() {
throw new RuntimeException(e);
}

final int returnCode;
try {
returnCode =
command.main(
args,
locale,
stdin,
new PrintStream(out, false, outCharset.name()),
new PrintStream(err, false, errCharset.name()));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
final int returnCode =
command.main(
args,
locale,
stdin,
new PrintStream(out, false, outCharset),
new PrintStream(err, false, errCharset));

restoreAuth();

Expand Down Expand Up @@ -203,7 +193,7 @@ protected Boolean hasPermission(Sid u, Permission permission) {
@NonNull
@Override
public Collection<String> getGroups() {
return Collections.emptySet();
return Set.of();
}
}

Expand Down Expand Up @@ -280,25 +270,15 @@ public int returnCode() {
}

public String stdout() {

try {
return out.toString(outCharset.name());
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return out.toString(outCharset);
}

public byte[] stdoutBinary() {
return out.toByteArray();
}

public String stderr() {

try {
return err.toString(errCharset.name());
} catch (UnsupportedEncodingException e) {
throw new AssertionError(e);
}
return err.toString(errCharset);
}

public byte[] stderrBinary() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
Expand Down Expand Up @@ -68,7 +67,7 @@ public ExtractChangeLogSet parse(AbstractBuild build, InputStream changeLogStrea
while ((fileName = br.readLine()) != null) {
entry.addFile(new FileInZip(fileName));
}
return new ExtractChangeLogSet(build, Collections.singletonList(entry));
return new ExtractChangeLogSet(build, List.of(entry));
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/jvnet/hudson/test/ExtractChangeLogSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import hudson.scm.ChangeLogSet;

import java.util.List;
import java.util.Collections;
import java.util.Iterator;


Expand All @@ -42,7 +41,7 @@ public ExtractChangeLogSet(AbstractBuild<?, ?> build, List<ExtractChangeLogParse
for (ExtractChangeLogParser.ExtractChangeLogEntry entry : changeLogs) {
entry.setParent(this);
}
this.changeLogs = Collections.unmodifiableList(changeLogs);
this.changeLogs = List.copyOf(changeLogs);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void saveToChangeLog(File changeLogFile, ExtractChangeLogParser.ExtractCh
}

public void saveToChangeLog(File changeLogFile, Charset charset, ExtractChangeLogParser.ExtractChangeLogEntry changeLog) throws IOException {
try (PrintStream ps = new PrintStream(changeLogFile, charset.name())) {
try (PrintStream ps = new PrintStream(changeLogFile, charset)) {
ps.println(changeLog.getZipFile());
for (String fileName : changeLog.getAffectedPaths()) {
ps.println(fileName);
Expand All @@ -128,6 +128,6 @@ public void saveToChangeLog(File changeLogFile, Charset charset, ExtractChangeLo
protected Object writeReplace() { return new Object(); }

@Override public SCMDescriptor<?> getDescriptor() {
return new SCMDescriptor<ExtractResourceWithChangesSCM>(ExtractResourceWithChangesSCM.class, null) {};
return new SCMDescriptor<>(ExtractResourceWithChangesSCM.class, null) {};
}
}
11 changes: 5 additions & 6 deletions src/main/java/org/jvnet/hudson/test/FakeChangeLogSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import hudson.scm.EditType;
import hudson.scm.NullSCM;
import hudson.scm.RepositoryBrowser;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;
import hudson.scm.SCMRevisionState;
import org.xml.sax.SAXException;
Expand All @@ -45,9 +44,9 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
* Fake SCM implementation that can report arbitrary commits from arbitrary users.
Expand Down Expand Up @@ -80,7 +79,7 @@ public ChangeLogParser createChangeLogParser() {
}

@Override public SCMDescriptor<?> getDescriptor() {
return new SCMDescriptor<SCM>(null) {};
return new SCMDescriptor<>(null) {};
}

public static class ChangelogAction extends InvisibleAction {
Expand All @@ -102,7 +101,7 @@ public FakeChangeLogSet parse(Run build, RepositoryBrowser<?> browser, File chan
return new FakeChangeLogSet(build, action.entries);
}
}
return new FakeChangeLogSet(build, Collections.emptyList());
return new FakeChangeLogSet(build, List.of());
}
}

Expand Down Expand Up @@ -159,7 +158,7 @@ public User getAuthor() {

@Override
public Collection<String> getAffectedPaths() {
return Collections.singleton(path);
return Set.of(path);
}

@Override
Expand All @@ -175,7 +174,7 @@ public EditType getEditType() {
return EditType.EDIT;
}
};
return Collections.singleton(affectedFile);
return Set.of(affectedFile);
}

private static final long serialVersionUID = 1L;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/jvnet/hudson/test/HudsonHomeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.FilePath;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
Expand Down
18 changes: 6 additions & 12 deletions src/main/java/org/jvnet/hudson/test/HudsonTestCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.Iterator;
Expand Down Expand Up @@ -669,7 +668,7 @@ public DumbSlave createSlave(String nodeName, String labels, EnvVars env) throws
synchronized (jenkins) {
DumbSlave slave = new DumbSlave(nodeName, "dummy",
createTmpDir().getPath(), "1", Mode.NORMAL, labels==null?"":labels, createComputerLauncher(env),
RetentionStrategy.NOOP, Collections.emptyList());
RetentionStrategy.NOOP, List.of());
jenkins.addNode(slave);
return slave;
}
Expand Down Expand Up @@ -1005,7 +1004,7 @@ public void assertStringContains(String haystack, String needle) {
* ','-separated list of properties whose help files should exist.
*/
public void assertHelpExists(final Class<? extends Describable> type, final String properties) throws Exception {
executeOnServer(new Callable<Object>() {
executeOnServer(new Callable<>() {
@Override
public Object call() throws Exception {
Descriptor d = jenkins.getDescriptor(type);
Expand Down Expand Up @@ -1265,7 +1264,7 @@ protected void waitUntilNoActivityUpTo(int timeout) throws Exception {
}
}
throw new AssertionError(String.format("Jenkins is still doing something after %dms: queue=%s building=%s",
timeout, Arrays.asList(jenkins.getQueue().getItems()), building));
timeout, List.of(jenkins.getQueue().getItems()), building));
}
}
}
Expand Down Expand Up @@ -1294,12 +1293,7 @@ protected void recipe() throws Exception {
if(r==null) continue;
final Runner runner = r.value().newInstance();
recipes.add(runner);
tearDowns.add(new LenientRunnable() {
@Override
public void run() throws Exception {
runner.tearDown(HudsonTestCase.this,a);
}
});
tearDowns.add(() -> runner.tearDown(HudsonTestCase.this, a));
runner.setup(this,a);
}
} catch (NoSuchMethodException e) {
Expand Down Expand Up @@ -1690,7 +1684,7 @@ public WebRequest addCrumb(WebRequest req) {
com.gargoylesoftware.htmlunit.util.NameValuePair crumb = new com.gargoylesoftware.htmlunit.util.NameValuePair(
jenkins.getCrumbIssuer().getDescriptor().getCrumbRequestField(),
jenkins.getCrumbIssuer().getCrumb( null ));
req.setRequestParameters(Collections.singletonList(crumb));
req.setRequestParameters(List.of(crumb));
return req;
}

Expand Down Expand Up @@ -1786,7 +1780,7 @@ public boolean isLoggable(LogRecord record) {

private static final Logger LOGGER = Logger.getLogger(HudsonTestCase.class.getName());

protected static final List<ToolProperty<?>> NO_PROPERTIES = Collections.emptyList();
protected static final List<ToolProperty<?>> NO_PROPERTIES = List.of();

/**
* Specify this to a TCP/IP port number to have slaves started with the debugger.
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/jvnet/hudson/test/InboundAgentRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -281,20 +279,20 @@ public void start(@NonNull RealJenkinsRule r, Options options) throws Throwable
public void start(AgentArguments agentArguments, Options options) throws Exception {
Objects.requireNonNull(options.getName());
stop(options.getName());
List<String> cmd = new ArrayList<>(Arrays.asList(JavaEnvUtils.getJreExecutable("java"),
List<String> cmd = new ArrayList<>(List.of(JavaEnvUtils.getJreExecutable("java"),
"-Xmx512m",
"-XX:+PrintCommandLineFlags",
"-Djava.awt.headless=true"));
if (JenkinsRule.SLAVE_DEBUG_PORT > 0) {
cmd.add("-Xdebug");
cmd.add("Xrunjdwp:transport=dt_socket,server=y,address=" + (JenkinsRule.SLAVE_DEBUG_PORT + agentArguments.numberOfNodes - 1));
}
cmd.addAll(Arrays.asList(
cmd.addAll(List.of(
"-jar", agentArguments.agentJar.getAbsolutePath(),
"-jnlpUrl", agentArguments.agentJnlpUrl));
if (options.isSecret()) {
// To watch it fail: secret = secret.replace('1', '2');
cmd.addAll(Arrays.asList("-secret", agentArguments.secret));
cmd.addAll(List.of("-secret", agentArguments.secret));
}
cmd.addAll(agentArguments.commandLineArgs);
ProcessBuilder pb = new ProcessBuilder(cmd);
Expand Down Expand Up @@ -410,7 +408,7 @@ private static AgentArguments get(JenkinsRule r, String name) throws IOException
throw new AssertionError("agent " + node + " has no executor");
}
JNLPLauncher launcher = (JNLPLauncher) c.getLauncher();
List<String> commandLineArgs = Collections.emptyList();
List<String> commandLineArgs = List.of();
if (!launcher.getWorkDirSettings().isDisabled()) {
commandLineArgs = launcher.getWorkDirSettings().toCommandLineArgs(c);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private void doTests(TestResult result) throws Exception {

@Override
protected void runGroupedTests(final TestResult result) throws Exception {
h.executeOnServer(new Callable<Object>() {
h.executeOnServer(new Callable<>() {
// this code now inside a request handling thread
@Override
public Object call() throws Exception {
Expand Down