Skip to content

Commit 1f66559

Browse files
authored
Support local variable arrays (#270)
This PR enables the existing mechanism to support for injecting annotation on local variables to fix errors coming from NullAway.
1 parent fda7e64 commit 1f66559

File tree

4 files changed

+36
-1
lines changed
  • annotator-core
    • src/test
      • java/edu/ucr/cs/riple/core
      • resources/assignNullableToNonnullArrayComponentTypeLocalVariableTest/expected/Target/src/main/java/test
  • injector/src/main/java/edu/ucr/cs/riple/injector/location

4 files changed

+36
-1
lines changed

annotator-core/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ spotless {
5757
}
5858

5959
// Should be the latest supporting version of NullAway.
60-
def NULLAWAY_TEST = "0.12.3"
60+
def NULLAWAY_TEST = "0.12.4"
6161

6262
tasks.test.dependsOn(':annotator-scanner:publishToMavenLocal')
6363

annotator-core/src/test/java/edu/ucr/cs/riple/core/CoreTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.google.common.collect.ImmutableList;
3131
import edu.ucr.cs.riple.core.tools.TReport;
3232
import edu.ucr.cs.riple.injector.location.OnField;
33+
import edu.ucr.cs.riple.injector.location.OnLocalVariable;
3334
import edu.ucr.cs.riple.injector.location.OnMethod;
3435
import edu.ucr.cs.riple.injector.location.OnParameter;
3536
import java.util.Collections;
@@ -597,4 +598,28 @@ public void assignNullableToNonnullArrayComponentTypeTest() {
597598
.enableJSpecifyMode()
598599
.start();
599600
}
601+
602+
@Test
603+
public void assignNullableToNonnullArrayComponentTypeLocalVariableTest() {
604+
coreTestHelper
605+
.onTarget()
606+
.withSourceLines(
607+
"Main.java",
608+
"package test;",
609+
"public class Main {",
610+
" void run() {",
611+
" Object[] arr = new Object[1];",
612+
" arr[0] = null;",
613+
" }",
614+
"}")
615+
.withExpectedReports(
616+
new TReport(
617+
new OnLocalVariable("Main.java", "test.Main", "run()", "arr"),
618+
ImmutableList.of(ImmutableList.of(1, 0)),
619+
-1))
620+
.disableBailOut()
621+
.checkExpectedOutput("assignNullableToNonnullArrayComponentTypeLocalVariableTest/expected")
622+
.enableJSpecifyMode()
623+
.start();
624+
}
600625
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package test;
2+
import org.jspecify.annotations.Nullable;
3+
public class Main {
4+
void run() {
5+
@Nullable Object[] arr = new Object[1];
6+
arr[0] = null;
7+
}
8+
}

injector/src/main/java/edu/ucr/cs/riple/injector/location/Location.java

+2
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public static Location createLocationFromArrayInfo(String[] values) {
111111
return new OnMethod(path, clazz, values[2]);
112112
case PARAMETER:
113113
return new OnParameter(path, clazz, values[2], Integer.parseInt(values[4]));
114+
case LOCAL_VARIABLE:
115+
return new OnLocalVariable(path, clazz, values[2], values[3]);
114116
default:
115117
throw new RuntimeException(
116118
"Cannot reach this statement, values: " + Arrays.toString(values));

0 commit comments

Comments
 (0)