Skip to content

Commit 81a684b

Browse files
add jspecify
1 parent 9112f3e commit 81a684b

File tree

5 files changed

+44
-28
lines changed

5 files changed

+44
-28
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ public void fieldAssignNullableNonNullArrayContent() {
599599
== found.getOverallEffect(coreTestHelper.getConfig()))
600600
.disableBailOut()
601601
.checkExpectedOutput("fieldAssignNullableNonNullArrayContent/expected")
602-
.start("org.jspecify.annotations.Nullable");
602+
.enableJSpecify()
603+
.start();
603604
}
604605
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void init() {
7676
root = temporaryFolder.getRoot().toPath();
7777
CoreTestHelper helper = new CoreTestHelper(root, root).onEmptyProject();
7878
Path configPath = root.resolve("context.json");
79-
helper.makeAnnotatorConfigFile(configPath, "javax.annotation.Nullable");
79+
helper.makeAnnotatorConfigFile(configPath);
8080
Utility.runTestWithMockedBuild(
8181
root,
8282
() -> {

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,15 @@ public class CoreTestHelper {
109109

110110
private ParserConfiguration.LanguageLevel languageLevel;
111111

112+
private boolean jSpecifyEnabled = false;
113+
112114
public CoreTestHelper(Path projectPath, Path outDirPath) {
113115
this.projectPath = projectPath;
114116
this.outDirPath = outDirPath;
115117
this.expectedReports = new HashSet<>();
116118
this.projectBuilder = new ProjectBuilder(this, projectPath);
117119
this.languageLevel = ParserConfiguration.LanguageLevel.JAVA_17;
120+
this.jSpecifyEnabled = false;
118121
}
119122

120123
public Module onTarget() {
@@ -241,16 +244,20 @@ public CoreTestHelper withLanguageLevel(ParserConfiguration.LanguageLevel langua
241244
return this;
242245
}
243246

244-
/** Starts the test process. */
245-
public void start() {
246-
start("javax.annotation.Nullable");
247+
/**
248+
* Enables JSpecify.
249+
* @return This instance of {@link CoreTestHelper}.
250+
*/
251+
public CoreTestHelper enableJSpecify() {
252+
this.jSpecifyEnabled = true;
253+
return this;
247254
}
248255

249256
/** Starts the test process. */
250-
public void start(String annotName) {
257+
public void start() {
251258
Path configPath = outDirPath.resolve("config.json");
252259
checkSourcePackages();
253-
makeAnnotatorConfigFile(configPath, annotName);
260+
makeAnnotatorConfigFile(configPath);
254261
config = new Config(configPath);
255262
Annotator annotator = new Annotator(config);
256263
annotator.start();
@@ -421,7 +428,7 @@ private void compare(Collection<Report> actualOutput) {
421428
*
422429
* @param configPath Path to the config file.
423430
*/
424-
public void makeAnnotatorConfigFile(Path configPath, String nullableAnnotation) {
431+
public void makeAnnotatorConfigFile(Path configPath) {
425432
Config.Builder builder = new Config.Builder();
426433
final int[] id = {0};
427434
builder.configPaths =
@@ -436,7 +443,7 @@ public void makeAnnotatorConfigFile(Path configPath, String nullableAnnotation)
436443
.collect(Collectors.toList());
437444
builder.checker = NullAway.NAME;
438445
builder.nullableAnnotation =
439-
nullableAnnotation == null ? "javax.annotation.Nullable" : nullableAnnotation;
446+
jSpecifyEnabled ? "org.jspecify.annotations" : "javax.annotation.Nullable";
440447
// In tests, we use NullAway @Initializer annotation.
441448
builder.initializerAnnotation = "com.uber.nullaway.annotations.Initializer";
442449
builder.outputDir = outDirPath.toString();
@@ -459,17 +466,17 @@ public void makeAnnotatorConfigFile(Path configPath, String nullableAnnotation)
459466
!getEnvironmentVariable("ANNOTATOR_TEST_DISABLE_PARALLEL_PROCESSING");
460467
if (downstreamDependencyAnalysisActivated) {
461468
builder.buildCommand =
462-
projectBuilder.computeTargetBuildCommandWithLibraryModelLoaderDependency(this.outDirPath);
469+
projectBuilder.computeTargetBuildCommandWithLibraryModelLoaderDependency(this.outDirPath, jSpecifyEnabled);
463470
builder.downstreamBuildCommand =
464471
projectBuilder.computeDownstreamDependencyBuildCommandWithLibraryModelLoaderDependency(
465-
this.outDirPath);
472+
this.outDirPath, jSpecifyEnabled);
466473
builder.nullawayLibraryModelLoaderPath =
467474
Utility.getPathToLibraryModel(outDirPath)
468475
.resolve(
469476
Paths.get(
470477
"src", "main", "resources", "edu", "ucr", "cs", "riple", "librarymodel"));
471478
} else {
472-
builder.buildCommand = projectBuilder.computeTargetBuildCommand(this.outDirPath);
479+
builder.buildCommand = projectBuilder.computeTargetBuildCommand(this.outDirPath, jSpecifyEnabled);
473480
}
474481
builder.write(configPath);
475482
}

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,21 @@ CoreTestHelper exitProjectConstruction() {
103103
* root to project root dir, command to compile the project and the computed paths to config files
104104
* which will be passed through gradle command line arguments.
105105
*
106-
* @param outDirPath Path to serialization output directory,
106+
* @param outDirPath Path to serialization output directory,
107+
* @param jSpecifyEnabled Flag to enable jSpecify
107108
* @return The command to build the project including the command line arguments, this command can
108-
* * be executed from any directory.
109+
* * be executed from any directory.
109110
*/
110-
public String computeTargetBuildCommand(Path outDirPath) {
111+
public String computeTargetBuildCommand(Path outDirPath, boolean jSpecifyEnabled) {
111112
return String.format(
112-
"%s && ./gradlew %s %s -Plibrary-model-loader-path=%s --rerun-tasks",
113+
"%s && ./gradlew %s %s -Plibrary-model-loader-path=%s -Pjspecify=%s --rerun-tasks",
113114
Utility.changeDirCommand(pathToProject),
114115
computeCompileGradleCommandForModules(modules.subList(0, 1)),
115116
String.join(" ", Utility.computeConfigPathsWithGradleArguments(outDirPath, modules)),
116117
Utility.getPathToLibraryModel(outDirPath)
117-
.resolve(Paths.get("build", "libs", "librarymodel.jar")));
118+
.resolve(Paths.get("build", "libs", "librarymodel.jar")),
119+
jSpecifyEnabled
120+
);
118121
}
119122

120123
/**
@@ -123,19 +126,21 @@ public String computeTargetBuildCommand(Path outDirPath) {
123126
* loader jar and the computed paths to config files which will be passed through gradle command
124127
* line arguments.
125128
*
126-
* @param outDirPath Path to serialization output directory,
129+
* @param outDirPath Path to serialization output directory,
130+
* @param jSpecifyEnabled Flag to enable jSpecify
127131
* @return The command to build the project including the command line arguments, this command can
128-
* * be executed from any directory.
132+
* * be executed from any directory.
129133
*/
130-
public String computeTargetBuildCommandWithLibraryModelLoaderDependency(Path outDirPath) {
134+
public String computeTargetBuildCommandWithLibraryModelLoaderDependency(Path outDirPath, boolean jSpecifyEnabled) {
131135
return String.format(
132-
"%s && ./gradlew library-model-loader:jar --rerun-tasks && %s && ./gradlew %s %s -Plibrary-model-loader-path=%s --rerun-tasks",
136+
"%s && ./gradlew library-model-loader:jar --rerun-tasks && %s && ./gradlew %s %s -Plibrary-model-loader-path=%s -Pjspecify=%s --rerun-tasks",
133137
Utility.changeDirCommand(outDirPath.resolve("Annotator")),
134138
Utility.changeDirCommand(pathToProject),
135139
computeCompileGradleCommandForModules(modules.subList(0, 1)),
136140
String.join(" ", Utility.computeConfigPathsWithGradleArguments(outDirPath, modules)),
137141
Utility.getPathToLibraryModel(outDirPath)
138-
.resolve(Paths.get("build", "libs", "librarymodel.jar")));
142+
.resolve(Paths.get("build", "libs", "librarymodel.jar")),
143+
jSpecifyEnabled);
139144
}
140145

141146
/**
@@ -144,20 +149,23 @@ public String computeTargetBuildCommandWithLibraryModelLoaderDependency(Path out
144149
* loader jar and the computed paths to config files which will be passed through gradle command
145150
* line arguments.
146151
*
147-
* @param outDirPath Path to serialization output directory,
152+
* @param outDirPath Path to serialization output directory,
153+
* @param jSpecifyEnabled Flag to enable jSpecify
148154
* @return The command to build the project including the command line arguments, this command can
149-
* * be executed from any directory.
155+
* * be executed from any directory.
150156
*/
151157
public String computeDownstreamDependencyBuildCommandWithLibraryModelLoaderDependency(
152-
Path outDirPath) {
158+
Path outDirPath, boolean jSpecifyEnabled) {
153159
return String.format(
154-
"%s && ./gradlew library-model-loader:jar --rerun-tasks && %s && ./gradlew %s %s -Plibrary-model-loader-path=%s --rerun-tasks",
160+
"%s && ./gradlew library-model-loader:jar --rerun-tasks && %s && ./gradlew %s %s -Plibrary-model-loader-path=%s -Pjspecify=%s --rerun-tasks",
155161
Utility.changeDirCommand(outDirPath.resolve("Annotator")),
156162
Utility.changeDirCommand(pathToProject),
157163
computeCompileGradleCommandForModules(modules.subList(1, modules.size())),
158164
String.join(" ", Utility.computeConfigPathsWithGradleArguments(outDirPath, modules)),
159165
Utility.getPathToLibraryModel(outDirPath)
160-
.resolve(Paths.get("build", "libs", "librarymodel.jar")));
166+
.resolve(Paths.get("build", "libs", "librarymodel.jar")),
167+
jSpecifyEnabled
168+
);
161169
}
162170

163171
/**

annotator-core/src/test/resources/templates/nullable-multi-modular/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ subprojects {
7373
option("NullAway:SerializeFixMetadata", "true")
7474
option("NullAway:FixSerializationConfigPath", project.getProperty(project.name + "-nullaway-config-path"))
7575
option("NullAway:AcknowledgeLibraryModelsOfAnnotatedCode", "true")
76-
option("NullAway:JSpecifyMode", "true")
76+
option("NullAway:JSpecifyMode", project.getProperty(project.name + "-jspecify"))
7777
option("AnnotatorScanner:ConfigPath", project.getProperty(project.name + "-scanner-config-path"))
7878
}
7979
}

0 commit comments

Comments
 (0)