From 6e49b3e3e2792e9b357c895104edb64668c64eee Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 15 Nov 2022 11:23:45 -0800 Subject: [PATCH 1/2] Miscellaneous code cleanup --- .../installers/BatchCommandInstaller.java | 5 +++-- .../utils/EnvStringParseHelper.java | 10 +++++++--- .../AuthenticatedDownloadCallable.java | 1 + .../AuthenticatedZipExtractionInstaller.java | 17 ++++++++++------- .../installers/FindOnPathCallable.java | 1 + .../installers/IsAlreadyOnPath.java | 5 ++--- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/synopsys/arc/jenkinsci/plugins/extratoolinstallers/installers/BatchCommandInstaller.java b/src/main/java/com/synopsys/arc/jenkinsci/plugins/extratoolinstallers/installers/BatchCommandInstaller.java index 7687a11..179f98b 100644 --- a/src/main/java/com/synopsys/arc/jenkinsci/plugins/extratoolinstallers/installers/BatchCommandInstaller.java +++ b/src/main/java/com/synopsys/arc/jenkinsci/plugins/extratoolinstallers/installers/BatchCommandInstaller.java @@ -61,8 +61,9 @@ public BatchCommandInstaller(String label, String command, String toolHome, bool private static String fixCrLf(String s) { // eliminate CR int idx; - while((idx=s.indexOf("\r\n"))!=-1) - s = s.substring(0,idx)+s.substring(idx+1); + while ((idx = s.indexOf("\r\n")) != -1) { + s = s.substring(0, idx) + s.substring(idx + 1); + } return s; } diff --git a/src/main/java/com/synopsys/arc/jenkinsci/plugins/extratoolinstallers/utils/EnvStringParseHelper.java b/src/main/java/com/synopsys/arc/jenkinsci/plugins/extratoolinstallers/utils/EnvStringParseHelper.java index 3fa0751..5d6992d 100644 --- a/src/main/java/com/synopsys/arc/jenkinsci/plugins/extratoolinstallers/utils/EnvStringParseHelper.java +++ b/src/main/java/com/synopsys/arc/jenkinsci/plugins/extratoolinstallers/utils/EnvStringParseHelper.java @@ -42,7 +42,9 @@ private EnvStringParseHelper() {} * @since 0.3 */ public static String substituteEnvVars(String macroString, EnvVars environment) { - if (macroString == null) return null; + if (macroString == null) { + return null; + } if (!macroString.contains("${")) { return macroString; } @@ -57,7 +59,9 @@ public static String substituteEnvVars(String macroString, EnvVars environment) */ @Nullable public static String substituteNodeVariables(@CheckForNull String macroString, @NonNull Node node) { - if (macroString == null) return null; + if (macroString == null) { + return null; + } if (!macroString.contains("${")) { return macroString; } @@ -69,7 +73,7 @@ public static String substituteNodeVariables(@CheckForNull String macroString, @ } // Substitute global variables - final Jenkins jenkinsInstance = Jenkins.getInstance(); + final Jenkins jenkinsInstance = Jenkins.get(); for (NodeProperty entry : jenkinsInstance.getGlobalNodeProperties()) { substitutedString = substituteNodeProperty(substitutedString, entry); } diff --git a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedDownloadCallable.java b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedDownloadCallable.java index c175906..86e097d 100644 --- a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedDownloadCallable.java +++ b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedDownloadCallable.java @@ -81,6 +81,7 @@ class AuthenticatedDownloadCallable extends MasterToSlaveFileCallable { this.logOrNull = logOrNull; } + @Override public Date invoke(@NonNull File d, VirtualChannel channel) throws IOException, InterruptedException { final FilePath whereToDownloadTo = new FilePath(d); return downloadAndUnpack(uri, usernameOrNull, passwordOrNull, timestampOfLocalContents, nodeName, diff --git a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedZipExtractionInstaller.java b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedZipExtractionInstaller.java index bfdcaf8..669a627 100644 --- a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedZipExtractionInstaller.java +++ b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedZipExtractionInstaller.java @@ -285,7 +285,7 @@ protected Date downloadOnRemoteNode(@NonNull final URI uri, @CheckForNull final private static @CheckForNull StandardCredentials getCredentialsOrNull(@NonNull final String credentialsId, @CheckForNull final String urlHostOrNullOrEmpty) { final List forOurUrl = getDomainRequirements(urlHostOrNullOrEmpty); - final ItemGroup allOfJenkins = Jenkins.getInstance(); + final ItemGroup allOfJenkins = Jenkins.getInstanceOrNull(); final CredentialsMatcher onlyOurCredentials = CredentialsMatchers.allOf(CREDENTIAL_TYPES_WE_CAN_HANDLE, CredentialsMatchers.withId(credentialsId)); final List allJenkinsCredentialsForOurUrl = CredentialsProvider @@ -328,9 +328,9 @@ protected Date downloadOnRemoteNode(@NonNull final URI uri, @CheckForNull final private static @NonNull List getDomainRequirements( @CheckForNull final String urlHostOrNullOrEmpty) { if (Util.fixEmpty(urlHostOrNullOrEmpty) != null) { - return Collections.singletonList(new HostnameRequirement(urlHostOrNullOrEmpty)); + return Collections.singletonList(new HostnameRequirement(urlHostOrNullOrEmpty)); } else { - return Collections.emptyList(); + return Collections.emptyList(); } } @@ -338,9 +338,11 @@ protected Date downloadOnRemoteNode(@NonNull final URI uri, @CheckForNull final static class ChmodRecAPlusX extends MasterToSlaveFileCallable { private static final long serialVersionUID = 1L; + @Override public Void invoke(File d, VirtualChannel channel) throws IOException { - if (!Functions.isWindows()) + if (!Functions.isWindows()) { process(d); + } return null; } @@ -363,6 +365,7 @@ private void process(File f) { */ @Extension @Symbol("authenticatedzip") public static class DescriptorImpl extends ToolInstallerDescriptor { + @Override public String getDisplayName() { return Messages.AuthenticatedZipExtractionInstaller_DescriptorImpl_displayName(); } @@ -391,7 +394,7 @@ public ListBoxModel doFillCredentialsIdItems(@QueryParameter String credentialsI // suppress exception as url is validated elsewhere } /* System.out.println(" urlHost = " + urlHost); */ - final ItemGroup allOfJenkins = Jenkins.getInstance(); + final ItemGroup allOfJenkins = Jenkins.getInstanceOrNull(); final List domainRequirements = getDomainRequirements(urlHostOrNullOrEmpty); result.includeMatchingAs(ACL.SYSTEM, allOfJenkins, StandardCredentials.class, domainRequirements, CREDENTIAL_TYPES_WE_CAN_HANDLE); @@ -454,7 +457,7 @@ public FormValidation doCheckUrl(@QueryParameter String credentialsId, @QueryPar */ private static @NonNull FormValidation checkUrlAndCredentialsId(final boolean checkUrl, @CheckForNull final String credentialsIdOrNull, @CheckForNull final String urlOrNull) { - if (!(hasPermissionToConfigure())) { + if (!hasPermissionToConfigure()) { /* * System.out.println( * "checkUrlAndCredentialsId: NOT hasPermissionToConfigure"); @@ -558,7 +561,7 @@ public FormValidation doCheckUrl(@QueryParameter String credentialsId, @QueryPar } private static boolean hasPermissionToConfigure() { - return Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER); + return Jenkins.get().hasPermission(Jenkins.ADMINISTER); } } } diff --git a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/FindOnPathCallable.java b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/FindOnPathCallable.java index 5e48456..6412ac3 100644 --- a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/FindOnPathCallable.java +++ b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/FindOnPathCallable.java @@ -40,6 +40,7 @@ class FindOnPathCallable extends MasterToSlaveFileCallable { this.logOrNull = logOrNull; } + @Override public String invoke(@NonNull File d, VirtualChannel channel) throws IOException, InterruptedException { return findOnPath(executableName, getPath(), logOrNull); } diff --git a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/IsAlreadyOnPath.java b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/IsAlreadyOnPath.java index 2a1019c..851fcfa 100644 --- a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/IsAlreadyOnPath.java +++ b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/IsAlreadyOnPath.java @@ -20,8 +20,6 @@ import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.QueryParameter; -import com.google.common.base.Joiner; - import hudson.Extension; import hudson.FilePath; import hudson.Launcher; @@ -132,7 +130,7 @@ public String getVersionCmdString() { if (v == null) { return ""; } - return Joiner.on('\n').join(v); + return String.join("\n", v); } /** @@ -337,6 +335,7 @@ FindOnPathCallable mkCallable(@NonNull final String exeName, @CheckForNull final @Extension @Symbol("findonpath") public static class DescriptorImpl extends ToolInstallerDescriptor { + @Override public String getDisplayName() { return Messages.IsAlreadyOnPath_DescriptorImpl_displayName(); } From b8e5ff41222cbbb95d26aa71f47cc9be27e26c21 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Tue, 15 Nov 2022 11:31:17 -0800 Subject: [PATCH 2/2] SpotBugs --- .../installers/AuthenticatedZipExtractionInstaller.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedZipExtractionInstaller.java b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedZipExtractionInstaller.java index 669a627..c41eafe 100644 --- a/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedZipExtractionInstaller.java +++ b/src/main/java/io/jenkins/plugins/extratoolinstallers/installers/AuthenticatedZipExtractionInstaller.java @@ -9,6 +9,7 @@ import edu.umd.cs.findbugs.annotations.CheckForNull; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.URI; @@ -371,6 +372,7 @@ public String getDisplayName() { } /* List credentials that can be used on the specified URL */ + @SuppressFBWarnings(value = "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE", justification = "TODO needs triage") public ListBoxModel doFillCredentialsIdItems(@QueryParameter String credentialsId, @QueryParameter String url) { /* * System.out.println("doFillCredentialsIdItems(" + item + "," +