forked from square/dagger
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add static analysis @nullable checks. Injected parameters that aren't…
… marked @nullable cannot by injected by an @provides or component method that has @nullable. Also generate null-checks in provision factories & component factories if they aren't @nullable. This can be controlled by a "dagger.nullableValidation={ERROR,WARNING}" flag, with the default being ERROR. WARNING will cause compilation to produce warnings when there's a nullable mismatch, and will ignore null return values from providers/component methods. ERROR will trigger the new behavior. This also changes compile-testing to support passing options to the compiler. ------------- Created by MOE: http://code.google.com/p/moe-java MOE_MIGRATED_REVID=85151739
- Loading branch information
Showing
23 changed files
with
763 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
compiler/src/it/functional-tests/src/main/java/test/nullables/NullComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (C) 2015 Google, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package test.nullables; | ||
|
||
import javax.inject.Provider; | ||
|
||
import dagger.Component; | ||
|
||
@Component(modules = NullModule.class) | ||
interface NullComponent { | ||
NullFoo nullFoo(); | ||
@Nullable String string(); | ||
Provider<String> stringProvider(); | ||
Number number(); | ||
Provider<Number> numberProvider(); | ||
} |
28 changes: 28 additions & 0 deletions
28
...ler/src/it/functional-tests/src/main/java/test/nullables/NullComponentWithDependency.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (C) 2015 Google, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package test.nullables; | ||
|
||
import javax.inject.Provider; | ||
|
||
import dagger.Component; | ||
|
||
@Component(dependencies = NullComponent.class) | ||
interface NullComponentWithDependency { | ||
@Nullable String string(); | ||
Provider<String> stringProvider(); | ||
Number number(); | ||
Provider<Number> numberProvider(); | ||
} |
56 changes: 56 additions & 0 deletions
56
compiler/src/it/functional-tests/src/main/java/test/nullables/NullFoo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) 2015 Google, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package test.nullables; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Provider; | ||
|
||
class NullFoo { | ||
final String string; | ||
final Provider<String> stringProvider; | ||
final Number number; | ||
final Provider<Number> numberProvider; | ||
|
||
@Inject | ||
NullFoo(@Nullable String string, | ||
Provider<String> stringProvider, | ||
Number number, | ||
Provider<Number> numberProvider) { | ||
this.string = string; | ||
this.stringProvider = stringProvider; | ||
this.number = number; | ||
this.numberProvider = numberProvider; | ||
} | ||
|
||
String methodInjectedString; | ||
Provider<String> methodInjectedStringProvider; | ||
Number methodInjectedNumber; | ||
Provider<Number> methodInjectedNumberProvider; | ||
@Inject void inject(@Nullable String string, | ||
Provider<String> stringProvider, | ||
Number number, | ||
Provider<Number> numberProvider) { | ||
this.methodInjectedString = string; | ||
this.methodInjectedStringProvider = stringProvider; | ||
this.methodInjectedNumber = number; | ||
this.methodInjectedNumberProvider = numberProvider; | ||
} | ||
|
||
@Nullable @Inject String fieldInjectedString; | ||
@Inject Provider<String> fieldInjectedStringProvider; | ||
@Inject Number fieldInjectedNumber; | ||
@Inject Provider<Number> fieldInjectedNumberProvider; | ||
} |
35 changes: 35 additions & 0 deletions
35
compiler/src/it/functional-tests/src/main/java/test/nullables/NullModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) 2015 Google, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package test.nullables; | ||
|
||
import dagger.Module; | ||
import dagger.Provides; | ||
|
||
@Module | ||
class NullModule { | ||
Number numberValue = null; | ||
|
||
@Nullable | ||
@Provides | ||
String provideNullableString() { | ||
return null; | ||
} | ||
|
||
@Provides | ||
Number provideNumber() { | ||
return numberValue; | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
compiler/src/it/functional-tests/src/main/java/test/nullables/Nullable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package test.nullables; | ||
|
||
@interface Nullable {} |
110 changes: 110 additions & 0 deletions
110
compiler/src/it/functional-tests/src/test/java/test/nullables/NullabilityTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* Copyright (C) 2015 Google, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package test.nullables; | ||
|
||
import javax.inject.Provider; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
@RunWith(JUnit4.class) | ||
public class NullabilityTest { | ||
@Test public void testNullability_provides() { | ||
NullModule module = new NullModule(); | ||
NullComponent component = Dagger_NullComponent.builder().nullModule(module).build(); | ||
|
||
// Can't construct NullFoo because it depends on Number, and Number was null. | ||
try { | ||
component.nullFoo(); | ||
fail(); | ||
} catch (NullPointerException npe) { | ||
assertThat(npe).hasMessage("Cannot return null from a non-@Nullable @Provides method"); | ||
} | ||
|
||
// set number to non-null so we can create | ||
module.numberValue = 1; | ||
NullFoo nullFoo = component.nullFoo(); | ||
|
||
// Then set it back to null so we can test its providers. | ||
module.numberValue = null; | ||
validate(true, nullFoo.string, nullFoo.stringProvider, nullFoo.numberProvider); | ||
validate(true, nullFoo.methodInjectedString, nullFoo.methodInjectedStringProvider, | ||
nullFoo.methodInjectedNumberProvider); | ||
validate(true, nullFoo.fieldInjectedString, nullFoo.fieldInjectedStringProvider, | ||
nullFoo.fieldInjectedNumberProvider); | ||
} | ||
|
||
@Test public void testNullability_components() { | ||
NullComponent nullComponent = new NullComponent() { | ||
@Override public Provider<String> stringProvider() { | ||
return new Provider<String>() { | ||
@Override public String get() { | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
@Override public String string() { | ||
return null; | ||
} | ||
|
||
@Override public Provider<Number> numberProvider() { | ||
return new Provider<Number>() { | ||
@Override public Number get() { | ||
return null; | ||
} | ||
}; | ||
} | ||
|
||
@Override public Number number() { | ||
return null; | ||
} | ||
|
||
@Override public NullFoo nullFoo() { | ||
return null; | ||
} | ||
}; | ||
NullComponentWithDependency component = | ||
Dagger_NullComponentWithDependency.builder().nullComponent(nullComponent).build(); | ||
validate(false, component.string(), component.stringProvider(), component.numberProvider()); | ||
|
||
// Also validate that the component's number() method fails | ||
try { | ||
component.number(); | ||
fail(); | ||
} catch (NullPointerException npe) { | ||
assertThat(npe).hasMessage("Cannot return null from a non-@Nullable component method"); | ||
} | ||
} | ||
|
||
private void validate(boolean fromProvides, | ||
String string, | ||
Provider<String> stringProvider, | ||
Provider<Number> numberProvider) { | ||
assertThat(string).isNull(); | ||
assertThat(numberProvider).isNotNull(); | ||
try { | ||
numberProvider.get(); | ||
fail(); | ||
} catch(NullPointerException npe) { | ||
assertThat(npe).hasMessage("Cannot return null from a non-@Nullable " | ||
+ (fromProvides ? "@Provides" : "component") + " method"); | ||
} | ||
assertThat(stringProvider.get()).isNull(); | ||
} | ||
} |
Oops, something went wrong.