Skip to content

Commit

Permalink
Reduce usage of StringUtils (#8999)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Mar 1, 2024
1 parent aff3714 commit 3c042d1
Show file tree
Hide file tree
Showing 28 changed files with 57 additions and 72 deletions.
4 changes: 2 additions & 2 deletions cli/src/main/java/hudson/cli/CLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,9 @@ public static int _main(String[] _args) throws Exception {

if (auth == null && bearer == null) {
// -auth option not set
if (StringUtils.isNotBlank(userIdEnv) && StringUtils.isNotBlank(tokenEnv)) {
if ((userIdEnv != null && !userIdEnv.isBlank()) && (tokenEnv != null && !tokenEnv.isBlank())) {
auth = StringUtils.defaultString(userIdEnv).concat(":").concat(StringUtils.defaultString(tokenEnv));
} else if (StringUtils.isNotBlank(userIdEnv) || StringUtils.isNotBlank(tokenEnv)) {
} else if ((userIdEnv != null && !userIdEnv.isBlank()) || (tokenEnv != null && !tokenEnv.isBlank())) {
printUsage(Messages.CLI_BadAuth());
return -1;
} // Otherwise, none credentials were set
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.input.CountingInputStream;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
Expand Down Expand Up @@ -1712,7 +1711,7 @@ public String invoke(File dir, VirtualChannel channel) throws IOException {
public FilePath createTempDir(final String prefix, final String suffix) throws IOException, InterruptedException {
try {
String[] s;
if (StringUtils.isBlank(suffix)) {
if (suffix == null || suffix.isBlank()) {
s = new String[]{prefix, "tmp"}; // see File.createTempFile - tmp is used if suffix is null
} else {
s = new String[]{prefix, suffix};
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/hudson/Functions.java
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ public static String inferHudsonURL(StaplerRequest req) {
public static String getFooterURL() {
if (footerURL == null) {
footerURL = SystemProperties.getString("hudson.footerURL");
if (StringUtils.isBlank(footerURL)) {
if (footerURL == null || footerURL.isBlank()) {
footerURL = "https://www.jenkins.io/";
}
}
Expand Down Expand Up @@ -2194,7 +2194,7 @@ private String getJellyViewsInformationForCurrentRequest() {
int firstPeriod = part.indexOf(".");
return slash > 0 && firstPeriod > 0 && slash < firstPeriod;
}).collect(Collectors.joining(" "));
if (StringUtils.isBlank(views)) {
if (views == null || views.isBlank()) {
// fallback to full thread name if there are no apparent views
return threadName;
}
Expand Down
15 changes: 8 additions & 7 deletions core/src/main/java/hudson/PluginManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ PluginManager doCreate(@NonNull Class<? extends PluginManager> klass,
*/
public static @NonNull PluginManager createDefault(@NonNull Jenkins jenkins) {
String pmClassName = SystemProperties.getString(CUSTOM_PLUGIN_MANAGER);
if (!StringUtils.isBlank(pmClassName)) {
if (pmClassName != null && !pmClassName.isBlank()) {
LOGGER.log(FINE, String.format("Use of custom plugin manager [%s] requested.", pmClassName));
try {
final Class<? extends PluginManager> klass = Class.forName(pmClassName).asSubclass(PluginManager.class);
Expand Down Expand Up @@ -370,7 +370,7 @@ protected PluginManager(ServletContext context, File rootDir) {
throw new UncheckedIOException(e);
}
String workDir = SystemProperties.getString(PluginManager.class.getName() + ".workDir");
this.workDir = StringUtils.isBlank(workDir) ? null : new File(workDir);
this.workDir = workDir == null || workDir.isBlank() ? null : new File(workDir);

strategy = createPluginStrategy();
}
Expand Down Expand Up @@ -1429,7 +1429,7 @@ public HttpResponse doPluginsSearch(@QueryParameter String query, @QueryParamete
for (UpdateSite site : Jenkins.get().getUpdateCenter().getSiteList()) {
List<JSONObject> sitePlugins = site.getAvailables().stream()
.filter(plugin -> {
if (StringUtils.isBlank(query)) {
if (query == null || query.isBlank()) {
return true;
}
return StringUtils.containsIgnoreCase(plugin.name, query) ||
Expand Down Expand Up @@ -1864,9 +1864,10 @@ public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, Servl
File tmpDir = Files.createTempDirectory("uploadDir").toFile();
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD, tmpDir));
List<FileItem> items = upload.parseRequest(req);
if (StringUtils.isNotBlank(items.get(1).getString())) {
String string = items.get(1).getString();
if (string != null && !string.isBlank()) {
// this is a URL deployment
fileName = items.get(1).getString();
fileName = string;
copier = new UrlPluginCopier(fileName);
} else {
// this is a file upload
Expand Down Expand Up @@ -1909,7 +1910,7 @@ public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, Servl
}
String deps = m.getMainAttributes().getValue("Plugin-Dependencies");

if (StringUtils.isNotBlank(deps)) {
if (deps != null && !deps.isBlank()) {
// now we get to parse it!
String[] plugins = deps.split(",");
for (String p : plugins) {
Expand Down Expand Up @@ -1940,7 +1941,7 @@ public HttpResponse doUploadPlugin(StaplerRequest req) throws IOException, Servl

@Restricted(NoExternalUse.class)
@RequirePOST public FormValidation doCheckPluginUrl(StaplerRequest request, @QueryParameter String value) throws IOException {
if (StringUtils.isNotBlank(value)) {
if (value != null && !value.isBlank()) {
try {
URL url = new URL(value);
if (!url.getProtocol().startsWith("http")) {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/model/Computer.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
import jenkins.util.SystemProperties;
import jenkins.widgets.HasWidgets;
import net.jcip.annotations.GuardedBy;
import org.apache.commons.lang.StringUtils;
import org.jenkins.ui.icon.Icon;
import org.jenkins.ui.icon.IconSet;
import org.kohsuke.accmod.Restricted;
Expand Down Expand Up @@ -1522,7 +1521,7 @@ public void doConfigSubmit(StaplerRequest req, StaplerResponse rsp) throws IOExc
}

String nExecutors = req.getSubmittedForm().getString("numExecutors");
if (StringUtils.isBlank(nExecutors) || Integer.parseInt(nExecutors) <= 0) {
if (nExecutors == null || nExecutors.isBlank() || Integer.parseInt(nExecutors) <= 0) {
throw new FormException(Messages.Slave_InvalidConfig_Executors(nodeName), "numExecutors");
}

Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/model/DirectoryBrowserSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import jenkins.util.SystemProperties;
import jenkins.util.VirtualFile;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.kohsuke.accmod.Restricted;
Expand Down Expand Up @@ -280,7 +279,7 @@ private void serveFile(StaplerRequest req, StaplerResponse rsp, VirtualFile root
if (zip) {
rsp.setContentType("application/zip");
String includes, prefix;
if (StringUtils.isBlank(rest)) {
if (rest == null || rest.isBlank()) {
includes = "**";
// JENKINS-19947, JENKINS-61473: traditional behavior is to prepend the directory name
prefix = baseFile.getName();
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/hudson/model/TopLevelItemDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import jenkins.model.item_category.ItemCategory;
import org.apache.commons.jelly.Script;
import org.apache.commons.jelly.XMLOutput;
import org.apache.commons.lang.StringUtils;
import org.jenkins.ui.icon.Icon;
import org.jenkins.ui.icon.IconSet;
import org.jenkins.ui.icon.IconSpec;
Expand Down Expand Up @@ -215,8 +214,9 @@ public String getIconFilePathPattern() {
@CheckForNull
@Deprecated
public String getIconFilePath(String size) {
if (!StringUtils.isBlank(getIconFilePathPattern())) {
return getIconFilePathPattern().replace(":size", size);
String iconFilePathPattern = getIconFilePathPattern();
if (iconFilePathPattern != null && !iconFilePathPattern.isBlank()) {
return iconFilePathPattern.replace(":size", size);
}
return null;
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
import jenkins.security.UserDetailsCache;
import jenkins.util.SystemProperties;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand Down Expand Up @@ -795,7 +794,7 @@ static File getRootDir() {
* @since 1.600
*/
public static boolean isIdOrFullnameAllowed(@CheckForNull String id) {
if (StringUtils.isBlank(id)) {
if (id == null || id.isBlank()) {
return false;
}
final String trimmedId = id.trim();
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/hudson/model/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkins.ui.icon.Icon;
import org.jenkins.ui.icon.IconSet;
import org.kohsuke.accmod.Restricted;
Expand Down Expand Up @@ -1121,7 +1120,7 @@ public Categories doItemCategories(StaplerRequest req, StaplerResponse rsp, @Que
int order = 0;
String resUrl;

if (StringUtils.isNotBlank(iconStyle)) {
if (iconStyle != null && !iconStyle.isBlank()) {
resUrl = req.getContextPath() + Jenkins.RESOURCE_PATH;
} else {
resUrl = null;
Expand All @@ -1137,7 +1136,7 @@ public Categories doItemCategories(StaplerRequest req, StaplerResponse rsp, @Que
metadata.put("description", descriptor.getDescription());
metadata.put("iconFilePathPattern", descriptor.getIconFilePathPattern());
String iconClassName = descriptor.getIconClassName();
if (StringUtils.isNotBlank(iconClassName)) {
if (iconClassName != null && !iconClassName.isBlank()) {
metadata.put("iconClassName", iconClassName);
if (resUrl != null) {
Icon icon = IconSet.icons
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/model/ViewDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.Objects;
import jenkins.model.DirectlyModifiableTopLevelItemGroup;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.stapler.AncestorInPath;
Expand Down Expand Up @@ -141,7 +140,7 @@ public List<Descriptor<ViewJobFilter>> getJobFiltersDescriptors() {
*/
@SuppressWarnings("unused") // expose utility check method to subclasses
protected FormValidation checkDisplayName(@NonNull View view, @CheckForNull String value) {
if (StringUtils.isBlank(value)) {
if (value == null || value.isBlank()) {
// no custom name, no need to check
return FormValidation.ok();
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/security/SecurityRealm.java
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ public static String getFrom() {

// Return encoded value or at least "/" in the case exception occurred during encode()
// or if the encoded content is blank value
return StringUtils.isBlank(returnValue) ? "/" : returnValue;
return returnValue == null || returnValue.isBlank() ? "/" : returnValue;
}

private static class None extends SecurityRealm {
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/hudson/tasks/BuildTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ public FormValidation doCheck(@AncestorInPath AbstractProject project, @QueryPar
boolean hasProjects = false;
while (tokens.hasMoreTokens()) {
String projectName = tokens.nextToken().trim();
if (StringUtils.isNotBlank(projectName)) {
if (projectName != null && !projectName.isBlank()) {
Item item = Jenkins.get().getItem(projectName, project, Item.class);
if (item == null) {
Job<?, ?> nearest = Items.findNearest(Job.class, projectName, project.getParent());
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/hudson/tasks/Maven.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
import jenkins.mvn.SettingsProvider;
import jenkins.security.MasterToSlaveCallable;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
Expand Down Expand Up @@ -331,13 +330,13 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

if (!S_PATTERN.matcher(targets).find()) { // check the given target/goals do not contain settings parameter already
String settingsPath = SettingsProvider.getSettingsRemotePath(getSettings(), build, listener);
if (StringUtils.isNotBlank(settingsPath)) {
if (settingsPath != null && !settingsPath.isBlank()) {
args.add("-s", settingsPath);
}
}
if (!GS_PATTERN.matcher(targets).find()) {
String settingsPath = GlobalSettingsProvider.getSettingsRemotePath(getGlobalSettings(), build, listener);
if (StringUtils.isNotBlank(settingsPath)) {
if (settingsPath != null && !settingsPath.isBlank()) {
args.add("-gs", settingsPath);
}
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/triggers/SCMTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
import jenkins.util.SystemProperties;
import net.sf.json.JSONObject;
import org.apache.commons.jelly.XMLOutput;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
Expand Down Expand Up @@ -381,7 +380,7 @@ public FormValidation doCheckPollingThreadCount(@QueryParameter String value) {
public FormValidation doCheckScmpoll_spec(@QueryParameter String value,
@QueryParameter boolean ignorePostCommitHooks,
@AncestorInPath Item item) {
if (StringUtils.isBlank(value)) {
if (value == null || value.isBlank()) {
if (ignorePostCommitHooks) {
return FormValidation.ok(Messages.SCMTrigger_no_schedules_no_hooks());
} else {
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/util/io/ZipArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.nio.file.InvalidPathException;
import java.nio.file.OpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.Zip64Mode;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
Expand All @@ -62,7 +61,7 @@ final class ZipArchiver extends Archiver {
@Restricted(NoExternalUse.class)
ZipArchiver(OutputStream out, String prefix, OpenOption... openOptions) {
this.openOptions = openOptions;
if (StringUtils.isBlank(prefix)) {
if (prefix == null || prefix.isBlank()) {
this.prefix = "";
} else {
this.prefix = Util.ensureEndsWith(prefix, "/");
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/jenkins/install/InstallState.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import jenkins.security.apitoken.ApiTokenPropertyConfiguration;
import jenkins.security.stapler.StaplerAccessibleType;
import jenkins.util.Timer;
import org.apache.commons.lang.StringUtils;

/**
* Jenkins install state.
Expand Down Expand Up @@ -152,7 +151,8 @@ private static final class ConfigureInstance extends InstallState {
public void initializeState() {
// Skip this state if a boot script already configured the root URL
// in case we add more fields in this page, this should be adapted
if (StringUtils.isNotBlank(JenkinsLocationConfiguration.getOrDie().getUrl())) {
String url = JenkinsLocationConfiguration.getOrDie().getUrl();
if (url != null && !url.isBlank()) {
InstallUtil.proceedToNextStateFrom(this);
}
}
Expand Down Expand Up @@ -315,7 +315,7 @@ public void initializeState() {
@Deprecated
protected Object readResolve() {
// If we get invalid state from the configuration, fallback to unknown
if (StringUtils.isBlank(name)) {
if (name == null || name.isBlank()) {
LOGGER.log(Level.WARNING, "Read install state with blank name: ''{0}''. It will be ignored", name);
return UNKNOWN;
}
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/jenkins/install/InstallUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import jenkins.model.Jenkins;
import jenkins.util.SystemProperties;
import jenkins.util.xml.XMLUtils;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;

Expand Down Expand Up @@ -214,7 +213,7 @@ public static void saveLastExecVersion() {
String version = Files.readString(Util.fileToPath(lastExecVersionFile), Charset.defaultCharset());
// JENKINS-37438 blank will force the setup
// wizard regardless of current state of the system
if (StringUtils.isBlank(version)) {
if (version == null || version.isBlank()) {
return FORCE_NEW_INSTALL_VERSION.toString();
}
return version;
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/jenkins/model/AssetManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import jenkins.ClassLoaderReflectionToolkit;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
Expand Down Expand Up @@ -76,7 +75,7 @@ public void doDynamic(StaplerRequest req, StaplerResponse rsp) throws IOExceptio
* doesn't find it, fall back to the parent classloader.
*/
private @CheckForNull URL findResource(@NonNull String path) throws IOException {
if (StringUtils.isBlank(path)) {
if (path == null || path.isBlank()) {
return null;
}

Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/jenkins/model/ProjectNamingStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.servlet.ServletException;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
Expand Down Expand Up @@ -172,7 +171,7 @@ public PatternProjectNamingStrategy(String namePattern, String description, bool

@Override
public void checkName(String name) {
if (StringUtils.isNotBlank(namePattern) && StringUtils.isNotBlank(name)) {
if ((namePattern != null && !namePattern.isBlank()) && (name != null && !name.isBlank())) {
if (!Pattern.matches(namePattern, name)) {
throw new Failure(description == null || description.isEmpty() ?
Messages.Hudson_JobNameConventionNotApplyed(name, namePattern) :
Expand Down
Loading

0 comments on commit 3c042d1

Please sign in to comment.