com.societegenerale.commons.plugin.rules.NoAutowiredFieldTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of arch-unit-build-plugin-core Show documentation
Show all versions of arch-unit-build-plugin-core Show documentation
The core logic for Maven or Gradle ArchUnit plugin
package com.societegenerale.commons.plugin.rules;
import java.util.Collection;
import com.societegenerale.commons.plugin.service.ScopePathProvider;
import com.societegenerale.commons.plugin.utils.ArchUtils;
import com.tngtech.archunit.core.domain.JavaField;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.fields;
/**
* We usually favor constructor injection rather than field injection through Spring's @Autowired : this way we can make sure the object is in correct state whenever it's used.
*/
public class NoAutowiredFieldTest implements ArchRuleTest {
protected static final String NO_AUTOWIRED_FIELD_MESSAGE = "Favor constructor injection and avoid autowiring fields - ";
@Override
public void execute(String packagePath, ScopePathProvider scopePathProvider, Collection excludedPaths) {
fields().should(notBeAutowired()).check(ArchUtils.importAllClassesInPackage(scopePathProvider.getMainClassesPath(),packagePath,excludedPaths));
}
protected static ArchCondition notBeAutowired() {
return new ArchCondition("not be autowired") {
@Override
public void check(JavaField javaField, ConditionEvents events) {
if(javaField.isAnnotatedWith("org.springframework.beans.factory.annotation.Autowired")){
events.add(SimpleConditionEvent.violated(javaField, NO_AUTOWIRED_FIELD_MESSAGE
+" - class: "+javaField.getOwner().getName()
+" - field name: "+javaField.getName()));
}
}
};
}
}