Skip to content

Add missing @Nullable type annotations to getBean signatures#50761

Closed
subhramit wants to merge 1 commit into
spring-projects:mainfrom
subhramit:fix-nullwarnings
Closed

Add missing @Nullable type annotations to getBean signatures#50761
subhramit wants to merge 1 commit into
spring-projects:mainfrom
subhramit:fix-nullwarnings

Conversation

@subhramit

@subhramit subhramit commented Jun 12, 2026

Copy link
Copy Markdown

Context

Nullaway had some warnings highlighted in https://github.com/uber/NullAway/actions/runs/27324825986/job/80868514555
On taking a look, they seemed to have merit.

For example, in

public <T> AbstractObjectAssert<?, T> getBean(String name, Class<T> type) {
if (this.startupFailure != null) {
throwAssertionError(contextFailedToStartWhenExpecting(this.startupFailure,
"to contain a bean of name:%n <%s> (%s)", name, type));
}
Object bean = findBean(name);
if (bean != null && type != null && !type.isInstance(bean)) {
throwAssertionError(new BasicErrorMessageFactory(
"%nExpecting:%n <%s>%nto contain a bean of name:%n <%s> (%s)%nbut found:%n <%s> of type <%s>",
getApplicationContext(), name, type, bean, bean.getClass()));
}
return assertThat((T) bean).as("Bean of name <%s> and type <%s> from <%s>", name, type,
getApplicationContext());
}

assertThat(bean) where bean is @Nullable T infers ObjectAssert<@Nullable T>, i.e. AbstractObjectAssert<ObjectAssert<@Nullable T>, @Nullable T>. The declared return type is AbstractObjectAssert<?, T> with a non-null T in the actual (second) type-argument. So @Nullable T flowing into a T slot is flagged by NullAway.

The javadoc also states:

* bean becoming the object under test. If no bean of the specified name can be found
* an assert on {@code null} is returned.

So the actual value under test genuinely is nullable.

The PR fixes this and two other cases by making the return types of covered getBean signatures honest with @Nullable, thus also bringing the signatures in line with the documented behavior.

…tionContextAssert.java`

Signed-off-by: subhramit <subhramit.bb@live.in>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 12, 2026
@subhramit subhramit changed the title Add missing @Nullable annotations to getBean signatures Add missing @Nullable type annotations to getBean signatures Jun 12, 2026
@subhramit

Copy link
Copy Markdown
Author

Closing as covered by #50701.

@subhramit subhramit closed this Jun 12, 2026
@wilkinsona wilkinsona added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: duplicate A duplicate of another issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants