Skip to content

Commit

Permalink
Default models support for Spring's Autowired (#477)
Browse files Browse the repository at this point in the history
This adds support for spring's `@Autowired` annotation as both a way to exclude fields from initialization checking (since Spring will inject them before any methods are called), and to mark methods as initializers. See [Spring's documentation](https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html) and the discussion in #396 

This is based on the original PR #475 contributed by @chat-eau-lumi (note CLA signature in that PR). It adds fixes to test cases, particularly around importing Spring classes and the constructor usage of `@Autowired`, contributed by @lazaroclapp.
  • Loading branch information
lazaroclapp committed May 14, 2021
1 parent 7a33853 commit 067c31d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
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();
}
}

0 comments on commit 067c31d

Please sign in to comment.