use of com.tngtech.archunit.lang.SimpleConditionEvent in project sirius-components by eclipse-sirius.
the class AbstractCodingRulesTests method notContainBusinessCode.
/**
* This predicate will be used to identify business code in a class.
*
* <p>
* For that it will look for the following patterns in a Java class:
* <p>
* <ul>
* <li>methods which are not getters with a field matching the name of the method</li>
* </ul>
*
* @return A predicate which can be used to identify business code in a class
*/
private ArchCondition<JavaClass> notContainBusinessCode() {
return new // $NON-NLS-1$
ArchCondition<>(// $NON-NLS-1$
"not contain business code") {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
boolean isConditionSatisfied = true;
Set<JavaMethod> methods = javaClass.getMethods();
Iterator<JavaMethod> iterator = methods.iterator();
while (isConditionSatisfied && iterator.hasNext()) {
JavaMethod javaMethod = iterator.next();
String name = javaMethod.getName();
if (name.startsWith(IS) && javaMethod.getRawReturnType().isAssignableTo(Boolean.class)) {
name = name.substring(IS.length());
} else if (name.startsWith(GET)) {
name = name.substring(GET.length());
}
if (!name.isBlank()) {
name = name.substring(0, 1).toLowerCase() + name.substring(1, name.length());
isConditionSatisfied = javaClass.tryGetField(name).isPresent();
} else {
isConditionSatisfied = false;
}
}
// $NON-NLS-1$
String message = "The abstract class does not have any business code";
if (!isConditionSatisfied) {
// $NON-NLS-1$
String pattern = "The abstract class {0} does contain business code, please favor composition over inheritance to share business code";
message = MessageFormat.format(pattern, javaClass.getSimpleName());
}
events.add(new SimpleConditionEvent(javaClass, isConditionSatisfied, message));
}
};
}
use of com.tngtech.archunit.lang.SimpleConditionEvent in project sirius-components by eclipse-sirius.
the class AbstractImmutableTests method haveABuilderMethod.
private ArchCondition<JavaClass> haveABuilderMethod() {
return new // $NON-NLS-1$
ArchCondition<>(// $NON-NLS-1$
"have a builder method") {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
// @formatter:off
long count = javaClass.getMethods().stream().filter(// $NON-NLS-1$
method -> method.getName().equals("new" + javaClass.getSimpleName())).filter(// $NON-NLS-1$
method -> "Builder".equals(method.getRawReturnType().getSimpleName())).count();
// @formatter:on
boolean isConditionSatisfied = count > 0;
// $NON-NLS-1$
String message = "The class has a builder method";
if (!isConditionSatisfied) {
// $NON-NLS-1$
message = MessageFormat.format("The class {0} does not have a builder method", javaClass.getSimpleName());
}
events.add(new SimpleConditionEvent(javaClass, isConditionSatisfied, message));
}
};
}
use of com.tngtech.archunit.lang.SimpleConditionEvent in project sirius-components by eclipse-sirius.
the class HaveAValidBuilderCondition method check.
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
String fullName = javaClass.getFullName();
// $NON-NLS-1$
JavaClass builderJavaClass = this.javaClasses.get(fullName + "$Builder");
boolean isValidBuilder = builderJavaClass.getModifiers().contains(FINAL);
isValidBuilder = isValidBuilder && builderJavaClass.getModifiers().contains(PUBLIC);
// @formatter:off
List<JavaField> javaFields = javaClass.getAllFields().stream().filter(field -> !field.getModifiers().contains(STATIC)).collect(Collectors.toUnmodifiableList());
for (JavaField javaField : javaFields) {
JavaField builderField = builderJavaClass.getField(javaField.getName());
isValidBuilder = isValidBuilder && builderField != null && builderField.getRawType().getName().equals(javaField.getRawType().getName());
}
for (JavaMethod javaMethod : builderJavaClass.getMethods()) {
if (!BUILD_METHOD_NAME.equals(javaMethod.getName()) && !javaMethod.getName().contains(LAMBDA)) {
isValidBuilder = isValidBuilder && javaMethod.getRawReturnType().equals(builderJavaClass);
JavaField javaField = builderJavaClass.getField(javaMethod.getName());
isValidBuilder = isValidBuilder && javaField != null;
isValidBuilder = isValidBuilder && javaMethod.getRawParameterTypes().size() == 1;
}
}
// @formatter:off
long buildMethodCount = builderJavaClass.getMethods().stream().filter(method -> BUILD_METHOD_NAME.equals(method.getName())).filter(method -> javaClass.getSimpleName().equals(method.getRawReturnType().getSimpleName())).count();
// @formatter:on
isValidBuilder = isValidBuilder && buildMethodCount == 1;
// $NON-NLS-1$
String message = "The builder is valid";
if (!isValidBuilder) {
// $NON-NLS-1$
message = MessageFormat.format("The builder of the class {0} is not valid", javaClass.getSimpleName());
}
events.add(new SimpleConditionEvent(builderJavaClass, isValidBuilder, message));
}
use of com.tngtech.archunit.lang.SimpleConditionEvent in project ArchUnit by TNG.
the class SessionBeanRulesTest method haveAUniqueImplementation.
private static ArchCondition<JavaClass> haveAUniqueImplementation() {
return new ArchCondition<JavaClass>("have a unique implementation") {
@Override
public void check(JavaClass businessInterface, ConditionEvents events) {
events.add(new SimpleConditionEvent(businessInterface, businessInterface.getAllSubclasses().size() <= 1, describe(businessInterface)));
}
private String describe(JavaClass businessInterface) {
return String.format("%s is implemented by %s", businessInterface.getSimpleName(), joinNamesOf(businessInterface.getAllSubclasses()));
}
private String joinNamesOf(Set<JavaClass> implementations) {
if (implementations.isEmpty()) {
return "";
}
Deque<JavaClass> toJoin = new LinkedList<>(implementations);
StringBuilder sb = new StringBuilder(toJoin.pollFirst().getSimpleName());
for (JavaClass javaClass : toJoin) {
sb.append(", ").append(javaClass.getSimpleName());
}
return sb.toString();
}
};
}
use of com.tngtech.archunit.lang.SimpleConditionEvent in project ArchUnit by TNG.
the class GivenMembersTest method beAnnotatedWith.
static ArchCondition<JavaMember> beAnnotatedWith(final Class<? extends Annotation> annotationType) {
return new ArchCondition<JavaMember>("be annotated with @%s", annotationType.getSimpleName()) {
@Override
public void check(JavaMember member, ConditionEvents events) {
boolean satisfied = member.isAnnotatedWith(annotationType);
String message = String.format("Member '%s' %s @%s", formatMember(member), satisfied ? "is annotated with" : "is not annotated with", annotationType.getSimpleName());
events.add(new SimpleConditionEvent(member, satisfied, message));
}
};
}
Aggregations