本文整理了Java中org.springframework.util.Assert
类的一些代码示例,展示了Assert
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assert
类的具体详情如下:
包路径:org.springframework.util.Assert
类名称:Assert
[英]Assertion utility class that assists in validating arguments. Useful for identifying programmer errors early and clearly at runtime.
For example, if the contract of a public method states it does not allow null
arguments, Assert can be used to validate that contract. Doing this clearly indicates a contract violation when it occurs and protects the class's invariants.
Typically used to validate method arguments rather than configuration properties, to check for cases that are usually programmer errors rather than configuration errors. In contrast to config initialization code, there is usally no point in falling back to defaults in such methods.
This class is similar to JUnit's assertion library. If an argument value is deemed invalid, an IllegalArgumentException is thrown (typically). For example:
Assert.notNull(clazz, "The class must not be null");
Assert.isTrue(i > 0, "The value must be greater than zero");
Mainly for internal use within the framework; consider Jakarta's Commons Lang >= 2.0 for a more comprehensive suite of assertion utilities.
[中]辅助验证参数的断言实用程序类。有助于在运行时尽早清楚地识别程序员错误。
例如,如果公共方法的约定声明不允许null
参数,则可以使用Assert来验证该约定。这样做清楚地表明,当发生契约冲突时,它会保护类的不变量。
通常用于验证方法参数而不是配置属性,用于检查通常是程序员错误而不是配置错误的情况。与配置初始化代码不同,在这种方法中,通常没有必要回到默认值。
此类类似于JUnit的断言库。如果参数值被视为无效,则抛出IllegalArgumentException(通常)。例如:
Assert.notNull(clazz, "The class must not be null");
Assert.isTrue(i > 0, "The value must be greater than zero");
主要用于框架内部使用;考虑一个更全面的断言实用程序,雅加达的Calm Lang>=2。
代码示例来源:origin: spring-projects/spring-framework
/**
* Set the {@link BeanWiringInfoResolver} to use.
* <p>The default behavior is to look for a bean with the same name as the class.
* As an alternative, consider using annotation-driven bean wiring.
* @see ClassNameBeanWiringInfoResolver
* @see org.springframework.beans.factory.annotation.AnnotationBeanWiringInfoResolver
*/
public void setBeanWiringInfoResolver(BeanWiringInfoResolver beanWiringInfoResolver) {
Assert.notNull(beanWiringInfoResolver, "BeanWiringInfoResolver must not be null");
this.beanWiringInfoResolver = beanWiringInfoResolver;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Set the interface that the proxy must implement.
* @param serviceInterface the interface that the proxy must implement
* @throws IllegalArgumentException if the supplied {@code serviceInterface}
* is not an interface type
*/
public void setServiceInterface(Class<?> serviceInterface) {
Assert.notNull(serviceInterface, "'serviceInterface' must not be null");
Assert.isTrue(serviceInterface.isInterface(), "'serviceInterface' must be an interface");
this.serviceInterface = serviceInterface;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Create a new {@code PropertySource} with the given name and source object.
*/
public PropertySource(String name, T source) {
Assert.hasText(name, "Property source name must contain at least one character");
Assert.notNull(source, "Property source must not be null");
this.name = name;
this.source = source;
}
代码示例来源:origin: spring-projects/spring-framework
@SuppressWarnings("unchecked")
HttpMessageConverterExtractor(Type responseType, List<HttpMessageConverter<?>> messageConverters, Log logger) {
Assert.notNull(responseType, "'responseType' must not be null");
Assert.notEmpty(messageConverters, "'messageConverters' must not be empty");
this.responseType = responseType;
this.responseClass = (responseType instanceof Class ? (Class<T>) responseType : null);
this.messageConverters = messageConverters;
this.logger = logger;
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public final void afterPropertiesSet() throws Exception {
Assert.notNull(this.host, "Host must not be null");
Assert.isTrue(this.port >= 0, "Port must not be a negative number");
Assert.isTrue(this.httpHandler != null || this.handlerMap != null, "No HttpHandler configured");
Assert.state(!this.running, "Cannot reconfigure while running");
synchronized (this.lifecycleMonitor) {
initServer();
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public List<WebSocketExtension> getExtensions() {
Assert.state(this.extensions != null, "WebSocket session is not yet initialized");
return this.extensions;
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public void sendRedirect(String url) throws IOException {
Assert.state(!isCommitted(), "Cannot send redirect - response is already committed");
Assert.notNull(url, "Redirect URL must not be null");
setHeader(HttpHeaders.LOCATION, url);
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
setCommitted(true);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Set the start delay in milliseconds.
* <p>The start delay is added to the current system time (when the bean starts)
* to control the start time of the trigger.
*/
public void setStartDelay(long startDelay) {
Assert.isTrue(startDelay >= 0, "Start delay cannot be negative");
this.startDelay = startDelay;
}
代码示例来源:origin: spring-projects/spring-framework
private static void assertValidContextPath(String contextPath) {
Assert.hasText(contextPath, "Context path must not be empty");
if (contextPath.equals("/")) {
return;
}
Assert.isTrue(contextPath.startsWith("/"), "Context path must begin with '/'");
Assert.isTrue(!contextPath.endsWith("/"), "Context path must not end with '/'");
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Set the name of the session header to use for the session id.
* The name is used to extract the session id from the request headers as
* well to set the session id on the response headers.
* <p>By default set to {@code DEFAULT_HEADER_NAME}
* @param headerName the header name
*/
public void setHeaderName(String headerName) {
Assert.hasText(headerName, "'headerName' must not be empty");
this.headerName = headerName;
}
代码示例来源:origin: spring-projects/spring-framework
protected ParameterizedTypeReference() {
Class<?> parameterizedTypeReferenceSubclass = findParameterizedTypeReferenceSubclass(getClass());
Type type = parameterizedTypeReferenceSubclass.getGenericSuperclass();
Assert.isInstanceOf(ParameterizedType.class, type, "Type must be a parameterized type");
ParameterizedType parameterizedType = (ParameterizedType) type;
Type[] actualTypeArguments = parameterizedType.getActualTypeArguments();
Assert.isTrue(actualTypeArguments.length == 1, "Number of type arguments must be 1");
this.type = actualTypeArguments[0];
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Create an instance with the given converters.
*/
public CompositeMessageConverter(Collection<MessageConverter> converters) {
Assert.notEmpty(converters, "Converters must not be empty");
this.converters = new ArrayList<>(converters);
}
代码示例来源:origin: spring-projects/spring-framework
public final void sendMessage(WebSocketMessage<?> message) throws IOException {
Assert.state(!isClosed(), "Cannot send a message when session is closed");
Assert.isInstanceOf(TextMessage.class, message, "SockJS supports text messages only");
sendMessageInternal(((TextMessage) message).getPayload());
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Construct a new {@code MappingJackson2CborHttpMessageConverter} with a custom {@link ObjectMapper}
* (must be configured with a {@code CBORFactory} instance).
* You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
* @see Jackson2ObjectMapperBuilder#cbor()
*/
public MappingJackson2CborHttpMessageConverter(ObjectMapper objectMapper) {
super(objectMapper, new MediaType("application", "cbor"));
Assert.isInstanceOf(CBORFactory.class, objectMapper.getFactory(), "CBORFactory required");
}
代码示例来源:origin: spring-projects/spring-framework
public WebMvcStompWebSocketEndpointRegistration(
String[] paths, WebSocketHandler webSocketHandler, TaskScheduler sockJsTaskScheduler) {
Assert.notEmpty(paths, "No paths specified");
Assert.notNull(webSocketHandler, "WebSocketHandler must not be null");
this.paths = paths;
this.webSocketHandler = webSocketHandler;
this.sockJsTaskScheduler = sockJsTaskScheduler;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Return the Quartz Scheduler instance that this accessor operates on.
*/
@Override
public Scheduler getScheduler() {
Assert.state(this.scheduler != null, "No Scheduler set");
return this.scheduler;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Create a new <code>FastByteArrayOutputStream</code>
* with the specified initial capacity.
* @param initialBlockSize the initial buffer size in bytes
*/
public FastByteArrayOutputStream(int initialBlockSize) {
Assert.isTrue(initialBlockSize > 0, "Initial block size must be greater than 0");
this.initialBlockSize = initialBlockSize;
this.nextBlockSize = initialBlockSize;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Set the STOMP message broker host.
*/
public void setRelayHost(String relayHost) {
Assert.hasText(relayHost, "relayHost must not be empty");
this.relayHost = relayHost;
}
代码示例来源:origin: spring-projects/spring-framework
/**
* Set the list of {@link MediaType} objects supported by this converter.
*/
public void setSupportedMediaTypes(List<MediaType> supportedMediaTypes) {
Assert.notEmpty(supportedMediaTypes, "MediaType List must not be empty");
this.supportedMediaTypes = new ArrayList<>(supportedMediaTypes);
}
代码示例来源:origin: spring-projects/spring-framework
/**
* {@inheritDoc}
* The {@code ObjectMapper} must be configured with a {@code SmileFactory} instance.
*/
@Override
public void setObjectMapper(ObjectMapper objectMapper) {
Assert.isInstanceOf(SmileFactory.class, objectMapper.getFactory(), "SmileFactory required");
super.setObjectMapper(objectMapper);
}
内容来源于网络,如有侵权,请联系作者删除!