Class GeneralCodingRules

java.lang.Object
com.tngtech.archunit.library.GeneralCodingRules

public final class GeneralCodingRules extends Object
GeneralCodingRules provides a set of very general ArchConditions and ArchRules for coding that might be useful in various projects.

When checking these rules, it is always important to remember that all necessary classes need to be imported. E.g. if ACCESS_STANDARD_STREAMS is checked, which looks for calls on classes assignable to Throwable, it is important to ensure that Exception and RuntimeException are imported as well, otherwise ArchUnit does not know about the inheritance structure of these exceptions, and thus will not consider a call of RuntimeException a violation, since it does not know that RuntimeException extends Throwable. For further information refer to ClassFileImporter.

  • Field Details

    • ACCESS_STANDARD_STREAMS

      @PublicAPI(usage=ACCESS) public static final ArchCondition<JavaClass> ACCESS_STANDARD_STREAMS
      A condition that matches classes that access System.out or System.err.

      Example:

      
       System.out.println("foo"); // matches
       System.err.println("bar"); // matches
      
       OutputStream out = System.out; // matches
       out.write(bytes);
      
       try {
           // ...
       } catch (Exception e) {
           e.printStackTrace(); // matches
       }
       

      For information about checking this condition, refer to GeneralCodingRules.

      See Also:
    • NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS

      @PublicAPI(usage=ACCESS) public static final ArchRule NO_CLASSES_SHOULD_ACCESS_STANDARD_STREAMS
      A rule that checks that none of the given classes access the standard streams System.out and System.err.

      It is generally good practice to use correct logging instead of writing to the console.

      • Writing to the console cannot be configured in production
      • Writing to the console is synchronized and can lead to bottle necks

      Example:

      
       System.out.println("foo"); // violation
       System.err.println("bar"); // violation
      
       OutputStream out = System.out; // violation
       out.write(bytes);
      
       try {
           // ...
       } catch (Exception e) {
           e.printStackTrace(); // violation
       }
       

      For information about checking this rule, refer to GeneralCodingRules.

      See Also:
    • THROW_GENERIC_EXCEPTIONS

      @PublicAPI(usage=ACCESS) public static final ArchCondition<JavaClass> THROW_GENERIC_EXCEPTIONS
      A condition that matches classes that throw generic exceptions like Exception, RuntimeException, or Throwable. More precisely, the condition matches when a constructor of the mentioned classes is called.

      Example:

      
       throw new Exception(); // matches
       throw new RuntimeException("error"); // matches
       throw new Throwable("error"); // matches
      
       class CustomException extends Throwable { // matches
       }
      
       class CustomException extends Exception {
           CustomException() {
               super("error"); // does not match
           }
       }
       

      For information about checking this condition, refer to GeneralCodingRules.

      See Also:
    • NO_CLASSES_SHOULD_THROW_GENERIC_EXCEPTIONS

      @PublicAPI(usage=ACCESS) public static final ArchRule NO_CLASSES_SHOULD_THROW_GENERIC_EXCEPTIONS
      A rule that checks that none of the given classes throw generic exceptions like Exception, RuntimeException, or Throwable. More precisely, the rule reports a violation when a constructor of the mentioned classes is called.

      It is generally good practice to throw specific exceptions like IllegalArgumentException or custom exceptions, instead of throwing generic exceptions like RuntimeException.

      Example:

      
       throw new Exception(); // violation
       throw new RuntimeException("error"); // violation
       throw new Throwable("error"); // violation
      
       class CustomException extends Throwable { // violation
       }
      
       class CustomException extends Exception {
           CustomException() {
               super("error"); // no violation
           }
       }
       

      For information about checking this rule, refer to GeneralCodingRules.

      See Also:
    • USE_JAVA_UTIL_LOGGING

      @PublicAPI(usage=ACCESS) public static final ArchCondition<JavaClass> USE_JAVA_UTIL_LOGGING
      A condition that matches classes that access Java Util Logging.

      Example:

      
       import java.util.logging.Logger;
      
       Logger logger = Logger.getLogger("Example"); // matches
       

      For information about checking this condition, refer to GeneralCodingRules.

      See Also:
    • NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING

      @PublicAPI(usage=ACCESS) public static final ArchRule NO_CLASSES_SHOULD_USE_JAVA_UTIL_LOGGING
      A rule that checks that none of the given classes access Java Util Logging.

      Most projects use the more powerful LOG4J or Logback instead of java.util.logging, often hidden behind SLF4J. In this case it's important to ensure consistent use of the agreed logging framework.

      Example:

      
       import java.util.logging.Logger;
      
       Logger logger = Logger.getLogger("Example"); // violation
       

      For information about checking this rule, refer to GeneralCodingRules.

      See Also:
    • USE_JODATIME

      @PublicAPI(usage=ACCESS) public static final ArchCondition<JavaClass> USE_JODATIME
      A condition that matches classes that access JodaTime.

      Example:

      
       import org.joda.time.DateTime;
      
       DateTime now = DateTime.now(); // matches
       

      For information about checking this condition, refer to GeneralCodingRules.

      See Also:
    • NO_CLASSES_SHOULD_USE_JODATIME

      @PublicAPI(usage=ACCESS) public static final ArchRule NO_CLASSES_SHOULD_USE_JODATIME
      A rule that checks that none of the given classes access JodaTime.

      Modern Java projects use the [java.time] API instead of the JodaTime library.

      Example:

      
       import org.joda.time.DateTime;
      
       DateTime now = DateTime.now(); // violation
       

      For information about checking this rule, refer to GeneralCodingRules.

      See Also:
    • BE_ANNOTATED_WITH_AN_INJECTION_ANNOTATION

      @PublicAPI(usage=ACCESS) public static final ArchCondition<JavaField> BE_ANNOTATED_WITH_AN_INJECTION_ANNOTATION
      A condition that matches fields that have an annotation for injection.

      Example:

      
       class Example {
      
           @Resource
           DataSource dataSource; // matches
      
           @Inject
           File configFile; // matches
      
           @Autowired
           CustomerService customerService; // matches
       }
       

      For information about checking this condition, refer to GeneralCodingRules.

      See Also:
    • NO_CLASSES_SHOULD_USE_FIELD_INJECTION

      @PublicAPI(usage=ACCESS) public static final ArchRule NO_CLASSES_SHOULD_USE_FIELD_INJECTION
      A rule that checks that none of the given classes uses field injection.

      Field injection is seen as an anti-pattern. It is a good practice to use constructor injection for mandatory dependencies and setter injection for optional dependencies.

      Example:

      
       class Example {
      
           @Resource
           DataSource dataSource; // violation
      
           @Inject
           File configFile; // violation
      
           @Autowired
           CustomerService customerService; // violation
       }
       

      For information about checking this rule, refer to GeneralCodingRules.

      See Also: