Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default models support for Spring's Autowired #477

Merged
merged 5 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def test = [
commonsLang3 : "org.apache.commons:commons-lang3:3.8.1",
commonsLang : "commons-lang:commons-lang:2.6",
lombok : "org.projectlombok:lombok:1.18.12",
springBeans : "org.springframework:spring-beans:5.3.7",
springContext : "org.springframework:spring-context:5.3.7",
]

ext.deps = [
Expand Down
2 changes: 2 additions & 0 deletions nullaway/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ dependencies {
testImplementation deps.test.commonsLang3
testImplementation project(":test-library-models")
testImplementation deps.test.lombok
testImplementation deps.test.springBeans
testImplementation deps.test.springContext
}

javadoc {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ final class ErrorProneCLIFlagsConfig extends AbstractConfig {
"org.junit.Before",
"org.junit.BeforeClass",
"org.junit.jupiter.api.BeforeAll",
"org.junit.jupiter.api.BeforeEach"); // + Anything with @Initializer as its "simple name"
"org.junit.jupiter.api.BeforeEach",
"org.springframework.beans.factory.annotation.Autowired");
// + Anything with @Initializer as its "simple name"

static final ImmutableSet<String> DEFAULT_EXTERNAL_INIT_ANNOT = ImmutableSet.of("lombok.Builder");

Expand All @@ -122,7 +124,8 @@ final class ErrorProneCLIFlagsConfig extends AbstractConfig {
"jakarta.inject.Inject", // no explicit initialization when there is dependency injection
"javax.inject.Inject", // no explicit initialization when there is dependency injection
"com.google.errorprone.annotations.concurrent.LazyInit",
"org.checkerframework.checker.nullness.qual.MonotonicNonNull");
"org.checkerframework.checker.nullness.qual.MonotonicNonNull",
"org.springframework.beans.factory.annotation.Autowired");

private static final String DEFAULT_URL = "http://t.uber.com/nullaway";

Expand Down
65 changes: 65 additions & 0 deletions nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3058,4 +3058,69 @@ public void overridingNativeModelsInAnnotatedCodeDoesNotGenerateSafetyHoles() {
"}")
.doTest();
}

@Test
public void springAutowiredFieldTest() {
defaultCompilationHelper
.addSourceLines(
"Foo.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"import org.springframework.stereotype.Component;",
"@Component",
"public class Foo {",
" @Nullable String bar;",
" public void setBar(String s) {",
" bar = s;",
" }",
"}")
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.springframework.beans.factory.annotation.Autowired;",
"import org.springframework.stereotype.Service;",
"@Service",
"public class Test {",
" @Autowired",
" Foo f;", // Initialized by spring.
" public void Fun() {",
" f.setBar(\"hello\");",
" }",
"}")
.doTest();
}

@Test
public void springAutowiredConstructorTest() {
defaultCompilationHelper
.addSourceLines(
"Foo.java",
"package com.uber;",
"import javax.annotation.Nullable;",
"import org.springframework.stereotype.Component;",
"@Component",
"public class Foo {",
" @Nullable String bar;",
" public void setBar(String s) {",
" bar = s;",
" }",
"}")
.addSourceLines(
"Test.java",
"package com.uber;",
"import org.springframework.beans.factory.annotation.Autowired;",
"import org.springframework.stereotype.Service;",
"@Service",
"public class Test {",
" Foo f;", // Initialized by spring.
" @Autowired",
" public void init() {",
" f = new Foo();",
" }",
" public void Fun() {",
" f.setBar(\"hello\");",
" }",
"}")
.doTest();
}
}