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

Code modernization #1294

Merged
merged 10 commits into from
Aug 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/main/java/hudson/plugins/git/GitSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import java.io.PrintStream;
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;
Expand Down Expand Up @@ -150,7 +151,7 @@ public class GitSCM extends GitSCMBackwardCompatibility {
public String gitTool;
@CheckForNull
private GitRepositoryBrowser browser;
private Collection<SubmoduleConfig> submoduleCfg = Collections.<SubmoduleConfig>emptyList();
private Collection<SubmoduleConfig> submoduleCfg = Collections.emptyList();
public static final String GIT_BRANCH = "GIT_BRANCH";
public static final String GIT_LOCAL_BRANCH = "GIT_LOCAL_BRANCH";
public static final String GIT_CHECKOUT_DIR = "GIT_CHECKOUT_DIR";
Expand Down Expand Up @@ -192,7 +193,7 @@ public GitSCM(String repositoryUrl) {
this(
createRepoList(repositoryUrl, null),
Collections.singletonList(new BranchSpec("")),
null, null, Collections.<GitSCMExtension>emptyList());
null, null, Collections.emptyList());
}

@Deprecated
Expand Down Expand Up @@ -1479,7 +1480,7 @@ private void computeChangeLog(GitClient git, Revision revToBuild, TaskListener l
boolean executed = false;
ChangelogCommand changelog = git.changelog();
changelog.includes(revToBuild.getSha1());
try (Writer out = new OutputStreamWriter(changelogFile.write(),"UTF-8")) {
try (Writer out = new OutputStreamWriter(changelogFile.write(), StandardCharsets.UTF_8)) {
boolean exclusion = false;
ChangelogToBranch changelogToBranch = getExtensions().get(ChangelogToBranch.class);
if (changelogToBranch != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import hudson.plugins.git.extensions.GitSCMExtension;
import hudson.plugins.git.extensions.GitSCMExtensionDescriptor;
import hudson.plugins.git.extensions.impl.*;
import hudson.plugins.git.extensions.impl.DisableRemotePoll;
import hudson.plugins.git.opt.PreBuildMergeOptions;
import hudson.plugins.git.util.BuildChooser;
import hudson.plugins.git.util.DefaultBuildChooser;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/UserRemoteConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath Item project,
/* Construct a fake project, suppress the deprecation warning because the
* replacement for the deprecated API isn't accessible in this context. */
@SuppressWarnings("deprecation")
Item fakeProject = new FreeStyleProject(Jenkins.get(), "fake-" + UUID.randomUUID().toString());
Item fakeProject = new FreeStyleProject(Jenkins.get(), "fake-" + UUID.randomUUID());
project = fakeProject;
}
return new StandardListBoxModel()
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/hudson/plugins/git/browser/CGit.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private QueryBuilder param(URL url) {
@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
URL url = getUrl();
return new URL(url, url.getPath() + "commit/" + param(url).add("id=" + changeSet.getId()).toString());
return new URL(url, url.getPath() + "commit/" + param(url).add("id=" + changeSet.getId()));
}

/**
Expand All @@ -57,7 +57,7 @@ public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
public URL getDiffLink(Path path) throws IOException {
GitChangeSet changeSet = path.getChangeSet();
URL url = getUrl();
return new URL(url, url.getPath() + "diff/" + path.getPath() + param(url).add("id=" + changeSet.getId()).toString());
return new URL(url, url.getPath() + "diff/" + path.getPath() + param(url).add("id=" + changeSet.getId()));
}

/**
Expand All @@ -73,9 +73,9 @@ public URL getFileLink(Path path) throws IOException {
GitChangeSet changeSet = path.getChangeSet();
URL url = getUrl();
if (path.getEditType() == EditType.DELETE) {
return encodeURL(new URL(url, url.getPath() + "tree/" + path.getPath() + param(url).add("id=" + changeSet.getParentCommit()).toString()));
return encodeURL(new URL(url, url.getPath() + "tree/" + path.getPath() + param(url).add("id=" + changeSet.getParentCommit())));
} else {
return encodeURL(new URL(url, url.getPath() + "tree/" + path.getPath() + param(url).add("id=" + changeSet.getId()).toString()));
return encodeURL(new URL(url, url.getPath() + "tree/" + path.getPath() + param(url).add("id=" + changeSet.getId())));
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/main/java/hudson/plugins/git/browser/GitLab.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
public URL getDiffLink(Path path) throws IOException {
final GitChangeSet changeSet = path.getChangeSet();
String filelink;
if(getVersionDouble() < 8.0) {
filelink = "#" + path.getPath();
} else
{
filelink = "#diff-" + String.valueOf(getIndexOfPath(path));
if (getVersionDouble() < 8.0) {
filelink = "#" + path.getPath();
} else {
filelink = "#diff-" + getIndexOfPath(path);
}
return new URL(getUrl(), calculatePrefix() + changeSet.getId() + filelink);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/browser/GitList.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public URL getDiffLink(Path path) throws IOException {
*/
private URL getDiffLinkRegardlessOfEditType(Path path) throws IOException {
//GitList diff indices begin at 1
return encodeURL(new URL(getChangeSetLink(path.getChangeSet()), "#" + String.valueOf(getIndexOfPath(path) + 1)));
return encodeURL(new URL(getChangeSetLink(path.getChangeSet()), "#" + (getIndexOfPath(path) + 1)));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/browser/GitWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected boolean getNormalizeUrl() {
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
URL url = getUrl();

return new URL(url, url.getPath()+param(url).add("a=commit").add("h=" + changeSet.getId()).toString());
return new URL(url, url.getPath()+ param(url).add("a=commit").add("h=" + changeSet.getId()));
}

private QueryBuilder param(URL url) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/browser/GithubWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public URL getDiffLink(Path path) throws IOException {
*/
private URL getDiffLinkRegardlessOfEditType(Path path) throws IOException {
// Github seems to sort the output alphabetically by the path.
return new URL(getChangeSetLink(path.getChangeSet()), "#diff-" + String.valueOf(getIndexOfPath(path)));
return new URL(getChangeSetLink(path.getChangeSet()), "#diff-" + getIndexOfPath(path));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/browser/GogsGit.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public URL getDiffLink(Path path) throws IOException {
*/
private URL getDiffLinkRegardlessOfEditType(Path path) throws IOException {
// Gogs diff indices begin at 1.
return encodeURL(new URL(getChangeSetLink(path.getChangeSet()), "#diff-" + String.valueOf(getIndexOfPath(path) + 1)));
return encodeURL(new URL(getChangeSetLink(path.getChangeSet()), "#diff-" + (getIndexOfPath(path) + 1)));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/hudson/plugins/git/browser/KilnGit.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private QueryBuilder param(URL url) {
@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
URL url = getUrl();
return encodeURL(new URL(url, url.getPath() + "History/" + changeSet.getId() + param(url).toString()));
return encodeURL(new URL(url, url.getPath() + "History/" + changeSet.getId() + param(url)));
}

/**
Expand Down Expand Up @@ -76,7 +76,7 @@ private URL getDiffLinkRegardlessOfEditType(Path path) throws IOException {
if (i >= 0) {
// Kiln diff indices begin at 1.
URL url = getUrl();
return new URL(getChangeSetLink(changeSet), param(url).toString() + "#diff-" + String.valueOf(i + 1));
return new URL(getChangeSetLink(changeSet), param(url) + "#diff-" + (i + 1));
}
return getChangeSetLink(changeSet);
}
Expand All @@ -96,7 +96,7 @@ public URL getFileLink(Path path) throws IOException {
} else {
GitChangeSet changeSet = path.getChangeSet();
URL url = getUrl();
return encodeURL(new URL(url, url.getPath() + "FileHistory/" + path.getPath() + param(url).add("rev=" + changeSet.getId()).toString()));
return encodeURL(new URL(url, url.getPath() + "FileHistory/" + path.getPath() + param(url).add("rev=" + changeSet.getId())));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/browser/RhodeCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
public URL getDiffLink(Path path) throws IOException {
GitChangeSet changeSet = path.getChangeSet();
URL url = getUrl();
return new URL(url, url.getPath() + "diff/" + path.getPath() + param(url).add("diff2=" + changeSet.getParentCommit()).add("diff1=" + changeSet.getId()).toString() + "&diff=diff+to+revision");
return new URL(url, url.getPath() + "diff/" + path.getPath() + param(url).add("diff2=" + changeSet.getParentCommit()).add("diff1=" + changeSet.getId()) + "&diff=diff+to+revision");
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/hudson/plugins/git/browser/Stash.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ public URL getDiffLink(Path path) throws IOException {
URL url = getUrl();

if (path.getEditType() == EditType.DELETE) {
return new URL(url, url.getPath() + "diff/" + path.getPath() + param(url).add("at=" + changeSet.getParentCommit()).add("until=" + changeSet.getId()).toString());
return new URL(url, url.getPath() + "diff/" + path.getPath() + param(url).add("at=" + changeSet.getParentCommit()).add("until=" + changeSet.getId()));
} else {
return new URL(url, url.getPath() + "diff/" + path.getPath() + param(url).add("at=" + changeSet.getId()).add("until=" + changeSet.getId()).toString());
return new URL(url, url.getPath() + "diff/" + path.getPath() + param(url).add("at=" + changeSet.getId()).add("until=" + changeSet.getId()));
}
}

Expand All @@ -79,9 +79,9 @@ public URL getFileLink(Path path) throws IOException {
URL url = getUrl();

if (path.getEditType() == EditType.DELETE) {
return encodeURL(new URL(url, url.getPath() + "browse/" + path.getPath() + param(url).add("at=" + changeSet.getParentCommit()).toString()));
return encodeURL(new URL(url, url.getPath() + "browse/" + path.getPath() + param(url).add("at=" + changeSet.getParentCommit())));
} else {
return encodeURL(new URL(url, url.getPath() + "browse/" + path.getPath() + param(url).add("at=" + changeSet.getId()).toString()));
return encodeURL(new URL(url, url.getPath() + "browse/" + path.getPath() + param(url).add("at=" + changeSet.getId())));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/browser/ViewGitWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ private String buildCommitDiffSpec(URL url, Path path)
@Override
public URL getChangeSetLink(GitChangeSet changeSet) throws IOException {
URL url = getUrl();
return new URL(url, url.getPath() + param(url).add("p=" + projectName).add("a=commit").add("h=" + changeSet.getId()).toString());
return new URL(url, url.getPath() + param(url).add("p=" + projectName).add("a=commit").add("h=" + changeSet.getId()));
}

private QueryBuilder param(URL url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public int hashCode() {
@Override
public String toString() {
return "PreBuildMerge{" +
"options=" + options.toString() +
"options=" + options +
'}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SparseCheckoutPaths extends GitSCMExtension {

@DataBoundConstructor
public SparseCheckoutPaths(List<SparseCheckoutPath> sparseCheckoutPaths) {
this.sparseCheckoutPaths = sparseCheckoutPaths == null ? Collections.<SparseCheckoutPath>emptyList() : sparseCheckoutPaths;
this.sparseCheckoutPaths = sparseCheckoutPaths == null ? Collections.emptyList() : sparseCheckoutPaths;
}

@Whitelisted
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/hudson/plugins/git/util/BuildChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public BuildChooserDescriptor getDescriptor() {
*/
public static DescriptorExtensionList<BuildChooser,BuildChooserDescriptor> all() {
return Jenkins.get()
.<BuildChooser,BuildChooserDescriptor>getDescriptorList(BuildChooser.class);
.getDescriptorList(BuildChooser.class);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/hudson/plugins/git/util/GitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.logging.Logger;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

public class GitUtils implements Serializable {

Expand Down Expand Up @@ -311,7 +310,7 @@ public static EnvVars getPollEnvironment(AbstractProject p, FilePath ws, Launche
if (lastComputer != null) {
env = lastComputer.getEnvironment().overrideAll(b.getCharacteristicEnvVars());
for (NodeProperty nodeProperty : lastBuiltOn.getNodeProperties()) {
Environment environment = nodeProperty.setUp(b, launcher, (BuildListener) buildListener);
Environment environment = nodeProperty.setUp(b, launcher, buildListener);
if (environment != null) {
environment.buildEnvVars(env);
}
Expand Down Expand Up @@ -349,7 +348,7 @@ public static EnvVars getPollEnvironment(AbstractProject p, FilePath ws, Launche
env.put("WORKSPACE", ws.getRemote());

for (NodeProperty nodeProperty: jenkinsInstance.getGlobalNodeProperties()) {
Environment environment = nodeProperty.setUp(b, launcher, (BuildListener)buildListener);
Environment environment = nodeProperty.setUp(b, launcher, buildListener);
if (environment != null) {
environment.buildEnvVars(env);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,7 @@ public Collection<Revision> getCandidateRevisions(boolean isPollCall,
}

// Filter out branch revisions that have already been built
for (Iterator<Revision> i = branchRevs.iterator(); i.hasNext(); ) {
Revision r = i.next();
if (buildData.hasBeenBuilt(r.getSha1())) {
i.remove();
}
}
branchRevs.removeIf(r -> buildData.hasBeenBuilt(r.getSha1()));

// If we're in a build (not an SCM poll) and nothing new was found, run the last build again
if (!isPollCall && branchRevs.isEmpty() && buildData.getLastBuiltRevision() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,7 @@ protected List<Action> retrieveActions(@NonNull SCMHead head, @CheckForNull SCMH
for (GitRemoteHeadRefAction a: ((Actionable) owner).getActions(GitRemoteHeadRefAction.class)) {
if (getRemote().equals(a.getRemote())) {
if (head.getName().equals(a.getName())) {
return Collections.<Action>singletonList(new PrimaryInstanceMetadataAction());
return Collections.singletonList(new PrimaryInstanceMetadataAction());
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/jenkins/plugins/git/GitBranchSCMHead.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ public final String getRef() {

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("GitBranchSCMHead{");
sb.append("name='").append(getName()).append("'");
sb.append(", ref='").append(getRef()).append("'}");
return sb.toString();
return "GitBranchSCMHead{" + "name='" + getName() + "'" + ", ref='" + getRef() + "'}";
}

@Restricted(NoExternalUse.class)
Expand Down
5 changes: 1 addition & 4 deletions src/main/java/jenkins/plugins/git/GitRefSCMHead.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public String getRef() {

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("GitRefSCMHead{");
sb.append("name='").append(getName()).append("'");
sb.append(", ref='").append(ref).append("'}");
return sb.toString();
return "GitRefSCMHead{" + "name='" + getName() + "'" + ", ref='" + ref + "'}";
}
}
13 changes: 2 additions & 11 deletions src/main/java/jenkins/plugins/git/GitSCMBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -276,11 +275,7 @@ public final B withCredentials(@CheckForNull String credentialsId) {
public final B withExtension(@CheckForNull GitSCMExtension extension) {
if (extension != null) {
// the extensions only allow one of each type.
for (Iterator<GitSCMExtension> iterator = extensions.iterator(); iterator.hasNext(); ) {
if (extension.getClass().equals(iterator.next().getClass())) {
iterator.remove();
}
}
extensions.removeIf(gitSCMExtension -> extension.getClass().equals(gitSCMExtension.getClass()));
extensions.add(extension);
}
return (B) this;
Expand Down Expand Up @@ -505,11 +500,7 @@ public GitSCM build() {
SCMRevision revision = revision();
if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) {
// remove any conflicting BuildChooserSetting if present
for (Iterator<GitSCMExtension> iterator = extensions.iterator(); iterator.hasNext(); ) {
if (iterator.next() instanceof BuildChooserSetting) {
iterator.remove();
}
}
extensions.removeIf(gitSCMExtension -> gitSCMExtension instanceof BuildChooserSetting);
extensions.add(new BuildChooserSetting(new AbstractGitSCMSource.SpecificRevisionBuildChooser(
(AbstractGitSCMSource.SCMRevisionImpl) revision)));
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/jenkins/plugins/git/GitSCMFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
Expand Down Expand Up @@ -205,7 +206,7 @@ public boolean changesSince(@CheckForNull SCMRevision revision, @NonNull OutputS
}
boolean executed = false;
ChangelogCommand changelog = client.changelog();
try (Writer out = new OutputStreamWriter(changeLogStream, "UTF-8")) {
try (Writer out = new OutputStreamWriter(changeLogStream, StandardCharsets.UTF_8)) {
changelog.includes(commitId);
ObjectId fromCommitId;
if (revision instanceof AbstractGitSCMSource.SCMRevisionImpl) {
Expand Down Expand Up @@ -361,10 +362,9 @@ public SCMFileSystem build(@NonNull Item owner, @NonNull SCM scm, @CheckForNull
headName = branchSpec.getName();
}
}
client.fetch_().prune(true).from(remoteURI, Arrays
.asList(new RefSpec(
"+" + prefix + headName + ":" + Constants.R_REMOTES + remoteName + "/"
+ headName))).execute();
client.fetch_().prune(true).from(remoteURI, Collections.singletonList(new RefSpec(
"+" + prefix + headName + ":" + Constants.R_REMOTES + remoteName + "/"
+ headName))).execute();
listener.getLogger().println("Done.");
return new GitSCMFileSystem(client, remote, Constants.R_REMOTES + remoteName + "/" +headName, (AbstractGitSCMSource.SCMRevisionImpl) rev);
} finally {
Expand Down
Loading