Search code examples
javaarchunit

ArchUnit class link in violation messages


I've noticed that using standard ArchConditions display messages in the following format:

Class <full_class_path> does not <some_rule> in (<class_link>)

However, this is not the case for custom conditions that add violation messages to the event like:

events.add(SimpleConditionEvent.violated(item, item.getName() + " some message"));

With this, no link to the violating class is appended to the message automatically. I wonder what the first argument (correspondingObject) is actually used for then.

Is this a bug in the framework or am I missing something? Having these links are really useful. I've tried using the JavaDoc @link notation in the message string to no avail.


Solution

  • Most of the domain classes like JavaClass, JavaMember or JavaAccess implement the interface HasSourceCodeLocation, which contains the method getSourceCodeLocation to obtain the location in the source code. The returned object SourceCodeLocation can then be used to output the location via the toString method.

    For example

    events.add(SimpleConditionEvent.violated(item, item.getDescription() + " is violated in " + item.getSourceCodeLocation()));
    

    would return

    Class <org.example.Dummy> is violated in (Dummy.java:0)