use of com.tngtech.archunit.core.domain.JavaField in project AngularAndSpringWithMaps by Angular2Guy.
the class MyArchitectureTests method createNoFieldInjectionRule.
private static ArchRule createNoFieldInjectionRule() {
ArchCondition<JavaField> annotatedWithSpringAutowired = beAnnotatedWith("org.springframework.beans.factory.annotation.Autowired");
ArchCondition<JavaField> annotatedWithGuiceInject = beAnnotatedWith("com.google.inject.Inject");
ArchCondition<JavaField> annotatedWithJakartaInject = beAnnotatedWith("javax.inject.Inject");
ArchRule beAnnotatedWithAnInjectionAnnotation = ArchRuleDefinition.noFields().should(annotatedWithSpringAutowired.or(annotatedWithGuiceInject).or(annotatedWithJakartaInject).as("be annotated with an injection annotation"));
return beAnnotatedWithAnInjectionAnnotation;
}
use of com.tngtech.archunit.core.domain.JavaField in project arch-unit-build-plugin-core by societe-generale.
the class NoJodaTimeRuleTest method notUseJodaTime.
protected static ArchCondition<JavaClass> notUseJodaTime() {
return new ArchCondition<JavaClass>("not use Joda time ") {
@Override
public void check(JavaClass item, ConditionEvents events) {
List<JavaField> classesWithJodaTimeFields = item.getAllFields().stream().filter(this::isJodaTimeField).collect(toList());
for (JavaField field : classesWithJodaTimeFields) {
events.add(SimpleConditionEvent.violated(field, NO_JODA_VIOLATION_MESSAGE + " - class: " + field.getOwner().getName() + " - field name: " + field.getName()));
}
List<JavaMethodCall> methodsUsingJodaTimeInternally = item.getCodeUnits().stream().filter(codeUnit -> codeUnit instanceof JavaMethod).flatMap(method -> method.getMethodCallsFromSelf().stream()).filter(method -> method instanceof JavaMethodCall).filter(this::isMethodUsingJodaTimeInternally).collect(toList());
for (JavaMethodCall methodCall : methodsUsingJodaTimeInternally) {
events.add(SimpleConditionEvent.violated(methodCall.getOriginOwner(), NO_JODA_VIOLATION_MESSAGE + " - class: " + methodCall.getOriginOwner().getName() + " - method: " + methodCall.getTarget().getOwner().getSimpleName() + "." + methodCall.getTarget().getName() + " - line: " + methodCall.getLineNumber()));
}
}
private boolean isJodaTimeField(JavaField field) {
return field.getRawType().getName().startsWith(JODATIME_PACKAGE_PREFIX);
}
private boolean isMethodUsingJodaTimeInternally(JavaMethodCall javaMethodCall) {
return javaMethodCall.getTarget().getFullName().startsWith(JODATIME_PACKAGE_PREFIX);
}
};
}
use of com.tngtech.archunit.core.domain.JavaField in project MovieManager by Angular2Guy.
the class MyArchitectureTests method createNoFieldInjectionRule.
private static ArchRule createNoFieldInjectionRule() {
ArchCondition<JavaField> annotatedWithSpringAutowired = beAnnotatedWith("org.springframework.beans.factory.annotation.Autowired");
ArchCondition<JavaField> annotatedWithGuiceInject = beAnnotatedWith("com.google.inject.Inject");
ArchCondition<JavaField> annotatedWithJakartaInject = beAnnotatedWith("javax.inject.Inject");
ArchRule beAnnotatedWithAnInjectionAnnotation = ArchRuleDefinition.noFields().should(annotatedWithSpringAutowired.or(annotatedWithGuiceInject).or(annotatedWithJakartaInject).as("be annotated with an injection annotation"));
return beAnnotatedWithAnInjectionAnnotation;
}
use of com.tngtech.archunit.core.domain.JavaField 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.JavaField in project ArchUnit by TNG.
the class ClassFileImporterAccessesTest method resolve.
// only temporary to make sure resolveMember() and resolve() are in sync. Inline again when we throw out resolve()
private JavaField resolve(FieldAccessTarget fieldAccessTarget) {
JavaField field = fieldAccessTarget.resolveMember().get();
assertThat(fieldAccessTarget.resolve()).containsExactly(field);
return field;
}
Aggregations