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

Add method support for function callMethod #1228

Open
rodrigobittencourtlima opened this issue Jan 9, 2024 · 2 comments
Open

Add method support for function callMethod #1228

rodrigobittencourtlima opened this issue Jan 9, 2024 · 2 comments

Comments

@rodrigobittencourtlima
Copy link

Currently, it is possible to validate that certain classes must call a specific method from another class using the following rule:

@ArchTest
	public final ArchRule classShouldCallAudit = ArchRuleDefinition.classes().that()
			.areAnnotatedWith(Transactional.class).should()
			.callMethod(Audit.class, "audit", String.class, String.class)
			.as("Ensure system audit");

It would be interesting to add the same possibility to methods, using something similar to:

@ArchTest
	public final ArchRule methodShouldCallAudit = ArchRuleDefinition.methods().that()
			.areAnnotatedWith(Transactional.class).should()
			.callMethod(Audit.class, "audit", String.class, String.class)
			.as("Ensure system audit");
@hankem
Copy link
Member

hankem commented Jan 9, 2024

I agree that this would be a nice addition to the API!

For now, you can use the following workaround:

@ArchTest
public final ArchRule methodShouldCallAudit = ArchRuleDefinition.methods().that()
    .areAnnotatedWith(Transactional.class)
    .should(ArchCondition.from(DescribedPredicate.describe("call Audit.audit(String, String)", method ->
        method.getMethodCallsFromSelf().stream()
            .map(JavaMethodCall::getTarget)
            .anyMatch(methodCallTarget -> {
                List<JavaClass> rawParameterTypes = methodCallTarget.getRawParameterTypes();
                return methodCallTarget.getOwner().isEquivalentTo(Audit.class)
                    && methodCallTarget.getName().equals("audit")
                    && rawParameterTypes.size() == 2
                    && rawParameterTypes.get(0).isEquivalentTo(String.class)
                    && rawParameterTypes.get(1).isEquivalentTo(String.class);
            })
    )))
    .as("Ensure system audit");

@rodrigobittencourtlima
Copy link
Author

Amazing @hankem! Thank you!

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

No branches or pull requests

2 participants