Merge pull request #10658 from dreis2211:assert-state-supplier

* pr/10658:
  Polish "Use Assert.state() with Supplier where possible"
  Use Assert.state() with Supplier where possible
pull/10657/merge
Stephane Nicoll 7 years ago
commit 377e4f3156

@ -163,7 +163,7 @@ public final class EndpointRequest {
private String getPathId(Class<?> source) { private String getPathId(Class<?> source) {
Endpoint annotation = AnnotationUtils.findAnnotation(source, Endpoint.class); Endpoint annotation = AnnotationUtils.findAnnotation(source, Endpoint.class);
Assert.state(annotation != null, Assert.state(annotation != null,
"Class " + source + " is not annotated with @Endpoint"); () -> "Class " + source + " is not annotated with @Endpoint");
return annotation.id(); return annotation.id();
} }

@ -140,7 +140,7 @@ public abstract class AnnotationEndpointDiscoverer<T extends Operation, K>
AnnotationAttributes endpointAttributes = AnnotatedElementUtils AnnotationAttributes endpointAttributes = AnnotatedElementUtils
.getMergedAnnotationAttributes(endpointType, Endpoint.class); .getMergedAnnotationAttributes(endpointType, Endpoint.class);
Assert.state(isExposedOver(endpointAttributes, exposure), Assert.state(isExposedOver(endpointAttributes, exposure),
"Invalid extension " + beanType.getName() + "': endpoint '" () -> "Invalid extension " + beanType.getName() + "': endpoint '"
+ endpointType.getName() + endpointType.getName()
+ "' does not support such extension"); + "' does not support such extension");
EndpointInfo<T> info = getEndpointInfo(endpoints, beanType, endpointType); EndpointInfo<T> info = getEndpointInfo(endpoints, beanType, endpointType);
@ -163,7 +163,7 @@ public abstract class AnnotationEndpointDiscoverer<T extends Operation, K>
private EndpointInfo<T> getEndpointInfo(Map<Class<?>, EndpointInfo<T>> endpoints, private EndpointInfo<T> getEndpointInfo(Map<Class<?>, EndpointInfo<T>> endpoints,
Class<?> beanType, Class<?> endpointClass) { Class<?> beanType, Class<?> endpointClass) {
EndpointInfo<T> endpoint = endpoints.get(endpointClass); EndpointInfo<T> endpoint = endpoints.get(endpointClass);
Assert.state(endpoint != null, "Invalid extension '" + beanType.getName() Assert.state(endpoint != null, () -> "Invalid extension '" + beanType.getName()
+ "': no endpoint found with type '" + endpointClass.getName() + "'"); + "': no endpoint found with type '" + endpointClass.getName() + "'");
return endpoint; return endpoint;
} }

@ -52,7 +52,7 @@ final class CacheConfigurations {
public static String getConfigurationClass(CacheType cacheType) { public static String getConfigurationClass(CacheType cacheType) {
Class<?> configurationClass = MAPPINGS.get(cacheType); Class<?> configurationClass = MAPPINGS.get(cacheType);
Assert.state(configurationClass != null, "Unknown cache type " + cacheType); Assert.state(configurationClass != null, () -> "Unknown cache type " + cacheType);
return configurationClass.getName(); return configurationClass.getName();
} }

@ -124,7 +124,7 @@ public class FlywayAutoConfiguration {
"Migration script locations not configured"); "Migration script locations not configured");
boolean exists = hasAtLeastOneLocation(); boolean exists = hasAtLeastOneLocation();
Assert.state(exists, Assert.state(exists,
"Cannot find migrations location in: " + this.properties () -> "Cannot find migrations location in: " + this.properties
.getLocations() .getLocations()
+ " (please add migrations or check your Flyway configuration)"); + " (please add migrations or check your Flyway configuration)");
} }

@ -232,7 +232,7 @@ public class DataSourceProperties
public String determineDriverClassName() { public String determineDriverClassName() {
if (StringUtils.hasText(this.driverClassName)) { if (StringUtils.hasText(this.driverClassName)) {
Assert.state(driverClassIsLoadable(), Assert.state(driverClassIsLoadable(),
"Cannot load driver class: " + this.driverClassName); () -> "Cannot load driver class: " + this.driverClassName);
return this.driverClassName; return this.driverClassName;
} }
String driverClassName = null; String driverClassName = null;

@ -103,7 +103,7 @@ public class LiquibaseAutoConfiguration {
Resource resource = this.resourceLoader Resource resource = this.resourceLoader
.getResource(this.properties.getChangeLog()); .getResource(this.properties.getChangeLog());
Assert.state(resource.exists(), Assert.state(resource.exists(),
"Cannot find changelog location: " + resource () -> "Cannot find changelog location: " + resource
+ " (please add changelog or check your Liquibase " + " (please add changelog or check your Liquibase "
+ "configuration)"); + "configuration)");
} }

@ -48,7 +48,7 @@ final class SessionStoreMappings {
public static String getConfigurationClass(StoreType sessionStoreType) { public static String getConfigurationClass(StoreType sessionStoreType) {
Class<?> configurationClass = MAPPINGS.get(sessionStoreType); Class<?> configurationClass = MAPPINGS.get(sessionStoreType);
Assert.state(configurationClass != null, Assert.state(configurationClass != null,
"Unknown session store type " + sessionStoreType); () -> "Unknown session store type " + sessionStoreType);
return configurationClass.getName(); return configurationClass.getName();
} }

@ -178,7 +178,7 @@ public class SocialWebAutoConfiguration {
SecurityContext context = SecurityContextHolder.getContext(); SecurityContext context = SecurityContextHolder.getContext();
Authentication authentication = context.getAuthentication(); Authentication authentication = context.getAuthentication();
Assert.state(authentication != null, Assert.state(authentication != null,
"Unable to get a " + "ConnectionRepository: no user signed in"); "Unable to get a ConnectionRepository: no user signed in");
return authentication.getName(); return authentication.getName();
} }

@ -93,7 +93,7 @@ public final class CommandLineInvoker {
} }
File bin = new File(unpacked.listFiles()[0], "bin"); File bin = new File(unpacked.listFiles()[0], "bin");
File launchScript = new File(bin, isWindows() ? "spring.bat" : "spring"); File launchScript = new File(bin, isWindows() ? "spring.bat" : "spring");
Assert.state(launchScript.exists() && launchScript.isFile(), Assert.state(launchScript.exists() && launchScript.isFile(), () ->
"Could not find CLI launch script " + launchScript.getAbsolutePath()); "Could not find CLI launch script " + launchScript.getAbsolutePath());
return launchScript; return launchScript;
} }

@ -76,7 +76,7 @@ public final class ChangedFile {
File file = this.file.getAbsoluteFile(); File file = this.file.getAbsoluteFile();
String folderName = StringUtils.cleanPath(folder.getPath()); String folderName = StringUtils.cleanPath(folder.getPath());
String fileName = StringUtils.cleanPath(file.getPath()); String fileName = StringUtils.cleanPath(file.getPath());
Assert.state(fileName.startsWith(folderName), "The file " + fileName Assert.state(fileName.startsWith(folderName), () -> "The file " + fileName
+ " is not contained in the source folder " + folderName); + " is not contained in the source folder " + folderName);
return fileName.substring(folderName.length() + 1); return fileName.substring(folderName.length() + 1);
} }

@ -299,7 +299,7 @@ public class HttpTunnelServer {
long timeout = HttpTunnelServer.this.disconnectTimeout; long timeout = HttpTunnelServer.this.disconnectTimeout;
long duration = System.currentTimeMillis() - this.lastHttpRequestTime; long duration = System.currentTimeMillis() - this.lastHttpRequestTime;
Assert.state(duration < timeout, Assert.state(duration < timeout,
"Disconnect timeout: " + timeout + " " + duration); () -> "Disconnect timeout: " + timeout + " " + duration);
} }
} }

@ -67,7 +67,7 @@ final class SpringBootConfigurationFinder {
Set<BeanDefinition> components = this.scanner.findCandidateComponents(source); Set<BeanDefinition> components = this.scanner.findCandidateComponents(source);
if (!components.isEmpty()) { if (!components.isEmpty()) {
Assert.state(components.size() == 1, Assert.state(components.size() == 1,
"Found multiple @SpringBootConfiguration annotated classes " () -> "Found multiple @SpringBootConfiguration annotated classes "
+ components); + components);
return ClassUtils.resolveClassName( return ClassUtils.resolveClassName(
components.iterator().next().getBeanClassName(), null); components.iterator().next().getBeanClassName(), null);

@ -96,7 +96,7 @@ public class SpringBootContextLoader extends AbstractContextLoader {
Assert.state( Assert.state(
!ObjectUtils.isEmpty(configClasses) !ObjectUtils.isEmpty(configClasses)
|| !ObjectUtils.isEmpty(configLocations), || !ObjectUtils.isEmpty(configLocations),
"No configuration classes " () -> "No configuration classes "
+ "or locations found in @SpringApplicationConfiguration. " + "or locations found in @SpringApplicationConfiguration. "
+ "For default configuration detection to work you need " + "For default configuration detection to work you need "
+ "Spring 4.0.3 or better (found " + SpringVersion.getVersion() + "Spring 4.0.3 or better (found " + SpringVersion.getVersion()

@ -76,7 +76,7 @@ class DefinitionsParser {
private void parseMockBeanAnnotation(MockBean annotation, AnnotatedElement element) { private void parseMockBeanAnnotation(MockBean annotation, AnnotatedElement element) {
Set<ResolvableType> typesToMock = getOrDeduceTypes(element, annotation.value()); Set<ResolvableType> typesToMock = getOrDeduceTypes(element, annotation.value());
Assert.state(!typesToMock.isEmpty(), Assert.state(!typesToMock.isEmpty(),
"Unable to deduce type to mock from " + element); () -> "Unable to deduce type to mock from " + element);
if (StringUtils.hasLength(annotation.name())) { if (StringUtils.hasLength(annotation.name())) {
Assert.state(typesToMock.size() == 1, Assert.state(typesToMock.size() == 1,
"The name attribute can only be used when mocking a single class"); "The name attribute can only be used when mocking a single class");
@ -93,7 +93,7 @@ class DefinitionsParser {
private void parseSpyBeanAnnotation(SpyBean annotation, AnnotatedElement element) { private void parseSpyBeanAnnotation(SpyBean annotation, AnnotatedElement element) {
Set<ResolvableType> typesToSpy = getOrDeduceTypes(element, annotation.value()); Set<ResolvableType> typesToSpy = getOrDeduceTypes(element, annotation.value());
Assert.state(!typesToSpy.isEmpty(), Assert.state(!typesToSpy.isEmpty(),
"Unable to deduce type to spy from " + element); () -> "Unable to deduce type to spy from " + element);
if (StringUtils.hasLength(annotation.name())) { if (StringUtils.hasLength(annotation.name())) {
Assert.state(typesToSpy.size() == 1, Assert.state(typesToSpy.size() == 1,
"The name attribute can only be used when spying a single class"); "The name attribute can only be used when spying a single class");
@ -109,7 +109,8 @@ class DefinitionsParser {
private void addDefinition(AnnotatedElement element, Definition definition, private void addDefinition(AnnotatedElement element, Definition definition,
String type) { String type) {
boolean isNewDefinition = this.definitions.add(definition); boolean isNewDefinition = this.definitions.add(definition);
Assert.state(isNewDefinition, "Duplicate " + type + " definition " + definition); Assert.state(isNewDefinition, () ->
"Duplicate " + type + " definition " + definition);
if (element instanceof Field) { if (element instanceof Field) {
Field field = (Field) element; Field field = (Field) element;
this.definitionFields.put(definition, field); this.definitionFields.put(definition, field);

@ -380,7 +380,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
void inject(Field field, Object target, Definition definition) { void inject(Field field, Object target, Definition definition) {
String beanName = this.beanNameRegistry.get(definition); String beanName = this.beanNameRegistry.get(definition);
Assert.state(StringUtils.hasLength(beanName), Assert.state(StringUtils.hasLength(beanName),
"No bean found for definition " + definition); () -> "No bean found for definition " + definition);
inject(field, target, beanName, definition); inject(field, target, beanName, definition);
} }
@ -389,7 +389,7 @@ public class MockitoPostProcessor extends InstantiationAwareBeanPostProcessorAda
try { try {
field.setAccessible(true); field.setAccessible(true);
Assert.state(ReflectionUtils.getField(field, target) == null, Assert.state(ReflectionUtils.getField(field, target) == null,
"The field " + field + " cannot have an existing value"); () -> "The field " + field + " cannot have an existing value");
Object bean = this.beanFactory.getBean(beanName, field.getType()); Object bean = this.beanFactory.getBean(beanName, field.getType());
if (definition.isProxyTargetAware() && isAopProxy(bean)) { if (definition.isProxyTargetAware() && isAopProxy(bean)) {
MockitoAopProxyTargetInterceptor.applyTo(bean); MockitoAopProxyTargetInterceptor.applyTo(bean);

@ -44,7 +44,7 @@ public class JavaExecutable {
File bin = new File(new File(javaHome), "bin"); File bin = new File(new File(javaHome), "bin");
File command = new File(bin, "java.exe"); File command = new File(bin, "java.exe");
command = (command.exists() ? command : new File(bin, "java")); command = (command.exists() ? command : new File(bin, "java"));
Assert.state(command.exists(), "Unable to find java in " + javaHome); Assert.state(command.exists(), () -> "Unable to find java in " + javaHome);
return command; return command;
} }

@ -80,7 +80,7 @@ public class ApplicationTemp {
this.dir = new File(getTempDirectory(), toHexString(hash)); this.dir = new File(getTempDirectory(), toHexString(hash));
this.dir.mkdirs(); this.dir.mkdirs();
Assert.state(this.dir.exists(), Assert.state(this.dir.exists(),
"Unable to create temp directory " + this.dir); () -> "Unable to create temp directory " + this.dir);
} }
} }
return this.dir; return this.dir;
@ -90,8 +90,9 @@ public class ApplicationTemp {
String property = System.getProperty("java.io.tmpdir"); String property = System.getProperty("java.io.tmpdir");
Assert.state(StringUtils.hasLength(property), "No 'java.io.tmpdir' property set"); Assert.state(StringUtils.hasLength(property), "No 'java.io.tmpdir' property set");
File file = new File(property); File file = new File(property);
Assert.state(file.exists(), "Temp directory" + file + " does not exist"); Assert.state(file.exists(), () -> "Temp directory" + file + " does not exist");
Assert.state(file.isDirectory(), "Temp location " + file + " is not a directory"); Assert.state(file.isDirectory(), () -> "Temp location " + file
+ " is not a directory");
return file; return file;
} }

@ -131,7 +131,7 @@ public abstract class JsonObjectDeserializer<T>
Assert.notNull(tree, "Tree must not be null"); Assert.notNull(tree, "Tree must not be null");
JsonNode node = tree.get(fieldName); JsonNode node = tree.get(fieldName);
Assert.state(node != null && !(node instanceof NullNode), Assert.state(node != null && !(node instanceof NullNode),
"Missing JSON field '" + fieldName + "'"); () -> "Missing JSON field '" + fieldName + "'");
return node; return node;
} }

@ -92,8 +92,8 @@ public class JettyWebServer implements WebServer {
@Override @Override
protected void doStart() throws Exception { protected void doStart() throws Exception {
for (Connector connector : JettyWebServer.this.connectors) { for (Connector connector : JettyWebServer.this.connectors) {
Assert.state(connector.isStopped(), "Connector " + connector Assert.state(connector.isStopped(), () -> "Connector "
+ " has been started prematurely"); + connector + " has been started prematurely");
} }
JettyWebServer.this.server.setConnectors(null); JettyWebServer.this.server.setConnectors(null);
} }

@ -492,7 +492,7 @@ public class TomcatServletWebServerFactory extends AbstractServletWebServerFacto
private void configurePersistSession(Manager manager) { private void configurePersistSession(Manager manager) {
Assert.state(manager instanceof StandardManager, Assert.state(manager instanceof StandardManager,
"Unable to persist HTTP session state using manager type " () -> "Unable to persist HTTP session state using manager type "
+ manager.getClass().getName()); + manager.getClass().getName());
File dir = getValidSessionStoreDir(); File dir = getValidSessionStoreDir();
File file = new File(dir, "SESSIONS.ser"); File file = new File(dir, "SESSIONS.ser");

@ -135,7 +135,7 @@ public abstract class RegistrationBean implements ServletContextInitializer, Ord
*/ */
protected void configure(Registration.Dynamic registration) { protected void configure(Registration.Dynamic registration) {
Assert.state(registration != null, Assert.state(registration != null,
"Registration is null. Was something already registered for name=[" () -> "Registration is null. Was something already registered for name=["
+ this.name + "]?"); + this.name + "]?");
registration.setAsyncSupported(this.asyncSupported); registration.setAsyncSupported(this.asyncSupported);
if (!this.initParameters.isEmpty()) { if (!this.initParameters.isEmpty()) {

Loading…
Cancel
Save