0
# Source Predicates
1
2
Source-based predicates for filtering and validating Java classes by their source file characteristics. Essential for mixed Java/Scala projects where ArchUnit rules need to operate exclusively on Java classes.
3
4
## Capabilities
5
6
### Java Class Filtering
7
8
Tests whether a given class is actually a Java class (not Scala or other JVM languages).
9
10
```java { .api }
11
/**
12
* Tests that a given class is a Java class, not Scala or other JVM language
13
* @return DescribedPredicate that returns true if class is a Java class
14
*/
15
public static DescribedPredicate<JavaClass> areJavaClasses();
16
17
/**
18
* Checks whether the given JavaClass is actually a Java class, not Scala
19
* @param clazz The JavaClass to test
20
* @return true if the class is a Java class, false otherwise
21
*/
22
static boolean isJavaClass(JavaClass clazz);
23
```
24
25
**Usage Examples:**
26
27
```java
28
import static org.apache.flink.architecture.common.SourcePredicates.areJavaClasses;
29
import static org.apache.flink.architecture.common.SourcePredicates.isJavaClass;
30
31
// Filter classes to only include Java classes
32
ArchRule javaOnlyRule = classes()
33
.that(areJavaClasses())
34
.should()
35
.notBeInterfaces();
36
37
// Manual checking in custom predicates
38
DescribedPredicate<JavaClass> customJavaClassCheck =
39
DescribedPredicate.describe("are custom Java classes", clazz -> {
40
return isJavaClass(clazz) && clazz.getSimpleName().startsWith("Flink");
41
});
42
```
43
44
**Integration with Other Components:**
45
46
```java
47
// Used internally by GivenJavaClasses
48
ClassesThat<GivenClassesConjunction> javaClasses =
49
GivenJavaClasses.javaClassesThat(); // Uses areJavaClasses() internally
50
51
// Used in Conditions for type filtering
52
ArchCondition<JavaMethod> leafTypeCondition =
53
Conditions.haveLeafTypes(areJavaClasses().and(other -> other.isTopLevel()));
54
```
55
56
## Design Rationale
57
58
**Mixed Language Support**: ArchUnit doesn't fully support Scala yet, making these predicates essential for projects that contain both Java and Scala code. Without proper filtering, architecture rules may fail or produce incorrect results when encountering Scala classes.
59
60
**Source File Detection**: The implementation checks for `.java` extension in the source file name, providing a reliable heuristic for distinguishing Java classes from Scala classes (which typically have `.scala` extensions).
61
62
**Performance Considerations**: The predicates perform minimal checks on the JavaClass source information, making them suitable for use in large-scale architecture validation where performance matters.
63
64
**Defensive Programming**: Both methods handle edge cases where source information might not be available, returning false when the source file cannot be determined rather than throwing exceptions.