Skip to content

Commit c590ec8

Browse files
committed
add fields().that().are[Not]{Static,Final} syntax
Signed-off-by: Manfred Hanke <[email protected]>
1 parent 85c5d06 commit c590ec8

File tree

4 files changed

+81
-4
lines changed

4 files changed

+81
-4
lines changed

archunit/src/main/java/com/tngtech/archunit/lang/syntax/MembersThatInternal.java

+20
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ public CONJUNCTION areNotPrivate() {
111111
return givenWith(SyntaxPredicates.areNotPrivate());
112112
}
113113

114+
// only applicable to fields and methods; therefore not exposed via MembersThat
115+
public CONJUNCTION areStatic() {
116+
return givenWith(SyntaxPredicates.areStatic());
117+
}
118+
119+
// only applicable to fields and methods; therefore not exposed via MembersThat
120+
public CONJUNCTION areNotStatic() {
121+
return givenWith(SyntaxPredicates.areNotStatic());
122+
}
123+
124+
// only applicable to (classes,) fields and methods; therefore not exposed via MembersThat
125+
public CONJUNCTION areFinal() {
126+
return givenWith(SyntaxPredicates.areFinal());
127+
}
128+
129+
// only applicable to (classes,) fields and methods; therefore not exposed via MembersThat
130+
public CONJUNCTION areNotFinal() {
131+
return givenWith(SyntaxPredicates.areNotFinal());
132+
}
133+
114134
@Override
115135
public CONJUNCTION haveModifier(JavaModifier modifier) {
116136
return givenWith(SyntaxPredicates.haveModifier(modifier));

archunit/src/main/java/com/tngtech/archunit/lang/syntax/SyntaxPredicates.java

+18
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@
2828
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleNameContaining;
2929
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleNameEndingWith;
3030
import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleNameStartingWith;
31+
import static com.tngtech.archunit.core.domain.JavaModifier.FINAL;
3132
import static com.tngtech.archunit.core.domain.JavaModifier.PRIVATE;
3233
import static com.tngtech.archunit.core.domain.JavaModifier.PROTECTED;
3334
import static com.tngtech.archunit.core.domain.JavaModifier.PUBLIC;
35+
import static com.tngtech.archunit.core.domain.JavaModifier.STATIC;
3436
import static com.tngtech.archunit.core.domain.properties.HasModifiers.Predicates.modifier;
3537
import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.nameMatching;
3638
import static com.tngtech.archunit.lang.conditions.ArchConditions.fullyQualifiedName;
@@ -85,6 +87,22 @@ static DescribedPredicate<HasModifiers> areNotPrivate() {
8587
return not(modifier(PRIVATE)).as("are not private");
8688
}
8789

90+
static DescribedPredicate<HasModifiers> areStatic() {
91+
return modifier(STATIC).as("are static");
92+
}
93+
94+
static DescribedPredicate<HasModifiers> areNotStatic() {
95+
return not(modifier(STATIC)).as("are not static");
96+
}
97+
98+
static DescribedPredicate<HasModifiers> areFinal() {
99+
return modifier(FINAL).as("are final");
100+
}
101+
102+
static DescribedPredicate<HasModifiers> areNotFinal() {
103+
return not(modifier(FINAL)).as("are not final");
104+
}
105+
88106
static DescribedPredicate<HasName> haveFullyQualifiedName(String name) {
89107
return have(fullyQualifiedName(name));
90108
}

archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/FieldsThat.java

+32
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,36 @@ public interface FieldsThat<CONJUNCTION extends GivenFieldsConjunction> extends
137137
*/
138138
@PublicAPI(usage = ACCESS)
139139
CONJUNCTION doNotHaveRawType(DescribedPredicate<? super JavaClass> predicate);
140+
141+
/**
142+
* Matches static fields.
143+
*
144+
* @return A syntax conjunction element, which can be completed to form a full rule
145+
*/
146+
@PublicAPI(usage = ACCESS)
147+
CONJUNCTION areStatic();
148+
149+
/**
150+
* Matches non-static fields.
151+
*
152+
* @return A syntax conjunction element, which can be completed to form a full rule
153+
*/
154+
@PublicAPI(usage = ACCESS)
155+
CONJUNCTION areNotStatic();
156+
157+
/**
158+
* Matches final fields.
159+
*
160+
* @return A syntax conjunction element, which can be completed to form a full rule
161+
*/
162+
@PublicAPI(usage = ACCESS)
163+
CONJUNCTION areFinal();
164+
165+
/**
166+
* Matches non-final fields.
167+
*
168+
* @return A syntax conjunction element, which can be completed to form a full rule
169+
*/
170+
@PublicAPI(usage = ACCESS)
171+
CONJUNCTION areNotFinal();
140172
}

archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenFieldsTest.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ public static Object[][] restricted_property_rule_starts() {
5555

5656
$(described(fields().that().doNotHaveRawType(String.class)), allFieldsExcept(FIELD_A)),
5757
$(described(fields().that().doNotHaveRawType(String.class.getName())), allFieldsExcept(FIELD_A)),
58-
$(described(fields().that().doNotHaveRawType(equivalentTo(String.class))), allFieldsExcept(FIELD_A))
58+
$(described(fields().that().doNotHaveRawType(equivalentTo(String.class))), allFieldsExcept(FIELD_A)),
59+
60+
$(described(fields().that().areFinal()), ImmutableSet.of(FIELD_A, FIELD_B)),
61+
$(described(fields().that().areNotFinal()), ImmutableSet.of(FIELD_C, FIELD_D)),
62+
$(described(fields().that().areStatic()), ImmutableSet.of(FIELD_B, FIELD_D)),
63+
$(described(fields().that().areNotStatic()), ImmutableSet.of(FIELD_A, FIELD_C)),
64+
65+
$(described(fields().that().areStatic().and().areFinal()), ImmutableSet.of(FIELD_B))
5966
);
6067
}
6168

@@ -80,11 +87,11 @@ private static Set<String> allFieldsExcept(String... fieldNames) {
8087

8188
@SuppressWarnings({"unused"})
8289
private static class ClassWithVariousMembers {
83-
private String fieldA;
90+
private final String fieldA = "A";
8491
@A
85-
protected Object fieldB;
92+
protected static final Object fieldB = 'B';
8693
public List<?> fieldC;
87-
Map<?, ?> fieldD;
94+
static Map<?, ?> fieldD;
8895
}
8996

9097
private @interface A {

0 commit comments

Comments
 (0)