use of com.tngtech.archunit.core.domain.JavaClass 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.core.domain.JavaClass 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.JavaClass 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.JavaClass in project sirius-components by eclipse-sirius.
the class AbstractServicesTests method nonAuditedRepositoryMethod.
private DescribedPredicate<JavaMethodCall> nonAuditedRepositoryMethod() {
return new // $NON-NLS-1$
DescribedPredicate<>(// $NON-NLS-1$
"the repository method called has not been audited") {
@Override
public boolean apply(JavaMethodCall javaMethodCall) {
JavaClass targetJavaClass = javaMethodCall.getTargetOwner();
String fullName = targetJavaClass.getFullName();
// $NON-NLS-1$
boolean isRepository = fullName.endsWith("Repository");
boolean hasBeenAudited = javaMethodCall.getTarget().isAnnotatedWith(Audited.class);
return isRepository && !hasBeenAudited;
}
};
}
use of com.tngtech.archunit.core.domain.JavaClass in project ArchUnit by TNG.
the class ClassFileImporterTest method imports_urls_of_files.
@Test
public void imports_urls_of_files() {
Set<URL> urls = newHashSet(urlOf(ClassToImportOne.class), urlOf(ClassWithNestedClass.class));
Set<JavaClass> classesFoundAtUrls = new HashSet<>();
for (JavaClass javaClass : new ClassFileImporter().importUrls(urls)) {
if (!Object.class.getName().equals(javaClass.getName())) {
classesFoundAtUrls.add(javaClass);
}
}
assertThat(classesFoundAtUrls).as("Number of classes at the given URLs").hasSize(2);
}
Aggregations