Skip to content

Commit

Permalink
Use Java 17 language features
Browse files Browse the repository at this point in the history
instanceof pattern matching

Formatted strings
  • Loading branch information
MarkEWaite committed Oct 20, 2024
1 parent eecf8fc commit 3071963
Show file tree
Hide file tree
Showing 69 changed files with 272 additions and 181 deletions.
17 changes: 17 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,23 @@
<failOnViolation>true</failOnViolation>
</configuration>
</plugin>
<plugin>
<groupId>org.openrewrite.maven</groupId>
<artifactId>rewrite-maven-plugin</artifactId>
<version>5.42.0</version>
<configuration>
<activeRecipes>
<recipe>org.openrewrite.java.migrate.UpgradeToJava17</recipe>
</activeRecipes>
</configuration>
<dependencies>
<dependency>
<groupId>org.openrewrite.recipe</groupId>
<artifactId>rewrite-migrate-java</artifactId>
<version>2.26.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

Expand Down
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) {

Check warning on line 29 in src/main/java/hudson/plugins/git/GitBranchSpecifierColumn.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered line

Line 29 is not covered by tests
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();

Check warning on line 60 in src/main/java/hudson/plugins/git/GitRevisionBuildParameters.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 58-60 are not covered by tests
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);

Check warning on line 788 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 788 is only partially covered, one branch is missing

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) {

Check warning on line 1179 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1179 is only partially covered, one branch is missing
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));

Check warning on line 1419 in src/main/java/hudson/plugins/git/GitSCM.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 1419 is only partially covered, one branch is missing
} 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())) {

Check warning on line 433 in src/main/java/hudson/plugins/git/GitStatus.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 433 is only partially covered, 2 branches are missing
//JENKINS-30178 Add default parameters defined in the job
if (project instanceof Job) {
if (project instanceof Job<?,?> job) {

Check warning on line 435 in src/main/java/hudson/plugins/git/GitStatus.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 435 is only partially covered, one branch is missing
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

Check warning on line 122 in src/main/java/hudson/plugins/git/UserRemoteConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 122 is only partially covered, one branch is missing
? 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)

Check warning on line 159 in src/main/java/hudson/plugins/git/UserRemoteConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Not covered lines

Lines 158-159 are not covered by tests
: 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) {

Check warning on line 202 in src/main/java/hudson/plugins/git/UserRemoteConfig.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 202 is only partially covered, one branch is missing
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(), getPath(path) + "?r1=%s&r2=%s".formatted(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,7 +46,7 @@ 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(),
(url.getPath() + "blobdiff?r=%s&h=%s&hb=%s").formatted(encodeString(projectName), path.getChangeSet().getId(),
path.getChangeSet().getParentCommit()));
}

Expand All @@ -55,14 +57,14 @@ 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(),
(url.getPath() + "blob?r=%s&h=%s&f=%s").formatted(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, (url.getPath() + "commit?r=%s&h=%s").formatted(encodeString(projectName), changeSet.getId()));
}

public String getProjectName() {
Expand Down
Loading

0 comments on commit 3071963

Please sign in to comment.