use of com.tngtech.archunit.core.domain.JavaModifier.PUBLIC 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.core.domain.JavaModifier.PUBLIC 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.core.domain.JavaModifier.PUBLIC in project ArchUnit by TNG.
the class ClassFileImporterMembersTest method classes_know_which_instanceof_checks_check_their_type.
@Test
public void classes_know_which_instanceof_checks_check_their_type() {
JavaClass clazz = new ClassFileImporter().importPackagesOf(InstanceofChecked.class).get(InstanceofChecked.class);
Set<JavaClass> origins = clazz.getInstanceofChecksWithTypeOfSelf().stream().map(instanceofCheck -> instanceofCheck.getOwner().getOwner()).collect(toSet());
assertThatTypes(origins).matchInAnyOrder(ChecksInstanceofInMethod.class, ChecksInstanceofInConstructor.class, ChecksInstanceofInStaticInitializer.class);
}
use of com.tngtech.archunit.core.domain.JavaModifier.PUBLIC in project sirius-components by eclipse-sirius.
the class AbstractImmutableTests method notHaveSetters.
private ArchCondition<JavaClass> notHaveSetters() {
return new // $NON-NLS-1$
ArchCondition<>(// $NON-NLS-1$
"not have setters") {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
// @formatter:off
long settersCount = javaClass.getMethods().stream().filter(// $NON-NLS-1$
javaMethod -> javaMethod.getName().startsWith("set")).count();
// @formatter:on
boolean isConditionSatisfied = settersCount == 0;
// $NON-NLS-1$
String message = "The class does not have any setters";
if (!isConditionSatisfied) {
// $NON-NLS-1$//$NON-NLS-2$
message = "The class " + javaClass.getName() + " does have setters";
}
events.add(new SimpleConditionEvent(javaClass, isConditionSatisfied, message));
}
};
}
Aggregations