Skip to content

Commit

Permalink
Add static analysis @nullable checks. Injected parameters that aren't…
Browse files Browse the repository at this point in the history
… 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
sameb authored and cgruber committed Feb 4, 2015
1 parent 959f5cd commit 725e783
Show file tree
Hide file tree
Showing 23 changed files with 763 additions and 169 deletions.
1 change: 1 addition & 0 deletions compiler/src/it/functional-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ limitations under the License.
<annotationProcessors>
<annotationProcessor>dagger.internal.codegen.ComponentProcessor</annotationProcessor>
</annotationProcessors>
<compilerArgument>-Adagger.nullableValidation=ERROR</compilerArgument>
</configuration>
</plugin>
<plugin>
Expand Down
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();
}
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();
}
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;
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package test.nullables;

@interface Nullable {}
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();
}
}
Loading

0 comments on commit 725e783

Please sign in to comment.