Skip to content

Commit

Permalink
add fields().that().are[Not]{Static,Final} syntax
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Hanke <Manfred.Hanke@tngtech.com>
  • Loading branch information
hankem committed Mar 29, 2019
1 parent 85c5d06 commit c590ec8
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,6 +87,22 @@ static DescribedPredicate<HasModifiers> areNotPrivate() {
return not(modifier(PRIVATE)).as("are not private");
}

static DescribedPredicate<HasModifiers> areStatic() {
return modifier(STATIC).as("are static");
}

static DescribedPredicate<HasModifiers> areNotStatic() {
return not(modifier(STATIC)).as("are not static");
}

static DescribedPredicate<HasModifiers> areFinal() {
return modifier(FINAL).as("are final");
}

static DescribedPredicate<HasModifiers> areNotFinal() {
return not(modifier(FINAL)).as("are not final");
}

static DescribedPredicate<HasName> haveFullyQualifiedName(String name) {
return have(fullyQualifiedName(name));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,36 @@ public interface FieldsThat<CONJUNCTION extends GivenFieldsConjunction> extends
*/
@PublicAPI(usage = ACCESS)
CONJUNCTION doNotHaveRawType(DescribedPredicate<? super JavaClass> 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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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))
);
}

Expand All @@ -80,11 +87,11 @@ private static Set<String> 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 {
Expand Down

0 comments on commit c590ec8

Please sign in to comment.