Polish Collection.toArray

Consistently use `StringUtils.toStringArray`, `ClassUtils.toClassArray`
or zero length when converting collections to arrays.

Fixes gh-12160
This commit is contained in:
Phillip Webb
2018-02-22 21:10:02 -08:00
parent 358adcd6e3
commit 4b9c3c137e
84 changed files with 210 additions and 189 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -96,7 +96,7 @@ public final class SpringCli {
}
}
}
return urls.toArray(new URL[urls.size()]);
return urls.toArray(new URL[0]);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -195,7 +195,7 @@ public class CommandRunner implements Iterable<Command> {
}
rtn.add(arg);
}
return rtn.toArray(new String[rtn.size()]);
return StringUtils.toStringArray(rtn);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import joptsimple.OptionSet;
import org.springframework.boot.cli.util.ResourceUtils;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
/**
* Extract source file options (anything following '--' in an {@link OptionSet}).
@@ -125,7 +126,11 @@ public class SourceOptions {
}
public String[] getArgsArray() {
return this.args.toArray(new String[this.args.size()]);
return this.args.stream().map(this::asString).toArray(String[]::new);
}
private String asString(Object arg) {
return (arg == null ? null : String.valueOf(arg));
}
public List<String> getSources() {
@@ -133,7 +138,7 @@ public class SourceOptions {
}
public String[] getSourcesArray() {
return this.sources.toArray(new String[this.sources.size()]);
return StringUtils.toStringArray(this.sources);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import org.springframework.boot.cli.command.AbstractCommand;
import org.springframework.boot.cli.command.Command;
import org.springframework.boot.cli.command.status.ExitStatus;
import org.springframework.boot.loader.tools.RunProcess;
import org.springframework.util.StringUtils;
/**
* Special {@link Command} used to run a process from the shell. NOTE: this command is not
@@ -49,7 +50,7 @@ class RunProcessCommand extends AbstractCommand {
protected ExitStatus run(Collection<String> args) throws IOException {
this.process = new RunProcess(this.command);
int code = this.process.run(true, args.toArray(new String[args.size()]));
int code = this.process.run(true, StringUtils.toStringArray(args));
if (code == 0) {
return ExitStatus.OK;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -169,7 +169,7 @@ public class DependencyManagementBomTransformation
private void updateDependencyResolutionContext(
List<Map<String, String>> bomDependencies) {
URI[] uris = Grape.getInstance().resolve(null,
bomDependencies.toArray(new Map[bomDependencies.size()]));
bomDependencies.toArray(new Map[0]));
DefaultModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();
for (URI uri : uris) {
try {

View File

@@ -186,7 +186,7 @@ public class ExtendedGroovyClassLoader extends GroovyClassLoader {
findGroovyJarsFromClassPath(urls);
}
Assert.state(!urls.isEmpty(), "Unable to find groovy JAR");
return new ArrayList<>(urls).toArray(new URL[urls.size()]);
return new ArrayList<>(urls).toArray(new URL[0]);
}
private void findGroovyJarsDirectly(ClassLoader classLoader, Set<URL> urls) {

View File

@@ -49,6 +49,7 @@ import org.springframework.boot.cli.compiler.grape.DependencyResolutionContext;
import org.springframework.boot.cli.compiler.grape.GrapeEngineInstaller;
import org.springframework.boot.cli.util.ResourceUtils;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.util.ClassUtils;
/**
* Compiler for Groovy sources. Primarily a simple Facade for
@@ -220,7 +221,7 @@ public class GroovyCompiler {
classes.add(0, mainClass);
}
return classes.toArray(new Class<?>[classes.size()]);
return ClassUtils.toClassArray(classes);
}
@SuppressWarnings("rawtypes")

View File

@@ -290,7 +290,7 @@ public class AetherGrapeEngine implements GrapeEngine {
for (File file : files) {
uris.add(file.toURI());
}
return uris.toArray(new URI[uris.size()]);
return uris.toArray(new URI[0]);
}
catch (Exception ex) {
throw new DependencyResolutionFailedException(ex);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,6 +43,7 @@ import org.springframework.boot.cli.command.grab.GrabCommand;
import org.springframework.boot.cli.command.run.RunCommand;
import org.springframework.boot.test.rule.OutputCapture;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
/**
* {@link TestRule} that can be used to invoke CLI commands.
@@ -84,7 +85,7 @@ public class CliTester implements TestRule {
"--classpath=.:" + new File("target/test-classes").getAbsolutePath());
}
Future<RunCommand> future = submitCommand(new RunCommand(),
updatedArgs.toArray(new String[updatedArgs.size()]));
StringUtils.toStringArray(updatedArgs));
this.commands.add(future.get(this.timeout, TimeUnit.MILLISECONDS));
return getOutput();
}