Skip to content

Commit 3d8dfb5

Browse files
Use Java 17 features, remove deprecations, cleanup
1 parent d5c0345 commit 3d8dfb5

File tree

154 files changed

+1108
-994
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

154 files changed

+1108
-994
lines changed

Jenkinsfile-integ

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ pipeline {
66
stage('Linux Build & Test') {
77

88
environment {
9-
JAVA_HOME = '/usr/lib/jvm/java-11-openjdk-amd64'
9+
JAVA_HOME = '/usr/lib/jvm/java-17-openjdk-amd64'
1010
}
11-
11+
1212
agent {
1313
label 'p4java'
1414
}
15-
15+
1616
stages {
1717
stage('Compile') {
1818
steps {
@@ -49,10 +49,10 @@ pipeline {
4949
}
5050
}
5151
}
52-
52+
5353
stage('Windows Build & Test') {
5454
environment {
55-
JAVA_HOME = "C:\\Program Files\\Java\\jdk-11.0.16.1"
55+
JAVA_HOME = "C:\\Program Files\\Java\\jdk-17.0.14"
5656
}
5757
agent {
5858
label 'p4java-win'
@@ -86,7 +86,7 @@ pipeline {
8686
}
8787
}
8888
}
89-
89+
9090
stage('Launch system tests') {
9191
steps {
9292
build job: 'jenkins-system-tests/main', wait: false

pom.xml

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@
7070
<dependencies>
7171

7272
<dependency>
73-
<groupId>com.perforce</groupId>
74-
<artifactId>p4java</artifactId>
75-
<version>2024.2.2695691</version>
73+
<groupId>io.jenkins.plugins</groupId>
74+
<artifactId>commons-lang3-api</artifactId>
7675
</dependency>
7776

7877
<dependency>
79-
<groupId>io.jenkins.plugins</groupId>
80-
<artifactId>commons-lang3-api</artifactId>
78+
<groupId>com.perforce</groupId>
79+
<artifactId>p4java</artifactId>
80+
<version>2024.2.2695691</version>
8181
</dependency>
8282

8383
<dependency>
@@ -92,24 +92,9 @@
9292
</dependency>
9393

9494
<dependency>
95-
<groupId>com.mashape.unirest</groupId>
96-
<artifactId>unirest-java</artifactId>
97-
<version>1.4.9</version>
98-
<exclusions>
99-
<exclusion>
100-
<groupId>org.apache.httpcomponents</groupId>
101-
<artifactId>*</artifactId>
102-
</exclusion>
103-
<exclusion>
104-
<groupId>org.json</groupId>
105-
<artifactId>json</artifactId>
106-
</exclusion>
107-
</exclusions>
108-
</dependency>
109-
110-
<dependency>
111-
<groupId>org.jenkins-ci.plugins</groupId>
112-
<artifactId>apache-httpcomponents-client-4-api</artifactId>
95+
<groupId>com.konghq</groupId>
96+
<artifactId>unirest-java-core</artifactId>
97+
<version>4.4.5</version>
11398
</dependency>
11499

115100
<!-- Jenkins dependencies -->
@@ -221,11 +206,6 @@
221206
<scope>test</scope>
222207
</dependency>
223208

224-
<dependency>
225-
<groupId>io.jenkins.plugins</groupId>
226-
<artifactId>json-api</artifactId>
227-
</dependency>
228-
229209
</dependencies>
230210

231211
<build>

src/main/java/org/jenkinsci/plugins/p4/ConfigurationListener.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ public class ConfigurationListener extends SaveableListener {
2424
private static final Logger logger = Logger.getLogger(ConfigurationListener.class.getName());
2525

2626
public void onChange(Saveable o, XmlFile xml) {
27-
if (!(o instanceof PerforceScm.DescriptorImpl)) {
27+
if (!(o instanceof DescriptorImpl p4scm)) {
2828
// must be a PerforceScm
2929
return;
3030
}
3131

32-
PerforceScm.DescriptorImpl p4scm = (PerforceScm.DescriptorImpl) o;
33-
3432
// Exit early if disabled
3533
if (!p4scm.isAutoSave()) {
3634
return;
@@ -80,7 +78,7 @@ private ClientHelper getClientHelper(DescriptorImpl p4scm) throws IOException {
8078
depotPath = "\"" + depotPath + "\"";
8179
}
8280

83-
Jenkins j = Jenkins.getInstance();
81+
Jenkins j = Jenkins.get();
8482
String rootPath = j.getRootDir().getCanonicalPath();
8583

8684
String view = ViewMapHelper.getClientView(depotPath, clientName, true);
@@ -91,6 +89,6 @@ private ClientHelper getClientHelper(DescriptorImpl p4scm) throws IOException {
9189
workspace.setExpand(new HashMap<>());
9290
workspace.setRootPath(rootPath);
9391

94-
return new ClientHelper(Jenkins.getInstance(), credential, listener, workspace);
92+
return new ClientHelper(Jenkins.get(), credential, listener, workspace);
9593
}
9694
}

src/main/java/org/jenkinsci/plugins/p4/PerforceScm.java

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.cloudbees.plugins.credentials.CredentialsProvider;
44
import com.perforce.p4java.exception.P4JavaException;
5+
import edu.umd.cs.findbugs.annotations.NonNull;
56
import hudson.AbortException;
67
import hudson.EnvVars;
78
import hudson.Extension;
@@ -88,7 +89,6 @@
8889
import java.io.IOException;
8990
import java.io.PrintStream;
9091
import java.util.ArrayList;
91-
import java.util.Arrays;
9292
import java.util.Collection;
9393
import java.util.List;
9494
import java.util.Map;
@@ -160,12 +160,11 @@ public static PerforceScm convertToPerforceScm(SCM scm) {
160160
if (scm instanceof PerforceScm) {
161161
perforceScm = (PerforceScm) scm;
162162
} else {
163-
Jenkins jenkins = Jenkins.getInstance();
163+
Jenkins jenkins = Jenkins.get();
164164
if (jenkins != null) {
165165
Plugin multiSCMPlugin = jenkins.getPlugin("multiple-scms");
166166
if (multiSCMPlugin != null) {
167-
if (scm instanceof MultiSCM) {
168-
MultiSCM multiSCM = (MultiSCM) scm;
167+
if (scm instanceof MultiSCM multiSCM) {
169168
for (SCM configuredSCM : multiSCM.getConfiguredSCMs()) {
170169
if (configuredSCM instanceof PerforceScm) {
171170
perforceScm = (PerforceScm) configuredSCM;
@@ -235,40 +234,36 @@ public PerforceScm(String credential, Workspace workspace, Populate populate) {
235234
this.revision = null;
236235
}
237236

237+
@NonNull
238238
@Override
239239
public String getKey() {
240240
String delim = "-";
241-
StringBuffer key = new StringBuffer("p4");
241+
StringBuilder key = new StringBuilder("p4");
242242

243243
// add Credential
244244
key.append(delim);
245245
key.append(credential);
246246

247247
// add Mapping/Stream
248248
key.append(delim);
249-
if (workspace instanceof ManualWorkspaceImpl) {
250-
ManualWorkspaceImpl ws = (ManualWorkspaceImpl) workspace;
249+
if (workspace instanceof ManualWorkspaceImpl ws) {
251250
key.append(ws.getSpec().getView());
252251
key.append(ws.getSpec().getStreamName());
253252
key.append(ws.getName());
254253
}
255-
if (workspace instanceof StreamWorkspaceImpl) {
256-
StreamWorkspaceImpl ws = (StreamWorkspaceImpl) workspace;
254+
if (workspace instanceof StreamWorkspaceImpl ws) {
257255
key.append(ws.getStreamName());
258256
key.append(ws.getStreamAtChange());
259257
key.append(ws.getName());
260258
}
261-
if (workspace instanceof SpecWorkspaceImpl) {
262-
SpecWorkspaceImpl ws = (SpecWorkspaceImpl) workspace;
259+
if (workspace instanceof SpecWorkspaceImpl ws) {
263260
key.append(ws.getSpecPath());
264261
key.append(ws.getName());
265262
}
266-
if (workspace instanceof StaticWorkspaceImpl) {
267-
StaticWorkspaceImpl ws = (StaticWorkspaceImpl) workspace;
263+
if (workspace instanceof StaticWorkspaceImpl ws) {
268264
key.append(ws.getName());
269265
}
270-
if (workspace instanceof TemplateWorkspaceImpl) {
271-
TemplateWorkspaceImpl ws = (TemplateWorkspaceImpl) workspace;
266+
if (workspace instanceof TemplateWorkspaceImpl ws) {
272267
key.append(ws.getTemplateName());
273268
key.append(ws.getName());
274269
}
@@ -315,8 +310,8 @@ public static P4Browser findBrowser(String scmCredential) {
315310
* the build as an Action for later retrieval.
316311
*/
317312
@Override
318-
public SCMRevisionState calcRevisionsFromBuild(Run<?, ?> run, FilePath buildWorkspace, Launcher launcher,
319-
TaskListener listener) throws IOException, InterruptedException {
313+
public SCMRevisionState calcRevisionsFromBuild(@NonNull Run<?, ?> run, FilePath buildWorkspace, Launcher launcher,
314+
@NonNull TaskListener listener) {
320315
// return the Perforce change; this gets updated during polling...
321316
return new PerforceRevisionState(new P4LabelRef("now"));
322317
}
@@ -327,8 +322,8 @@ public SCMRevisionState calcRevisionsFromBuild(Run<?, ?> run, FilePath buildWork
327322
* detected by this poll.
328323
*/
329324
@Override
330-
public PollingResult compareRemoteRevisionWith(Job<?, ?> job, Launcher launcher, FilePath buildWorkspace,
331-
TaskListener listener, SCMRevisionState baseline) throws IOException, InterruptedException {
325+
public PollingResult compareRemoteRevisionWith(@NonNull Job<?, ?> job, Launcher launcher, FilePath buildWorkspace,
326+
@NonNull TaskListener listener, @NonNull SCMRevisionState baseline) throws IOException, InterruptedException {
332327

333328
String jobName = job.getName();
334329
logger.finer("P4: polling[" + jobName + "] started...");
@@ -338,10 +333,9 @@ public PollingResult compareRemoteRevisionWith(Job<?, ?> job, Launcher launcher,
338333
// Get last run and build workspace
339334
Run<?, ?> lastRun = job.getLastBuild();
340335

341-
Queue.Item[] items = Jenkins.getInstance().getQueue().getItems();
336+
Queue.Item[] items = Jenkins.get().getQueue().getItems();
342337
for (Queue.Item item : items) {
343-
if (item.task instanceof WorkflowJob) {
344-
WorkflowJob task = (WorkflowJob) item.task;
338+
if (item.task instanceof WorkflowJob task) {
345339
if (task.equals(job)) {
346340
if (item instanceof Queue.WaitingItem) {
347341
logger.info("P4: polling[" + jobName + "] skipping WaitingItem");
@@ -450,8 +444,7 @@ private PollingResult pollWorkspace(EnvVars envVars, TaskListener listener, File
450444
return PollingResult.NO_CHANGES;
451445
} else {
452446
changes.forEach((c) -> listener.getLogger().println("P4: Polling found change: " + c));
453-
if (baseline instanceof PerforceRevisionState) {
454-
PerforceRevisionState p4Baseline = (PerforceRevisionState) baseline;
447+
if (baseline instanceof PerforceRevisionState p4Baseline) {
455448
setLatestChange(changes, listener, p4Baseline);
456449
}
457450
return PollingResult.BUILD_NOW;
@@ -522,8 +515,8 @@ private List<P4Ref> lookForChanges(FilePath buildWorkspace, Workspace ws, Run<?,
522515
* workspace. Authorisation
523516
*/
524517
@Override
525-
public void checkout(Run<?, ?> run, Launcher launcher, FilePath buildWorkspace, TaskListener listener,
526-
File changelogFile, SCMRevisionState baseline) throws IOException, InterruptedException {
518+
public void checkout(@NonNull Run<?, ?> run, @NonNull Launcher launcher, @NonNull FilePath buildWorkspace, @NonNull TaskListener listener,
519+
File changelogFile, SCMRevisionState baseline) throws IOException, InterruptedException {
527520

528521
String jobName = run.getParent().getName();
529522
logger.finer("P4: checkout[" + jobName + "] started...");
@@ -610,16 +603,15 @@ To help lookForChanges() find the correct change to build, sending it the previo
610603

611604
// If the Latest Change filter is set, apply the baseline change to the checkout task.
612605
if (FilterLatestChangeImpl.isActive(getFilter())) {
613-
if (baseline instanceof PerforceRevisionState) {
614-
PerforceRevisionState p4baseline = (PerforceRevisionState) baseline;
606+
if (baseline instanceof PerforceRevisionState p4baseline) {
615607
log.println("Baseline: " + p4baseline.getChange().toString());
616608
task.setBuildChange(p4baseline.getChange());
617609
}
618610
}
619611

620612
// SCMRevision build per change
621613
if (revision != null) {
622-
List<P4Ref> changes = Arrays.asList(revision);
614+
List<P4Ref> changes = List.of(revision);
623615
task.setIncrementalChanges(changes);
624616
}
625617

@@ -726,27 +718,24 @@ private String getScriptPath(Run<?, ?> run) {
726718
return script;
727719
}
728720

729-
if (!(run instanceof WorkflowRun)) {
721+
if (!(run instanceof WorkflowRun workflowRun)) {
730722
return null;
731723
}
732724

733-
WorkflowRun workflowRun = (WorkflowRun) run;
734725
WorkflowJob job = workflowRun.getParent();
735726
return getScriptPath(job);
736727
}
737728

738729
public String getScriptPath(Item item) {
739-
if (!(item instanceof WorkflowJob)) {
730+
if (!(item instanceof WorkflowJob job)) {
740731
return null;
741732
}
742733

743-
WorkflowJob job = (WorkflowJob) item;
744734
FlowDefinition definition = job.getDefinition();
745-
if (!(definition instanceof CpsScmFlowDefinition)) {
735+
if (!(definition instanceof CpsScmFlowDefinition cps)) {
746736
return null;
747737
}
748738

749-
CpsScmFlowDefinition cps = (CpsScmFlowDefinition) definition;
750739
if (!(cps.getScm() instanceof PerforceScm)) {
751740
return null;
752741
}
@@ -776,8 +765,7 @@ private void cleanupPerforceClient(Run<?, ?> run, FilePath buildWorkspace, TaskL
776765

777766
// Get Matrix Execution options
778767
private MatrixExecutionStrategy getMatrixExecutionStrategy(Job<?, ?> job) {
779-
if (job instanceof MatrixProject) {
780-
MatrixProject matrixProj = (MatrixProject) job;
768+
if (job instanceof MatrixProject matrixProj) {
781769
return matrixProj.getExecutionStrategy();
782770
}
783771
return null;
@@ -795,7 +783,7 @@ boolean isBuildParent(Job<?, ?> job) {
795783
}
796784

797785
private List<P4ChangeEntry> calculateChanges(Run<?, ?> run, CheckoutTask task) {
798-
List<P4ChangeEntry> list = new ArrayList<P4ChangeEntry>();
786+
List<P4ChangeEntry> list = new ArrayList<>();
799787

800788
Run<?, ?> lastBuild;
801789
PerforceScm.DescriptorImpl scm = getDescriptor();
@@ -834,7 +822,7 @@ private List<P4ChangeEntry> calculateChanges(Run<?, ?> run, CheckoutTask task) {
834822
}
835823

836824
// Post Jenkins 2.60 JENKINS-37584 JENKINS-40885 JENKINS-52806 JENKINS-60074
837-
public void buildEnvironment(Run<?, ?> run, Map<String, String> env) {
825+
public void buildEnvironment(@NonNull Run<?, ?> run, @NonNull Map<String, String> env) {
838826
P4EnvironmentContributor.buildEnvironment(TagAction.getLastAction(run), env);
839827
P4EnvironmentContributor.buildEnvironment(tagAction, env);
840828
}
@@ -899,7 +887,7 @@ public ChangeLogParser createChangeLogParser() {
899887
* opportunity to perform clean up.
900888
*/
901889
@Override
902-
public boolean processWorkspaceBeforeDeletion(Job<?, ?> job, FilePath buildWorkspace, Node node)
890+
public boolean processWorkspaceBeforeDeletion(@NonNull Job<?, ?> job, @NonNull FilePath buildWorkspace, @NonNull Node node)
903891
throws IOException, InterruptedException {
904892

905893
logger.finer("processWorkspaceBeforeDeletion");
@@ -928,8 +916,7 @@ public boolean processWorkspaceBeforeDeletion(Job<?, ?> job, FilePath buildWorks
928916
return false;
929917
}
930918
//JENKINS-60144 Checking if the template ws exists before deleting client, otherwise it throws exception along the line.
931-
if (workspace instanceof TemplateWorkspaceImpl) {
932-
TemplateWorkspaceImpl template = (TemplateWorkspaceImpl) workspace;
919+
if (workspace instanceof TemplateWorkspaceImpl template) {
933920
boolean exists = template.templateExists(connection.getConnection());
934921
if (!exists) {
935922
return false;
@@ -1059,6 +1046,7 @@ public DescriptorImpl() {
10591046
* Returns the name of the SCM, this is the name that will show up next
10601047
* to CVS and Subversion when configuring a job.
10611048
*/
1049+
@NonNull
10621050
@Override
10631051
public String getDisplayName() {
10641052
return "Perforce Software";
@@ -1070,7 +1058,7 @@ public boolean isApplicable(Job project) {
10701058
}
10711059

10721060
@Override
1073-
public SCM newInstance(StaplerRequest2 req, JSONObject formData) throws FormException {
1061+
public SCM newInstance(StaplerRequest2 req, @NonNull JSONObject formData) throws FormException {
10741062
PerforceScm scm = (PerforceScm) super.newInstance(req, formData);
10751063
return scm;
10761064
}
@@ -1083,7 +1071,7 @@ public SCM newInstance(StaplerRequest2 req, JSONObject formData) throws FormExce
10831071
* defined in the global.jelly page.
10841072
*/
10851073
@Override
1086-
public boolean configure(StaplerRequest2 req, JSONObject json) throws FormException {
1074+
public boolean configure(StaplerRequest2 req, JSONObject json) {
10871075

10881076
try {
10891077
autoSave = json.getBoolean("autoSave");

0 commit comments

Comments
 (0)