Skip to content

Commit

Permalink
Add sample project to execute Annotator (#260)
Browse files Browse the repository at this point in the history
This PR introduces a sample script to easily run the Annotator on the example provided in the README. The script handles downloading annotator JAR and is designed to work directly within the project without requiring any hardcoded values, making it straightforward to execute.

Also resolves minor issues in the README have been addressed, and it now reflects recent changes for better clarity and accuracy.

This PR does not fully address #255 but is a good start.
  • Loading branch information
nimakarimipour authored Nov 9, 2024
1 parent f8fb0ea commit 98163b2
Show file tree
Hide file tree
Showing 11 changed files with 539 additions and 92 deletions.
16 changes: 5 additions & 11 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,16 @@ assignees: ''
---

**Describe the bug**
A clear and concise description of what the bug is.
A clear and concise description of what the bug is and the used version of the software.

**To Reproduce**
**To Reproduce** [OPTIONAL]
Steps to reproduce the behavior: (Prefer a code snippet)

**Expected behavior**
**Expected behavior** [OPTIONAL]
A clear and concise description of what you expected to happen.

**Stack trace**
**Stack trace** [OPTIONAL]
If applicable, add stack trace of the exception below.

**OS (please complete the following information):**
- OS: [e.g. iOS]
- Version [e.g. 22]
- Annotator Version [e.g. v1.3.1-LOCAL]


**Additional context**
**Additional context** [OPTIONAL]
Add any other context about the problem here.
7 changes: 2 additions & 5 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ assignees: ''
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is.

**Describe the solution you'd like**
**Describe the solution you'd like** [OPTIONAL]
A clear and concise description of what you want to happen.

**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.

**Additional context**
**Additional context** [OPTIONAL]
Add any other context or screenshots about the feature request here.
17 changes: 0 additions & 17 deletions .github/ISSUE_TEMPLATE/todo.md

This file was deleted.

145 changes: 86 additions & 59 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@

`NullAwayAnnotator`, or simply `Annotator`, is a tool that automatically infers nullability types in the given source code and injects the corresponding annotations to pass [NullAway](https://github.com/uber/NullAway) checks.

Applying NullAway to build systems requires manual effort in annotating the source code. Even if the code is free of nullability errors, annotations are still needed to pass NullAway checks. A tool that can automatically infer types in the source code and inject the corresponding annotations to pass NullAway checks can significantly reduce the effort of integrating NullAway into build systems.
Applying NullAway to build systems requires manual effort in annotating the source code.
Even if the code is free of nullability errors, annotations are still needed to pass NullAway checks.
A tool that can automatically infer types in the source code
and inject the corresponding annotations to pass NullAway checks can significantly reduce the effort
of integrating NullAway into build systems.

`Annotator` minimizes the number of reported NullAway errors by inferring nullability types of elements in the source code and injecting the corresponding annotations. For errors that are not resolvable with any annotations, Annotator injects appropriate suppression annotations. The final output of Annotator is a source code that passes NullAway checks with no remaining errors.
`Annotator` minimizes the number of reported NullAway errors by inferring nullability types of elements in the source code and injecting the corresponding annotations.
For errors that are not resolvable with any annotations, Annotator injects appropriate suppression annotations.
The final output of Annotator is a source code that passes NullAway checks with no remaining errors.

## Code Example

Expand Down Expand Up @@ -38,7 +44,8 @@ class Test{
}
```

`Annotator` can infer the nullable types in the code above and inject the corresponding annotations. For unresolved errors, suppression annotations are injected.
`Annotator` can infer the nullable types in the code above and inject the corresponding annotations.
For unresolved errors, suppression annotations are injected.
The output below shows the result of running `Annotator` on the code above.

```java
Expand Down Expand Up @@ -75,7 +82,8 @@ class Test{
`Annotator` propagates the effects of a change throughout the entire module and injects several follow-up annotations to fully resolve a specific warning.
It is also capable of processing modules within monorepos, taking into account the modules public APIs and the impacts of annotations on downstream dependencies for improved results.
## Installation
We ship Annotator on [Maven](https://repo.maven.apache.org/maven2/edu/ucr/cs/riple/annotator/), as a JAR. You can find the artifact information below -
We ship [Annotator](https://repo.maven.apache.org/maven2/edu/ucr/cs/riple/annotator/) on [Maven](https://repo.maven.apache.org/) as a JAR.
You can find the artifact information below:
```
GROUP: edu.ucr.cs.riple.annotator
ID: annotator-core
Expand All @@ -86,71 +94,74 @@ ID: annotator-scanner

This sections describes how to run `Annotator` on any project.

- ### Requirements for the Target Project
### Requirements for the Target Project

#### Dependencies
- `NullAway` checker must be activated with version >= `0.10.10`
- `AnnotatorScanner` checker must be activated with version >= `1.3.6`, see more about `AnnotatorScanner` [here](../type-annotator-scanner/README.md).
#### Dependencies
- `NullAway` checker must be activated with a version >= `0.10.10`
- `AnnotatorScanner` checker must be activated with a version >= `1.3.6`, see more about `AnnotatorScanner` [here](../type-annotator-scanner/README.md).

#### Error Prone Flags
Since Nullaway is built as a plugin for [Error Prone](https://github.com/google/error-prone), we need to set the following flags in our build.gradle,
```
#### Error Prone Flags
Since Nullaway is built as a plugin for [Error Prone](https://github.com/google/error-prone), we need to set the following flags in our build.gradle,
```
"-Xep:NullAway:ERROR", // to activate NullAway
"-XepOpt:NullAway:SerializeFixMetadata=true",
"-XepOpt:NullAway:FixSerializationConfigPath=path_to_nullaway_config.xml",
"-Xep:AnnotatorScanner:ERROR", // to activate Annotator AnnotatorScanner
"-XepOpt:AnnotatorScanner:ConfigPath=path_to_scanner_config.xml",
```




The following code snippet demonstrates how to configure the `JavaCompile` tasks in your `build.gradle` to use NullAway as a plugin for [Error Prone](https://github.com/google/error-prone):
```groovy
dependencies {
annotationProcessor 'edu.ucr.cs.riple.annotator:annotator-scanner:1.3.6'
annotationProcessor "com.uber.nullaway:nullaway:0.10.10"
errorprone "com.google.errorprone:error_prone_core:2.4.0"
errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"
//All other target project dependencies
}
tasks.withType(JavaCompile) {
// remove the if condition if you want to run NullAway on test code
if (!name.toLowerCase().contains("test")) {
options.errorprone {
check("NullAway", CheckSeverity.ERROR)
check("AnnotatorScanner", CheckSeverity.ERROR)
option("NullAway:AnnotatedPackages", "org.example")
option("NullAway:SerializeFixMetadata", "true")
option("NullAway:FixSerializationConfigPath", "path_to/nullaway.xml")
option("AnnotatorScanner:ConfigPath", "path_to/scanner.xml")
}
options.compilerArgs << "-Xmaxerrs"<< "100000"
options.compilerArgs << "-Xmaxwarns" << "100000"
}
}
```
`path_to_nullaway_config.xml` and `path_to_scanner_config.xml` are configuration files that **do not need to be created** during the initial project setup. The script will generate these files, facilitating seamless communication between the script and the analysis. At this point, the target project is prepared for the Annotator to process.

You must provide the Annotator with the paths to `path_to_nullaway_config.xml` and `path_to_scanner_config.xml`. Further details on this process are described in the sections below.

- ### Running Annotator
`Annotator` necessitates specific flag values for successful execution. You can provide these values through command line arguments.
```

The following code snippet demonstrates how to configure the `JavaCompile` tasks in your `build.gradle` to use NullAway as a plugin for [Error Prone](https://github.com/google/error-prone):
```groovy
dependencies {
annotationProcessor 'edu.ucr.cs.riple.annotator:annotator-scanner:1.3.6'
annotationProcessor "com.uber.nullaway:nullaway:0.10.10"
errorprone "com.google.errorprone:error_prone_core:2.4.0"
errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"
// add required annotation dependencies
// Initializer
compileOnly 'com.uber.nullaway:nullaway-annotations:0.10.10'
// Nullable annotations
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
// JSpecify annotations for NullUnmarked
compileOnly "org.jspecify:jspecify:0.3.0"
//All other target project dependencies
}
tasks.withType(JavaCompile) {
// remove the if condition if you want to run NullAway on test code
if (!name.toLowerCase().contains("test")) {
options.errorprone {
check("NullAway", CheckSeverity.ERROR)
check("AnnotatorScanner", CheckSeverity.ERROR)
option("NullAway:AnnotatedPackages", "org.example")
option("NullAway:SerializeFixMetadata", "true")
option("NullAway:FixSerializationConfigPath", "path_to/nullaway.xml")
option("AnnotatorScanner:ConfigPath", "path_to/scanner.xml")
}
options.compilerArgs << "-Xmaxerrs"<< "100000"
options.compilerArgs << "-Xmaxwarns" << "100000"
}
}
```
`path_to_nullaway_config.xml` and `path_to_scanner_config.xml` are configuration files that **do not need to be created** during the initial project setup. The script will generate these files, facilitating seamless communication between the script and the analysis. At this point, the target project is prepared for the Annotator to process.

You must provide the Annotator with the absolute paths to `path_to_nullaway_config.xml` and `path_to_scanner_config.xml`.
Further details on this process are described in the sections below.

To run `Annotator` on the target project `P`, the arguments below **must** be passed to `Annotator`:
### Running Annotator
`Annotator` necessitates specific flag values for successful execution. You can provide these values through command line arguments.

| Flag | Description |
|------|-------------|
| `-bc,--build-command <arg>` | Command to run `NullAway` on target `P` enclosed in **""**. Please note that this command should be executable from any directory (e.g., `"cd /Absolute/Path/To/P && ./build"`). |
| `-i,--initializer <arg>` | Fully qualified name of the `@Initializer` annotation. |
| `-d,--dir <arg>` | Directory where all outputs of `AnnotatorScanner` and `NullAway` are serialized. |
| `-cp, --config-paths` | Path to a TSV file containing values defined in [Error Prone](./README.md#Error-Prone-Flags) config paths given in the format: (`path_to_nullaway_config.xml \t path_to_scanner_config`). |
| `-cn, --checker-name` | Checker name to be used for the analysis. (use NULLAWAY to request inference for NullAway.)|
To run `Annotator` on the target project `P`, the arguments below **must** be passed to `Annotator`:

| Flag | Description |
|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `-bc,--build-command <arg>` | Command to run `NullAway` on target `P` enclosed in **""**. Please note that this command should be executable from any directory (e.g., `"cd /Absolute/Path/To/P && ./build"`). |
| `-i,--initializer <arg>` | Fully qualified name of the `@Initializer` annotation. |
| `-d,--dir <arg>` | Absolute path of an **Empty** Directory where all outputs of `AnnotatorScanner` and `NullAway` are serialized. |
| `-cp, --config-paths` | Path to a TSV file containing value of config paths given in the format: (`path_to_nullaway_config.xml \t path_to_scanner_config`). |
| `-cn, --checker-name` | Checker name to be used for the analysis. (use `NULLAWAY` to request inference for NullAway.) |
| `-sre, --supress-remaning-errors` <arg> | Suppress remaining errors in the code with the given `@NullUnmared` annotation (e.g. `org.jspecify.annotations.NullUnmarked`) |

By default, `Annotator` has the configuration below:

Expand All @@ -176,8 +187,24 @@ To learn more about all the __optional__ arguments, please refer to [OPTIONS.md]

Here is a template command you can use to run Annotator from the CLI, using CLI options-
```bash
java -jar ./path/to/annotator-core.jar -d "/path/to/output/directory" -cp "/path/to/config/paths.tsv" -i com.example.Initializer -bc "cd /path/to/targetProject && ./gradlew build -x test"
curl -O https://repo.maven.apache.org/maven2/edu/ucr/cs/riple/annotator/annotator-core/1.3.15/annotator-core-1.3.15.jar
java -jar annotator-core-1.3.15.jar \
-bc "cd project && command_to_compile_target_project_using_javac" \
-d "path_to_selected_annotator_out_dir" \
-n javax.annotation.Nullable \
-cp sample/annotator-out/paths.tsv \
-cn NULLAWAY \
-i com.uber.nullaway.annotations.Initializer \
-sre org.jspecify.annotations.NullUnmarked
```

## Running Annotator on the [example project](#code-example)
The example in this readme is available in module `sample` in this project.
To run Annotator on the example project, you can use the following command:
```bash
./annotator-sample-command.sh
```
It will run annotator on the sample project and will produce the output shown in this readme.

To view descriptions of all flags, simply run the JAR with the `--help` option.

Expand Down
44 changes: 44 additions & 0 deletions annotator-sample-command.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash
#
# Copyright (c) 2024 University of California, Riverside.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

# download annotator-core.jar from maven repository
curl -O https://repo.maven.apache.org/maven2/edu/ucr/cs/riple/annotator/annotator-core/1.3.15/annotator-core-1.3.15.jar

# make an EMPTY directory for the annotator output
annotator_out_dir="$(pwd)/sample/annotator-out"
rm -rvf "$annotator_out_dir" && mkdir -p "$annotator_out_dir"

# make paths.tsv and add the placed nullaway_config_path and scanner_config_path
scanner_config_path="$(pwd)/sample/annotator-out/scanner.xml"
nullaway_config_path="$(pwd)/sample/annotator-out/nullaway.xml"
echo -e "$nullaway_config_path\t$scanner_config_path" > sample/annotator-out/paths.tsv

# run the annotator
java -jar annotator-core-1.3.15.jar \
-bc "cd $(pwd)/sample && ./gradlew compileJava" \
-d "$annotator_out_dir" \
-n javax.annotation.Nullable \
-cp sample/annotator-out/paths.tsv \
-cn NULLAWAY \
-i com.uber.nullaway.annotations.Initializer \
-sre org.jspecify.annotations.NullUnmarked
68 changes: 68 additions & 0 deletions sample/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) 2024 University of California, Riverside.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import net.ltgt.gradle.errorprone.CheckSeverity

plugins {
id 'java'
id "net.ltgt.errorprone" version "2.0.1"
}

repositories {
mavenCentral()
}

dependencies {
annotationProcessor "com.uber.nullaway:nullaway:0.10.10"
// Add annotator scanner
annotationProcessor "edu.ucr.cs.riple.annotator:annotator-scanner:1.3.15"

// add required annotation dependencies
// Initializer
compileOnly 'com.uber.nullaway:nullaway-annotations:0.10.10'
// Nullable annotations
compileOnly "com.google.code.findbugs:jsr305:3.0.2"
// JSpecify annotations for NullUnmarked
compileOnly "org.jspecify:jspecify:0.3.0"

annotationProcessor "com.uber.nullaway:nullaway:0.10.10"
errorprone "com.google.errorprone:error_prone_core:2.4.0"
errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"
}

def scanner_config_path = "${project.projectDir.absolutePath}/annotator-out/scanner.xml"
def nullaway_config_path = "${project.projectDir.absolutePath}/annotator-out/nullaway.xml"


tasks.withType(JavaCompile).configureEach {
if (!name.toLowerCase().contains("test")) {
options.errorprone {
check("NullAway", CheckSeverity.ERROR)
check("AnnotatorScanner", CheckSeverity.ERROR)
option("NullAway:AnnotatedPackages", "edu.ucr.cs.riple")
option("NullAway:SerializeFixMetadata", "true")
option("NullAway:FixSerializationConfigPath", nullaway_config_path)
option("AnnotatorScanner:ConfigPath", scanner_config_path)
}
options.compilerArgs << "-Xmaxerrs" << "100000"
options.compilerArgs << "-Xmaxwarns" << "100000"
}
}
Binary file added sample/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions sample/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 98163b2

Please sign in to comment.