From c590ec8cd94f4b84eac84553863cbbb07ad969ef Mon Sep 17 00:00:00 2001 From: Manfred Hanke Date: Wed, 20 Mar 2019 18:02:35 +0100 Subject: [PATCH] add fields().that().are[Not]{Static,Final} syntax Signed-off-by: Manfred Hanke --- .../lang/syntax/MembersThatInternal.java | 20 ++++++++++++ .../lang/syntax/SyntaxPredicates.java | 18 +++++++++++ .../lang/syntax/elements/FieldsThat.java | 32 +++++++++++++++++++ .../lang/syntax/elements/GivenFieldsTest.java | 15 ++++++--- 4 files changed, 81 insertions(+), 4 deletions(-) diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/MembersThatInternal.java b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/MembersThatInternal.java index 8ec874f21c..d4a35062f7 100644 --- a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/MembersThatInternal.java +++ b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/MembersThatInternal.java @@ -111,6 +111,26 @@ public CONJUNCTION areNotPrivate() { return givenWith(SyntaxPredicates.areNotPrivate()); } + // only applicable to fields and methods; therefore not exposed via MembersThat + public CONJUNCTION areStatic() { + return givenWith(SyntaxPredicates.areStatic()); + } + + // only applicable to fields and methods; therefore not exposed via MembersThat + public CONJUNCTION areNotStatic() { + return givenWith(SyntaxPredicates.areNotStatic()); + } + + // only applicable to (classes,) fields and methods; therefore not exposed via MembersThat + public CONJUNCTION areFinal() { + return givenWith(SyntaxPredicates.areFinal()); + } + + // only applicable to (classes,) fields and methods; therefore not exposed via MembersThat + public CONJUNCTION areNotFinal() { + return givenWith(SyntaxPredicates.areNotFinal()); + } + @Override public CONJUNCTION haveModifier(JavaModifier modifier) { return givenWith(SyntaxPredicates.haveModifier(modifier)); diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/SyntaxPredicates.java b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/SyntaxPredicates.java index 260e3451b0..7852eaa482 100644 --- a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/SyntaxPredicates.java +++ b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/SyntaxPredicates.java @@ -28,9 +28,11 @@ import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleNameContaining; import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleNameEndingWith; import static com.tngtech.archunit.core.domain.JavaClass.Predicates.simpleNameStartingWith; +import static com.tngtech.archunit.core.domain.JavaModifier.FINAL; import static com.tngtech.archunit.core.domain.JavaModifier.PRIVATE; import static com.tngtech.archunit.core.domain.JavaModifier.PROTECTED; import static com.tngtech.archunit.core.domain.JavaModifier.PUBLIC; +import static com.tngtech.archunit.core.domain.JavaModifier.STATIC; import static com.tngtech.archunit.core.domain.properties.HasModifiers.Predicates.modifier; import static com.tngtech.archunit.core.domain.properties.HasName.Predicates.nameMatching; import static com.tngtech.archunit.lang.conditions.ArchConditions.fullyQualifiedName; @@ -85,6 +87,22 @@ static DescribedPredicate areNotPrivate() { return not(modifier(PRIVATE)).as("are not private"); } + static DescribedPredicate areStatic() { + return modifier(STATIC).as("are static"); + } + + static DescribedPredicate areNotStatic() { + return not(modifier(STATIC)).as("are not static"); + } + + static DescribedPredicate areFinal() { + return modifier(FINAL).as("are final"); + } + + static DescribedPredicate areNotFinal() { + return not(modifier(FINAL)).as("are not final"); + } + static DescribedPredicate haveFullyQualifiedName(String name) { return have(fullyQualifiedName(name)); } diff --git a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/FieldsThat.java b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/FieldsThat.java index 9632ca4c17..f59764430c 100644 --- a/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/FieldsThat.java +++ b/archunit/src/main/java/com/tngtech/archunit/lang/syntax/elements/FieldsThat.java @@ -137,4 +137,36 @@ public interface FieldsThat extends */ @PublicAPI(usage = ACCESS) CONJUNCTION doNotHaveRawType(DescribedPredicate predicate); + + /** + * Matches static fields. + * + * @return A syntax conjunction element, which can be completed to form a full rule + */ + @PublicAPI(usage = ACCESS) + CONJUNCTION areStatic(); + + /** + * Matches non-static fields. + * + * @return A syntax conjunction element, which can be completed to form a full rule + */ + @PublicAPI(usage = ACCESS) + CONJUNCTION areNotStatic(); + + /** + * Matches final fields. + * + * @return A syntax conjunction element, which can be completed to form a full rule + */ + @PublicAPI(usage = ACCESS) + CONJUNCTION areFinal(); + + /** + * Matches non-final fields. + * + * @return A syntax conjunction element, which can be completed to form a full rule + */ + @PublicAPI(usage = ACCESS) + CONJUNCTION areNotFinal(); } diff --git a/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenFieldsTest.java b/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenFieldsTest.java index 349876b564..cfb0537717 100644 --- a/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenFieldsTest.java +++ b/archunit/src/test/java/com/tngtech/archunit/lang/syntax/elements/GivenFieldsTest.java @@ -55,7 +55,14 @@ public static Object[][] restricted_property_rule_starts() { $(described(fields().that().doNotHaveRawType(String.class)), allFieldsExcept(FIELD_A)), $(described(fields().that().doNotHaveRawType(String.class.getName())), allFieldsExcept(FIELD_A)), - $(described(fields().that().doNotHaveRawType(equivalentTo(String.class))), allFieldsExcept(FIELD_A)) + $(described(fields().that().doNotHaveRawType(equivalentTo(String.class))), allFieldsExcept(FIELD_A)), + + $(described(fields().that().areFinal()), ImmutableSet.of(FIELD_A, FIELD_B)), + $(described(fields().that().areNotFinal()), ImmutableSet.of(FIELD_C, FIELD_D)), + $(described(fields().that().areStatic()), ImmutableSet.of(FIELD_B, FIELD_D)), + $(described(fields().that().areNotStatic()), ImmutableSet.of(FIELD_A, FIELD_C)), + + $(described(fields().that().areStatic().and().areFinal()), ImmutableSet.of(FIELD_B)) ); } @@ -80,11 +87,11 @@ private static Set allFieldsExcept(String... fieldNames) { @SuppressWarnings({"unused"}) private static class ClassWithVariousMembers { - private String fieldA; + private final String fieldA = "A"; @A - protected Object fieldB; + protected static final Object fieldB = 'B'; public List fieldC; - Map fieldD; + static Map fieldD; } private @interface A {