Merge pull request #12630 from dreis2211:supplier-asserts

* pr/12630:
  Polish "Use Supplier variants of Assert methods"
  Use Supplier variants of Assert methods
pull/12638/merge
Stephane Nicoll 7 years ago
commit a847cd9852

@ -50,8 +50,8 @@ public class DiskSpaceHealthIndicatorProperties {
} }
public void setPath(File path) { public void setPath(File path) {
Assert.isTrue(path.exists(), "Path '" + path + "' does not exist"); Assert.isTrue(path.exists(), () -> "Path '" + path + "' does not exist");
Assert.isTrue(path.canRead(), "Path '" + path + "' cannot be read"); Assert.isTrue(path.canRead(), () -> "Path '" + path + "' cannot be read");
this.path = path; this.path = path;
} }

@ -105,7 +105,7 @@ public final class MetricsRun {
Class<?>[] exportAutoConfigurations) { Class<?>[] exportAutoConfigurations) {
for (Class<?> configuration : exportAutoConfigurations) { for (Class<?> configuration : exportAutoConfigurations) {
Assert.state(EXPORT_AUTO_CONFIGURATIONS.contains(configuration), Assert.state(EXPORT_AUTO_CONFIGURATIONS.contains(configuration),
"Unknown export auto-configuration " + configuration.getName()); () -> "Unknown export auto-configuration " + configuration.getName());
} }
return contextRunner return contextRunner
.withPropertyValues("management.metrics.use-global-registry=false") .withPropertyValues("management.metrics.use-global-registry=false")

@ -432,7 +432,7 @@ public abstract class EndpointDiscoverer<E extends ExposableEndpoint<O>, O exten
this.enabledByDefault = (Boolean) attributes.get("enableByDefault"); this.enabledByDefault = (Boolean) attributes.get("enableByDefault");
this.filter = getFilter(this.bean.getClass()); this.filter = getFilter(this.bean.getClass());
Assert.state(StringUtils.hasText(this.id), Assert.state(StringUtils.hasText(this.id),
"No @Endpoint id attribute specified for " () -> "No @Endpoint id attribute specified for "
+ bean.getClass().getName()); + bean.getClass().getName());
} }

@ -51,7 +51,7 @@ class OperationMethodParameters implements OperationParameters {
String[] parameterNames = parameterNameDiscoverer.getParameterNames(method); String[] parameterNames = parameterNameDiscoverer.getParameterNames(method);
Parameter[] parameters = method.getParameters(); Parameter[] parameters = method.getParameters();
Assert.state(parameterNames != null, Assert.state(parameterNames != null,
"Failed to extract parameter names for " + method); () -> "Failed to extract parameter names for " + method);
this.operationParameters = getOperationParameters(parameters, parameterNames); this.operationParameters = getOperationParameters(parameters, parameterNames);
} }

@ -129,8 +129,9 @@ public class AutoConfigurationImportSelector
AnnotationAttributes attributes = AnnotationAttributes AnnotationAttributes attributes = AnnotationAttributes
.fromMap(metadata.getAnnotationAttributes(name, true)); .fromMap(metadata.getAnnotationAttributes(name, true));
Assert.notNull(attributes, Assert.notNull(attributes,
"No auto-configuration attributes found. Is " + metadata.getClassName() () -> "No auto-configuration attributes found. Is "
+ " annotated with " + ClassUtils.getShortName(name) + "?"); + metadata.getClassName() + " annotated with "
+ ClassUtils.getShortName(name) + "?");
return attributes; return attributes;
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -149,7 +149,7 @@ public class CacheAutoConfiguration {
@PostConstruct @PostConstruct
public void checkHasCacheManager() { public void checkHasCacheManager() {
Assert.notNull(this.cacheManager, Assert.notNull(this.cacheManager,
"No cache manager could " () -> "No cache manager could "
+ "be auto-configured, check your configuration (caching " + "be auto-configured, check your configuration (caching "
+ "type is '" + this.cacheProperties.getType() + "')"); + "type is '" + this.cacheProperties.getType() + "')");
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -107,7 +107,7 @@ public class CacheProperties {
*/ */
public Resource resolveConfigLocation(Resource config) { public Resource resolveConfigLocation(Resource config) {
if (config != null) { if (config != null) {
Assert.isTrue(config.exists(), "Cache configuration does not exist '" Assert.isTrue(config.exists(), () -> "Cache configuration does not exist '"
+ config.getDescription() + "'"); + config.getDescription() + "'");
return config; return config;
} }

@ -503,8 +503,8 @@ class OnBeanCondition extends SpringBootCondition implements ConfigurationCondit
@Override @Override
protected void validate(BeanTypeDeductionException ex) { protected void validate(BeanTypeDeductionException ex) {
Assert.isTrue(getTypes().size() == 1, annotationName() + " annotations must " Assert.isTrue(getTypes().size() == 1, () -> annotationName()
+ "specify only one type (got " + getTypes() + ")"); + " annotations must specify only one type (got " + getTypes() + ")");
} }
} }

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,8 +52,8 @@ public class HazelcastProperties {
if (this.config == null) { if (this.config == null) {
return null; return null;
} }
Assert.isTrue(this.config.exists(), "Hazelcast configuration does not exist '" Assert.isTrue(this.config.exists(), () -> "Hazelcast configuration does not "
+ this.config.getDescription() + "'"); + "exist '" + this.config.getDescription() + "'");
return this.config; return this.config;
} }

@ -323,8 +323,8 @@ public class JacksonAutoConfiguration {
// that may be added by Jackson in the future) // that may be added by Jackson in the future)
Field field = ReflectionUtils.findField(PropertyNamingStrategy.class, Field field = ReflectionUtils.findField(PropertyNamingStrategy.class,
fieldName, PropertyNamingStrategy.class); fieldName, PropertyNamingStrategy.class);
Assert.notNull(field, "Constant named '" + fieldName + "' not found on " Assert.notNull(field, () -> "Constant named '" + fieldName
+ PropertyNamingStrategy.class.getName()); + "' not found on " + PropertyNamingStrategy.class.getName());
try { try {
builder.propertyNamingStrategy( builder.propertyNamingStrategy(
(PropertyNamingStrategy) field.get(null)); (PropertyNamingStrategy) field.get(null));

@ -124,14 +124,15 @@ abstract class ArchiveCommand extends OptionParsingCommand {
protected ExitStatus run(OptionSet options) throws Exception { protected ExitStatus run(OptionSet options) throws Exception {
List<?> nonOptionArguments = new ArrayList<Object>( List<?> nonOptionArguments = new ArrayList<Object>(
options.nonOptionArguments()); options.nonOptionArguments());
Assert.isTrue(nonOptionArguments.size() >= 2, "The name of the resulting " Assert.isTrue(nonOptionArguments.size() >= 2, () -> "The name of the "
+ this.type + " and at least one source file must be specified"); + "resulting" + this.type + " and at least one source file must be "
+ "specified");
File output = new File((String) nonOptionArguments.remove(0)); File output = new File((String) nonOptionArguments.remove(0));
Assert.isTrue( Assert.isTrue(
output.getName().toLowerCase(Locale.ENGLISH) output.getName().toLowerCase(Locale.ENGLISH)
.endsWith("." + this.type), .endsWith("." + this.type),
"The output '" + output + "' is not a " () -> "The output '" + output + "' is not a "
+ this.type.toUpperCase(Locale.ENGLISH) + " file."); + this.type.toUpperCase(Locale.ENGLISH) + " file.");
deleteIfExists(output); deleteIfExists(output);

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -79,7 +79,7 @@ class FolderSnapshot {
Assert.notNull(snapshot, "Snapshot must not be null"); Assert.notNull(snapshot, "Snapshot must not be null");
File folder = this.folder; File folder = this.folder;
Assert.isTrue(snapshot.folder.equals(folder), Assert.isTrue(snapshot.folder.equals(folder),
"Snapshot source folder must be '" + folder + "'"); () -> "Snapshot source folder must be '" + folder + "'");
Set<ChangedFile> changes = new LinkedHashSet<>(); Set<ChangedFile> changes = new LinkedHashSet<>();
Map<File, FileSnapshot> previousFiles = getFilesMap(); Map<File, FileSnapshot> previousFiles = getFilesMap();
for (FileSnapshot currentFile : snapshot.files) { for (FileSnapshot currentFile : snapshot.files) {

@ -111,8 +111,9 @@ public class ClassPathChangeUploader
headers.setContentLength(bytes.length); headers.setContentLength(bytes.length);
FileCopyUtils.copy(bytes, request.getBody()); FileCopyUtils.copy(bytes, request.getBody());
ClientHttpResponse response = request.execute(); ClientHttpResponse response = request.execute();
Assert.state(response.getStatusCode() == HttpStatus.OK, HttpStatus statusCode = response.getStatusCode();
"Unexpected " + response.getStatusCode() Assert.state(statusCode == HttpStatus.OK,
() -> "Unexpected " + statusCode
+ " response uploading class files"); + " response uploading class files");
logUpload(classLoaderFiles); logUpload(classLoaderFiles);
return; return;

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -56,7 +56,8 @@ public class ClassLoaderFile implements Serializable {
public ClassLoaderFile(Kind kind, long lastModified, byte[] contents) { public ClassLoaderFile(Kind kind, long lastModified, byte[] contents) {
Assert.notNull(kind, "Kind must not be null"); Assert.notNull(kind, "Kind must not be null");
Assert.isTrue(kind == Kind.DELETED ? contents == null : contents != null, Assert.isTrue(kind == Kind.DELETED ? contents == null : contents != null,
"Contents must " + (kind == Kind.DELETED ? "" : "not ") + "be null"); () -> "Contents must " + (kind == Kind.DELETED ? "" : "not ")
+ "be null");
this.kind = kind; this.kind = kind;
this.lastModified = lastModified; this.lastModified = lastModified;
this.contents = contents; this.contents = contents;

@ -73,7 +73,8 @@ class ConfigurationPropertiesBinder {
public void bind(Bindable<?> target) { public void bind(Bindable<?> target) {
ConfigurationProperties annotation = target ConfigurationProperties annotation = target
.getAnnotation(ConfigurationProperties.class); .getAnnotation(ConfigurationProperties.class);
Assert.state(annotation != null, "Missing @ConfigurationProperties on " + target); Assert.state(annotation != null,
() -> "Missing @ConfigurationProperties on " + target);
List<Validator> validators = getValidators(target); List<Validator> validators = getValidators(target);
BindHandler bindHandler = getBindHandler(annotation, validators); BindHandler bindHandler = getBindHandler(annotation, validators);
getBinder().bind(annotation.prefix(), target, bindHandler); getBinder().bind(annotation.prefix(), target, bindHandler);

@ -125,7 +125,7 @@ class EnableConfigurationPropertiesImportSelector implements ImportSelector {
private void assertHasAnnotation(Class<?> type) { private void assertHasAnnotation(Class<?> type) {
Assert.notNull( Assert.notNull(
AnnotationUtils.findAnnotation(type, ConfigurationProperties.class), AnnotationUtils.findAnnotation(type, ConfigurationProperties.class),
"No " + ConfigurationProperties.class.getSimpleName() () -> "No " + ConfigurationProperties.class.getSimpleName()
+ " annotation found on '" + type.getName() + "'."); + " annotation found on '" + type.getName() + "'.");
} }

@ -153,7 +153,7 @@ public final class Bindable<T> {
Assert.isTrue( Assert.isTrue(
existingValue == null || this.type.isArray() existingValue == null || this.type.isArray()
|| this.boxedType.resolve().isInstance(existingValue), || this.boxedType.resolve().isInstance(existingValue),
"ExistingValue must be an instance of " + this.type); () -> "ExistingValue must be an instance of " + this.type);
Supplier<T> value = (existingValue == null ? null : () -> existingValue); Supplier<T> value = (existingValue == null ? null : () -> existingValue);
return new Bindable<>(this.type, this.boxedType, value, NO_ANNOTATIONS); return new Bindable<>(this.type, this.boxedType, value, NO_ANNOTATIONS);
} }

@ -39,8 +39,8 @@ final class StringToEnumIgnoringCaseConverterFactory
while (enumType != null && !enumType.isEnum()) { while (enumType != null && !enumType.isEnum()) {
enumType = enumType.getSuperclass(); enumType = enumType.getSuperclass();
} }
Assert.notNull(enumType, Assert.notNull(enumType, () -> "The target type " + targetType.getName()
"The target type " + targetType.getName() + " does not refer to an enum"); + " does not refer to an enum");
return new StringToEnum(enumType); return new StringToEnum(enumType);
} }

Loading…
Cancel
Save