Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[How to] Detect reference for field in methods #857

Closed
omuomugin opened this issue Apr 28, 2022 · 4 comments · Fixed by #1105
Closed

[How to] Detect reference for field in methods #857

omuomugin opened this issue Apr 28, 2022 · 4 comments · Fixed by #1105

Comments

@omuomugin
Copy link

I've trying to write tests like below

ArchRuleDefinition.fields()
  .that().inSomeCondition()
  .should()
  .notBeAccessedFromMethods().inSomeCondition()

or

ArchRuleDefinition.methods()
  .that().inSomeCondition()
  .should()
  .notBeAccessFields().inSomeCondition()

Is there any kind of support for detecting dependency of fields in methods.

@codecholeric
Copy link
Collaborator

I think this is unfortunately still missing from the fluent API, even though it would be a nice addition! For the moment you will have to resort to add this yourself, e.g. like

@ArchTest
public static final ArchRule example =
  fields().that().inSomeCondition()
    .should(notBeAccessedByMethodsThat(inSomeConditionPredicate));

private static ArchCondition<JavaField> notBeAccessedByMethodsThat(DescribedPredicate<JavaMethod> predicate) {
  return new ArchCondition<JavaField>("not be accessed by methods that " + predicate.getDescription()) {
  @Override
  public void check(JavaField field, ConditionEvents events) {
    field.getAccessesToSelf().stream()
      .filter(access ->
        access.getOrigin() instanceof JavaMethod
          && predicate.test((JavaMethod) access.getOrigin())
      )
      .forEach(violation ->
        events.add(SimpleConditionEvent.violated(field, violation.getDescription()))
      );
  }
  };
}

I'll keep the issue open, maybe somebody wants to contribute this to the fluent API (or I'll do it once I get around to it, but no promises there about the timeline, since there are a lot of other issues I have to tackle first 😬)

@omuomugin
Copy link
Author

omuomugin commented May 3, 2022

Thanks for your help.
I succeeded to handle it just like the example you gave me.

One little point to point out was predicate.test was actually predicate.apply (just in case for other people not to be confused)

However adding these kinds of support to fluent API will help myself so I can give it a try.

I'm thinking of API like below if possible.
(I will dig into code sometime after.)

fields()
.that()
.inSomeCondition()
.should()
.notBeAccessedByMethods()
.that()
.inSomeCondition()

Were there any alternative that you were thinking of?

@codecholeric
Copy link
Collaborator

Oh sorry, I was playing around on the branch #833 😬 Once that is merged (for ArchUnit 1.0.0rc1 😉) the method will be predicate.test(..)...
Anyway, glad you could solve it 😃

@leonardhusmann
Copy link
Contributor

Hi, am I correct in assuming that this issue is still available? If so, I would like to work on it :)

leonardhusmann added a commit to leonardhusmann/ArchUnit that referenced this issue Apr 28, 2023
This adds a method to the public API of FieldsShould.java to check if a field has been accessed by methods matching a given predicate.

Issue: TNG#857
Signed-off-by: Leonard Husmann <leonard.husmann@tum.de>
leonardhusmann added a commit to leonardhusmann/ArchUnit that referenced this issue Apr 29, 2023
Issue: TNG#857
Signed-off-by: Leonard Husmann <leonard.husmann@tum.de>
codecholeric pushed a commit to leonardhusmann/ArchUnit that referenced this issue Oct 8, 2023
This adds a method to the public API of FieldsShould.java to check if a field has been accessed by methods matching a given predicate.

Issue: TNG#857
Signed-off-by: Leonard Husmann <leonard.husmann@tum.de>
codecholeric pushed a commit to leonardhusmann/ArchUnit that referenced this issue Oct 8, 2023
Issue: TNG#857
Signed-off-by: Leonard Husmann <leonard.husmann@tum.de>
codecholeric pushed a commit to leonardhusmann/ArchUnit that referenced this issue Oct 8, 2023
This adds a method to the public API of FieldsShould.java to check if a field has been accessed by methods matching a given predicate.

Issue: TNG#857
Signed-off-by: Leonard Husmann <leonard.husmann@tum.de>
codecholeric pushed a commit to leonardhusmann/ArchUnit that referenced this issue Oct 8, 2023
Issue: TNG#857
Signed-off-by: Leonard Husmann <leonard.husmann@tum.de>
codecholeric pushed a commit to leonardhusmann/ArchUnit that referenced this issue Nov 4, 2023
Adds `FieldsShould.{be/notBe}AccessedByMethodsThat(predicate)` to the rules API.

Issue: TNG#857
Signed-off-by: Leonard Husmann <leonard.husmann@tum.de>
codecholeric added a commit that referenced this issue Nov 4, 2023
Adds `FieldsShould.{be/notBe}AccessedByMethodsThat(predicate)` to the rules API.

Resolves: #857
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants