CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-flink--flink-architecture-tests-base

Base library for Apache Flink architecture tests that provides common ArchUnit extensions and utilities for validating architectural constraints in both production and test code

Pending
Overview
Eval results
Files

general-predicates.mddocs/

General Predicates

Core predicates for analyzing Java classes and fields, providing powerful capabilities for annotation checking, field property validation, and class hierarchy inspection.

Capabilities

Annotation Analysis

Tests if a class is directly annotated with at least one of the specified annotations.

/**
 * Tests if a class is directly annotated with at least one of the given annotations
 * @param annotations Variable number of annotation classes to check for
 * @return DescribedPredicate that returns true if class has at least one of the annotations
 */
@SafeVarargs
public static DescribedPredicate<JavaClass> areDirectlyAnnotatedWithAtLeastOneOf(
    Class<? extends Annotation>... annotations);

Usage Example:

import org.apache.flink.architecture.common.Predicates;
import javax.annotation.Component;
import javax.annotation.Service;

// Check if classes are annotated with Component or Service
DescribedPredicate<JavaClass> isComponentOrService = 
    Predicates.areDirectlyAnnotatedWithAtLeastOneOf(Component.class, Service.class);

ArchRule rule = GivenJavaClasses.javaClassesThat(isComponentOrService)
    .should()
    .haveSimpleNameNotContaining("Impl");

Field Hierarchy Analysis

Tests if any fields in the class hierarchy (including inherited fields) match the given predicate.

/**
 * Tests if any fields in the class hierarchy match the given predicate
 * @param predicate Predicate to test against fields in the class hierarchy
 * @return DescribedPredicate that returns true if any field in hierarchy matches
 */
public static DescribedPredicate<JavaClass> containAnyFieldsInClassHierarchyThat(
    DescribedPredicate<? super JavaField> predicate);

Usage Example:

import static org.apache.flink.architecture.common.Predicates.containAnyFieldsInClassHierarchyThat;
import static org.apache.flink.architecture.common.JavaFieldPredicates.isStatic;
import static org.apache.flink.architecture.common.JavaFieldPredicates.ofType;

// Check if classes contain static Logger fields
DescribedPredicate<JavaClass> hasStaticLogger = containAnyFieldsInClassHierarchyThat(
    isStatic().and(ofType("org.slf4j.Logger"))
);

ArchRule loggerRule = GivenJavaClasses.javaClassesThat(hasStaticLogger)
    .should()
    .haveSimpleNameNotEndingWith("Test");

Field Type and Modifier Validation

Tests field properties including visibility, mutability, and type constraints.

/**
 * Tests that field is public static and has the specified fully qualified type
 * @param fqClassName Fully qualified class name to match
 * @return DescribedPredicate for public static fields of the specified type
 */
public static DescribedPredicate<JavaField> arePublicStaticOfType(String fqClassName);

/**
 * Tests that field has the specified type and modifiers
 * @param fqClassName Fully qualified class name to match
 * @param modifiers Variable number of JavaModifier values (PUBLIC, STATIC, FINAL, etc.)
 * @return DescribedPredicate for fields matching type and modifiers
 */
public static DescribedPredicate<JavaField> areFieldOfType(
    String fqClassName, JavaModifier... modifiers);

/**
 * Tests that field is public final (non-static) of the specified type
 * @param fqClassName Fully qualified class name to match
 * @return DescribedPredicate for public final instance fields
 */
public static DescribedPredicate<JavaField> arePublicFinalOfType(String fqClassName);

/**
 * Tests that field is public static final and assignable to the given class
 * @param clazz Class type to check assignability against
 * @return DescribedPredicate for public static final fields assignable to clazz
 */
public static DescribedPredicate<JavaField> arePublicStaticFinalAssignableTo(Class<?> clazz);

/**
 * Tests that field is public static final of the specified type
 * @param fqClassName Fully qualified class name to match
 * @return DescribedPredicate for public static final fields of specified type
 */
public static DescribedPredicate<JavaField> arePublicStaticFinalOfType(String fqClassName);

Usage Examples:

// Check for public static String constants
DescribedPredicate<JavaField> isPublicStaticString = 
    Predicates.arePublicStaticOfType("java.lang.String");

// Check for public final configuration fields
DescribedPredicate<JavaField> isConfigField = 
    Predicates.arePublicFinalOfType("org.apache.flink.configuration.Configuration");

// Check for constants assignable to Number
DescribedPredicate<JavaField> isNumericConstant = 
    Predicates.arePublicStaticFinalAssignableTo(Number.class);

Annotated Field Validation

Tests field properties combined with annotation requirements.

/**
 * Tests that field is public final of specified type with given annotation
 * @param fqClassName Fully qualified class name to match
 * @param annotationType Annotation class that must be present
 * @return DescribedPredicate for annotated public final fields
 */
public static DescribedPredicate<JavaField> arePublicFinalOfTypeWithAnnotation(
    String fqClassName, Class<? extends Annotation> annotationType);

/**
 * Tests that field is public static final of specified type with given annotation
 * @param fqClassName Fully qualified class name to match
 * @param annotationType Annotation class that must be present
 * @return DescribedPredicate for annotated public static final fields
 */
public static DescribedPredicate<JavaField> arePublicStaticFinalOfTypeWithAnnotation(
    String fqClassName, Class<? extends Annotation> annotationType);

/**
 * Tests that field is static final of specified type with given annotation (any visibility)
 * @param fqClassName Fully qualified class name to match
 * @param annotationType Annotation class that must be present
 * @return DescribedPredicate for annotated static final fields
 */
public static DescribedPredicate<JavaField> areStaticFinalOfTypeWithAnnotation(
    String fqClassName, Class<? extends Annotation> annotationType);

Predicate Logic

Utility method for creating exclusive-or logic between multiple predicates.

/**
 * Tests that exactly one of the given predicates matches
 * @param other Variable number of predicates to test
 * @return DescribedPredicate that returns true if exactly one predicate matches
 */
@SafeVarargs
public static <T> DescribedPredicate<T> exactlyOneOf(
    final DescribedPredicate<? super T>... other);

Usage Example:

// Ensure class has exactly one of these field types
DescribedPredicate<JavaClass> hasExactlyOneConfigType = containAnyFieldsInClassHierarchyThat(
    Predicates.exactlyOneOf(
        ofType("org.apache.flink.configuration.Configuration"),
        ofType("java.util.Properties"),
        ofType("java.util.Map")
    )
);

Utility Methods

Helper method for extracting simple class names from fully qualified names.

/**
 * Extracts the simple class name from a fully qualified class name
 * @param fqClassName Fully qualified class name (e.g., "com.example.MyClass")
 * @return Simple class name (e.g., "MyClass")
 * @throws NullPointerException if fqClassName is null
 * @throws IllegalArgumentException if fqClassName is empty or invalid
 */
public static String getClassSimpleNameFromFqName(String fqClassName);

Usage Example:

String simpleName = Predicates.getClassSimpleNameFromFqName("org.apache.flink.streaming.api.StreamExecutionEnvironment");
// Returns: "StreamExecutionEnvironment"

String innerClassName = Predicates.getClassSimpleNameFromFqName("org.apache.flink.api.common.ExecutionConfig$GlobalJobParameters");
// Returns: "GlobalJobParameters"

Design Principles

  • Fully Qualified Names: All methods accepting class names recommend using fully qualified names instead of Class objects to avoid circular dependencies between project modules
  • Composability: All predicates are designed to be combined using .and(), .or(), and .negate() for complex rules
  • Type Safety: Strong typing ensures compile-time validation of predicate combinations
  • Performance: Efficient implementations suitable for analyzing large codebases

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-flink--flink-architecture-tests-base

docs

field-analysis.md

general-predicates.md

import-control.md

index.md

java-only-rules.md

method-validation.md

source-predicates.md

tile.json