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
(cherry picked from commit e5f6097)
  • Loading branch information
paul58914080 authored and vincent-fuchs committed Nov 19, 2023
1 parent 1a243d5 commit 1a1627e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
9 changes: 7 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
<jacoco.version>0.8.8</jacoco.version>
<mockito.version>3.0.0</mockito.version>

<jakarta.annotation-api.version>2.1.1</jakarta.annotation-api.version>

<project.testresult.directory>${project.build.directory}/test-results</project.testresult.directory>
</properties>

Expand Down Expand Up @@ -218,8 +220,11 @@
<version>2.10.0</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,6 +1,5 @@
package com.societegenerale.aut.main;

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

Expand All @@ -9,14 +8,23 @@

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 1a1627e

Please sign in to comment.