From 0cc9e37d2f1fe567f8cfc00830aaef00b1a5f830 Mon Sep 17 00:00:00 2001 From: Stephan Pirnbaum Date: Wed, 15 Jan 2025 11:36:57 +0100 Subject: [PATCH] #488 resolve rules for effective rules API (#773) * #488 revolse rules for effective rules API * #488 fix Checkstyle violation --- .../commandline/task/AbstractTask.java | 6 +-- .../jqassistant/core/rule/api/RuleHelper.java | 40 +++++++------------ .../runtime/api/bootstrap/RuleProvider.java | 15 +++++-- .../core/test/plugin/BootstrapIT.java | 4 +- core/test/src/test/resources/.jqassistant.yml | 9 +++++ .../scm/maven/AvailableRulesMojo.java | 5 +-- .../scm/maven/EffectiveRulesMojo.java | 2 +- 7 files changed, 42 insertions(+), 39 deletions(-) diff --git a/cli/application/src/main/java/com/buschmais/jqassistant/commandline/task/AbstractTask.java b/cli/application/src/main/java/com/buschmais/jqassistant/commandline/task/AbstractTask.java index e6edd67877..c764ec8b12 100644 --- a/cli/application/src/main/java/com/buschmais/jqassistant/commandline/task/AbstractTask.java +++ b/cli/application/src/main/java/com/buschmais/jqassistant/commandline/task/AbstractTask.java @@ -11,13 +11,9 @@ import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; public abstract class AbstractTask implements Task { - private static final Logger LOGGER = LoggerFactory.getLogger(AbstractTask.class); - protected File outputDirectory; protected StoreFactory storeFactory; @@ -29,7 +25,7 @@ public final void initialize(PluginRepository pluginRepository, StoreFactory sto this.outputDirectory = new File(DEFAULT_OUTPUT_DIRECTORY); this.pluginRepository = pluginRepository; this.storeFactory = storeFactory; - this.ruleHelper = new RuleHelper(LOGGER); + this.ruleHelper = new RuleHelper(); } @Override diff --git a/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/api/RuleHelper.java b/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/api/RuleHelper.java index 395a31b5e1..fcbdfc8af3 100644 --- a/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/api/RuleHelper.java +++ b/core/rule/src/main/java/com/buschmais/jqassistant/core/rule/api/RuleHelper.java @@ -7,25 +7,15 @@ import com.buschmais.jqassistant.core.rule.api.executor.RuleSetExecutor; import com.buschmais.jqassistant.core.rule.api.model.*; -import org.slf4j.Logger; +import lombok.extern.slf4j.Slf4j; /** * Provides utility functionality for creating reports. */ +@Slf4j public class RuleHelper { public static final String LOG_LINE_PREFIX = " \""; - private final Logger logger; - - /** - * Constructor. - * - * @param log - * The logger to use for printing messages. - */ - public RuleHelper(Logger log) { - this.logger = log; - } /** * Logs the given rule set on level info. @@ -63,22 +53,22 @@ public void printRuleSet(RuleSet ruleSet, RuleSelection ruleSelection, Rule conf * The visitor. */ private void printValidRules(CollectRulesVisitor visitor) { - logger.info("Groups [" + visitor.getGroups() + log.info("Groups [" + visitor.getGroups() .size() + "]"); for (Group group : visitor.getGroups()) { - logger.info(LOG_LINE_PREFIX + group.getId() + "\""); + log.info(LOG_LINE_PREFIX + group.getId() + "\""); } - logger.info("Constraints [" + visitor.getConstraints() + log.info("Constraints [" + visitor.getConstraints() .size() + "]"); for (Constraint constraint : visitor.getConstraints() .keySet()) { - logger.info(LOG_LINE_PREFIX + constraint.getId() + "\" - " + constraint.getDescription()); + log.info(LOG_LINE_PREFIX + constraint.getId() + "\" - " + constraint.getDescription()); } - logger.info("Concepts [" + visitor.getConcepts() + log.info("Concepts [" + visitor.getConcepts() .size() + "]"); for (Concept concept : visitor.getConcepts() .keySet()) { - logger.info(LOG_LINE_PREFIX + concept.getId() + "\" - " + concept.getDescription()); + log.info(LOG_LINE_PREFIX + concept.getId() + "\" - " + concept.getDescription()); } } @@ -93,7 +83,7 @@ private void printValidRules(CollectRulesVisitor visitor) { * @throws RuleException * If the rules cannot be evaluated. */ - private CollectRulesVisitor getAllRules(RuleSet ruleSet, RuleSelection ruleSelection, Rule configuration) throws RuleException { + public CollectRulesVisitor getAllRules(RuleSet ruleSet, RuleSelection ruleSelection, Rule configuration) throws RuleException { CollectRulesVisitor visitor = new CollectRulesVisitor(); RuleSetExecutor executor = new RuleSetExecutor<>(visitor, configuration); executor.execute(ruleSet, ruleSelection); @@ -109,23 +99,23 @@ private CollectRulesVisitor getAllRules(RuleSet ruleSet, RuleSelection ruleSelec private void printMissingRules(CollectRulesVisitor visitor) { Set missingConcepts = visitor.getMissingConcepts(); if (!missingConcepts.isEmpty()) { - logger.info("Missing concepts [{}]", missingConcepts.size()); + log.info("Missing concepts [{}]", missingConcepts.size()); for (String missingConcept : missingConcepts) { - logger.warn(LOG_LINE_PREFIX + "{}", missingConcept); + log.warn(LOG_LINE_PREFIX + "{}", missingConcept); } } Set missingConstraints = visitor.getMissingConstraints(); if (!missingConstraints.isEmpty()) { - logger.info("Missing constraints [{}]", missingConstraints.size()); + log.info("Missing constraints [{}]", missingConstraints.size()); for (String missingConstraint : missingConstraints) { - logger.warn(LOG_LINE_PREFIX + "{}", missingConstraint); + log.warn(LOG_LINE_PREFIX + "{}", missingConstraint); } } Set missingGroups = visitor.getMissingGroups(); if (!missingGroups.isEmpty()) { - logger.info("Missing groups [{}]", missingGroups.size()); + log.info("Missing groups [{}]", missingGroups.size()); for (String missingGroup : missingGroups) { - logger.warn(LOG_LINE_PREFIX + "{}", missingGroup); + log.warn(LOG_LINE_PREFIX + "{}", missingGroup); } } } diff --git a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/bootstrap/RuleProvider.java b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/bootstrap/RuleProvider.java index 533cdd054a..2eb71475a0 100644 --- a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/bootstrap/RuleProvider.java +++ b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/bootstrap/RuleProvider.java @@ -7,6 +7,8 @@ import java.util.List; import com.buschmais.jqassistant.core.analysis.api.configuration.Analyze; +import com.buschmais.jqassistant.core.rule.api.RuleHelper; +import com.buschmais.jqassistant.core.rule.api.executor.CollectRulesVisitor; import com.buschmais.jqassistant.core.rule.api.model.RuleException; import com.buschmais.jqassistant.core.rule.api.model.RuleSelection; import com.buschmais.jqassistant.core.rule.api.model.RuleSet; @@ -32,7 +34,7 @@ public class RuleProvider { private final RuleSet availableRules; @Getter - private final RuleSelection effectiveRules; + private final CollectRulesVisitor effectiveRules; public static RuleProvider create(Configuration configuration, String defaultRuleDirectory, PluginRepository pluginRepository) throws RuleException { return new RuleProvider(configuration, pluginRepository, defaultRuleDirectory); @@ -78,8 +80,15 @@ private RuleSet initAvailableRules() throws RuleException { return ruleParser.parse(ruleSources); } - private RuleSelection initEffectiveRules() { + private CollectRulesVisitor initEffectiveRules() throws RuleException { Analyze analyze = this.configuration.analyze(); - return RuleSelection.select(this.availableRules, analyze.groups(), analyze.constraints(), analyze.excludeConstraints(), analyze.concepts()); + RuleSelection selection = RuleSelection.select(this.availableRules, analyze.groups(), analyze.constraints(), analyze.excludeConstraints(), analyze.concepts()); + RuleHelper ruleHelper = new RuleHelper(); + try { + return ruleHelper.getAllRules(this.availableRules, selection, configuration.analyze().rule()); + } catch (RuleException e) { + throw new RuleException(e.getMessage()); + } } + } diff --git a/core/test/src/main/java/com/buschmais/jqassistant/core/test/plugin/BootstrapIT.java b/core/test/src/main/java/com/buschmais/jqassistant/core/test/plugin/BootstrapIT.java index 9ed9b90e1b..2ab391d137 100644 --- a/core/test/src/main/java/com/buschmais/jqassistant/core/test/plugin/BootstrapIT.java +++ b/core/test/src/main/java/com/buschmais/jqassistant/core/test/plugin/BootstrapIT.java @@ -55,7 +55,9 @@ void bootstrap() throws RuleException { log.info("Rule sources: {}.", ruleProvider.getRuleSources()); log.info("Available rules: {}.", ruleProvider.getAvailableRules()); - log.info("Effective rules: {}.", ruleProvider.getEffectiveRules()); + log.info("Effective rules (Groups): {}.", ruleProvider.getEffectiveRules().getGroups()); + log.info("Effective rules (Concepts): {}.", ruleProvider.getEffectiveRules().getConcepts()); + log.info("Effective rules (Constraints): {}.", ruleProvider.getEffectiveRules().getConstraints()); } @ConfigMapping(prefix = Configuration.PREFIX) diff --git a/core/test/src/test/resources/.jqassistant.yml b/core/test/src/test/resources/.jqassistant.yml index 528f311439..45608093d0 100644 --- a/core/test/src/test/resources/.jqassistant.yml +++ b/core/test/src/test/resources/.jqassistant.yml @@ -1,4 +1,8 @@ jqassistant: + plugins: + - group-id: com.buschmais.jqassistant.plugin + artifact-id: java + version: 2.6.0-M2 store: embedded: neo4j-plugins: @@ -6,3 +10,8 @@ jqassistant: artifact-id: apoc-core classifier: core version: ${neo4j_5x.version} + analyze: + rule: + directory: ${project.basedir}/test/resources/rules + groups: + - bootstrap:Default diff --git a/maven/src/main/java/com/buschmais/jqassistant/scm/maven/AvailableRulesMojo.java b/maven/src/main/java/com/buschmais/jqassistant/scm/maven/AvailableRulesMojo.java index 70bb0fc1d2..ac1085714a 100644 --- a/maven/src/main/java/com/buschmais/jqassistant/scm/maven/AvailableRulesMojo.java +++ b/maven/src/main/java/com/buschmais/jqassistant/scm/maven/AvailableRulesMojo.java @@ -8,22 +8,19 @@ import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.LifecyclePhase; import org.apache.maven.plugins.annotations.Mojo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; /** * Lists all available rules. */ @Mojo(name = "available-rules", defaultPhase = LifecyclePhase.VALIDATE, threadSafe = true) public class AvailableRulesMojo extends AbstractRuleMojo { - private static final Logger LOGGER = LoggerFactory.getLogger(AvailableRulesMojo.class); @Override public void aggregate(MojoExecutionContext mojoExecutionContext) throws MojoExecutionException, MojoFailureException { getLog().info("Available rules for '" + mojoExecutionContext.getRootModule() .getName() + "'."); RuleSet ruleSet = readRules(mojoExecutionContext); - RuleHelper ruleHelper = new RuleHelper(LOGGER); + RuleHelper ruleHelper = new RuleHelper(); try { ruleHelper.printRuleSet(ruleSet, mojoExecutionContext.getConfiguration() .analyze() diff --git a/maven/src/main/java/com/buschmais/jqassistant/scm/maven/EffectiveRulesMojo.java b/maven/src/main/java/com/buschmais/jqassistant/scm/maven/EffectiveRulesMojo.java index a4614a7554..c2ca50fcd7 100644 --- a/maven/src/main/java/com/buschmais/jqassistant/scm/maven/EffectiveRulesMojo.java +++ b/maven/src/main/java/com/buschmais/jqassistant/scm/maven/EffectiveRulesMojo.java @@ -29,7 +29,7 @@ public void aggregate(MojoExecutionContext mojoExecutionContext) throws MojoExec .analyze(); RuleSet ruleSet = readRules(mojoExecutionContext); RuleSelection ruleSelection = RuleSelection.select(ruleSet, analyze.groups(), analyze.constraints(), analyze.excludeConstraints(), analyze.concepts()); - RuleHelper ruleHelper = new RuleHelper(LOGGER); + RuleHelper ruleHelper = new RuleHelper(); try { ruleHelper.printRuleSet(ruleSet, ruleSelection, analyze.rule()); } catch (RuleException e) {