Skip to content

Commit

Permalink
Use Java 17 language features (jenkinsci#1681)
Browse files Browse the repository at this point in the history
* Use Java 17 language features

instanceof pattern matching

Formatted strings

* Remove OpenRewrite config from pom

* Use formatted strings more widely

No need to concatenate a string then use a formatted string for the rest
  • Loading branch information
MarkEWaite authored Oct 21, 2024
1 parent eecf8fc commit 47d8e46
Show file tree
Hide file tree
Showing 68 changed files with 263 additions and 183 deletions.
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/git/BranchSpec.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hudson.model.Descriptor;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -32,6 +33,7 @@
*/
@ExportedBean
public class BranchSpec extends AbstractDescribableImpl<BranchSpec> implements Serializable {
@Serial
private static final long serialVersionUID = -6177158367915899356L;

private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ public List<String> getBranchSpecifier( final Item item ) {
SCMTriggerItem s = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(item);
if(s != null) {
for(SCM scm : s.getSCMs()) {
if (scm instanceof GitSCM) {
GitSCM gitScm = (GitSCM)scm;
if (scm instanceof GitSCM gitScm) {
for(BranchSpec spec : gitScm.getBranches()) {
branchSpec.add(spec.getName());
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/hudson/plugins/git/GitPublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@

import jakarta.servlet.ServletException;
import java.io.IOException;
import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class GitPublisher extends Recorder implements Serializable {
@Serial
private static final long serialVersionUID = 1L;

/**
Expand Down Expand Up @@ -482,6 +484,7 @@ private FormValidation checkFieldNotEmpty(String value, String field) {
}

public static abstract class PushConfig extends AbstractDescribableImpl<PushConfig> implements Serializable {
@Serial
private static final long serialVersionUID = 1L;

private String targetRepoName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public GitRevisionBuildParameters() {
public Action getAction(AbstractBuild<?,?> build, TaskListener listener) {
BuildData data = build.getAction(BuildData.class);
if (data == null && Jenkins.get().getPlugin("promoted-builds") != null) {
if (build instanceof hudson.plugins.promoted_builds.Promotion) {
if (build instanceof hudson.plugins.promoted_builds.Promotion promotion) {
// We are running as a build promotion, so have to retrieve the git scm from target job
AbstractBuild<?,?> targetBuild = ((hudson.plugins.promoted_builds.Promotion) build).getTargetBuild();
AbstractBuild<?,?> targetBuild = promotion.getTargetBuild();
if (targetBuild != null) {
data = targetBuild.getAction(BuildData.class);
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.Serial;
import java.io.Serializable;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.AbstractList;
import java.util.ArrayList;
Expand Down Expand Up @@ -710,13 +711,13 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
listener.getLogger().println("[poll] Last Built Revision: " + buildData.lastBuild.revision);
}

final EnvVars pollEnv = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener, false) : lastBuild.getEnvironment(listener);
final EnvVars pollEnv = project instanceof AbstractProject<?,?> ap ? GitUtils.getPollEnvironment(ap, workspace, launcher, listener, false) : lastBuild.getEnvironment(listener);

final String singleBranch = getSingleBranch(pollEnv);

if (!requiresWorkspaceForPolling(pollEnv)) {

final EnvVars environment = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener, false) : new EnvVars();
final EnvVars environment = project instanceof AbstractProject<?,?> ap ? GitUtils.getPollEnvironment(ap, workspace, launcher, listener, false) : new EnvVars();

GitClient git = createClient(listener, environment, lastBuild, Jenkins.get(), null);

Expand Down Expand Up @@ -784,7 +785,7 @@ private PollingResult compareRemoteRevisionWithImpl(Job<?, ?> project, Launcher
}

final Node node = GitUtils.workspaceToNode(workspace);
final EnvVars environment = project instanceof AbstractProject ? GitUtils.getPollEnvironment((AbstractProject) project, workspace, launcher, listener) : project.getEnvironment(node, listener);
final EnvVars environment = project instanceof AbstractProject<?,?> ap ? GitUtils.getPollEnvironment(ap, workspace, launcher, listener) : project.getEnvironment(node, listener);

FilePath workingDirectory = workingDirectory(project,workspace,environment,listener);

Expand Down Expand Up @@ -1175,8 +1176,7 @@ public EnvVars getEnvironment() {
log.println("Multiple candidate revisions");
if (checkForMultipleRevisions) {
Job<?, ?> job = build.getParent();
if (job instanceof AbstractProject) {
AbstractProject project = (AbstractProject) job;
if (job instanceof AbstractProject<?,?> project) {
if (!project.isDisabled()) {
log.println("Scheduling another build to catch up with " + project.getFullDisplayName());
if (!project.scheduleBuild(0, new SCMTrigger.SCMTriggerCause("This build was triggered by build "
Expand Down Expand Up @@ -1416,7 +1416,7 @@ private static boolean isRemoteUrlValid(String remoteUrl) {
}
try {
// Check for local remotes with no protocol like /path/to/repo.git/
return !Files.exists(Paths.get(remoteUrl));
return !Files.exists(Path.of(remoteUrl));
} catch (InvalidPathException e) {
return true;
}
Expand Down Expand Up @@ -1937,6 +1937,7 @@ public void populateEnvironmentVariables(Map<String,String> env) {
// }
}

@Serial
private static final long serialVersionUID = 1L;

@Whitelisted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import hudson.util.DescribableList;

import java.io.IOException;
import java.io.Serial;
import java.io.Serializable;
import java.util.Set;

Expand Down Expand Up @@ -486,5 +487,6 @@ public String getLocalBranch() {
}


@Serial
private static final long serialVersionUID = 1L;
}
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/git/GitStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,9 +430,9 @@ public List<ResponseContributor> onNotifyCommit(String origin, URIish uri, Strin
}
if (!branchFound) continue;
urlFound = true;
if (!(project instanceof ParameterizedJobMixIn.ParameterizedJob && ((ParameterizedJobMixIn.ParameterizedJob) project).isDisabled())) {
if (!(project instanceof ParameterizedJobMixIn.ParameterizedJob<?,?> job && job.isDisabled())) {
//JENKINS-30178 Add default parameters defined in the job
if (project instanceof Job) {
if (project instanceof Job<?,?> job) {
Set<String> buildParametersNames = new HashSet<>();
if (allowNotifyCommitParameters || !safeParameters.isEmpty()) {
for (ParameterValue parameterValue: allBuildParameters) {
Expand All @@ -442,7 +442,7 @@ public List<ResponseContributor> onNotifyCommit(String origin, URIish uri, Strin
}
}

List<ParameterValue> jobParametersValues = getDefaultParametersValues((Job) project);
List<ParameterValue> jobParametersValues = getDefaultParametersValues(job);
for (ParameterValue defaultParameterValue : jobParametersValues) {
if (!buildParametersNames.contains(defaultParameterValue.getName())) {
allBuildParameters.add(defaultParameterValue);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/git/RevisionParameterAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.gitclient.GitClient;

import java.io.Serial;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -200,6 +201,7 @@ public void foldIntoExisting(Queue.Item item, Queue.Task owner, List<Action> oth
}
}

@Serial
private static final long serialVersionUID = 2L;
private static final Logger LOGGER = Logger.getLogger(RevisionParameterAction.class.getName());
}
3 changes: 2 additions & 1 deletion src/main/java/hudson/plugins/git/SubmoduleConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;
import org.kohsuke.stapler.DataBoundConstructor;


import java.io.Serial;
import java.util.Collection;
import java.util.Collections;

Expand All @@ -16,6 +16,7 @@
*/
@Deprecated
public class SubmoduleConfig implements java.io.Serializable {
@Serial
private static final long serialVersionUID = 1L;
private static final String[] EMPTY_ARRAY = new String[0];
String submoduleName = null;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/hudson/plugins/git/UserMergeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,8 @@ public String getDisplayName() {
public Map<String, Object> customInstantiate(Map<String, Object> arguments) {
Map<String, Object> r = new HashMap<>(arguments);
Object mergeStrategy = r.get("mergeStrategy");
if (mergeStrategy instanceof String) {
r.put("mergeStrategy", ((String) mergeStrategy).toUpperCase(Locale.ROOT));
if (mergeStrategy instanceof String string) {
r.put("mergeStrategy", string.toUpperCase(Locale.ROOT));
}
return r;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/hudson/plugins/git/UserRemoteConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project,
return new StandardListBoxModel()
.includeEmptyValue()
.includeMatchingAs(
project instanceof Queue.Task
? Tasks.getAuthenticationOf((Queue.Task) project)
project instanceof Queue.Task t
? Tasks.getAuthenticationOf(t)
: ACL.SYSTEM,
project,
StandardUsernameCredentials.class,
Expand Down Expand Up @@ -155,8 +155,8 @@ public FormValidation doCheckCredentialsId(@AncestorInPath Item project,
return FormValidation.ok();
}
for (ListBoxModel.Option o : CredentialsProvider
.listCredentialsInItem(StandardUsernameCredentials.class, project, project instanceof Queue.Task
? Tasks.getAuthenticationOf2((Queue.Task) project)
.listCredentialsInItem(StandardUsernameCredentials.class, project, project instanceof Queue.Task t
? Tasks.getAuthenticationOf2(t)
: ACL.SYSTEM2,
GitURIRequirementsBuilder.fromUri(url).build(),
GitClient.CREDENTIALS_MATCHER)) {
Expand Down Expand Up @@ -199,8 +199,8 @@ public FormValidation doCheckUrl(@AncestorInPath Item item,
// get git executable on controller
EnvVars environment;
Jenkins jenkins = Jenkins.get();
if (item instanceof Job) {
environment = ((Job) item).getEnvironment(jenkins, TaskListener.NULL);
if (item instanceof Job<?,?> job) {
environment = job.getEnvironment(jenkins, TaskListener.NULL);
} else {
Computer computer = jenkins.toComputer();
environment = computer == null ? new EnvVars() : computer.buildEnvironment(TaskListener.NULL);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/git/browser/AssemblaWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.servlet.ServletException;
import java.io.IOException;
import java.io.Serial;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -32,6 +33,7 @@
*/
public class AssemblaWeb extends GitRepositoryBrowser {

@Serial
private static final long serialVersionUID = 1L;

@DataBoundConstructor
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/git/browser/BitbucketServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.io.Serial;
import java.net.URL;

/**
* Git Browser URLs for on-premise Bitbucket Server installation.
*/
public class BitbucketServer extends GitRepositoryBrowser {

@Serial
private static final long serialVersionUID = 1L;

@DataBoundConstructor
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/git/browser/BitbucketWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.io.Serial;
import java.net.URL;

/**
* Git Browser URLs
*/
public class BitbucketWeb extends GitRepositoryBrowser {

@Serial
private static final long serialVersionUID = 1L;

@DataBoundConstructor
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/git/browser/CGit.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.io.Serial;
import java.net.URL;

/**
* Git Browser URLs
*/
public class CGit extends GitRepositoryBrowser {

@Serial
private static final long serialVersionUID = 1L;

@DataBoundConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.servlet.ServletException;
import java.io.IOException;
import java.io.Serial;
import java.net.URL;
import java.util.regex.Pattern;

public class FisheyeGitRepositoryBrowser extends GitRepositoryBrowser {

private static final long serialVersionUID = 2881872624557203410L;
@Serial
private static final long serialVersionUID = 2881872624557203410L;

@DataBoundConstructor
public FisheyeGitRepositoryBrowser(String repoUrl) {
Expand All @@ -38,7 +40,7 @@ public URL getDiffLink(Path path) throws IOException {
return null; // no diff if this is not an edit change
String r1 = path.getChangeSet().getParentCommit();
String r2 = path.getChangeSet().getId();
return new URL(getUrl(), getPath(path) + String.format("?r1=%s&r2=%s", r1, r2));
return new URL(getUrl(), "%s?r1=%s&r2=%s".formatted(getPath(path), r1, r2));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jakarta.servlet.ServletException;
import java.io.IOException;
import java.io.Serial;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

public class GitBlitRepositoryBrowser extends GitRepositoryBrowser {

@Serial
private static final long serialVersionUID = 1L;

private final String projectName;
Expand All @@ -44,8 +46,10 @@ public GitBlitRepositoryBrowser(String repoUrl, String projectName) {
public URL getDiffLink(Path path) throws IOException {
URL url = getUrl();
return new URL(url,
String.format(url.getPath() + "blobdiff?r=%s&h=%s&hb=%s", encodeString(projectName), path.getChangeSet().getId(),
path.getChangeSet().getParentCommit()));
"%sblobdiff?r=%s&h=%s&hb=%s".formatted(url.getPath(),
encodeString(projectName),
path.getChangeSet().getId(),
path.getChangeSet().getParentCommit()));
}

@Override
Expand All @@ -55,14 +59,18 @@ public URL getFileLink(Path path) throws IOException {
}
URL url = getUrl();
return new URL(url,
String.format(url.getPath() + "blob?r=%s&h=%s&f=%s", encodeString(projectName), path.getChangeSet().getId(),
encodeString(path.getPath())));
"%sblob?r=%s&h=%s&f=%s".formatted(url.getPath(),
encodeString(projectName),
path.getChangeSet().getId(),
encodeString(path.getPath())));
}

@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
URL url = getUrl();
return new URL(url, String.format(url.getPath() + "commit?r=%s&h=%s", encodeString(projectName), changeSet.getId()));
return new URL(url, "%scommit?r=%s&h=%s".formatted(url.getPath(),
encodeString(projectName),
changeSet.getId()));
}

public String getProjectName() {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/hudson/plugins/git/browser/GitLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.kohsuke.stapler.StaplerRequest2;

import java.io.IOException;
import java.io.Serial;
import java.net.URL;

import edu.umd.cs.findbugs.annotations.NonNull;
Expand All @@ -28,6 +29,7 @@
*/
public class GitLab extends GitRepositoryBrowser {

@Serial
private static final long serialVersionUID = 1L;

private Double version;
Expand Down
Loading

0 comments on commit 47d8e46

Please sign in to comment.