-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
External Library Models Integration (#922)
The newly added `library-model` module consists of a CLI process that takes an input directory with annotated java source files as a command line parameter and uses `com.github.javaparser` APIS to generate `libmodels.astubx` file containing method stubs for methods that return @nullable. This can be run using the existing `JarInferEnabled` and `JarInferUseReturnAnnotations` flags. This allows us to be able catch issues as shown in the below example from externally annotated source code: ```java @NullMarked public class AnnotationExample { @nullable public String makeUpperCase(String inputString) { if (inputString == null || inputString.isEmpty()) { return null; } else { return inputString.toUpperCase(); } } } ``` ```java class Test { static AnnotationExample annotationExample = new AnnotationExample(); static void test(String value){} static void testPositive() { // BUG: Diagnostic contains: passing @nullable parameter 'annotationExample.makeUpperCase(\"nullaway\")' test(annotationExample.makeUpperCase(\"nullaway\")); } } ``` --------- Co-authored-by: Manu Sridharan <[email protected]> Co-authored-by: Lázaro Clapp <[email protected]>
- Loading branch information
1 parent
5acd394
commit 09db47a
Showing
21 changed files
with
743 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
...infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/MethodAnnotationsRecord.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright (C) 2024. Uber Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
plugins { | ||
id "java-library" | ||
id "com.github.johnrengelman.shadow" | ||
} | ||
|
||
jar{ | ||
manifest { | ||
attributes('Main-Class':'com.uber.nullaway.libmodel.LibraryModelGeneratorCLI') | ||
} | ||
// add this classifier so that the output file for the jar task differs from | ||
// the output file for the shadowJar task (otherwise they overwrite each other's | ||
// outputs, forcing the tasks to always re-run) | ||
archiveClassifier = "nonshadow" | ||
} | ||
|
||
shadowJar { | ||
mergeServiceFiles() | ||
configurations = [ | ||
project.configurations.runtimeClasspath | ||
] | ||
archiveClassifier = "" | ||
} | ||
shadowJar.dependsOn jar | ||
assemble.dependsOn shadowJar | ||
|
||
dependencies { | ||
implementation project(":library-model:library-model-generator") | ||
} |
47 changes: 47 additions & 0 deletions
47
...odel-generator-cli/src/main/java/com/uber/nullaway/libmodel/LibraryModelGeneratorCLI.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2024 Uber Technologies, Inc. | ||
* | ||
* 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. | ||
*/ | ||
|
||
package com.uber.nullaway.libmodel; | ||
|
||
/** | ||
* A CLI tool for invoking the process for {@link LibraryModelGenerator} which generates astubx | ||
* file(s) from a directory containing annotated source code to be used as external library models. | ||
*/ | ||
public class LibraryModelGeneratorCLI { | ||
/** | ||
* This is the main method of the cli tool. It parses the source files within a specified | ||
* directory, obtains meaningful Nullability annotation information and writes it into an astubx | ||
* file. | ||
* | ||
* @param args Command line arguments for the directory containing source files and the output | ||
* directory. | ||
*/ | ||
public static void main(String[] args) { | ||
if (args.length != 2) { | ||
System.out.println( | ||
"Incorrect number of command line arguments. Required arguments: <inputSourceDirectory> <outputDirectory>"); | ||
return; | ||
} | ||
LibraryModelGenerator libraryModelGenerator = new LibraryModelGenerator(); | ||
libraryModelGenerator.generateAstubxForLibraryModels(args[0], args[1]); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
library-model/library-model-generator-integration-test/build.gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2024. Uber Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
plugins { | ||
id "java-library" | ||
id "nullaway.java-test-conventions" | ||
} | ||
|
||
dependencies { | ||
testImplementation project(":nullaway") | ||
testImplementation project(":library-model:test-library-model-generator") | ||
testImplementation deps.test.junit4 | ||
testImplementation(deps.build.errorProneTestHelpers) { | ||
exclude group: "junit", module: "junit" | ||
} | ||
implementation deps.build.guava | ||
implementation deps.build.javaparser | ||
compileOnly deps.apt.autoValueAnnot | ||
annotationProcessor deps.apt.autoValue | ||
} |
126 changes: 126 additions & 0 deletions
126
...ntegration-test/src/test/java/com/uber/nullaway/libmodel/LibraryModelIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package com.uber.nullaway.libmodel; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import com.uber.nullaway.NullAway; | ||
import java.util.Arrays; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
|
||
public class LibraryModelIntegrationTest { | ||
|
||
@Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); | ||
|
||
private CompilationTestHelper compilationHelper; | ||
|
||
@Before | ||
public void setup() { | ||
compilationHelper = CompilationTestHelper.newInstance(NullAway.class, getClass()); | ||
} | ||
|
||
@Test | ||
public void libraryModelNullableReturnsTest() { | ||
compilationHelper | ||
.setArgs( | ||
Arrays.asList( | ||
"-d", | ||
temporaryFolder.getRoot().getAbsolutePath(), | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber", | ||
"-XepOpt:NullAway:JarInferEnabled=true", | ||
"-XepOpt:NullAway:JarInferUseReturnAnnotations=true")) | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import com.uber.nullaway.libmodel.AnnotationExample;", | ||
"class Test {", | ||
" static AnnotationExample annotationExample = new AnnotationExample();", | ||
" static void test(String value){", | ||
" }", | ||
" static void testPositive() {", | ||
" // BUG: Diagnostic contains: passing @Nullable parameter 'annotationExample.makeUpperCase(\"nullaway\")'", | ||
" test(annotationExample.makeUpperCase(\"nullaway\"));", | ||
" }", | ||
" static void testNegative() {", | ||
" test(annotationExample.nullReturn());", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void libraryModelNullableReturnsArrayTest() { | ||
compilationHelper | ||
.setArgs( | ||
Arrays.asList( | ||
"-d", | ||
temporaryFolder.getRoot().getAbsolutePath(), | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber", | ||
"-XepOpt:NullAway:JarInferEnabled=true", | ||
"-XepOpt:NullAway:JarInferUseReturnAnnotations=true")) | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import com.uber.nullaway.libmodel.AnnotationExample;", | ||
"class Test {", | ||
" static AnnotationExample annotationExample = new AnnotationExample();", | ||
" static void test(Integer[] value){", | ||
" }", | ||
" static void testPositive() {", | ||
" // BUG: Diagnostic contains: passing @Nullable parameter 'annotationExample.generateIntArray(7)'", | ||
" test(annotationExample.generateIntArray(7));", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void libraryModelWithoutJarInferEnabledTest() { | ||
compilationHelper | ||
.setArgs( | ||
Arrays.asList( | ||
"-d", | ||
temporaryFolder.getRoot().getAbsolutePath(), | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber")) | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import com.uber.nullaway.libmodel.AnnotationExample;", | ||
"class Test {", | ||
" static AnnotationExample annotationExample = new AnnotationExample();", | ||
" static void test(String value){", | ||
" }", | ||
" static void testNegative() {", | ||
" // Since the JarInferEnabled and JarInferUseReturnAnnotations flags are not set, we don't get an error here", | ||
" test(annotationExample.makeUpperCase(\"nullaway\"));", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
|
||
@Test | ||
public void libraryModelInnerClassNullableReturnsTest() { | ||
compilationHelper | ||
.setArgs( | ||
Arrays.asList( | ||
"-d", | ||
temporaryFolder.getRoot().getAbsolutePath(), | ||
"-XepOpt:NullAway:AnnotatedPackages=com.uber", | ||
"-XepOpt:NullAway:JarInferEnabled=true", | ||
"-XepOpt:NullAway:JarInferUseReturnAnnotations=true")) | ||
.addSourceLines( | ||
"Test.java", | ||
"package com.uber;", | ||
"import com.uber.nullaway.libmodel.AnnotationExample;", | ||
"class Test {", | ||
" static AnnotationExample.InnerExample innerExample = new AnnotationExample.InnerExample();", | ||
" static void test(String value){", | ||
" }", | ||
" static void testPositive() {", | ||
" // BUG: Diagnostic contains: passing @Nullable parameter 'innerExample.returnNull()'", | ||
" test(innerExample.returnNull());", | ||
" }", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright (C) 2024. Uber Technologies | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
plugins { | ||
id 'java-library' | ||
id 'nullaway.java-test-conventions' | ||
} | ||
|
||
dependencies { | ||
implementation deps.build.guava | ||
implementation deps.build.javaparser | ||
compileOnly deps.apt.autoValueAnnot | ||
annotationProcessor deps.apt.autoValue | ||
} |
Oops, something went wrong.