Skip to content

Commit

Permalink
add jspecify
Browse files Browse the repository at this point in the history
  • Loading branch information
nimakarimipour committed Feb 13, 2025
1 parent 9112f3e commit 81a684b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,7 @@ public void fieldAssignNullableNonNullArrayContent() {
== found.getOverallEffect(coreTestHelper.getConfig()))
.disableBailOut()
.checkExpectedOutput("fieldAssignNullableNonNullArrayContent/expected")
.start("org.jspecify.annotations.Nullable");
.enableJSpecify()
.start();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public void init() {
root = temporaryFolder.getRoot().toPath();
CoreTestHelper helper = new CoreTestHelper(root, root).onEmptyProject();
Path configPath = root.resolve("context.json");
helper.makeAnnotatorConfigFile(configPath, "javax.annotation.Nullable");
helper.makeAnnotatorConfigFile(configPath);
Utility.runTestWithMockedBuild(
root,
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ public class CoreTestHelper {

private ParserConfiguration.LanguageLevel languageLevel;

private boolean jSpecifyEnabled = false;

public CoreTestHelper(Path projectPath, Path outDirPath) {
this.projectPath = projectPath;
this.outDirPath = outDirPath;
this.expectedReports = new HashSet<>();
this.projectBuilder = new ProjectBuilder(this, projectPath);
this.languageLevel = ParserConfiguration.LanguageLevel.JAVA_17;
this.jSpecifyEnabled = false;
}

public Module onTarget() {
Expand Down Expand Up @@ -241,16 +244,20 @@ public CoreTestHelper withLanguageLevel(ParserConfiguration.LanguageLevel langua
return this;
}

/** Starts the test process. */
public void start() {
start("javax.annotation.Nullable");
/**
* Enables JSpecify.
* @return This instance of {@link CoreTestHelper}.
*/
public CoreTestHelper enableJSpecify() {
this.jSpecifyEnabled = true;
return this;
}

/** Starts the test process. */
public void start(String annotName) {
public void start() {
Path configPath = outDirPath.resolve("config.json");
checkSourcePackages();
makeAnnotatorConfigFile(configPath, annotName);
makeAnnotatorConfigFile(configPath);
config = new Config(configPath);
Annotator annotator = new Annotator(config);
annotator.start();
Expand Down Expand Up @@ -421,7 +428,7 @@ private void compare(Collection<Report> actualOutput) {
*
* @param configPath Path to the config file.
*/
public void makeAnnotatorConfigFile(Path configPath, String nullableAnnotation) {
public void makeAnnotatorConfigFile(Path configPath) {
Config.Builder builder = new Config.Builder();
final int[] id = {0};
builder.configPaths =
Expand All @@ -436,7 +443,7 @@ public void makeAnnotatorConfigFile(Path configPath, String nullableAnnotation)
.collect(Collectors.toList());
builder.checker = NullAway.NAME;
builder.nullableAnnotation =
nullableAnnotation == null ? "javax.annotation.Nullable" : nullableAnnotation;
jSpecifyEnabled ? "org.jspecify.annotations" : "javax.annotation.Nullable";
// In tests, we use NullAway @Initializer annotation.
builder.initializerAnnotation = "com.uber.nullaway.annotations.Initializer";
builder.outputDir = outDirPath.toString();
Expand All @@ -459,17 +466,17 @@ public void makeAnnotatorConfigFile(Path configPath, String nullableAnnotation)
!getEnvironmentVariable("ANNOTATOR_TEST_DISABLE_PARALLEL_PROCESSING");
if (downstreamDependencyAnalysisActivated) {
builder.buildCommand =
projectBuilder.computeTargetBuildCommandWithLibraryModelLoaderDependency(this.outDirPath);
projectBuilder.computeTargetBuildCommandWithLibraryModelLoaderDependency(this.outDirPath, jSpecifyEnabled);
builder.downstreamBuildCommand =
projectBuilder.computeDownstreamDependencyBuildCommandWithLibraryModelLoaderDependency(
this.outDirPath);
this.outDirPath, jSpecifyEnabled);
builder.nullawayLibraryModelLoaderPath =
Utility.getPathToLibraryModel(outDirPath)
.resolve(
Paths.get(
"src", "main", "resources", "edu", "ucr", "cs", "riple", "librarymodel"));
} else {
builder.buildCommand = projectBuilder.computeTargetBuildCommand(this.outDirPath);
builder.buildCommand = projectBuilder.computeTargetBuildCommand(this.outDirPath, jSpecifyEnabled);
}
builder.write(configPath);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,18 +103,21 @@ CoreTestHelper exitProjectConstruction() {
* root to project root dir, command to compile the project and the computed paths to config files
* which will be passed through gradle command line arguments.
*
* @param outDirPath Path to serialization output directory,
* @param outDirPath Path to serialization output directory,
* @param jSpecifyEnabled Flag to enable jSpecify
* @return The command to build the project including the command line arguments, this command can
* * be executed from any directory.
* * be executed from any directory.
*/
public String computeTargetBuildCommand(Path outDirPath) {
public String computeTargetBuildCommand(Path outDirPath, boolean jSpecifyEnabled) {
return String.format(
"%s && ./gradlew %s %s -Plibrary-model-loader-path=%s --rerun-tasks",
"%s && ./gradlew %s %s -Plibrary-model-loader-path=%s -Pjspecify=%s --rerun-tasks",
Utility.changeDirCommand(pathToProject),
computeCompileGradleCommandForModules(modules.subList(0, 1)),
String.join(" ", Utility.computeConfigPathsWithGradleArguments(outDirPath, modules)),
Utility.getPathToLibraryModel(outDirPath)
.resolve(Paths.get("build", "libs", "librarymodel.jar")));
.resolve(Paths.get("build", "libs", "librarymodel.jar")),
jSpecifyEnabled
);
}

/**
Expand All @@ -123,19 +126,21 @@ public String computeTargetBuildCommand(Path outDirPath) {
* loader jar and the computed paths to config files which will be passed through gradle command
* line arguments.
*
* @param outDirPath Path to serialization output directory,
* @param outDirPath Path to serialization output directory,
* @param jSpecifyEnabled Flag to enable jSpecify
* @return The command to build the project including the command line arguments, this command can
* * be executed from any directory.
* * be executed from any directory.
*/
public String computeTargetBuildCommandWithLibraryModelLoaderDependency(Path outDirPath) {
public String computeTargetBuildCommandWithLibraryModelLoaderDependency(Path outDirPath, boolean jSpecifyEnabled) {
return String.format(
"%s && ./gradlew library-model-loader:jar --rerun-tasks && %s && ./gradlew %s %s -Plibrary-model-loader-path=%s --rerun-tasks",
"%s && ./gradlew library-model-loader:jar --rerun-tasks && %s && ./gradlew %s %s -Plibrary-model-loader-path=%s -Pjspecify=%s --rerun-tasks",
Utility.changeDirCommand(outDirPath.resolve("Annotator")),
Utility.changeDirCommand(pathToProject),
computeCompileGradleCommandForModules(modules.subList(0, 1)),
String.join(" ", Utility.computeConfigPathsWithGradleArguments(outDirPath, modules)),
Utility.getPathToLibraryModel(outDirPath)
.resolve(Paths.get("build", "libs", "librarymodel.jar")));
.resolve(Paths.get("build", "libs", "librarymodel.jar")),
jSpecifyEnabled);
}

/**
Expand All @@ -144,20 +149,23 @@ public String computeTargetBuildCommandWithLibraryModelLoaderDependency(Path out
* loader jar and the computed paths to config files which will be passed through gradle command
* line arguments.
*
* @param outDirPath Path to serialization output directory,
* @param outDirPath Path to serialization output directory,
* @param jSpecifyEnabled Flag to enable jSpecify
* @return The command to build the project including the command line arguments, this command can
* * be executed from any directory.
* * be executed from any directory.
*/
public String computeDownstreamDependencyBuildCommandWithLibraryModelLoaderDependency(
Path outDirPath) {
Path outDirPath, boolean jSpecifyEnabled) {
return String.format(
"%s && ./gradlew library-model-loader:jar --rerun-tasks && %s && ./gradlew %s %s -Plibrary-model-loader-path=%s --rerun-tasks",
"%s && ./gradlew library-model-loader:jar --rerun-tasks && %s && ./gradlew %s %s -Plibrary-model-loader-path=%s -Pjspecify=%s --rerun-tasks",
Utility.changeDirCommand(outDirPath.resolve("Annotator")),
Utility.changeDirCommand(pathToProject),
computeCompileGradleCommandForModules(modules.subList(1, modules.size())),
String.join(" ", Utility.computeConfigPathsWithGradleArguments(outDirPath, modules)),
Utility.getPathToLibraryModel(outDirPath)
.resolve(Paths.get("build", "libs", "librarymodel.jar")));
.resolve(Paths.get("build", "libs", "librarymodel.jar")),
jSpecifyEnabled
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ subprojects {
option("NullAway:SerializeFixMetadata", "true")
option("NullAway:FixSerializationConfigPath", project.getProperty(project.name + "-nullaway-config-path"))
option("NullAway:AcknowledgeLibraryModelsOfAnnotatedCode", "true")
option("NullAway:JSpecifyMode", "true")
option("NullAway:JSpecifyMode", project.getProperty(project.name + "-jspecify"))
option("AnnotatorScanner:ConfigPath", project.getProperty(project.name + "-scanner-config-path"))
}
}
Expand Down

0 comments on commit 81a684b

Please sign in to comment.