Commit 39d65f8
Add
### Description
This PR adds support for nullability preserving methods annotated with a
new `PureExceptLambda` annotation.
When a method is marked as `@PureExceptLambda`, it means that there's no
way that it could effect the variables used within a lambda passed to it
(no side-effects except those possibly performed by the lambda once
invoked), and that the lambda will only be called within that method and
not stored for later use. Allowing developers to implement their own
lambda api's without needing explicit checks in NullAway.
Here's an example of a simple adapter pattern with the issue:
```java
@FunctionalInterface
public interface LegacyListener {
void onEvent(String payload);
}
public static class EventAdapter {
@PureExceptLambda
public void withLegacyListener(String payload, LegacyListener listener) {
listener.onEvent(payload);
}
}
public class Service {
private @nullable Database db; // set elsewhere
public void process(String input) {
if (db == null) {
// After this, `db` is known non-null in this branch
throw new IllegalStateException("db not initialized");
}
EventAdapter.withLegacyListener(input, payload -> db.execute(payload));
}
}
```
Before this PR, NullAway would have warned that `db` is null inside the
lambda returned from `EventSource.withListener`. However, now its seen
as nullness preserving, it preserves the non-null state.
<details>
<summary>Details</summary>
Please note that once you click "Create Pull Request" you will be asked
to sign our [Uber Contributor License
Agreement](https://cla-assistant.io/uber/NullAway) via [CLA
assistant](https://cla-assistant.io/).
Before pressing the "Create Pull Request" button, please provide the
following:
- [x] A description about what and why you are contributing, even if
it's trivial.
- [x] The issue number(s) or PR number(s) in the description if you are
contributing in response to those.
- [x] If applicable, unit tests.
</details>
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit
* **New Features**
* Added @PureExceptLambda annotation to mark methods that preserve
nullness properties when invoking lambda parameters, enabling improved
null-safety analysis for lambda-based APIs.
* **Tests**
* Added test coverage validating nullness fact preservation in methods
annotated with @PureExceptLambda.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
---------
Co-authored-by: Manu Sridharan <msridhar@gmail.com>PureExceptLambda annotation (#1325)1 parent 82784a2 commit 39d65f8
File tree
3 files changed
+155
-0
lines changed- annotations/src/main/java/com/uber/nullaway/annotations
- nullaway/src
- main/java/com/uber/nullaway/handlers
- test/java/com/uber/nullaway
3 files changed
+155
-0
lines changedLines changed: 52 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
Lines changed: 19 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| 22 | + | |
| 23 | + | |
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
| |||
67 | 69 | | |
68 | 70 | | |
69 | 71 | | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
70 | 77 | | |
71 | 78 | | |
72 | 79 | | |
| |||
91 | 98 | | |
92 | 99 | | |
93 | 100 | | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
94 | 113 | | |
Lines changed: 84 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
0 commit comments