22
33import com .cloudbees .plugins .credentials .CredentialsProvider ;
44import com .perforce .p4java .exception .P4JavaException ;
5+ import edu .umd .cs .findbugs .annotations .NonNull ;
56import hudson .AbortException ;
67import hudson .EnvVars ;
78import hudson .Extension ;
8889import java .io .IOException ;
8990import java .io .PrintStream ;
9091import java .util .ArrayList ;
91- import java .util .Arrays ;
9292import java .util .Collection ;
9393import java .util .List ;
9494import 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