From 8b3167dcc6402244431b790b43d3ab78d3bfa36c Mon Sep 17 00:00:00 2001 From: Jerry Lee Date: Sat, 30 Mar 2024 13:57:12 +0800 Subject: [PATCH] =?UTF-8?q?build/ci:=20add=20`check-forbidden-classes.sh`?= =?UTF-8?q?=20=F0=9F=9A=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 11 +++++-- scripts/check-forbidden-classes.sh | 29 +++++++++++++++++++ .../inspectablewrappers/Inspector.java | 4 +-- .../utils/WrapperAdapterUtils.java | 4 +-- .../foldright/demo/ChattyExecutorWrapper.java | 4 --- .../foldright/demo/LazyExecutorWrapper.java | 4 --- 6 files changed, 41 insertions(+), 15 deletions(-) create mode 100755 scripts/check-forbidden-classes.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77604dc..b6da6e9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,18 +23,23 @@ jobs: distribution: zulu java-version: ${{ matrix.java }} cache: maven - - run: ./mvnw -V --no-transfer-progress clean package + - name: Run dev mode test + run: ./mvnw -V --no-transfer-progress clean package if: matrix.java != 17 - - run: ./mvnw -V --no-transfer-progress clean package spotbugs:check git-commit-id:validateRevision -DperformRelease + - name: Run release mode test + run: ./mvnw -V --no-transfer-progress clean package spotbugs:check git-commit-id:validateRevision -DperformRelease if: matrix.java == 17 # https://github.com/marketplace/actions/codecov - uses: codecov/codecov-action@v4 env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} + - name: Check forbidden classes + run: scripts/check-forbidden-classes.sh + # https://docs.github.com/en/actions/learn-github-actions/variables#detecting-the-operating-system + if: runner.os != 'Windows' # https://remarkablemark.org/blog/2017/10/12/check-git-dirty/ - name: Check git dirty run: | git status --short [ -z "$(git status --short)" ] - # https://docs.github.com/en/actions/learn-github-actions/variables#detecting-the-operating-system if: runner.os != 'Windows' diff --git a/scripts/check-forbidden-classes.sh b/scripts/check-forbidden-classes.sh new file mode 100755 index 0000000..433d230 --- /dev/null +++ b/scripts/check-forbidden-classes.sh @@ -0,0 +1,29 @@ +#!/bin/bash +set -eEuo pipefail + +# cd to project root dir +cd "${0%/*}"/.. + +readonly forbidden_classes=( + # use edu.umd.cs.findbugs.annotations.Nullable + javax.annotation.Nullable + org.jetbrains.annotations.Nullable + + # use edu.umd.cs.findbugs.annotations.NonNull + javax.annotation.Nonnull + org.jetbrains.annotations.NotNull + + # use edu.umd.cs.findbugs.annotations.CheckForNull + javax.annotation.CheckReturnValue + org.jetbrains.annotations.CheckReturnValue + + # use @edu.umd.cs.findbugs.annotations.DefaultAnnotationForParameters(NonNull.class) + javax.annotation.ParametersAreNonnullByDefault +) + +grep_pattern=$(printf '%s\n' "${forbidden_classes[@]}") + +grep_options=("$grep_pattern" -F -n -C2 -r src/) +[[ "${GITHUB_ACTIONS:-}" = true || -t 1 ]] && grep_options=("${grep_options[@]}" --color=always) + +! grep "${grep_options[@]}" diff --git a/src/main/java/io/foldright/inspectablewrappers/Inspector.java b/src/main/java/io/foldright/inspectablewrappers/Inspector.java index faba630..5055843 100644 --- a/src/main/java/io/foldright/inspectablewrappers/Inspector.java +++ b/src/main/java/io/foldright/inspectablewrappers/Inspector.java @@ -1,9 +1,9 @@ package io.foldright.inspectablewrappers; +import edu.umd.cs.findbugs.annotations.DefaultAnnotationForParameters; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -import javax.annotation.ParametersAreNonnullByDefault; import java.util.Optional; import java.util.function.Function; import java.util.function.Predicate; @@ -49,7 +49,7 @@ * @see Attachable * @see WrapperAdapter */ -@ParametersAreNonnullByDefault +@DefaultAnnotationForParameters(NonNull.class) public final class Inspector { /** * Reports whether any instance on the wrapper chain matches the given type. diff --git a/src/main/java/io/foldright/inspectablewrappers/utils/WrapperAdapterUtils.java b/src/main/java/io/foldright/inspectablewrappers/utils/WrapperAdapterUtils.java index 1f6d5ff..1c9d546 100644 --- a/src/main/java/io/foldright/inspectablewrappers/utils/WrapperAdapterUtils.java +++ b/src/main/java/io/foldright/inspectablewrappers/utils/WrapperAdapterUtils.java @@ -1,12 +1,12 @@ package io.foldright.inspectablewrappers.utils; +import edu.umd.cs.findbugs.annotations.DefaultAnnotationForParameters; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import io.foldright.inspectablewrappers.Attachable; import io.foldright.inspectablewrappers.Wrapper; import io.foldright.inspectablewrappers.WrapperAdapter; -import javax.annotation.ParametersAreNonnullByDefault; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; @@ -23,7 +23,7 @@ * * @author Jerry Lee (oldratlee at gmail dot com) */ -@ParametersAreNonnullByDefault +@DefaultAnnotationForParameters(NonNull.class) public final class WrapperAdapterUtils { /** * Creates a {@link WrapperAdapter} instance of the given biz interface type by diff --git a/src/test/java/io/foldright/demo/ChattyExecutorWrapper.java b/src/test/java/io/foldright/demo/ChattyExecutorWrapper.java index 19b8e2c..f7cdf3e 100644 --- a/src/test/java/io/foldright/demo/ChattyExecutorWrapper.java +++ b/src/test/java/io/foldright/demo/ChattyExecutorWrapper.java @@ -1,14 +1,10 @@ package io.foldright.demo; -import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault; import io.foldright.inspectablewrappers.Wrapper; -import javax.annotation.ParametersAreNonnullByDefault; import java.util.concurrent.Executor; -@ParametersAreNonnullByDefault -@ReturnValuesAreNonnullByDefault public class ChattyExecutorWrapper implements Executor, Wrapper { private final Executor executor; diff --git a/src/test/java/io/foldright/demo/LazyExecutorWrapper.java b/src/test/java/io/foldright/demo/LazyExecutorWrapper.java index a31bb14..87c97b8 100644 --- a/src/test/java/io/foldright/demo/LazyExecutorWrapper.java +++ b/src/test/java/io/foldright/demo/LazyExecutorWrapper.java @@ -1,17 +1,13 @@ package io.foldright.demo; import edu.umd.cs.findbugs.annotations.Nullable; -import edu.umd.cs.findbugs.annotations.ReturnValuesAreNonnullByDefault; import io.foldright.inspectablewrappers.Attachable; import io.foldright.inspectablewrappers.Wrapper; import io.foldright.inspectablewrappers.utils.AttachableDelegate; -import javax.annotation.ParametersAreNonnullByDefault; import java.util.concurrent.Executor; -@ParametersAreNonnullByDefault -@ReturnValuesAreNonnullByDefault public class LazyExecutorWrapper implements Executor, Wrapper, Attachable { private final Executor executor;