From 52a266e7464d87ab4ff1b5f3f473f9d5d1a4e21e Mon Sep 17 00:00:00 2001 From: mnhock Date: Thu, 20 Jun 2024 08:09:22 +0200 Subject: [PATCH] Ensures that there is at least one character before default matching --- .../java/com/enofex/taikai/internal/Modifiers.java | 10 ++++++++++ .../java/com/enofex/taikai/java/ConstantNaming.java | 4 +++- .../enofex/taikai/spring/ConfigurationsConfigurer.java | 2 +- .../enofex/taikai/spring/ControllersConfigurer.java | 2 +- .../enofex/taikai/spring/RepositoriesConfigurer.java | 2 +- .../com/enofex/taikai/spring/ServicesConfigurer.java | 2 +- .../java/com/enofex/taikai/test/JUnit5Configurer.java | 5 ++++- 7 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/enofex/taikai/internal/Modifiers.java b/src/main/java/com/enofex/taikai/internal/Modifiers.java index ffbc2e7..197de66 100644 --- a/src/main/java/com/enofex/taikai/internal/Modifiers.java +++ b/src/main/java/com/enofex/taikai/internal/Modifiers.java @@ -96,4 +96,14 @@ public static boolean isFieldProtected(JavaField field) { public static boolean isFieldFinal(JavaField field) { return field.getModifiers().contains(JavaModifier.FINAL); } + + /** + * Checks if a field is synthetic. + * + * @param field the Java field to check + * @return true if the field is synthetic, false otherwise + */ + public static boolean isFieldSynthetic(JavaField field) { + return field.getModifiers().contains(JavaModifier.SYNTHETIC); + } } diff --git a/src/main/java/com/enofex/taikai/java/ConstantNaming.java b/src/main/java/com/enofex/taikai/java/ConstantNaming.java index 2cc243c..b9f338b 100644 --- a/src/main/java/com/enofex/taikai/java/ConstantNaming.java +++ b/src/main/java/com/enofex/taikai/java/ConstantNaming.java @@ -1,5 +1,7 @@ package com.enofex.taikai.java; +import static com.enofex.taikai.internal.Modifiers.isFieldSynthetic; + import com.tngtech.archunit.core.domain.JavaField; import com.tngtech.archunit.lang.ArchCondition; import com.tngtech.archunit.lang.ConditionEvents; @@ -17,7 +19,7 @@ static ArchCondition shouldFollowConstantNamingConvention() { return new ArchCondition<>("follow constant naming convention") { @Override public void check(JavaField field, ConditionEvents events) { - if (!field.getOwner().isEnum() + if (!isFieldSynthetic(field) && !"serialVersionUID".equals(field.getName()) && !CONSTANT_NAME_PATTERN.matcher(field.getName()).matches()) { events.add(SimpleConditionEvent.violated(field, diff --git a/src/main/java/com/enofex/taikai/spring/ConfigurationsConfigurer.java b/src/main/java/com/enofex/taikai/spring/ConfigurationsConfigurer.java index 5a5f6a8..a6441c7 100644 --- a/src/main/java/com/enofex/taikai/spring/ConfigurationsConfigurer.java +++ b/src/main/java/com/enofex/taikai/spring/ConfigurationsConfigurer.java @@ -13,7 +13,7 @@ public final class ConfigurationsConfigurer extends AbstractConfigurer { - private static final String DEFAULT_CONFIGURATION_NAME_MATCHING = ".*Configuration"; + private static final String DEFAULT_CONFIGURATION_NAME_MATCHING = ".+Configuration"; ConfigurationsConfigurer(ConfigurerContext configurerContext) { super(configurerContext); diff --git a/src/main/java/com/enofex/taikai/spring/ControllersConfigurer.java b/src/main/java/com/enofex/taikai/spring/ControllersConfigurer.java index 52282e0..75e5004 100644 --- a/src/main/java/com/enofex/taikai/spring/ControllersConfigurer.java +++ b/src/main/java/com/enofex/taikai/spring/ControllersConfigurer.java @@ -18,7 +18,7 @@ public final class ControllersConfigurer extends AbstractConfigurer { - private static final String DEFAULT_CONTROLLER_NAME_MATCHING = ".*Controller"; + private static final String DEFAULT_CONTROLLER_NAME_MATCHING = ".+Controller"; ControllersConfigurer(ConfigurerContext configurerContext) { super(configurerContext); diff --git a/src/main/java/com/enofex/taikai/spring/RepositoriesConfigurer.java b/src/main/java/com/enofex/taikai/spring/RepositoriesConfigurer.java index 0f60276..f490a08 100644 --- a/src/main/java/com/enofex/taikai/spring/RepositoriesConfigurer.java +++ b/src/main/java/com/enofex/taikai/spring/RepositoriesConfigurer.java @@ -16,7 +16,7 @@ public final class RepositoriesConfigurer extends AbstractConfigurer { - private static final String DEFAULT_REPOSITORY_NAME_MATCHING = ".*Repository"; + private static final String DEFAULT_REPOSITORY_NAME_MATCHING = ".+Repository"; RepositoriesConfigurer(ConfigurerContext configurerContext) { super(configurerContext); diff --git a/src/main/java/com/enofex/taikai/spring/ServicesConfigurer.java b/src/main/java/com/enofex/taikai/spring/ServicesConfigurer.java index 820397a..7945980 100644 --- a/src/main/java/com/enofex/taikai/spring/ServicesConfigurer.java +++ b/src/main/java/com/enofex/taikai/spring/ServicesConfigurer.java @@ -16,7 +16,7 @@ public final class ServicesConfigurer extends AbstractConfigurer { - private static final String DEFAULT_SERVICE_NAME_MATCHING = ".*Service"; + private static final String DEFAULT_SERVICE_NAME_MATCHING = ".+Service"; ServicesConfigurer(ConfigurerContext configurerContext) { super(configurerContext); diff --git a/src/main/java/com/enofex/taikai/test/JUnit5Configurer.java b/src/main/java/com/enofex/taikai/test/JUnit5Configurer.java index cffcd2e..1cd843e 100644 --- a/src/main/java/com/enofex/taikai/test/JUnit5Configurer.java +++ b/src/main/java/com/enofex/taikai/test/JUnit5Configurer.java @@ -6,6 +6,9 @@ import static com.enofex.taikai.test.JUnit5DescribedPredicates.ANNOTATION_PARAMETRIZED_TEST; import static com.enofex.taikai.test.JUnit5DescribedPredicates.ANNOTATION_TEST; import static com.enofex.taikai.test.JUnit5DescribedPredicates.annotatedWithTestOrParameterizedTest; +import static com.tngtech.archunit.base.DescribedPredicate.not; +import static com.tngtech.archunit.lang.conditions.ArchConditions.beInterfaces; + import static com.tngtech.archunit.lang.conditions.ArchPredicates.are; import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes; import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.methods; @@ -45,7 +48,7 @@ public JUnit5Configurer classesShouldBePackagePrivate(String regex) { public JUnit5Configurer classesShouldBePackagePrivate(String regex, Configuration configuration) { return addRule(TaikaiRule.of(classes() - .that().haveNameMatching(regex) + .that().areNotInterfaces().and().haveNameMatching(regex) .should().bePackagePrivate() .as("Classes with names matching %s should be package-private".formatted(regex)), configuration));