Skip to content

Commit

Permalink
make ArchConditions.createMessage(..) available as public API
Browse files Browse the repository at this point in the history
This convenience method also helps users to create standard ArchUnit violation messages, so we offer it as public API. Since we meanwhile have a Java 8 source level we can even put this method into `ConditionEvent` where it fits better.

Signed-off-by: Peter Gafert <[email protected]>
  • Loading branch information
codecholeric committed Jul 8, 2022
1 parent 65843a8 commit a3268fb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
import java.util.List;

import com.tngtech.archunit.PublicAPI;
import com.tngtech.archunit.base.HasDescription;
import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaMethodCall;
import com.tngtech.archunit.core.domain.properties.HasSourceCodeLocation;

import static com.tngtech.archunit.PublicAPI.State.EXPERIMENTAL;
import static com.tngtech.archunit.PublicAPI.Usage.ACCESS;
Expand Down Expand Up @@ -66,6 +69,21 @@ public interface ConditionEvent {
@PublicAPI(usage = INHERITANCE, state = EXPERIMENTAL)
void handleWith(Handler handler);

/**
* Convenience method to create a standard ArchUnit {@link ConditionEvent} message. It will prepend the
* description of the object that caused the event (e.g. a {@link JavaClass}) and append the source code
* location of the respective object.
*
* @param object The object to describe, e.g. the {@link JavaClass} {@code com.example.SomeClass}
* @param message The message that should be filled into the template, e.g. "does not have simple name 'Correct'"
* @return The formatted message, e.g. {@code Class <com.example.SomeClass> does not have simple name 'Correct' in (SomeClass.java:0)}
* @param <T> The object described by the event.
*/
@PublicAPI(usage = ACCESS)
static <T extends HasDescription & HasSourceCodeLocation> String createMessage(T object, String message) {
return object.getDescription() + " " + message + " in " + object.getSourceCodeLocation();
}

/**
* Handles the data of a {@link ConditionEvent} that is the corresponding objects and the description
* (compare {@link #handleWith(Handler)}).<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
import static com.tngtech.archunit.core.domain.properties.HasReturnType.Predicates.rawReturnType;
import static com.tngtech.archunit.core.domain.properties.HasThrowsClause.Predicates.throwsClauseContainingType;
import static com.tngtech.archunit.core.domain.properties.HasType.Predicates.rawType;
import static com.tngtech.archunit.lang.ConditionEvent.createMessage;
import static com.tngtech.archunit.lang.conditions.ArchPredicates.have;
import static java.util.Arrays.asList;

Expand Down Expand Up @@ -1284,10 +1285,6 @@ public static ArchCondition<JavaCodeUnit> onlyBeCalledByConstructorsThat(Describ
origin.is(constructor().and(predicate)), GET_CALLS_OF_SELF);
}

private static <T extends HasDescription & HasSourceCodeLocation> String createMessage(T object, String message) {
return object.getDescription() + " " + message + " in " + object.getSourceCodeLocation();
}

private static final IsConditionByPredicate<JavaClass> BE_TOP_LEVEL_CLASSES =
new IsConditionByPredicate<>("a top level class", JavaClass.Predicates.TOP_LEVEL_CLASSES);
private static final IsConditionByPredicate<JavaClass> BE_NESTED_CLASSES =
Expand Down

0 comments on commit a3268fb

Please sign in to comment.