Nullability refinements on private and static methods

Based on IntelliJ IDEA 2017.3 introspection results.

Issue: SPR-15756
This commit is contained in:
Juergen Hoeller
2017-09-22 18:22:12 +02:00
parent 60f47f4489
commit 7ae59d0c2a
88 changed files with 319 additions and 300 deletions

View File

@@ -68,7 +68,6 @@ public class MockExpressionEvaluator extends javax.servlet.jsp.el.ExpressionEval
public Object evaluate(String expression, Class expectedType, javax.servlet.jsp.el.VariableResolver variableResolver,
javax.servlet.jsp.el.FunctionMapper functionMapper) throws javax.servlet.jsp.el.ELException {
Assert.isNull(variableResolver, "Custom VariableResolver not supported");
return doEvaluate(expression, expectedType, functionMapper);
}
@@ -76,7 +75,6 @@ public class MockExpressionEvaluator extends javax.servlet.jsp.el.ExpressionEval
protected Object doEvaluate(String expression, Class expectedType, javax.servlet.jsp.el.FunctionMapper functionMapper)
throws javax.servlet.jsp.el.ELException {
Assert.isNull(functionMapper, "Custom FunctionMapper not supported");
try {
return ExpressionEvaluatorManager.evaluate("JSP EL expression", expression, expectedType, this.pageContext);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@@ -30,6 +30,7 @@ import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.lang.Nullable;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.jdbc.Sql.ExecutionPhase;
import org.springframework.test.context.jdbc.SqlConfig.ErrorMode;
@@ -231,6 +232,7 @@ public class SqlScriptsTestExecutionListener extends AbstractTestExecutionListen
}
}
@Nullable
private DataSource getDataSourceFromTransactionManager(PlatformTransactionManager transactionManager) {
try {
Method getDataSourceMethod = transactionManager.getClass().getMethod("getDataSource");

View File

@@ -23,7 +23,6 @@ import java.util.function.Function;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
@@ -69,7 +68,6 @@ abstract class AbstractExpressionEvaluatingCondition implements ExecutionConditi
/**
* Evaluate the expression configured via the supplied annotation type on
* the {@link AnnotatedElement} for the supplied {@link ExtensionContext}.
*
* @param annotationType the type of annotation to process
* @param expressionExtractor a function that extracts the expression from
* the annotation
@@ -88,6 +86,7 @@ abstract class AbstractExpressionEvaluatingCondition implements ExecutionConditi
Function<A, String> expressionExtractor, Function<A, String> reasonExtractor,
Function<A, Boolean> loadContextExtractor, boolean enabledOnTrue, ExtensionContext context) {
Assert.state(context.getElement().isPresent(), "No AnnotatedElement");
AnnotatedElement element = context.getElement().get();
Optional<A> annotation = findMergedAnnotation(element, annotationType);
@@ -100,13 +99,11 @@ abstract class AbstractExpressionEvaluatingCondition implements ExecutionConditi
return ConditionEvaluationResult.enabled(reason);
}
// @formatter:off
String expression = annotation.map(expressionExtractor).map(String::trim).filter(StringUtils::hasLength)
.orElseThrow(() -> new IllegalStateException(String.format(
"The expression in @%s on [%s] must not be blank", annotationType.getSimpleName(), element)));
// @formatter:on
boolean loadContext = annotation.map(loadContextExtractor).get();
boolean loadContext = loadContextExtractor.apply(annotation.get());
boolean evaluatedToTrue = evaluateExpression(expression, loadContext, annotationType, context);
if (evaluatedToTrue) {
@@ -127,20 +124,21 @@ abstract class AbstractExpressionEvaluatingCondition implements ExecutionConditi
if (logger.isDebugEnabled()) {
logger.debug(reason);
}
return (enabledOnTrue ? ConditionEvaluationResult.disabled(reason)
: ConditionEvaluationResult.enabled(reason));
return (enabledOnTrue ? ConditionEvaluationResult.disabled(reason) :
ConditionEvaluationResult.enabled(reason));
}
}
private <A extends Annotation> boolean evaluateExpression(String expression, boolean loadContext,
Class<A> annotationType, ExtensionContext extensionContext) {
Class<A> annotationType, ExtensionContext context) {
AnnotatedElement element = extensionContext.getElement().get();
Assert.state(context.getElement().isPresent(), "No AnnotatedElement");
AnnotatedElement element = context.getElement().get();
GenericApplicationContext gac = null;
ApplicationContext applicationContext;
if (loadContext) {
applicationContext = SpringExtension.getApplicationContext(extensionContext);
applicationContext = SpringExtension.getApplicationContext(context);
}
else {
gac = new GenericApplicationContext();
@@ -191,8 +189,9 @@ abstract class AbstractExpressionEvaluatingCondition implements ExecutionConditi
}
}
private static <A extends Annotation> Optional<A> findMergedAnnotation(AnnotatedElement element,
Class<A> annotationType) {
private static <A extends Annotation> Optional<A> findMergedAnnotation(
AnnotatedElement element, Class<A> annotationType) {
return Optional.ofNullable(AnnotatedElementUtils.findMergedAnnotation(element, annotationType));
}

View File

@@ -20,9 +20,9 @@ import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExtensionContext;
/**
* {@code DisabledIfCondition} is an {@link ExecutionCondition} that supports the
* {@link DisabledIf @DisabledIf} annotation when using the <em>Spring TestContext
* Framework</em> in conjunction with JUnit 5's <em>Jupiter</em> programming model.
* {@code DisabledIfCondition} is an {@link org.junit.jupiter.api.extension.ExecutionCondition}
* that supports the {@link DisabledIf @DisabledIf} annotation when using the <em>Spring
* TestContext Framework</em> in conjunction with JUnit 5's <em>Jupiter</em> programming model.
*
* <p>Any attempt to use the {@code DisabledIfCondition} without the presence of
* {@link DisabledIf @DisabledIf} will result in an <em>enabled</em>
@@ -43,8 +43,8 @@ public class DisabledIfCondition extends AbstractExpressionEvaluatingCondition {
*/
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
return evaluateAnnotation(DisabledIf.class, DisabledIf::expression, DisabledIf::reason, DisabledIf::loadContext,
false, context);
return evaluateAnnotation(DisabledIf.class, DisabledIf::expression, DisabledIf::reason,
DisabledIf::loadContext, false, context);
}
}

View File

@@ -20,9 +20,9 @@ import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExtensionContext;
/**
* {@code EnabledIfCondition} is an {@link ExecutionCondition} that supports the
* {@link EnabledIf @EnabledIf} annotation when using the <em>Spring TestContext
* Framework</em> in conjunction with JUnit 5's <em>Jupiter</em> programming model.
* {@code EnabledIfCondition} is an {@link org.junit.jupiter.api.extension.ExecutionCondition}
* that supports the {@link EnabledIf @EnabledIf} annotation when using the <em>Spring
* TestContext Framework</em> in conjunction with JUnit 5's <em>Jupiter</em> programming model.
*
* <p>Any attempt to use the {@code EnabledIfCondition} without the presence of
* {@link EnabledIf @EnabledIf} will result in an <em>enabled</em>
@@ -43,8 +43,8 @@ public class EnabledIfCondition extends AbstractExpressionEvaluatingCondition {
*/
@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
return evaluateAnnotation(EnabledIf.class, EnabledIf::expression, EnabledIf::reason, EnabledIf::loadContext,
true, context);
return evaluateAnnotation(EnabledIf.class, EnabledIf::expression, EnabledIf::reason,
EnabledIf::loadContext, true, context);
}
}

View File

@@ -115,7 +115,6 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
/**
* Set the {@link ApplicationContext} to be used by this test instance,
* provided via {@link ApplicationContextAware} semantics.
*
* @param applicationContext the ApplicationContext that this test runs in
*/
@Override
@@ -123,13 +122,11 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
this.applicationContext = applicationContext;
}
/**
* Delegates to the configured {@link TestContextManager} to call
* {@linkplain TestContextManager#beforeTestClass() 'before test class'}
* callbacks.
*
* @throws Exception if a registered TestExecutionListener throws an
* exception
* {@linkplain TestContextManager#beforeTestClass() 'before test class'} callbacks.
* @throws Exception if a registered TestExecutionListener throws an exception
*/
@BeforeClass(alwaysRun = true)
protected void springTestContextBeforeTestClass() throws Exception {
@@ -141,9 +138,7 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
* {@linkplain TestContextManager#prepareTestInstance(Object) prepare} this test
* instance prior to execution of any individual tests, for example for
* injecting dependencies, etc.
*
* @throws Exception if a registered TestExecutionListener throws an
* exception
* @throws Exception if a registered TestExecutionListener throws an exception
*/
@BeforeClass(alwaysRun = true, dependsOnMethods = "springTestContextBeforeTestClass")
protected void springTestContextPrepareTestInstance() throws Exception {
@@ -154,7 +149,6 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
* Delegates to the configured {@link TestContextManager} to
* {@linkplain TestContextManager#beforeTestMethod(Object,Method) pre-process}
* the test method before the actual test is executed.
*
* @param testMethod the test method which is about to be executed
* @throws Exception allows all exceptions to propagate
*/
@@ -167,7 +161,6 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
* Delegates to the {@linkplain IHookCallBack#runTestMethod(ITestResult) test
* method} in the supplied {@code callback} to execute the actual test
* and then tracks the exception thrown during test execution, if any.
*
* @see org.testng.IHookable#run(IHookCallBack, ITestResult)
*/
@Override
@@ -224,15 +217,14 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
/**
* Delegates to the configured {@link TestContextManager} to call
* {@linkplain TestContextManager#afterTestClass() 'after test class'} callbacks.
*
* @throws Exception if a registered TestExecutionListener throws an
* exception
* @throws Exception if a registered TestExecutionListener throws an exception
*/
@AfterClass(alwaysRun = true)
protected void springTestContextAfterTestClass() throws Exception {
this.testContextManager.afterTestClass();
}
private Throwable getTestResultException(ITestResult testResult) {
Throwable testResultException = testResult.getThrowable();
if (testResultException instanceof InvocationTargetException) {
@@ -241,12 +233,10 @@ public abstract class AbstractTestNGSpringContextTests implements IHookable, App
return testResultException;
}
@Nullable
private RuntimeException throwAsUncheckedException(Throwable t) {
throwAs(t);
// Appeasing the compiler: the following line will never be executed.
return null;
throw new IllegalStateException(t);
}
@SuppressWarnings("unchecked")

View File

@@ -95,7 +95,7 @@ public final class MockMvc {
* A default request builder merged into every performed request.
* @see org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder#defaultRequest(RequestBuilder)
*/
void setDefaultRequest(RequestBuilder requestBuilder) {
void setDefaultRequest(@Nullable RequestBuilder requestBuilder) {
this.defaultRequestBuilder = requestBuilder;
}
@@ -104,7 +104,7 @@ public final class MockMvc {
* @see org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder#alwaysExpect(ResultMatcher)
*/
void setGlobalResultMatchers(List<ResultMatcher> resultMatchers) {
Assert.notNull(resultMatchers, "resultMatchers is required");
Assert.notNull(resultMatchers, "ResultMatcher List is required");
this.defaultResultMatchers = resultMatchers;
}
@@ -113,7 +113,7 @@ public final class MockMvc {
* @see org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder#alwaysDo(ResultHandler)
*/
void setGlobalResultHandlers(List<ResultHandler> resultHandlers) {
Assert.notNull(resultHandlers, "resultHandlers is required");
Assert.notNull(resultHandlers, "ResultHandler List is required");
this.defaultResultHandlers = resultHandlers;
}

View File

@@ -41,7 +41,7 @@ import org.springframework.web.context.WebApplicationContext;
public abstract class MockMvcBuilderSupport {
protected final MockMvc createMockMvc(Filter[] filters, MockServletConfig servletConfig,
WebApplicationContext webAppContext, RequestBuilder defaultRequestBuilder,
WebApplicationContext webAppContext, @Nullable RequestBuilder defaultRequestBuilder,
List<ResultMatcher> globalResultMatchers, List<ResultHandler> globalResultHandlers,
@Nullable List<DispatcherServletCustomizer> dispatcherServletCustomizers) {