Skip to content

Commit

Permalink
fix: jakarta Nonnull assertion for method returning collections (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul58914080 authored Nov 16, 2023
1 parent fcbc9ea commit e5f6097
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<jsr305.version>3.0.2</jsr305.version>
<jackson.version>2.15.2</jackson.version>
<commons-io.version>2.13.0</commons-io.version>
<jakarta.annotation-api.version>2.1.1</jakarta.annotation-api.version>

<maven-compiler-plugin.version>3.11.0</maven-compiler-plugin.version>
<maven-jar-plugin.version>3.3.0</maven-jar-plugin.version>
Expand Down Expand Up @@ -229,6 +230,11 @@
<artifactId>commons-io</artifactId>
<version>${commons-io.version}</version>
</dependency>
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta.annotation-api.version}</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class DontReturnNullCollectionTest implements ArchRuleTest {

protected static final String NO_NULL_COLLECTION_MESSAGE = "we don't want callers to perform null check every time. Return an empty collection, not null. Please annotate the method with "+Nonnull.class.getCanonicalName();
protected static final String NO_NULL_COLLECTION_MESSAGE = "we don't want callers to perform null check every time. Return an empty collection, not null. Please annotate the method with "+Nonnull.class.getCanonicalName()+" or "+ jakarta.annotation.Nonnull.class.getCanonicalName();

@Override
public void execute(String packagePath, ScopePathProvider scopePathProvider, Collection<String> excludedPaths) {
Expand All @@ -33,6 +33,7 @@ public void execute(String packagePath, ScopePathProvider scopePathProvider, Col

ArchRule rule = methods().that(returnCollections).and(areNotLambdas)
.should().beAnnotatedWith(Nonnull.class)
.orShould().beAnnotatedWith(jakarta.annotation.Nonnull.class)
.because(NO_NULL_COLLECTION_MESSAGE)
.allowEmptyShould(true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package com.societegenerale.aut.main;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.Set;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;

import java.util.List;
import java.util.Set;

public class ObjectWithProperlyAnnotatedMethodsReturningCollections {

@Nonnull
public List returningANullList(){
@jakarta.annotation.Nonnull
public List returningANullListWithJakartaNonnullAnnotation(){
return emptyList();
}

@Nonnull
public Set returningANullSet(){
@jakarta.annotation.Nonnull
public Set returningANullSetWithJakartaNonnullAnnotation(){
return emptySet();
}

@javax.annotation.Nonnull
public List returningANullListWithJavaxNonnullAnnotation(){
return emptyList();
}

@javax.annotation.Nonnull
public Set returningANullSetWithJavaxNonnullAnnotation(){
return emptySet();
}
}

0 comments on commit e5f6097

Please sign in to comment.