use of com.tngtech.archunit.core.domain.JavaMethod 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.JavaMethod in project flink-mirror by flink-ci.
the class Conditions method haveLeafArgumentTypes.
/**
* Tests leaf argument types of a method against the given predicate.
*
* <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
*/
public static ArchCondition<JavaMethod> haveLeafArgumentTypes(DescribedPredicate<JavaClass> typePredicate) {
return new ArchCondition<JavaMethod>("have leaf argument types" + typePredicate.getDescription()) {
@Override
public void check(JavaMethod method, ConditionEvents events) {
final List<JavaClass> leafArgumentTypes = method.getParameterTypes().stream().flatMap(argumentType -> getLeafTypes(argumentType).stream()).collect(Collectors.toList());
for (JavaClass leafType : leafArgumentTypes) {
if (!isJavaClass(leafType)) {
continue;
}
if (!typePredicate.apply(leafType)) {
final String message = String.format("%s: Argument leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
events.add(SimpleConditionEvent.violated(method, message));
}
}
}
};
}
use of com.tngtech.archunit.core.domain.JavaMethod in project flink by apache.
the class Conditions method haveLeafReturnTypes.
/**
* Tests leaf return types of a method against the given predicate.
*
* <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
*/
public static ArchCondition<JavaMethod> haveLeafReturnTypes(DescribedPredicate<JavaClass> typePredicate) {
return new ArchCondition<JavaMethod>("have leaf return types" + typePredicate.getDescription()) {
@Override
public void check(JavaMethod method, ConditionEvents events) {
for (JavaClass leafType : getLeafTypes(method.getReturnType())) {
if (!isJavaClass(leafType)) {
continue;
}
if (!typePredicate.apply(leafType)) {
final String message = String.format("%s: Returned leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
events.add(SimpleConditionEvent.violated(method, message));
}
}
}
};
}
use of com.tngtech.archunit.core.domain.JavaMethod in project flink by apache.
the class Conditions method haveLeafArgumentTypes.
/**
* Tests leaf argument types of a method against the given predicate.
*
* <p>See {@link #haveLeafTypes(DescribedPredicate)} for details.
*/
public static ArchCondition<JavaMethod> haveLeafArgumentTypes(DescribedPredicate<JavaClass> typePredicate) {
return new ArchCondition<JavaMethod>("have leaf argument types" + typePredicate.getDescription()) {
@Override
public void check(JavaMethod method, ConditionEvents events) {
final List<JavaClass> leafArgumentTypes = method.getParameterTypes().stream().flatMap(argumentType -> getLeafTypes(argumentType).stream()).collect(Collectors.toList());
for (JavaClass leafType : leafArgumentTypes) {
if (!isJavaClass(leafType)) {
continue;
}
if (!typePredicate.apply(leafType)) {
final String message = String.format("%s: Argument leaf type %s does not satisfy: %s", method.getFullName(), leafType.getName(), typePredicate.getDescription());
events.add(SimpleConditionEvent.violated(method, message));
}
}
}
};
}
use of com.tngtech.archunit.core.domain.JavaMethod in project taskana by Taskana.
the class ArchitectureTest method notUseCurrentTimestampSqlFunction.
private static ArchCondition<JavaClass> notUseCurrentTimestampSqlFunction() {
Function<JavaMethod, List<String>> getSqlStringsFromMethod = CheckedFunction.wrap((method) -> {
List<String> values = new ArrayList<>();
final Optional<Select> selectAnnotation = method.tryGetAnnotationOfType(Select.class);
final Optional<Update> updateAnnotation = method.tryGetAnnotationOfType(Update.class);
final Optional<Insert> insertAnnotation = method.tryGetAnnotationOfType(Insert.class);
final Optional<Delete> deleteAnnotation = method.tryGetAnnotationOfType(Delete.class);
final Optional<SelectProvider> selectProviderAnnotation = method.tryGetAnnotationOfType(SelectProvider.class);
final Optional<UpdateProvider> updateProviderAnnotation = method.tryGetAnnotationOfType(UpdateProvider.class);
final Optional<InsertProvider> insertProviderAnnotation = method.tryGetAnnotationOfType(InsertProvider.class);
final Optional<DeleteProvider> deleteProviderAnnotation = method.tryGetAnnotationOfType(DeleteProvider.class);
if (selectAnnotation.isPresent()) {
values.addAll(Arrays.asList(selectAnnotation.get().value()));
}
if (updateAnnotation.isPresent()) {
values.addAll(Arrays.asList(updateAnnotation.get().value()));
}
if (insertAnnotation.isPresent()) {
values.addAll(Arrays.asList(insertAnnotation.get().value()));
}
if (deleteAnnotation.isPresent()) {
values.addAll(Arrays.asList(deleteAnnotation.get().value()));
}
if (selectProviderAnnotation.isPresent()) {
values.add(executeStaticProviderMethod(selectProviderAnnotation.get().type(), selectProviderAnnotation.get().method()));
}
if (updateProviderAnnotation.isPresent()) {
values.add(executeStaticProviderMethod(updateProviderAnnotation.get().type(), updateProviderAnnotation.get().method()));
}
if (insertProviderAnnotation.isPresent()) {
values.add(executeStaticProviderMethod(insertProviderAnnotation.get().type(), insertProviderAnnotation.get().method()));
}
if (deleteProviderAnnotation.isPresent()) {
values.add(executeStaticProviderMethod(deleteProviderAnnotation.get().type(), deleteProviderAnnotation.get().method()));
}
return values;
});
return new ArchCondition<>("not use the SQL function 'CURRENT_TIMESTAMP'") {
@Override
public void check(JavaClass javaClass, ConditionEvents events) {
for (JavaMethod method : javaClass.getAllMethods()) {
List<String> sqlStrings = getSqlStringsFromMethod.apply(method);
if (sqlStrings.isEmpty() && !method.tryGetAnnotationOfType(SelectProvider.class).isPresent()) {
String message = String.format("Method '%s#%s' does not contain any MyBatis SQL annotation", javaClass.getName(), method.getName());
events.add(SimpleConditionEvent.violated(javaClass, message));
}
if (sqlStrings.stream().anyMatch(s -> s.contains("CURRENT_TIMESTAMP"))) {
String message = String.format("Method '%s#%s' uses 'CURRENT_TIMESTAMP' SQL function", javaClass.getName(), method.getName());
events.add(SimpleConditionEvent.violated(javaClass, message));
}
}
}
};
}
Aggregations