From bcbbfef58c99352cfd247bdd397c5a0dd9a03dc9 Mon Sep 17 00:00:00 2001 From: purnhar Date: Sun, 28 Jan 2024 23:56:16 +0100 Subject: [PATCH] add configuration switch to disable fallback to root directory (#87) Co-authored-by: Ralf Purnhagen --- .../commons/plugin/service/RuleInvokerService.java | 12 +++++++++++- .../commons/plugin/utils/ArchUtils.java | 8 ++++++-- .../commons/plugin/rules/ArchUtilsTest.java | 10 ++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/societegenerale/commons/plugin/service/RuleInvokerService.java b/src/main/java/com/societegenerale/commons/plugin/service/RuleInvokerService.java index 31f0035..76e2cc8 100644 --- a/src/main/java/com/societegenerale/commons/plugin/service/RuleInvokerService.java +++ b/src/main/java/com/societegenerale/commons/plugin/service/RuleInvokerService.java @@ -29,6 +29,8 @@ public class RuleInvokerService { private Collection excludedPaths= emptySet(); + private boolean fallbackToRootDirectory = true; + public RuleInvokerService(Log log) { this.log=log; archUtils =new ArchUtils(log); @@ -49,6 +51,14 @@ public RuleInvokerService(Log log, ScopePathProvider scopePathProvider,Collectio this.excludedPaths=new ExcludedPathsPreProcessor().processExcludedPaths(log, projectBuildDir, excludedPaths); } + public RuleInvokerService(Log log, ScopePathProvider scopePathProvider,Collection excludedPaths, String projectBuildDir, boolean fallbackToRootDirectory) { + this.log=log; + archUtils =new ArchUtils(log); + + this.scopePathProvider=scopePathProvider; + this.excludedPaths=new ExcludedPathsPreProcessor().processExcludedPaths(log, projectBuildDir, excludedPaths); + this.fallbackToRootDirectory = fallbackToRootDirectory; + } public String invokeRules(Rules rules) throws InvocationTargetException, InstantiationException, IllegalAccessException { @@ -107,7 +117,7 @@ private String invokeConfigurableRules(ConfigurableRule rule) { String fullPathFromRootTopackage = getPackageNameOnWhichToApplyRules(rule); log.info("invoking ConfigurableRule "+rule.toString()+" on "+fullPathFromRootTopackage); - JavaClasses classes = archUtils.importAllClassesInPackage(new RootClassFolder(""), fullPathFromRootTopackage,excludedPaths); + JavaClasses classes = archUtils.importAllClassesInPackage(new RootClassFolder(""), fullPathFromRootTopackage, excludedPaths, fallbackToRootDirectory); InvocationResult result = invokableRules.invokeOn(classes); return result.getMessage(); diff --git a/src/main/java/com/societegenerale/commons/plugin/utils/ArchUtils.java b/src/main/java/com/societegenerale/commons/plugin/utils/ArchUtils.java index 6715890..6881187 100644 --- a/src/main/java/com/societegenerale/commons/plugin/utils/ArchUtils.java +++ b/src/main/java/com/societegenerale/commons/plugin/utils/ArchUtils.java @@ -24,10 +24,14 @@ public ArchUtils(Log log) { } public static JavaClasses importAllClassesInPackage(RootClassFolder rootClassFolder, String packagePath){ - return importAllClassesInPackage(rootClassFolder, packagePath,emptyList()); + return importAllClassesInPackage(rootClassFolder, packagePath,emptyList(), true); } public static JavaClasses importAllClassesInPackage(RootClassFolder rootClassFolder, String packagePath, Collection excludedPaths) { + return importAllClassesInPackage(rootClassFolder, packagePath, excludedPaths, true); + } + + public static JavaClasses importAllClassesInPackage(RootClassFolder rootClassFolder, String packagePath, Collection excludedPaths, boolean fallbackToRootDirectory) { //not great design, but since all the rules need to call this, it's very convenient to keep this method static if(log==null){ @@ -36,7 +40,7 @@ public static JavaClasses importAllClassesInPackage(RootClassFolder rootClassFol Path classesPath = Paths.get(rootClassFolder.getValue() + packagePath); - if (!classesPath.toFile().exists()) { + if (fallbackToRootDirectory && !classesPath.toFile().exists()) { StringBuilder warnMessage=new StringBuilder("Classpath ").append(classesPath.toFile()) .append(" doesn't exist : loading all classes from root, ie ") .append(rootClassFolder.getValue()) diff --git a/src/test/java/com/societegenerale/commons/plugin/rules/ArchUtilsTest.java b/src/test/java/com/societegenerale/commons/plugin/rules/ArchUtilsTest.java index a136ee1..04b5446 100644 --- a/src/test/java/com/societegenerale/commons/plugin/rules/ArchUtilsTest.java +++ b/src/test/java/com/societegenerale/commons/plugin/rules/ArchUtilsTest.java @@ -1,6 +1,7 @@ package com.societegenerale.commons.plugin.rules; import java.util.Arrays; +import java.util.Collections; import com.societegenerale.commons.plugin.SilentLog; import com.societegenerale.commons.plugin.model.RootClassFolder; @@ -39,6 +40,15 @@ public void shouldLoadAllClassesWhenGivenPakageDoesntExist() { assertThat(noOfClasses).isEqualTo(36); } + @Test + public void shouldNotLoadAllClassesWhenFallbackIsDisabled() { + JavaClasses classes = ArchUtils.importAllClassesInPackage(new RootClassFolder("./target/classes"), "someNotExistingFolder", Collections.emptyList(), false); + + long noOfClasses = classes.stream().filter(it -> !it.isNestedClass()).count(); + + assertThat(noOfClasses).isZero(); + } + @Test public void shouldIgnoreClassesFromConfiguredPaths() {