Thanks to visit codestin.com
Credit goes to github.com

Skip to content

add anyParameterThat and allParameters predicates#1498

Merged
hankem merged 1 commit into
TNG:mainfrom
mkhl:feature/core/HasParameters
Aug 6, 2025
Merged

add anyParameterThat and allParameters predicates#1498
hankem merged 1 commit into
TNG:mainfrom
mkhl:feature/core/HasParameters

Conversation

@mkhl
Copy link
Copy Markdown
Contributor

@mkhl mkhl commented Aug 4, 2025

In another project I'm writing rules about method parameter annotations,
and it seems like support for that in the DSL would make sense.

This change is the first step towards that,
it extracts a HasParameters interface from JavaCodeUnit
and provides DescribedPredicates for

  • checking that any parameter matches
  • checking that all parameters match

@mkhl
Copy link
Copy Markdown
Contributor Author

mkhl commented Aug 4, 2025

My imagined use-case is checking nullability annotations, for example:

  • forbidding certain "flavors" of them so we converge on a single set of annotations
  • ensuring that in certain layers, all method parameters are explicitly marked as either nullable or not nullable

The custom predicates I'm currently using include:

  private static DescribedPredicate<JavaCodeUnit> usesAnnotation(
      Class<? extends Annotation> annotationType) {
    return or(be(annotatedWith(annotationType)), have(anyParameter(is(annotatedWith(annotationType)))));
  }

  private static DescribedPredicate<JavaCodeUnit> anyParameter(
      DescribedPredicate<? super JavaParameter> predicate) {
    Function<JavaCodeUnit, List<JavaParameter>> getParameters = JavaCodeUnit::getParameters;
    return anyElementThat(predicate)
        .onResultOf(getParameters)
        .as("any parameter that %s", predicate.getDescription());
  }

and it's particularly annoying that I can't use JavaCodeUnit::getParameters without explicit type hints 😅

@mkhl mkhl force-pushed the feature/core/HasParameters branch from f20202f to be3457a Compare August 4, 2025 10:19
Copy link
Copy Markdown
Member

@hankem hankem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! 💚

I've left some comments on HasParameters's JavaDoc. (I think we cannot directly use JavaCodeUnit's.)

@mkhl mkhl force-pushed the feature/core/HasParameters branch from 390f7c2 to 092f1d9 Compare August 4, 2025 14:18
@hankem
Copy link
Copy Markdown
Member

hankem commented Aug 6, 2025

Could you please rebase the PR onto the main branch? I'd also squash all commits (I don't think we don't need to preserve the back and forth in the history). Oh, and maybe adapt the PR title. 😅

In another project I'm writing rules about method parameter annotations,
and it seems like support for that in the DSL would make sense.

This change is the first step towards that,
it provides `DescribedPredicate`s for
- checking that *any* parameter matches
- checking that *all* parameters match

Signed-off-by: Martin Kühl <[email protected]>
@mkhl mkhl force-pushed the feature/core/HasParameters branch from f668bf8 to 3518bf5 Compare August 6, 2025 08:03
@mkhl mkhl changed the title add HasParameters domain property add anyParameterThat and allParameters described properties Aug 6, 2025
@mkhl mkhl changed the title add anyParameterThat and allParameters described properties add anyParameterThat and allParameters predicates Aug 6, 2025
@mkhl
Copy link
Copy Markdown
Contributor Author

mkhl commented Aug 6, 2025

done :)

@hankem hankem merged commit b0922cc into TNG:main Aug 6, 2025
26 checks passed
hankem added a commit that referenced this pull request Aug 6, 2025
In another project I'm writing rules about method parameter annotations,
and it seems like support for that in the DSL would make sense.

This change is the first step towards that,
it extracts a HasParameters interface from JavaCodeUnit
and provides DescribedPredicates for
- checking that any parameter matches
- checking that all parameters match
@mkhl mkhl deleted the feature/core/HasParameters branch August 6, 2025 20:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants