Refactor the rule loading mechanism to use ServiceLoader Pattern#67
Closed
nureka-rodrigo wants to merge 3 commits intoballerina-platform:mainfrom
nureka-rodrigo:test
Closed
Refactor the rule loading mechanism to use ServiceLoader Pattern#67nureka-rodrigo wants to merge 3 commits intoballerina-platform:mainfrom nureka-rodrigo:test
ServiceLoader Pattern#67nureka-rodrigo wants to merge 3 commits intoballerina-platform:mainfrom
nureka-rodrigo:test
Conversation
MaryamZi
reviewed
Jun 4, 2025
5 tasks
MaryamZi
reviewed
Jun 4, 2025
ServiceLoader Pattern
There was a problem hiding this comment.
Pull Request Overview
Refactors the rule loading mechanism to use Java’s ServiceLoader instead of parsing a rules.json file.
- Introduces a
RuleProviderSPI and updatesmodule-info.javatousesit - Removes Gson-based JSON loading and adds
loadRulesFromEnumto pull rules viaServiceLoader - Updates
ProjectAnalyzerto collect and wrap external rules dynamically
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| scan-command/src/main/java/module-info.java | Declares uses io.ballerina.scan.RuleProvider for service loading |
| ProjectAnalyzer.java | Replaces JSON parsing with ServiceLoader<RuleProvider> logic |
| scan-command/src/main/java/io/ballerina/scan/RuleProvider.java | Adds the RuleProvider service interface |
Comments suppressed due to low confidence (4)
scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java:138
- Using Optional.toString() as the map key yields an unclear key (e.g. "Optional.empty"). Consider using a consistent plugin identifier, such as pluginDesc.plugin().getClassName() or org + "/" + name.
externalAnalyzers.put(pkgDependency.manifest().compilerPluginDescriptor().toString(), new ArrayList<>());
scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java:157
- [nitpick] Method name
loadRulesFromEnumis misleading since rules are loaded via ServiceLoader, not strictly from enums. Consider renaming toloadRulesFromProvidersorloadRulesWithServiceLoader.
private List<Rule> loadRulesFromEnum(CompilerPluginDescriptor pluginDesc, String org, String name)
scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java:126
- The new ServiceLoader path in
getExternalAnalyzerslacks direct unit tests. Consider adding tests that validate rule discovery from dummyRuleProviderimplementations.
Map<String, List<Rule>> getExternalAnalyzers() {
scan-command/src/main/java/io/ballerina/scan/RuleProvider.java:28
- The interface Javadoc should mention the requirement for a public no-argument constructor on implementations to support
ServiceLoaderinstantiation.
public interface RuleProvider {
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Purpose
Fixes #66
Approach
Replaced the
JSON-based rule loading mechanism with Java'sServiceLoaderpattern. Before the rules were loaded by parsing arules.jsonfile from the pluginJARusingGson. Rules are now loaded usingServiceLoader<RuleProvider>, where external plugins implement theRuleProviderinterface.Limitation
Migration Guide for Compiler Plugin Authors
With this change, external compiler plugins need to be updated to use the new
ServiceLoader-based approach:Ruleinterface:RuleProviderwith a public default constructor:META-INF/services/io.ballerina.scan.RuleProviderin the compiler plugin JAR:rules.jsonfile is no longer needed and can be removed from the plugin resources.Check List