Skip to content

Commit

Permalink
Added support for Apache Validate (#769)
Browse files Browse the repository at this point in the history
Added support for Apache Validate:
https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/Validate.java

We recently started to use these Apache methods to validate some data
but quickly found out that NullAway doesn't support them since they are
not properly annotated. Here's a fix that adds support for these methods
along with test cases for each method.

I noticed that Apache library generally has different validation methods
for String, Map and Collection, but they all end up with the same
signature when analyzed by NullAway (probably because these methods use
generics).
Examples of such methods:
```java
public static <T extends Collection<?>> T notEmpty(final T collection) {
}

public static <T extends Map<?, ?>> T notEmpty(final T map) {
}

public static <T extends CharSequence> T notEmpty(final T chars) {
}
```

But I decided to add a separate unit test for each of these methods in
case NullAway will change how it generates method's signature in the
future.
  • Loading branch information
Asapin committed Jun 21, 2023
1 parent 1ff88b6 commit 0466a02
Show file tree
Hide file tree
Showing 2 changed files with 430 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,54 @@ private static class DefaultLibraryModels implements LibraryModels {
"org.junit.jupiter.api.Assertions",
"assertNotNull(java.lang.Object,java.util.function.Supplier<java.lang.String>)"),
0)
.put(methodRef("org.apache.commons.lang3.Validate", "<T>notNull(T)"), 0)
.put(
methodRef(
"org.apache.commons.lang3.Validate",
"<T>notNull(T,java.lang.String,java.lang.Object...)"),
0)
.put(
methodRef(
"org.apache.commons.lang3.Validate",
"<T>notEmpty(T[],java.lang.String,java.lang.Object...)"),
0)
.put(methodRef("org.apache.commons.lang3.Validate", "<T>notEmpty(T[])"), 0)
.put(
methodRef(
"org.apache.commons.lang3.Validate",
"<T>notEmpty(T,java.lang.String,java.lang.Object...)"),
0)
.put(methodRef("org.apache.commons.lang3.Validate", "<T>notEmpty(T)"), 0)
.put(
methodRef(
"org.apache.commons.lang3.Validate",
"<T>notBlank(T,java.lang.String,java.lang.Object...)"),
0)
.put(methodRef("org.apache.commons.lang3.Validate", "<T>notBlank(T)"), 0)
.put(
methodRef(
"org.apache.commons.lang3.Validate",
"<T>noNullElements(T[],java.lang.String,java.lang.Object...)"),
0)
.put(methodRef("org.apache.commons.lang3.Validate", "<T>noNullElements(T[])"), 0)
.put(
methodRef(
"org.apache.commons.lang3.Validate",
"<T>noNullElements(T,java.lang.String,java.lang.Object...)"),
0)
.put(methodRef("org.apache.commons.lang3.Validate", "<T>noNullElements(T)"), 0)
.put(
methodRef(
"org.apache.commons.lang3.Validate",
"<T>validIndex(T[],int,java.lang.String,java.lang.Object...)"),
0)
.put(methodRef("org.apache.commons.lang3.Validate", "<T>validIndex(T[],int)"), 0)
.put(
methodRef(
"org.apache.commons.lang3.Validate",
"<T>validIndex(T,int,java.lang.String,java.lang.Object...)"),
0)
.put(methodRef("org.apache.commons.lang3.Validate", "<T>validIndex(T,int)"), 0)
.build();

private static final ImmutableSetMultimap<MethodRef, Integer> EXPLICITLY_NULLABLE_PARAMETERS =
Expand Down
Loading

0 comments on commit 0466a02

Please sign in to comment.