Skip to content

Commit

Permalink
Fix #73 by extending list of NullUnmarked injections. (#74)
Browse files Browse the repository at this point in the history
This PR resolves #73 by adding `@NullUnmarked` annotations for called methods in field initializations that received a `@Nullable`.
  • Loading branch information
nimakarimipour authored Nov 17, 2022
1 parent d0e6182 commit ffbefb6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
23 changes: 19 additions & 4 deletions core/src/main/java/edu/ucr/cs/riple/core/Annotator.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import edu.ucr.cs.riple.injector.changes.AddMarkerAnnotation;
import edu.ucr.cs.riple.injector.changes.AddSingleElementAnnotation;
import edu.ucr.cs.riple.injector.location.OnField;
import edu.ucr.cs.riple.injector.location.OnMethod;
import edu.ucr.cs.riple.scanner.Serializer;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -262,11 +263,25 @@ private void forceResolveRemainingErrors(
// the method level.
Set<AddAnnotation> nullUnMarkedAnnotations =
remainingErrors.stream()
// filter non-method regions.
.filter(error -> !error.encMethod().equals("null"))
// find the corresponding method nodes.
.map(error -> tree.findNode(error.encMethod(), error.encClass()))
// impossible, just sanity check or future nullness checker hints
.map(
error -> {
if (!error.encMethod().equals("null")) {
return tree.findNode(error.encMethod(), error.encClass());
}
if (error.nonnullTarget == null) {
// Just a sanity check.
return null;
}
// For methods invoked in an initialization region, where the error is that
// `@Nullable` is being passed as an argument, we add a `@NullUnmarked` annotation
// to the called method.
if (error.messageType.equals("PASS_NULLABLE")) {
OnMethod calledMethod = error.nonnullTarget.toMethod();
return tree.findNode(calledMethod.method, calledMethod.clazz);
}
return null;
})
.filter(Objects::nonNull)
.map(node -> new AddMarkerAnnotation(node.location, config.nullUnMarkedAnnotation))
.collect(Collectors.toSet());
Expand Down
24 changes: 23 additions & 1 deletion core/src/test/java/edu/ucr/cs/riple/core/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void field_assign_nullable() {
}

@Test
public void field_assign_nullable_constructor() {
public void fieldAssignNullableConstructor() {
coreTestHelper
.addInputLines(
"Main.java",
Expand All @@ -148,6 +148,28 @@ public void field_assign_nullable_constructor() {
.start();
}

@Test
public void fieldAssignNullableConstructorForceResolveEnabled() {
coreTestHelper
.addInputLines(
"Main.java",
"package test;",
"public class Main {",
" Object f;",
" Main(Object f) {",
" this.f = f;",
" }",
"}",
"class C {",
" Main main = new Main(null);",
"}")
.toDepth(1)
.addExpectedReports(
new TReport(new OnParameter("Main.java", "test.Main", "Main(java.lang.Object)", 0), 1))
.enableForceResolve()
.start();
}

@Test
public void multipleFieldDeclarationTest() {
coreTestHelper
Expand Down

0 comments on commit ffbefb6

Please sign in to comment.