Git Product home page Git Product logo

Comments (1)

codecholeric avatar codecholeric commented on June 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.