Skip to content

Commit

Permalink
resolve #71
Browse files Browse the repository at this point in the history
  • Loading branch information
nimakarimipour committed Sep 17, 2022
1 parent 5f57920 commit 44afe23
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 @@ -256,14 +257,27 @@ 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;
}
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());
// Collect methods received a nullable i their nonnull parameters where called in field
// declaration regions of classes (method == "null).
injector.injectAnnotations(nullUnMarkedAnnotations);

// Collect explicit Nullable initialization to fields
Expand Down Expand Up @@ -299,6 +313,7 @@ private void forceResolveRemainingErrors(
// Exclude already annotated fields with a general NullAway suppress warning.
.filter(f -> !suppressWarningsAnnotations.contains(f))
.collect(Collectors.toSet());

injector.injectAnnotations(initializationSuppressWarningsAnnotations);
}
}
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 @@ -127,7 +127,7 @@ public void field_assign_nullable() {
}

@Test
public void field_assign_nullable_constructor() {
public void fieldAssignNullableConstructor() {
coreTestHelper
.addInputLines(
"Main.java",
Expand All @@ -147,6 +147,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 44afe23

Please sign in to comment.