Comments (1)
I don't think what you want is completely possible, cause ArchUnit doesn't track any syntax tree. I.e. if one call is nested into another call in a lambda or something like that isn't really part of the available information. That being said, what you can test is that the origin of each call of a service has to be a lambda and you could also test that your loginfo
is called in the same places. But that's of course just a heuristic. But maybe it's good enough (potentially with some fuzzy matching like the line number doesn't need to be an exact match or something like that 🤷). In any case, it's not the prettiest thing 😉
classes().that().haveSimpleNameEndingWith("Service")
.should(new ArchCondition<JavaClass>("only be called through log infrastructure") {
@Override
public void check(JavaClass service, ConditionEvents events) {
Set<JavaMethodCall> callsOfService = service.getMethodCallsToSelf();
Set<JavaMethodCall> logInfoCalls = callsOfService.stream()
.map(JavaMethodCall::getOrigin)
.flatMap(clazz -> clazz.getMethodCallsFromSelf().stream())
.filter(callFromSomeOrigin ->
callFromSomeOrigin.getTargetOwner().isEquivalentTo(Loginfo.class) &&
callFromSomeOrigin.getTarget().getName().equals("call")
)
.collect(toSet());
Predicate<JavaMethodCall> existsLogInfoCallWithSameLineNumberAs = (JavaMethodCall call) ->
logInfoCalls.stream().anyMatch(logInfoCall ->
logInfoCall.getOrigin().equals(call.getOrigin()) &&
logInfoCall.getLineNumber() == call.getLineNumber()
);
callsOfService.stream()
.filter(call -> !call.isDeclaredInLambda() || !existsLogInfoCallWithSameLineNumberAs.test(call))
.forEach(call -> events.add(SimpleConditionEvent.violated(call, call.getDescription())));
}
})
I assumed something like this:
class MyService {
void action() {
}
}
class MyOther {
Logger logger;
MyService myService;
void callServiceOkay() {
logger.loginfo("okay").call(() -> myService.action());
}
void callServiceWrong() {
myService.action();
}
}
class Logger {
Loginfo loginfo(String info) {
return new Loginfo();
}
}
class Loginfo {
void call(Runnable runnable) {
runnable.run();
}
}
You'd probably have to adjust it to your use case...
from archunit.
Related Issues (20)
- Using MethodReferences is still breaking soundness HOT 5
- Usability: Java confuses the static and non-static versions of DescribedPredicate.and() when using the API the wrong way. HOT 3
- Unable to find the JavaAccess call HOT 8
- Feature suggestion: Freeze files and stored.rules validation / sanity check
- How To : validate return type is not `void` HOT 2
- How To : Is there way to filter classes when using `importJar()` HOT 1
- DEPRECATED_API_SHOULD_NOT_BE_USED does not consider constants nor final fields HOT 2
- ASM is not up-to-date
- Jakarta Inject package name incorrect in BE_ANNOTATED_WITH_AN_INJECTION_ANNOTATION rule HOT 1
- How can I find out which gradle module a class belongs to HOT 7
- Problem with maven multi module project HOT 2
- False positive results when I use ArchRuleDefinition.noClasses() HOT 7
- Check access classes only works for some cases HOT 1
- ArchUnit do not correctly check Annotation on enum Constants HOT 2
- ArchUnit thinks "Switch with arrows" produces non-final fields HOT 1
- Feature suggestion: Expose synchronized blocks in core API
- Performance: archunit-junit5 may scan complete JDK for `@ArchTest`
- SimpleName-Check failing because of trailing file extension (Kt) HOT 6
- How to fail tests if no classes are imported? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from archunit.