From 50903237990f747dcb3ae214ab56794213a8dcce Mon Sep 17 00:00:00 2001 From: Nureka Rodrigo Date: Wed, 4 Jun 2025 10:51:13 +0530 Subject: [PATCH 1/3] Refactor the rule loading mechanism to use ServiceLoader Pattern --- build.gradle | 21 -- .../io/ballerina/scan/test/TestReporter.java | 2 +- .../java/io/ballerina/scan/RuleProvider.java | 35 ++++ .../io/ballerina/scan/internal/IssueImpl.java | 4 +- .../scan/internal/ProjectAnalyzer.java | 166 +++++----------- .../ballerina/scan/internal/ReporterImpl.java | 4 +- .../scan/internal/ScanToolConstants.java | 22 +-- scan-command/src/main/java/module-info.java | 1 + .../scan/internal/ProjectAnalyzerTest.java | 68 ------- .../invalid-json-format-for-rule-kind.txt | 1 - .../common/invalid-json-format-for-rule.txt | 1 - .../common/invalid-json-format-for-rules.txt | 1 - .../common/invalid-json-rule-format.txt | 1 - .../common/invalid-json-rule-kind.txt | 1 - .../.gitignore | 4 - .../Ballerina.toml | 4 - .../Scan.toml | 3 - .../main.bal | 22 --- .../.gitignore | 4 - .../Ballerina.toml | 4 - .../Scan.toml | 3 - .../main.bal | 22 --- .../.gitignore | 4 - .../Ballerina.toml | 4 - .../Scan.toml | 3 - .../main.bal | 22 --- settings.gradle | 3 - .../examplestaticcodeanalyzer/CustomRule.java | 45 +++++ .../CustomRuleProvider.java} | 29 ++- .../services/io.ballerina.scan.RuleProvider | 1 + .../src/main/resources/rules.json | 17 -- .../CustomCodeAnalyzer.java | 2 +- .../examplestaticcodeanalyzer/CustomRule.java | 45 +++++ .../CustomRuleProvider.java | 36 ++++ .../services/io.ballerina.scan.RuleProvider | 1 + .../src/main/resources/rules.json | 17 -- .../CustomCodeAnalyzer.java | 2 +- .../examplestaticcodeanalyzer/CustomRule.java | 45 +++++ .../CustomRuleProvider.java | 36 ++++ .../services/io.ballerina.scan.RuleProvider | 1 + .../src/main/resources/rules.json | 17 -- .../invalid-ruleformat-plugin/build.gradle | 181 ------------------ .../.gitignore | 15 -- .../Ballerina.toml | 5 - .../CompilerPlugin.toml | 5 - .../README.md | 1 - .../main.bal | 15 -- .../ruleformat/CustomAnalysisTask.java | 48 ----- .../ruleformat/CustomCodeAnalyzer.java | 42 ---- .../ruleformat/CustomStaticCodeAnalyzer.java | 39 ---- .../src/main/resources/rules.json | 17 -- .../invalid-rulekind-plugin/build.gradle | 181 ------------------ .../.gitignore | 15 -- .../Ballerina.toml | 5 - .../CompilerPlugin.toml | 5 - .../README.md | 1 - .../main.bal | 15 -- .../invalid/rulekind/CustomAnalysisTask.java | 48 ----- .../invalid/rulekind/CustomCodeAnalyzer.java | 42 ---- .../rulekind/CustomStaticCodeAnalyzer.java | 39 ---- .../src/main/resources/rules.json | 17 -- .../invalid-rules-plugin/build.gradle | 181 ------------------ .../.gitignore | 15 -- .../Ballerina.toml | 5 - .../CompilerPlugin.toml | 5 - .../README.md | 1 - .../main.bal | 15 -- .../org/invalid/rules/CustomAnalysisTask.java | 48 ----- .../org/invalid/rules/CustomCodeAnalyzer.java | 42 ---- .../src/main/resources/rules.json | 5 - 70 files changed, 321 insertions(+), 1456 deletions(-) create mode 100644 scan-command/src/main/java/io/ballerina/scan/RuleProvider.java delete mode 100644 scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rule-kind.txt delete mode 100644 scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rule.txt delete mode 100644 scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rules.txt delete mode 100644 scan-command/src/test/resources/command-outputs/common/invalid-json-rule-format.txt delete mode 100644 scan-command/src/test/resources/command-outputs/common/invalid-json-rule-kind.txt delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/.gitignore delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/Ballerina.toml delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/Scan.toml delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/main.bal delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/.gitignore delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/Ballerina.toml delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/Scan.toml delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/main.bal delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/.gitignore delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/Ballerina.toml delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/Scan.toml delete mode 100644 scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/main.bal create mode 100644 test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java rename test-compiler-plugins/{invalid-rules-plugin/src/main/java/org/invalid/rules/CustomStaticCodeAnalyzer.java => ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRuleProvider.java} (50%) create mode 100644 test-compiler-plugins/ballerina-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider delete mode 100644 test-compiler-plugins/ballerina-plugin/src/main/resources/rules.json create mode 100644 test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRule.java create mode 100644 test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRuleProvider.java create mode 100644 test-compiler-plugins/ballerinax-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider delete mode 100644 test-compiler-plugins/ballerinax-plugin/src/main/resources/rules.json create mode 100644 test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRule.java create mode 100644 test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRuleProvider.java create mode 100644 test-compiler-plugins/exampleOrg-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider delete mode 100644 test-compiler-plugins/exampleOrg-plugin/src/main/resources/rules.json delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/build.gradle delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/.gitignore delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/Ballerina.toml delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/CompilerPlugin.toml delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/README.md delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/main.bal delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomAnalysisTask.java delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomCodeAnalyzer.java delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomStaticCodeAnalyzer.java delete mode 100644 test-compiler-plugins/invalid-ruleformat-plugin/src/main/resources/rules.json delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/build.gradle delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/.gitignore delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/Ballerina.toml delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/CompilerPlugin.toml delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/README.md delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/main.bal delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomAnalysisTask.java delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomCodeAnalyzer.java delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomStaticCodeAnalyzer.java delete mode 100644 test-compiler-plugins/invalid-rulekind-plugin/src/main/resources/rules.json delete mode 100644 test-compiler-plugins/invalid-rules-plugin/build.gradle delete mode 100644 test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/.gitignore delete mode 100644 test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/Ballerina.toml delete mode 100644 test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/CompilerPlugin.toml delete mode 100644 test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/README.md delete mode 100644 test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/main.bal delete mode 100644 test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomAnalysisTask.java delete mode 100644 test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomCodeAnalyzer.java delete mode 100644 test-compiler-plugins/invalid-rules-plugin/src/main/resources/rules.json diff --git a/build.gradle b/build.gradle index 16587b6e..b0904366 100644 --- a/build.gradle +++ b/build.gradle @@ -30,9 +30,6 @@ project(':scan-command') { dependsOn ':test-compiler-plugins:exampleOrg-plugin:build' dependsOn ':test-compiler-plugins:ballerina-plugin:build' dependsOn ':test-compiler-plugins:ballerinax-plugin:build' - dependsOn ':test-compiler-plugins:invalid-rules-plugin:build' - dependsOn ':test-compiler-plugins:invalid-ruleformat-plugin:build' - dependsOn ':test-compiler-plugins:invalid-rulekind-plugin:build' dependsOn ':test-static-code-analysis-platform-plugins:exampleOrg-static-code-analysis-platform-plugin:build' } } @@ -56,24 +53,6 @@ project(':test-compiler-plugins:ballerinax-plugin') { } } -project(':test-compiler-plugins:invalid-rules-plugin') { - dependencies { - implementation project(':scan-command') - } -} - -project(':test-compiler-plugins:invalid-ruleformat-plugin') { - dependencies { - implementation project(':scan-command') - } -} - -project(':test-compiler-plugins:invalid-rulekind-plugin') { - dependencies { - implementation project(':scan-command') - } -} - project(':test-static-code-analysis-platform-plugins:exampleOrg-static-code-analysis-platform-plugin') { dependencies { implementation project(':scan-command') diff --git a/scan-command-test-utils/src/main/java/io/ballerina/scan/test/TestReporter.java b/scan-command-test-utils/src/main/java/io/ballerina/scan/test/TestReporter.java index cecbdd2d..72706fd5 100644 --- a/scan-command-test-utils/src/main/java/io/ballerina/scan/test/TestReporter.java +++ b/scan-command-test-utils/src/main/java/io/ballerina/scan/test/TestReporter.java @@ -35,7 +35,7 @@ class TestReporter extends ReporterImpl { } @Override - protected List getIssues() { + public List getIssues() { return super.getIssues(); } } diff --git a/scan-command/src/main/java/io/ballerina/scan/RuleProvider.java b/scan-command/src/main/java/io/ballerina/scan/RuleProvider.java new file mode 100644 index 00000000..74c0af6d --- /dev/null +++ b/scan-command/src/main/java/io/ballerina/scan/RuleProvider.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org) + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.ballerina.scan; + +/** + * Service provider interface for discovering rules from code analyzers. + * This interface should be implemented by classes that extend CodeAnalyzer + * and want to provide rules through the service loading mechanism. + * + * @since 0.10.0 + */ +public interface RuleProvider { + /** + * Returns all Rule instances provided by this analyzer. + * + * @return an iterable of rules provided by this analyzer + */ + Iterable getRules(); +} diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/IssueImpl.java b/scan-command/src/main/java/io/ballerina/scan/internal/IssueImpl.java index bab85087..8c7bd164 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/IssueImpl.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/IssueImpl.java @@ -33,7 +33,7 @@ * */ public class IssueImpl implements Issue { private final BLangDiagnosticLocation location; - private final RuleImpl rule; + private final Rule rule; private final Source source; private final String fileName; private final String filePath; @@ -48,7 +48,7 @@ public class IssueImpl implements Issue { this.location = new BLangDiagnosticLocation(lineRange.fileName(), lineRange.startLine().line(), lineRange.endLine().line(), lineRange.startLine().offset(), lineRange.endLine().offset(), textRange.startOffset(), textRange.length()); - this.rule = (RuleImpl) rule; + this.rule = rule; this.source = source; this.fileName = fileName; this.filePath = filePath; diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java b/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java index d68cbb60..473b4b0d 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/ProjectAnalyzer.java @@ -18,10 +18,6 @@ package io.ballerina.scan.internal; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; import io.ballerina.compiler.api.SemanticModel; import io.ballerina.projects.CompilerPluginCache; import io.ballerina.projects.Document; @@ -37,55 +33,41 @@ import io.ballerina.projects.internal.model.CompilerPluginDescriptor; import io.ballerina.scan.Issue; import io.ballerina.scan.Rule; -import io.ballerina.scan.RuleKind; +import io.ballerina.scan.RuleProvider; import io.ballerina.scan.ScannerContext; import io.ballerina.scan.utils.DiagnosticCode; import io.ballerina.scan.utils.DiagnosticLog; import io.ballerina.scan.utils.ScanTomlFile; import io.ballerina.scan.utils.ScanToolException; -import java.io.BufferedReader; import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; -import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.util.ArrayList; -import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Optional; +import java.util.ServiceLoader; import java.util.Set; import java.util.UUID; import java.util.function.Consumer; -import java.util.stream.Collectors; import static io.ballerina.projects.util.ProjectConstants.IMPORT_PREFIX; -import static io.ballerina.scan.internal.ScanToolConstants.BUG; -import static io.ballerina.scan.internal.ScanToolConstants.CODE_SMELL; import static io.ballerina.scan.internal.ScanToolConstants.FORWARD_SLASH; import static io.ballerina.scan.internal.ScanToolConstants.IMPORT_GENERATOR_FILE; -import static io.ballerina.scan.internal.ScanToolConstants.RULES_FILE; -import static io.ballerina.scan.internal.ScanToolConstants.RULE_DESCRIPTION; -import static io.ballerina.scan.internal.ScanToolConstants.RULE_ID; -import static io.ballerina.scan.internal.ScanToolConstants.RULE_KIND; import static io.ballerina.scan.internal.ScanToolConstants.SCANNER_CONTEXT; import static io.ballerina.scan.internal.ScanToolConstants.USE_IMPORT_AS_UNDERSCORE; -import static io.ballerina.scan.internal.ScanToolConstants.VULNERABILITY; /** * Represents the project analyzer used for analyzing projects. * * @since 0.1.0 - * */ + */ public class ProjectAnalyzer { private final Project project; private final ScanTomlFile scanTomlFile; - private final Gson gson = new Gson(); private String pluginImportsDocumentName; protected ProjectAnalyzer(Project project, ScanTomlFile scanTomlFile) { @@ -144,26 +126,66 @@ Map> getExternalAnalyzers() { ResolvedPackageDependency rootPkgNode = new ResolvedPackageDependency(project.currentPackage(), PackageDependencyScope.DEFAULT); Map> externalAnalyzers = new HashMap<>(); + packageResolution.dependencyGraph().getDirectDependencies(rootPkgNode).stream() .map(ResolvedPackageDependency::packageInstance).forEach(pkgDependency -> { PackageManifest pkgManifest = pkgDependency.manifest(); String org = pkgManifest.org().value(); String name = pkgManifest.name().value(); - String pluginName = org + FORWARD_SLASH + name; if (pkgManifest.compilerPluginDescriptor().isEmpty()) { return; } - CompilerPluginDescriptor pluginDesc = pkgManifest.compilerPluginDescriptor().get(); - Optional ruleFileContent = loadRuleFileContent(pluginName, pluginDesc); - if (ruleFileContent.isEmpty()) { + + CompilerPluginDescriptor pluginDesc = pkgDependency.manifest().compilerPluginDescriptor() + .orElse(null); + if (pluginDesc == null) { + externalAnalyzers.put(pkgDependency.manifest().compilerPluginDescriptor().toString(), + new ArrayList<>()); return; } - List externalRules = loadExternalRules(org, name, pluginName, ruleFileContent.get()); + List externalRules; + try { + externalRules = loadRulesFromEnum(pluginDesc, org, name); + } catch (IOException e) { + DiagnosticLog.error(DiagnosticCode.FAILED_TO_LOAD_COMPILER_PLUGIN, + "IOException occurred while loading rules: " + e.getMessage()); + externalRules = new ArrayList<>(); + } catch (ScanToolException e) { + DiagnosticLog.error(DiagnosticCode.FAILED_TO_LOAD_COMPILER_PLUGIN, + "ScanToolException occurred while loading rules: " + e.getMessage()); + externalRules = new ArrayList<>(); + } externalAnalyzers.put(pluginDesc.plugin().getClassName(), externalRules); }); return externalAnalyzers; } + private List loadRulesFromEnum(CompilerPluginDescriptor pluginDesc, String org, String name) + throws IOException { + List jarUrls = pluginDesc.dependencies().stream().map(dependency -> { + try { + return Path.of(dependency.getPath()).toUri().toURL(); + } catch (MalformedURLException ex) { + throw new ScanToolException( + DiagnosticLog.error(DiagnosticCode.FAILED_TO_LOAD_COMPILER_PLUGIN, ex.getMessage())); + } + }).toList(); + + try (URLClassLoader ucl = URLClassLoader.newInstance(jarUrls.toArray(URL[]::new), + this.getClass().getClassLoader())) { + ServiceLoader loader = ServiceLoader.load(RuleProvider.class, ucl); + List rules = new ArrayList<>(); + for (RuleProvider provider : loader) { + for (Rule r : provider.getRules()) { + Rule inMemoryRule = RuleFactory.createRule(r.numericId(), + r.description(), r.kind(), org, name); + rules.add(inMemoryRule); + } + } + return rules; + } + } + private void extractAnalyzerImportsAndDependencies(ScanTomlFile.Analyzer analyzer, StringBuilder imports, StringBuilder dependencies) { String org = analyzer.org(); @@ -190,104 +212,12 @@ private void buildStringWithNewLine(StringBuilder stringBuilder, String content) stringBuilder.append(content).append(System.lineSeparator()); } - private Optional loadRuleFileContent(String pluginName, CompilerPluginDescriptor pluginDesc) { - InputStream resource = loadResource(pluginDesc); - if (resource == null) { - return Optional.empty(); - } - - String resourceContent; - try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource, - StandardCharsets.UTF_8))) { - resourceContent = reader.lines().collect(Collectors.joining()); - } catch (IOException ex) { - throw new ScanToolException(DiagnosticLog.error(DiagnosticCode.READING_RULES_FILE, RULES_FILE, pluginName, - ex.getMessage())); - } - return Optional.of(resourceContent); - } - - private InputStream loadResource(CompilerPluginDescriptor pluginDesc) { - List jarUrls = pluginDesc.dependencies().stream().map(dependency -> { - try { - return Path.of(dependency.getPath()).toUri().toURL(); - } catch (MalformedURLException ex) { - throw new ScanToolException( - DiagnosticLog.error(DiagnosticCode.FAILED_TO_LOAD_COMPILER_PLUGIN, - ex.getMessage())); - } - }).toList(); - - URLClassLoader ucl = URLClassLoader.newInstance(jarUrls.toArray(URL[]::new), - this.getClass().getClassLoader()); - return ucl.getResourceAsStream(RULES_FILE); - } - - private List loadExternalRules(String org, String name, String pluginName, String ruleFileContent) { - List rules = new ArrayList<>(); - JsonArray ruleArray = getRuleJsonArray(pluginName, ruleFileContent); - for (JsonElement rule : ruleArray) { - JsonObject ruleObject = getRuleObject(pluginName, rule); - RuleKind ruleKind = getRuleKind(pluginName, ruleObject.get(RULE_KIND).getAsString()); - Rule inMemoryRule = RuleFactory.createRule(ruleObject.get(RULE_ID).getAsInt(), - ruleObject.get(RULE_DESCRIPTION).getAsString(), ruleKind, org, name); - rules.add(inMemoryRule); - } - return rules; - } - - private JsonArray getRuleJsonArray(String pluginName, String ruleFileContent) { - Gson gson = new Gson(); - JsonElement element = gson.fromJson(ruleFileContent, JsonElement.class); - if (!element.isJsonArray()) { - throw new ScanToolException(DiagnosticLog.error(DiagnosticCode.INVALID_JSON_FORMAT, RULES_FILE, - pluginName, gson.toJson(element))); - } - return element.getAsJsonArray(); - } - - private JsonObject getRuleObject(String pluginName, JsonElement rule) { - JsonObject ruleObject = rule.getAsJsonObject(); - if (!isValidRule(ruleObject)) { - throw new ScanToolException(DiagnosticLog.error(DiagnosticCode.INVALID_JSON_FORMAT_RULE, - pluginName, gson.toJson(ruleObject))); - } - return ruleObject; - } - - private boolean isValidRule(JsonObject ruleObject) { - return ruleObject.has(RULE_ID) && - ruleObject.get(RULE_ID).isJsonPrimitive() && - ruleObject.get(RULE_ID).getAsJsonPrimitive().isNumber() && - ruleObject.has(RULE_KIND) && - ruleObject.has(RULE_DESCRIPTION); - } - - private RuleKind getRuleKind(String pluginName, String kind) { - switch (kind) { - case BUG -> { - return RuleKind.BUG; - } - case VULNERABILITY -> { - return RuleKind.VULNERABILITY; - } - case CODE_SMELL -> { - return RuleKind.CODE_SMELL; - } - default -> { - throw new ScanToolException(DiagnosticLog.error(DiagnosticCode.INVALID_JSON_FORMAT_RULE_KIND, - pluginName, Arrays.toString(RuleKind.values()), kind)); - } - } - } - List runExternalAnalyzers(Map> externalAnalyzers) { List scannerContextList = new ArrayList<>(externalAnalyzers.size()); for (Map.Entry> externalAnalyzer : externalAnalyzers.entrySet()) { ScannerContext scannerContext = getScannerContext(externalAnalyzer.getValue()); scannerContextList.add(scannerContext); - // Save the scanner context to plugin cache for the compiler plugin to use during package compilation Map pluginProperties = new HashMap<>(); pluginProperties.put(SCANNER_CONTEXT, scannerContext); project.projectEnvironmentContext() diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/ReporterImpl.java b/scan-command/src/main/java/io/ballerina/scan/internal/ReporterImpl.java index c4be7833..a1ee4536 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/ReporterImpl.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/ReporterImpl.java @@ -45,7 +45,7 @@ * Represents the implementation of the {@link Reporter} interface. * * @since 0.1.0 - * */ + */ public class ReporterImpl implements Reporter { private final List issues = new ArrayList<>(); private final Map rules = new HashMap<>(); @@ -65,7 +65,7 @@ public void reportIssue(Document reportedDocument, Location location, int ruleId @Override public void reportIssue(Document reportedDocument, Location location, Rule rule) { - issues.add(createIssue(reportedDocument, location, rule)); + issues.add(createIssue(reportedDocument, location, rules.get(rule.numericId()))); } private Issue createIssue(Document reportedDocument, Location location, Rule rule) { diff --git a/scan-command/src/main/java/io/ballerina/scan/internal/ScanToolConstants.java b/scan-command/src/main/java/io/ballerina/scan/internal/ScanToolConstants.java index 592feca4..61090cbd 100644 --- a/scan-command/src/main/java/io/ballerina/scan/internal/ScanToolConstants.java +++ b/scan-command/src/main/java/io/ballerina/scan/internal/ScanToolConstants.java @@ -24,21 +24,13 @@ * @since 0.1.0 */ public class ScanToolConstants { - static final String SCAN_COMMAND = "scan"; - static final String BALLERINA_RULE_PREFIX = "ballerina:"; - static final String BALLERINA_ORG = "ballerina"; - static final String BALLERINAI_ORG = "ballerinai"; - static final String BALLERINAX_ORG = "ballerinax"; - static final String USE_IMPORT_AS_UNDERSCORE = " as _;"; - static final String IMPORT_GENERATOR_FILE = "scan_file"; - static final String RULES_FILE = "rules.json"; - static final String RULE_KIND = "kind"; - static final String BUG = "BUG"; - static final String VULNERABILITY = "VULNERABILITY"; - static final String CODE_SMELL = "CODE_SMELL"; - static final String RULE_ID = "id"; - static final String RULE_DESCRIPTION = "description"; - + public static final String SCAN_COMMAND = "scan"; + public static final String BALLERINA_RULE_PREFIX = "ballerina:"; + public static final String BALLERINA_ORG = "ballerina"; + public static final String BALLERINAI_ORG = "ballerinai"; + public static final String BALLERINAX_ORG = "ballerinax"; + public static final String USE_IMPORT_AS_UNDERSCORE = " as _;"; + public static final String IMPORT_GENERATOR_FILE = "scan_file"; public static final String SCANNER_CONTEXT = "ScannerContext"; public static final String FORWARD_SLASH = "/"; diff --git a/scan-command/src/main/java/module-info.java b/scan-command/src/main/java/module-info.java index fccf3af1..923c4d3c 100644 --- a/scan-command/src/main/java/module-info.java +++ b/scan-command/src/main/java/module-info.java @@ -1,5 +1,6 @@ module io.ballerina.scan { uses io.ballerina.scan.StaticCodeAnalysisPlatformPlugin; + uses io.ballerina.scan.RuleProvider; requires io.ballerina.cli; requires io.ballerina.lang; requires io.ballerina.parser; diff --git a/scan-command/src/test/java/io/ballerina/scan/internal/ProjectAnalyzerTest.java b/scan-command/src/test/java/io/ballerina/scan/internal/ProjectAnalyzerTest.java index 2fa55a04..36f7fdc2 100644 --- a/scan-command/src/test/java/io/ballerina/scan/internal/ProjectAnalyzerTest.java +++ b/scan-command/src/test/java/io/ballerina/scan/internal/ProjectAnalyzerTest.java @@ -30,14 +30,12 @@ import io.ballerina.scan.RuleKind; import io.ballerina.scan.Source; import io.ballerina.scan.utils.ScanTomlFile; -import io.ballerina.scan.utils.ScanToolException; import io.ballerina.scan.utils.ScanUtils; import io.ballerina.tools.text.LineRange; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.Test; -import java.io.IOException; import java.util.List; import java.util.Map; import java.util.Optional; @@ -150,70 +148,4 @@ private void assertIssue(Issue issue, String fileName, int startLine, int startO Assert.assertEquals(rule.description(), description); Assert.assertEquals(rule.kind(), kind); } - - @Test(description = "Test analyzing project with invalid external analyzer rules.json configurations") - void testAnalyzingProjectWithInvalidExternalAnalyzerRules() throws IOException { - Project invalidProject = BuildProject.load(testResources.resolve("test-resources") - .resolve("bal-project-with-invalid-external-analyzer-rules")); - System.setProperty("user.dir", invalidProject.sourceRoot().toString()); - ScanTomlFile scanTomlFile = ScanUtils.loadScanTomlConfigurations(invalidProject, printStream) - .orElse(null); - Assert.assertNotNull(scanTomlFile); - System.setProperty("user.dir", userDir); - projectAnalyzer = new ProjectAnalyzer(invalidProject, scanTomlFile); - Map> externalAnalyzers = null; - String result = null; - try { - externalAnalyzers = projectAnalyzer.getExternalAnalyzers(); - } catch (ScanToolException ex) { - result = ex.getMessage(); - } - Assert.assertNull(externalAnalyzers); - String expected = getExpectedOutput("invalid-json-format-for-rules.txt"); - Assert.assertEquals(result, expected); - } - - @Test(description = "Test analyzing project with invalid external analyzer rule format") - void testAnalyzingProjectWithInvalidExternalAnalyzerRuleFormat() throws IOException { - Project invalidProject = BuildProject.load(testResources.resolve("test-resources") - .resolve("bal-project-with-invalid-external-analyzer-rule-format")); - System.setProperty("user.dir", invalidProject.sourceRoot().toString()); - ScanTomlFile scanTomlFile = ScanUtils.loadScanTomlConfigurations(invalidProject, printStream) - .orElse(null); - Assert.assertNotNull(scanTomlFile); - System.setProperty("user.dir", userDir); - projectAnalyzer = new ProjectAnalyzer(invalidProject, scanTomlFile); - Map> externalAnalyzers = null; - String result = null; - try { - externalAnalyzers = projectAnalyzer.getExternalAnalyzers(); - } catch (ScanToolException ex) { - result = ex.getMessage(); - } - Assert.assertNull(externalAnalyzers); - String expected = getExpectedOutput("invalid-json-rule-format.txt"); - Assert.assertEquals(result, expected); - } - - @Test(description = "Test analyzing project with invalid external analyzer rule kind") - void testAnalyzingProjectWithInvalidExternalAnalyzerRuleKind() throws IOException { - Project invalidProject = BuildProject.load(testResources.resolve("test-resources") - .resolve("bal-project-with-invalid-external-analyzer-rule-kind")); - System.setProperty("user.dir", invalidProject.sourceRoot().toString()); - ScanTomlFile scanTomlFile = ScanUtils.loadScanTomlConfigurations(invalidProject, printStream) - .orElse(null); - Assert.assertNotNull(scanTomlFile); - System.setProperty("user.dir", userDir); - projectAnalyzer = new ProjectAnalyzer(invalidProject, scanTomlFile); - Map> externalAnalyzers = null; - String result = null; - try { - externalAnalyzers = projectAnalyzer.getExternalAnalyzers(); - } catch (ScanToolException ex) { - result = ex.getMessage(); - } - Assert.assertNull(externalAnalyzers); - String expected = getExpectedOutput("invalid-json-rule-kind.txt"); - Assert.assertEquals(result, expected); - } } diff --git a/scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rule-kind.txt b/scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rule-kind.txt deleted file mode 100644 index 1c1d9474..00000000 --- a/scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rule-kind.txt +++ /dev/null @@ -1 +0,0 @@ -invalid kind for rule object in plugin 'exampleOrg/exampleName'. kind should be one of [CODE_SMELL, BUG, VULNERABILITY] but found: 'SECURITY_HOTSPOT' \ No newline at end of file diff --git a/scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rule.txt b/scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rule.txt deleted file mode 100644 index 40f0c0df..00000000 --- a/scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rule.txt +++ /dev/null @@ -1 +0,0 @@ -invalid json format for rule object in plugin 'exampleOrg/exampleName'. rule object should contain 'numeric id', 'kind', and 'description' but found: {'id':'1', 'severity':'BUG', 'description':'rule 1'} \ No newline at end of file diff --git a/scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rules.txt b/scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rules.txt deleted file mode 100644 index cef6087c..00000000 --- a/scan-command/src/test/resources/command-outputs/common/invalid-json-format-for-rules.txt +++ /dev/null @@ -1 +0,0 @@ -invalid json format for 'rules.json' in plugin 'invalid/invalid_module_rules_static_code_analyzer'. expected an array, but found: {"id":1,"kind":"CODE_SMELL","description":"rule 1"} \ No newline at end of file diff --git a/scan-command/src/test/resources/command-outputs/common/invalid-json-rule-format.txt b/scan-command/src/test/resources/command-outputs/common/invalid-json-rule-format.txt deleted file mode 100644 index 71fd1a9e..00000000 --- a/scan-command/src/test/resources/command-outputs/common/invalid-json-rule-format.txt +++ /dev/null @@ -1 +0,0 @@ -invalid json format for rule object in plugin 'invalid/invalid_module_ruleformat_static_code_analyzer'. rule object should contain 'id', 'kind', and 'description' but found: {"id":1,"severity":"CODE_SMELL","description":"rule 1"} \ No newline at end of file diff --git a/scan-command/src/test/resources/command-outputs/common/invalid-json-rule-kind.txt b/scan-command/src/test/resources/command-outputs/common/invalid-json-rule-kind.txt deleted file mode 100644 index 0964781c..00000000 --- a/scan-command/src/test/resources/command-outputs/common/invalid-json-rule-kind.txt +++ /dev/null @@ -1 +0,0 @@ -invalid kind for rule object in plugin 'invalid/invalid_module_rulekind_static_code_analyzer'. Expected one of [CODE_SMELL, BUG, VULNERABILITY], but found: 'SECURITY_HOTSPOT' \ No newline at end of file diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/.gitignore b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/.gitignore deleted file mode 100644 index 3749d15a..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -target -generated -Config.toml -Dependencies.toml diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/Ballerina.toml b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/Ballerina.toml deleted file mode 100644 index 9f09fdfe..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "ballerina" -name = "bal_project_with_invalid_external_analyzer_rule_format" -version = "0.1.0" diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/Scan.toml b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/Scan.toml deleted file mode 100644 index cd72247f..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/Scan.toml +++ /dev/null @@ -1,3 +0,0 @@ -[[analyzer]] -org = "invalid" -name = "invalid_module_ruleformat_static_code_analyzer" diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/main.bal b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/main.bal deleted file mode 100644 index 011d9b36..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-format/main.bal +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -function getResult() returns int|error => 1; - -public function main() { - // Non-compliant - int result = checkpanic getResult(); -} diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/.gitignore b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/.gitignore deleted file mode 100644 index 3749d15a..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -target -generated -Config.toml -Dependencies.toml diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/Ballerina.toml b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/Ballerina.toml deleted file mode 100644 index c2ec0bf5..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "ballerina" -name = "bal_project_with_invalid_external_analyzer_rule_kind" -version = "0.1.0" diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/Scan.toml b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/Scan.toml deleted file mode 100644 index 577dcedc..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/Scan.toml +++ /dev/null @@ -1,3 +0,0 @@ -[[analyzer]] -org = "invalid" -name = "invalid_module_rulekind_static_code_analyzer" diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/main.bal b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/main.bal deleted file mode 100644 index 011d9b36..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rule-kind/main.bal +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -function getResult() returns int|error => 1; - -public function main() { - // Non-compliant - int result = checkpanic getResult(); -} diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/.gitignore b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/.gitignore deleted file mode 100644 index 3749d15a..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -target -generated -Config.toml -Dependencies.toml diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/Ballerina.toml b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/Ballerina.toml deleted file mode 100644 index c7ef5a77..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/Ballerina.toml +++ /dev/null @@ -1,4 +0,0 @@ -[package] -org = "ballerina" -name = "bal_project_with_invalid_external_analyzer_rules" -version = "0.1.0" diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/Scan.toml b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/Scan.toml deleted file mode 100644 index a65d2585..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/Scan.toml +++ /dev/null @@ -1,3 +0,0 @@ -[[analyzer]] -org = "invalid" -name = "invalid_module_rules_static_code_analyzer" diff --git a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/main.bal b/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/main.bal deleted file mode 100644 index 011d9b36..00000000 --- a/scan-command/src/test/resources/test-resources/bal-project-with-invalid-external-analyzer-rules/main.bal +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. - -function getResult() returns int|error => 1; - -public function main() { - // Non-compliant - int result = checkpanic getResult(); -} diff --git a/settings.gradle b/settings.gradle index 252f6c17..66b45cd5 100644 --- a/settings.gradle +++ b/settings.gradle @@ -36,9 +36,6 @@ include 'scan-command-test-utils' include 'test-compiler-plugins:exampleOrg-plugin' include 'test-compiler-plugins:ballerina-plugin' include 'test-compiler-plugins:ballerinax-plugin' -include 'test-compiler-plugins:invalid-rules-plugin' -include 'test-compiler-plugins:invalid-ruleformat-plugin' -include 'test-compiler-plugins:invalid-rulekind-plugin' include 'test-static-code-analysis-platform-plugins:exampleOrg-static-code-analysis-platform-plugin' if (gradle.startParameter.buildScan) { diff --git a/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java new file mode 100644 index 00000000..48da4a92 --- /dev/null +++ b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java @@ -0,0 +1,45 @@ +package io.ballerina.examplestaticcodeanalyzer; + +import io.ballerina.scan.Rule; +import io.ballerina.scan.RuleKind; + +import static io.ballerina.scan.RuleKind.BUG; +import static io.ballerina.scan.RuleKind.CODE_SMELL; +import static io.ballerina.scan.RuleKind.VULNERABILITY; + +public enum CustomRule implements Rule { + RULE_1(1, "rule 1", CODE_SMELL), + RULE_2(2, "rule 2", + BUG), + RULE_3(3, "rule 3", VULNERABILITY); + + private final int id; + private final String description; + private final RuleKind kind; + + CustomRule(int id, String description, RuleKind kind) { + this.id = id; + this.description = description; + this.kind = kind; + } + + @Override + public String id() { + return ""; + } + + @Override + public int numericId() { + return id; + } + + @Override + public String description() { + return description; + } + + @Override + public RuleKind kind() { + return kind; + } +} diff --git a/test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomStaticCodeAnalyzer.java b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRuleProvider.java similarity index 50% rename from test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomStaticCodeAnalyzer.java rename to test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRuleProvider.java index 7ea9d7a2..38a00d6e 100644 --- a/test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomStaticCodeAnalyzer.java +++ b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRuleProvider.java @@ -16,24 +16,21 @@ * under the License. */ -package org.invalid.rules; +package io.ballerina.examplestaticcodeanalyzer; -import io.ballerina.projects.plugins.CompilerPlugin; -import io.ballerina.projects.plugins.CompilerPluginContext; -import io.ballerina.scan.ScannerContext; +import io.ballerina.scan.Rule; +import io.ballerina.scan.RuleProvider; -/** - * Represents a compiler plugin for a custom module. - * - * @since 0.1.0 - * */ -public class CustomStaticCodeAnalyzer extends CompilerPlugin { +import java.util.List; + +public class CustomRuleProvider implements RuleProvider { + /** + * Returns all Rule instances provided by this analyzer. + * + * @return an iterable of rules provided by this analyzer + */ @Override - public void init(CompilerPluginContext compilerPluginContext) { - Object context = compilerPluginContext.userData().get("ScannerContext"); - if (context != null) { - ScannerContext scannerContext = (ScannerContext) context; - compilerPluginContext.addCodeAnalyzer(new CustomCodeAnalyzer(scannerContext)); - } + public Iterable getRules() { + return List.of(CustomRule.values()); } } diff --git a/test-compiler-plugins/ballerina-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider b/test-compiler-plugins/ballerina-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider new file mode 100644 index 00000000..6cf48454 --- /dev/null +++ b/test-compiler-plugins/ballerina-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider @@ -0,0 +1 @@ +io.ballerina.examplestaticcodeanalyzer.CustomRuleProvider \ No newline at end of file diff --git a/test-compiler-plugins/ballerina-plugin/src/main/resources/rules.json b/test-compiler-plugins/ballerina-plugin/src/main/resources/rules.json deleted file mode 100644 index 98bf00bd..00000000 --- a/test-compiler-plugins/ballerina-plugin/src/main/resources/rules.json +++ /dev/null @@ -1,17 +0,0 @@ -[ - { - "id": 1, - "kind": "CODE_SMELL", - "description": "rule 1" - }, - { - "id": 2, - "kind": "BUG", - "description": "rule 2" - }, - { - "id": 3, - "kind": "VULNERABILITY", - "description": "rule 3" - } -] diff --git a/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomCodeAnalyzer.java b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomCodeAnalyzer.java index 6c433a2e..91142a5a 100644 --- a/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomCodeAnalyzer.java +++ b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomCodeAnalyzer.java @@ -24,7 +24,7 @@ import io.ballerina.scan.ScannerContext; /** - * Represents a code analyzer for a ballerinax module. + * Represents a code analyzer for a ballerina module. * * @since 0.1.0 * */ diff --git a/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRule.java b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRule.java new file mode 100644 index 00000000..44df77dc --- /dev/null +++ b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRule.java @@ -0,0 +1,45 @@ +package io.ballerinax.examplestaticcodeanalyzer; + +import io.ballerina.scan.Rule; +import io.ballerina.scan.RuleKind; + +import static io.ballerina.scan.RuleKind.BUG; +import static io.ballerina.scan.RuleKind.CODE_SMELL; +import static io.ballerina.scan.RuleKind.VULNERABILITY; + +public enum CustomRule implements Rule { + RULE_1(1, "rule 1", CODE_SMELL), + RULE_2(2, "rule 2", + BUG), + RULE_3(3, "rule 3", VULNERABILITY); + + private final int id; + private final String description; + private final RuleKind kind; + + CustomRule(int id, String description, RuleKind kind) { + this.id = id; + this.description = description; + this.kind = kind; + } + + @Override + public String id() { + return ""; + } + + @Override + public int numericId() { + return id; + } + + @Override + public String description() { + return description; + } + + @Override + public RuleKind kind() { + return kind; + } +} diff --git a/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRuleProvider.java b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRuleProvider.java new file mode 100644 index 00000000..6fe75b93 --- /dev/null +++ b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRuleProvider.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package io.ballerinax.examplestaticcodeanalyzer; + +import io.ballerina.scan.Rule; +import io.ballerina.scan.RuleProvider; + +import java.util.List; + +public class CustomRuleProvider implements RuleProvider { + /** + * Returns all Rule instances provided by this analyzer. + * + * @return an iterable of rules provided by this analyzer + */ + @Override + public Iterable getRules() { + return List.of(CustomRule.values()); + } +} diff --git a/test-compiler-plugins/ballerinax-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider b/test-compiler-plugins/ballerinax-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider new file mode 100644 index 00000000..869a8c70 --- /dev/null +++ b/test-compiler-plugins/ballerinax-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider @@ -0,0 +1 @@ +io.ballerinax.examplestaticcodeanalyzer.CustomRuleProvider \ No newline at end of file diff --git a/test-compiler-plugins/ballerinax-plugin/src/main/resources/rules.json b/test-compiler-plugins/ballerinax-plugin/src/main/resources/rules.json deleted file mode 100644 index 98bf00bd..00000000 --- a/test-compiler-plugins/ballerinax-plugin/src/main/resources/rules.json +++ /dev/null @@ -1,17 +0,0 @@ -[ - { - "id": 1, - "kind": "CODE_SMELL", - "description": "rule 1" - }, - { - "id": 2, - "kind": "BUG", - "description": "rule 2" - }, - { - "id": 3, - "kind": "VULNERABILITY", - "description": "rule 3" - } -] diff --git a/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomCodeAnalyzer.java b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomCodeAnalyzer.java index c70d4ae7..b0d27e61 100644 --- a/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomCodeAnalyzer.java +++ b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomCodeAnalyzer.java @@ -24,7 +24,7 @@ import io.ballerina.scan.ScannerContext; /** - * Represents a code analyzer for a custom module. + * Represents a code analyzer for a ballerina module. * * @since 0.1.0 * */ diff --git a/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRule.java b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRule.java new file mode 100644 index 00000000..235938d3 --- /dev/null +++ b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRule.java @@ -0,0 +1,45 @@ +package org.example.examplestaticcodeanalyzer; + +import io.ballerina.scan.Rule; +import io.ballerina.scan.RuleKind; + +import static io.ballerina.scan.RuleKind.BUG; +import static io.ballerina.scan.RuleKind.CODE_SMELL; +import static io.ballerina.scan.RuleKind.VULNERABILITY; + +public enum CustomRule implements Rule { + RULE_1(1, "rule 1", CODE_SMELL), + RULE_2(2, "rule 2", + BUG), + RULE_3(3, "rule 3", VULNERABILITY); + + private final int id; + private final String description; + private final RuleKind kind; + + CustomRule(int id, String description, RuleKind kind) { + this.id = id; + this.description = description; + this.kind = kind; + } + + @Override + public String id() { + return ""; + } + + @Override + public int numericId() { + return id; + } + + @Override + public String description() { + return description; + } + + @Override + public RuleKind kind() { + return kind; + } +} diff --git a/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRuleProvider.java b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRuleProvider.java new file mode 100644 index 00000000..51624374 --- /dev/null +++ b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRuleProvider.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). + * + * WSO2 LLC. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.example.examplestaticcodeanalyzer; + +import io.ballerina.scan.Rule; +import io.ballerina.scan.RuleProvider; + +import java.util.List; + +public class CustomRuleProvider implements RuleProvider { + /** + * Returns all Rule instances provided by this analyzer. + * + * @return an iterable of rules provided by this analyzer + */ + @Override + public Iterable getRules() { + return List.of(CustomRule.values()); + } +} diff --git a/test-compiler-plugins/exampleOrg-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider b/test-compiler-plugins/exampleOrg-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider new file mode 100644 index 00000000..d46a0109 --- /dev/null +++ b/test-compiler-plugins/exampleOrg-plugin/src/main/resources/META-INF/services/io.ballerina.scan.RuleProvider @@ -0,0 +1 @@ +org.example.examplestaticcodeanalyzer.CustomRuleProvider \ No newline at end of file diff --git a/test-compiler-plugins/exampleOrg-plugin/src/main/resources/rules.json b/test-compiler-plugins/exampleOrg-plugin/src/main/resources/rules.json deleted file mode 100644 index 98bf00bd..00000000 --- a/test-compiler-plugins/exampleOrg-plugin/src/main/resources/rules.json +++ /dev/null @@ -1,17 +0,0 @@ -[ - { - "id": 1, - "kind": "CODE_SMELL", - "description": "rule 1" - }, - { - "id": 2, - "kind": "BUG", - "description": "rule 2" - }, - { - "id": 3, - "kind": "VULNERABILITY", - "description": "rule 3" - } -] diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/build.gradle b/test-compiler-plugins/invalid-ruleformat-plugin/build.gradle deleted file mode 100644 index 4a6ed012..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/build.gradle +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import com.github.spotbugs.snom.Confidence -import com.github.spotbugs.snom.Effort -import org.apache.tools.ant.taskdefs.condition.Os - -plugins { - id 'java-library' - id 'checkstyle' - id "com.github.spotbugs" - id "de.undercouch.download" -} - -group = 'org.invalid.ruleformat' -version = "0.1.0" - -repositories { - mavenLocal() - mavenCentral() - - maven { - url = uri('https://repo.maven.apache.org/maven2/') - } - - maven { - url = 'https://maven.pkg.github.com/ballerina-platform/*' - credentials { - username System.getenv("packageUser") - password System.getenv("packagePAT") - } - } -} - -dependencies { - implementation "com.google.code.gson:gson:${gsonVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-lang', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-parser', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-runtime', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-tools-api', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'jballerina-tools', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-cli', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'toml-parser', version: "${ballerinaLangVersion}" - - checkstyle group: 'com.puppycrawl.tools', name: 'checkstyle', version: "${puppycrawlCheckstyleVersion}" - - // Adding dependency to the scan-command module to use interfaces - implementation project(':scan-command') -} - -tasks.withType(JavaExec).configureEach { - systemProperty 'ballerina.home', System.getenv("BALLERINA_HOME") -} - -// Setting up checkstyles -tasks.register('downloadCheckstyleRuleFiles', Download) { - src([ - 'https://raw.githubusercontent.com/wso2/code-quality-tools/v1.4/checkstyle/jdk-17/checkstyle.xml', - 'https://raw.githubusercontent.com/wso2/code-quality-tools/v1.4/checkstyle/jdk-17/suppressions.xml' - ]) - overwrite false - onlyIfNewer true - dest buildDir -} - -artifacts.add('default', file("${project.buildDir}/checkstyle.xml")) { - builtBy(downloadCheckstyleRuleFiles) -} - -artifacts.add('default', file("${project.buildDir}/suppressions.xml")) { - builtBy(downloadCheckstyleRuleFiles) -} - -def excludePattern = '**/module-info.java' -tasks.withType(Checkstyle).configureEach { - exclude excludePattern -} - -checkstyle { - toolVersion "${project.puppycrawlCheckstyleVersion}" - configFile rootProject.file("${project.buildDir}/checkstyle.xml") - configProperties = ["suppressionFile": file("${project.buildDir}/suppressions.xml")] -} - -checkstyleMain.dependsOn(downloadCheckstyleRuleFiles) -checkstyleTest.dependsOn(downloadCheckstyleRuleFiles) - -// Setting up spotbugs -spotbugsMain { - effort = Effort.valueOf("MAX") - reportLevel = Confidence.valueOf("LOW") - - reportsDir = file("$project.buildDir/reports/spotbugs") - - reports { - html.required.set(true) - text.required.set(true) - } - - def excludeFile = file("${projectDir}/spotbugs-exclude.xml") - if (excludeFile.exists()) { - excludeFilter = excludeFile - } -} - -spotbugsTest { - effort = Effort.valueOf("MAX") - reportLevel = Confidence.valueOf("LOW") - - reportsDir = file("$project.buildDir/reports/spotbugs") - - reports { - html.required.set(true) - text.required.set(true) - } - - def excludeFile = file("${projectDir}/spotbugs-exclude.xml") - if (excludeFile.exists()) { - excludeFilter = excludeFile - } -} - -// Configurations to automatically build and deploy custom static code analyzer compiler plugin -def packageOrg = "invalid" -def packageName = "invalid_module_ruleformat_static_code_analyzer" -def pluginCentralDirectory = System.getProperty("user.home") + "/.ballerina/repositories/central.ballerina.io/bala/${packageOrg}" -def pluginLocalDir = System.getProperty("user.home") + "/.ballerina/repositories/local/bala/${packageOrg}" -def balCentralCacheDir = project.file(System.getProperty("user.home") + "/.ballerina/repositories/central.ballerina.io") - -tasks.register('buildCompilerPlugin') { - doLast { - // Pack and push to local repo of distribution - exec { - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', 'cd invalid-module-ruleformat-static-code-analyzer & bal pack & bal push --repository=local' - } else { - commandLine 'sh', '-c', 'cd invalid-module-ruleformat-static-code-analyzer ; bal pack ; bal push --repository=local' - } - } - println("Successfully build and pushed the package ${packageOrg}/${packageName} to the local repository") - - // Remove the cache directories in the central repository - delete { - fileTree(balCentralCacheDir).matching { - include 'cache-*' - } - } - println("Successfully cleaned the .ballerina/cache* directories") - - // Update the central repository - def balDestinationDir = "$pluginCentralDirectory/$packageName" - def balSourceDir = "$pluginLocalDir/$packageName" - if (file(balDestinationDir).exists()) { - file(balDestinationDir).deleteDir() - } - copy { - from balSourceDir - into balDestinationDir - } - println("Successfully copied package ${packageOrg}/${packageName} from local/bala the central.ballerina.io/bala directory") - } -} - -build { - dependsOn buildCompilerPlugin -} diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/.gitignore b/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/.gitignore deleted file mode 100644 index 010e3c58..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/.gitignore +++ /dev/null @@ -1,15 +0,0 @@ -# Ballerina generates this directory during the compilation of a package. -# It contains compiler-generated artifacts and the final executable if this is an application package. -target/ - -# Ballerina maintains the compiler-generated source code here. -# Remove this if you want to commit generated sources. -generated/ - -# Contains configuration values used during development time. -# See https://ballerina.io/learn/provide-values-to-configurable-variables/ for more details. -Config.toml - -## Ballerina maintains the dependencies of the project here. -## Ignored since this is a test resource. -Dependencies.toml diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/Ballerina.toml b/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/Ballerina.toml deleted file mode 100644 index e9b405ce..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/Ballerina.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] -org = "invalid" -name = "invalid_module_ruleformat_static_code_analyzer" -version = "0.1.0" -distribution = "2201.9.1" diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/CompilerPlugin.toml b/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/CompilerPlugin.toml deleted file mode 100644 index 6f249805..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/CompilerPlugin.toml +++ /dev/null @@ -1,5 +0,0 @@ -[plugin] # Have to check if below path is needed -class = "org.invalid.ruleformat.CustomStaticCodeAnalyzer" - -[[dependency]] -path = "../build/libs/invalid-ruleformat-plugin-0.1.0.jar" diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/README.md b/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/README.md deleted file mode 100644 index 161e4795..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/README.md +++ /dev/null @@ -1 +0,0 @@ -# invalid/invalid_module_ruleformat_static_code_analyzer \ No newline at end of file diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/main.bal b/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/main.bal deleted file mode 100644 index 42249c2b..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/invalid-module-ruleformat-static-code-analyzer/main.bal +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomAnalysisTask.java b/test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomAnalysisTask.java deleted file mode 100644 index 2652c917..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomAnalysisTask.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.invalid.ruleformat; - -import io.ballerina.compiler.syntax.tree.ModulePartNode; -import io.ballerina.projects.Document; -import io.ballerina.projects.Module; -import io.ballerina.projects.plugins.AnalysisTask; -import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; -import io.ballerina.scan.Reporter; -import io.ballerina.scan.ScannerContext; - -/** - * Represents a syntax node analysis task for a custom module. - * - * @since 0.1.0 - * */ -public class CustomAnalysisTask implements AnalysisTask { - private final Reporter reporter; - - public CustomAnalysisTask(ScannerContext scannerContext) { - this.reporter = scannerContext.getReporter(); - } - - @Override - public void perform(SyntaxNodeAnalysisContext context) { - Module module = context.currentPackage().module(context.moduleId()); - Document document = module.document(context.documentId()); - ModulePartNode modulePartNode = (ModulePartNode) context.node(); - reporter.reportIssue(document, modulePartNode.location(), 1); - } -} diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomCodeAnalyzer.java b/test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomCodeAnalyzer.java deleted file mode 100644 index c1a4a3ae..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomCodeAnalyzer.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.invalid.ruleformat; - -import io.ballerina.compiler.syntax.tree.SyntaxKind; -import io.ballerina.projects.plugins.CodeAnalysisContext; -import io.ballerina.projects.plugins.CodeAnalyzer; -import io.ballerina.scan.ScannerContext; - -/** - * Represents a code analyzer for a custom module. - * - * @since 0.1.0 - * */ -public class CustomCodeAnalyzer extends CodeAnalyzer { - private final ScannerContext scannerContext; - - public CustomCodeAnalyzer(ScannerContext scannerContext) { - this.scannerContext = scannerContext; - } - - @Override - public void init(CodeAnalysisContext codeAnalysisContext) { - codeAnalysisContext.addSyntaxNodeAnalysisTask(new CustomAnalysisTask(scannerContext), SyntaxKind.MODULE_PART); - } -} diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomStaticCodeAnalyzer.java b/test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomStaticCodeAnalyzer.java deleted file mode 100644 index 3f3ed66a..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/src/main/java/org/invalid/ruleformat/CustomStaticCodeAnalyzer.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.invalid.ruleformat; - -import io.ballerina.projects.plugins.CompilerPlugin; -import io.ballerina.projects.plugins.CompilerPluginContext; -import io.ballerina.scan.ScannerContext; - -/** - * Represents a compiler plugin for a custom module. - * - * @since 0.1.0 - * */ -public class CustomStaticCodeAnalyzer extends CompilerPlugin { - @Override - public void init(CompilerPluginContext compilerPluginContext) { - Object context = compilerPluginContext.userData().get("ScannerContext"); - if (context != null) { - ScannerContext scannerContext = (ScannerContext) context; - compilerPluginContext.addCodeAnalyzer(new CustomCodeAnalyzer(scannerContext)); - } - } -} diff --git a/test-compiler-plugins/invalid-ruleformat-plugin/src/main/resources/rules.json b/test-compiler-plugins/invalid-ruleformat-plugin/src/main/resources/rules.json deleted file mode 100644 index 3a17fd25..00000000 --- a/test-compiler-plugins/invalid-ruleformat-plugin/src/main/resources/rules.json +++ /dev/null @@ -1,17 +0,0 @@ -[ - { - "id": 1, - "severity": "CODE_SMELL", - "description": "rule 1" - }, - { - "id": 2, - "severity": "BUG", - "description": "rule 2" - }, - { - "id": 3, - "severity": "VULNERABILITY", - "description": "rule 3" - } -] \ No newline at end of file diff --git a/test-compiler-plugins/invalid-rulekind-plugin/build.gradle b/test-compiler-plugins/invalid-rulekind-plugin/build.gradle deleted file mode 100644 index 3aadc6dc..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/build.gradle +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import com.github.spotbugs.snom.Confidence -import com.github.spotbugs.snom.Effort -import org.apache.tools.ant.taskdefs.condition.Os - -plugins { - id 'java-library' - id 'checkstyle' - id "com.github.spotbugs" - id "de.undercouch.download" -} - -group = 'org.invalid.rulekind' -version = "0.1.0" - -repositories { - mavenLocal() - mavenCentral() - - maven { - url = uri('https://repo.maven.apache.org/maven2/') - } - - maven { - url = 'https://maven.pkg.github.com/ballerina-platform/*' - credentials { - username System.getenv("packageUser") - password System.getenv("packagePAT") - } - } -} - -dependencies { - implementation "com.google.code.gson:gson:${gsonVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-lang', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-parser', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-runtime', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-tools-api', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'jballerina-tools', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-cli', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'toml-parser', version: "${ballerinaLangVersion}" - - checkstyle group: 'com.puppycrawl.tools', name: 'checkstyle', version: "${puppycrawlCheckstyleVersion}" - - // Adding dependency to the scan-command module to use interfaces - implementation project(':scan-command') -} - -tasks.withType(JavaExec).configureEach { - systemProperty 'ballerina.home', System.getenv("BALLERINA_HOME") -} - -// Setting up checkstyles -tasks.register('downloadCheckstyleRuleFiles', Download) { - src([ - 'https://raw.githubusercontent.com/wso2/code-quality-tools/v1.4/checkstyle/jdk-17/checkstyle.xml', - 'https://raw.githubusercontent.com/wso2/code-quality-tools/v1.4/checkstyle/jdk-17/suppressions.xml' - ]) - overwrite false - onlyIfNewer true - dest buildDir -} - -artifacts.add('default', file("${project.buildDir}/checkstyle.xml")) { - builtBy(downloadCheckstyleRuleFiles) -} - -artifacts.add('default', file("${project.buildDir}/suppressions.xml")) { - builtBy(downloadCheckstyleRuleFiles) -} - -def excludePattern = '**/module-info.java' -tasks.withType(Checkstyle).configureEach { - exclude excludePattern -} - -checkstyle { - toolVersion "${project.puppycrawlCheckstyleVersion}" - configFile rootProject.file("${project.buildDir}/checkstyle.xml") - configProperties = ["suppressionFile": file("${project.buildDir}/suppressions.xml")] -} - -checkstyleMain.dependsOn(downloadCheckstyleRuleFiles) -checkstyleTest.dependsOn(downloadCheckstyleRuleFiles) - -// Setting up spotbugs -spotbugsMain { - effort = Effort.valueOf("MAX") - reportLevel = Confidence.valueOf("LOW") - - reportsDir = file("$project.buildDir/reports/spotbugs") - - reports { - html.required.set(true) - text.required.set(true) - } - - def excludeFile = file("${projectDir}/spotbugs-exclude.xml") - if (excludeFile.exists()) { - excludeFilter = excludeFile - } -} - -spotbugsTest { - effort = Effort.valueOf("MAX") - reportLevel = Confidence.valueOf("LOW") - - reportsDir = file("$project.buildDir/reports/spotbugs") - - reports { - html.required.set(true) - text.required.set(true) - } - - def excludeFile = file("${projectDir}/spotbugs-exclude.xml") - if (excludeFile.exists()) { - excludeFilter = excludeFile - } -} - -// Configurations to automatically build and deploy custom static code analyzer compiler plugin -def packageOrg = "invalid" -def packageName = "invalid_module_rulekind_static_code_analyzer" -def pluginCentralDirectory = System.getProperty("user.home") + "/.ballerina/repositories/central.ballerina.io/bala/${packageOrg}" -def pluginLocalDir = System.getProperty("user.home") + "/.ballerina/repositories/local/bala/${packageOrg}" -def balCentralCacheDir = project.file(System.getProperty("user.home") + "/.ballerina/repositories/central.ballerina.io") - -tasks.register('buildCompilerPlugin') { - doLast { - // Pack and push to local repo of distribution - exec { - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', 'cd invalid-module-rulekind-static-code-analyzer & bal pack & bal push --repository=local' - } else { - commandLine 'sh', '-c', 'cd invalid-module-rulekind-static-code-analyzer ; bal pack ; bal push --repository=local' - } - } - println("Successfully build and pushed the package ${packageOrg}/${packageName} to the local repository") - - // Remove the cache directories in the central repository - delete { - fileTree(balCentralCacheDir).matching { - include 'cache-*' - } - } - println("Successfully cleaned the .ballerina/cache* directories") - - // Update the central repository - def balDestinationDir = "$pluginCentralDirectory/$packageName" - def balSourceDir = "$pluginLocalDir/$packageName" - if (file(balDestinationDir).exists()) { - file(balDestinationDir).deleteDir() - } - copy { - from balSourceDir - into balDestinationDir - } - println("Successfully copied package ${packageOrg}/${packageName} from local/bala the central.ballerina.io/bala directory") - } -} - -build { - dependsOn buildCompilerPlugin -} diff --git a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/.gitignore b/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/.gitignore deleted file mode 100644 index 010e3c58..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/.gitignore +++ /dev/null @@ -1,15 +0,0 @@ -# Ballerina generates this directory during the compilation of a package. -# It contains compiler-generated artifacts and the final executable if this is an application package. -target/ - -# Ballerina maintains the compiler-generated source code here. -# Remove this if you want to commit generated sources. -generated/ - -# Contains configuration values used during development time. -# See https://ballerina.io/learn/provide-values-to-configurable-variables/ for more details. -Config.toml - -## Ballerina maintains the dependencies of the project here. -## Ignored since this is a test resource. -Dependencies.toml diff --git a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/Ballerina.toml b/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/Ballerina.toml deleted file mode 100644 index 959f7c36..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/Ballerina.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] -org = "invalid" -name = "invalid_module_rulekind_static_code_analyzer" -version = "0.1.0" -distribution = "2201.9.1" diff --git a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/CompilerPlugin.toml b/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/CompilerPlugin.toml deleted file mode 100644 index 3c6f0587..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/CompilerPlugin.toml +++ /dev/null @@ -1,5 +0,0 @@ -[plugin] # Have to check if below path is needed -class = "org.invalid.rulekind.CustomStaticCodeAnalyzer" - -[[dependency]] -path = "../build/libs/invalid-rulekind-plugin-0.1.0.jar" diff --git a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/README.md b/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/README.md deleted file mode 100644 index 5d6f5a5f..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/README.md +++ /dev/null @@ -1 +0,0 @@ -# invalid/invalid_module_rulekind_static_code_analyzer \ No newline at end of file diff --git a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/main.bal b/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/main.bal deleted file mode 100644 index 42249c2b..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/invalid-module-rulekind-static-code-analyzer/main.bal +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. diff --git a/test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomAnalysisTask.java b/test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomAnalysisTask.java deleted file mode 100644 index 9a8431e5..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomAnalysisTask.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.invalid.rulekind; - -import io.ballerina.compiler.syntax.tree.ModulePartNode; -import io.ballerina.projects.Document; -import io.ballerina.projects.Module; -import io.ballerina.projects.plugins.AnalysisTask; -import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; -import io.ballerina.scan.Reporter; -import io.ballerina.scan.ScannerContext; - -/** - * Represents a syntax node analysis task for a custom module. - * - * @since 0.1.0 - * */ -public class CustomAnalysisTask implements AnalysisTask { - private final Reporter reporter; - - public CustomAnalysisTask(ScannerContext scannerContext) { - this.reporter = scannerContext.getReporter(); - } - - @Override - public void perform(SyntaxNodeAnalysisContext context) { - Module module = context.currentPackage().module(context.moduleId()); - Document document = module.document(context.documentId()); - ModulePartNode modulePartNode = (ModulePartNode) context.node(); - reporter.reportIssue(document, modulePartNode.location(), 1); - } -} diff --git a/test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomCodeAnalyzer.java b/test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomCodeAnalyzer.java deleted file mode 100644 index b506311b..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomCodeAnalyzer.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.invalid.rulekind; - -import io.ballerina.compiler.syntax.tree.SyntaxKind; -import io.ballerina.projects.plugins.CodeAnalysisContext; -import io.ballerina.projects.plugins.CodeAnalyzer; -import io.ballerina.scan.ScannerContext; - -/** - * Represents a code analyzer for a custom module. - * - * @since 0.1.0 - * */ -public class CustomCodeAnalyzer extends CodeAnalyzer { - private final ScannerContext scannerContext; - - public CustomCodeAnalyzer(ScannerContext scannerContext) { - this.scannerContext = scannerContext; - } - - @Override - public void init(CodeAnalysisContext codeAnalysisContext) { - codeAnalysisContext.addSyntaxNodeAnalysisTask(new CustomAnalysisTask(scannerContext), SyntaxKind.MODULE_PART); - } -} diff --git a/test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomStaticCodeAnalyzer.java b/test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomStaticCodeAnalyzer.java deleted file mode 100644 index 77642bee..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/src/main/java/org/invalid/rulekind/CustomStaticCodeAnalyzer.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.invalid.rulekind; - -import io.ballerina.projects.plugins.CompilerPlugin; -import io.ballerina.projects.plugins.CompilerPluginContext; -import io.ballerina.scan.ScannerContext; - -/** - * Represents a compiler plugin for a custom module. - * - * @since 0.1.0 - * */ -public class CustomStaticCodeAnalyzer extends CompilerPlugin { - @Override - public void init(CompilerPluginContext compilerPluginContext) { - Object context = compilerPluginContext.userData().get("ScannerContext"); - if (context != null) { - ScannerContext scannerContext = (ScannerContext) context; - compilerPluginContext.addCodeAnalyzer(new CustomCodeAnalyzer(scannerContext)); - } - } -} diff --git a/test-compiler-plugins/invalid-rulekind-plugin/src/main/resources/rules.json b/test-compiler-plugins/invalid-rulekind-plugin/src/main/resources/rules.json deleted file mode 100644 index 1479c69c..00000000 --- a/test-compiler-plugins/invalid-rulekind-plugin/src/main/resources/rules.json +++ /dev/null @@ -1,17 +0,0 @@ -[ - { - "id": 1, - "kind": "CODE_SMELL", - "description": "rule 1" - }, - { - "id": 2, - "kind": "BUG", - "description": "rule 2" - }, - { - "id": 3, - "kind": "SECURITY_HOTSPOT", - "description": "rule 3" - } -] diff --git a/test-compiler-plugins/invalid-rules-plugin/build.gradle b/test-compiler-plugins/invalid-rules-plugin/build.gradle deleted file mode 100644 index a8715c9f..00000000 --- a/test-compiler-plugins/invalid-rules-plugin/build.gradle +++ /dev/null @@ -1,181 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -import com.github.spotbugs.snom.Confidence -import com.github.spotbugs.snom.Effort -import org.apache.tools.ant.taskdefs.condition.Os - -plugins { - id 'java-library' - id 'checkstyle' - id "com.github.spotbugs" - id "de.undercouch.download" -} - -group = 'org.invalid.rules' -version = "0.1.0" - -repositories { - mavenLocal() - mavenCentral() - - maven { - url = uri('https://repo.maven.apache.org/maven2/') - } - - maven { - url = 'https://maven.pkg.github.com/ballerina-platform/*' - credentials { - username System.getenv("packageUser") - password System.getenv("packagePAT") - } - } -} - -dependencies { - implementation "com.google.code.gson:gson:${gsonVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-lang', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-parser', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-runtime', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-tools-api', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'jballerina-tools', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'ballerina-cli', version: "${ballerinaLangVersion}" - implementation group: 'org.ballerinalang', name: 'toml-parser', version: "${ballerinaLangVersion}" - - checkstyle group: 'com.puppycrawl.tools', name: 'checkstyle', version: "${puppycrawlCheckstyleVersion}" - - // Adding dependency to the scan-command module to use interfaces - implementation project(':scan-command') -} - -tasks.withType(JavaExec).configureEach { - systemProperty 'ballerina.home', System.getenv("BALLERINA_HOME") -} - -// Setting up checkstyles -tasks.register('downloadCheckstyleRuleFiles', Download) { - src([ - 'https://raw.githubusercontent.com/wso2/code-quality-tools/v1.4/checkstyle/jdk-17/checkstyle.xml', - 'https://raw.githubusercontent.com/wso2/code-quality-tools/v1.4/checkstyle/jdk-17/suppressions.xml' - ]) - overwrite false - onlyIfNewer true - dest buildDir -} - -artifacts.add('default', file("${project.buildDir}/checkstyle.xml")) { - builtBy(downloadCheckstyleRuleFiles) -} - -artifacts.add('default', file("${project.buildDir}/suppressions.xml")) { - builtBy(downloadCheckstyleRuleFiles) -} - -def excludePattern = '**/module-info.java' -tasks.withType(Checkstyle).configureEach { - exclude excludePattern -} - -checkstyle { - toolVersion "${project.puppycrawlCheckstyleVersion}" - configFile rootProject.file("${project.buildDir}/checkstyle.xml") - configProperties = ["suppressionFile": file("${project.buildDir}/suppressions.xml")] -} - -checkstyleMain.dependsOn(downloadCheckstyleRuleFiles) -checkstyleTest.dependsOn(downloadCheckstyleRuleFiles) - -// Setting up spotbugs -spotbugsMain { - effort = Effort.valueOf("MAX") - reportLevel = Confidence.valueOf("LOW") - - reportsDir = file("$project.buildDir/reports/spotbugs") - - reports { - html.required.set(true) - text.required.set(true) - } - - def excludeFile = file("${projectDir}/spotbugs-exclude.xml") - if (excludeFile.exists()) { - excludeFilter = excludeFile - } -} - -spotbugsTest { - effort = Effort.valueOf("MAX") - reportLevel = Confidence.valueOf("LOW") - - reportsDir = file("$project.buildDir/reports/spotbugs") - - reports { - html.required.set(true) - text.required.set(true) - } - - def excludeFile = file("${projectDir}/spotbugs-exclude.xml") - if (excludeFile.exists()) { - excludeFilter = excludeFile - } -} - -// Configurations to automatically build and deploy custom static code analyzer compiler plugin -def packageOrg = "invalid" -def packageName = "invalid_module_rules_static_code_analyzer" -def pluginCentralDirectory = System.getProperty("user.home") + "/.ballerina/repositories/central.ballerina.io/bala/${packageOrg}" -def pluginLocalDir = System.getProperty("user.home") + "/.ballerina/repositories/local/bala/${packageOrg}" -def balCentralCacheDir = project.file(System.getProperty("user.home") + "/.ballerina/repositories/central.ballerina.io") - -tasks.register('buildCompilerPlugin') { - doLast { - // Pack and push to local repo of distribution - exec { - if (Os.isFamily(Os.FAMILY_WINDOWS)) { - commandLine 'cmd', '/c', 'cd invalid-module-rules-static-code-analyzer & bal pack & bal push --repository=local' - } else { - commandLine 'sh', '-c', 'cd invalid-module-rules-static-code-analyzer ; bal pack ; bal push --repository=local' - } - } - println("Successfully build and pushed the package ${packageOrg}/${packageName} to the local repository") - - // Remove the cache directories in the central repository - delete { - fileTree(balCentralCacheDir).matching { - include 'cache-*' - } - } - println("Successfully cleaned the .ballerina/cache* directories") - - // Update the central repository - def balDestinationDir = "$pluginCentralDirectory/$packageName" - def balSourceDir = "$pluginLocalDir/$packageName" - if (file(balDestinationDir).exists()) { - file(balDestinationDir).deleteDir() - } - copy { - from balSourceDir - into balDestinationDir - } - println("Successfully copied package ${packageOrg}/${packageName} from local/bala the central.ballerina.io/bala directory") - } -} - -build { - dependsOn buildCompilerPlugin -} diff --git a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/.gitignore b/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/.gitignore deleted file mode 100644 index 010e3c58..00000000 --- a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/.gitignore +++ /dev/null @@ -1,15 +0,0 @@ -# Ballerina generates this directory during the compilation of a package. -# It contains compiler-generated artifacts and the final executable if this is an application package. -target/ - -# Ballerina maintains the compiler-generated source code here. -# Remove this if you want to commit generated sources. -generated/ - -# Contains configuration values used during development time. -# See https://ballerina.io/learn/provide-values-to-configurable-variables/ for more details. -Config.toml - -## Ballerina maintains the dependencies of the project here. -## Ignored since this is a test resource. -Dependencies.toml diff --git a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/Ballerina.toml b/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/Ballerina.toml deleted file mode 100644 index deab2687..00000000 --- a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/Ballerina.toml +++ /dev/null @@ -1,5 +0,0 @@ -[package] -org = "invalid" -name = "invalid_module_rules_static_code_analyzer" -version = "0.1.0" -distribution = "2201.9.1" diff --git a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/CompilerPlugin.toml b/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/CompilerPlugin.toml deleted file mode 100644 index 57174cdf..00000000 --- a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/CompilerPlugin.toml +++ /dev/null @@ -1,5 +0,0 @@ -[plugin] # Have to check if below path is needed -class = "org.invalid.rules.CustomStaticCodeAnalyzer" - -[[dependency]] -path = "../build/libs/invalid-rules-plugin-0.1.0.jar" diff --git a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/README.md b/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/README.md deleted file mode 100644 index 13592aaf..00000000 --- a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/README.md +++ /dev/null @@ -1 +0,0 @@ -# invalid/invalid_module_rules_static_code_analyzer \ No newline at end of file diff --git a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/main.bal b/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/main.bal deleted file mode 100644 index 42249c2b..00000000 --- a/test-compiler-plugins/invalid-rules-plugin/invalid-module-rules-static-code-analyzer/main.bal +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). -// -// WSO2 Inc. licenses this file to you under the Apache License, -// Version 2.0 (the "License"); you may not use this file except -// in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, -// software distributed under the License is distributed on an -// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -// KIND, either express or implied. See the License for the -// specific language governing permissions and limitations -// under the License. diff --git a/test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomAnalysisTask.java b/test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomAnalysisTask.java deleted file mode 100644 index c555e0e1..00000000 --- a/test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomAnalysisTask.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.invalid.rules; - -import io.ballerina.compiler.syntax.tree.ModulePartNode; -import io.ballerina.projects.Document; -import io.ballerina.projects.Module; -import io.ballerina.projects.plugins.AnalysisTask; -import io.ballerina.projects.plugins.SyntaxNodeAnalysisContext; -import io.ballerina.scan.Reporter; -import io.ballerina.scan.ScannerContext; - -/** - * Represents a syntax node analysis task for a custom module. - * - * @since 0.1.0 - * */ -public class CustomAnalysisTask implements AnalysisTask { - private final Reporter reporter; - - public CustomAnalysisTask(ScannerContext scannerContext) { - this.reporter = scannerContext.getReporter(); - } - - @Override - public void perform(SyntaxNodeAnalysisContext context) { - Module module = context.currentPackage().module(context.moduleId()); - Document document = module.document(context.documentId()); - ModulePartNode modulePartNode = (ModulePartNode) context.node(); - reporter.reportIssue(document, modulePartNode.location(), 1); - } -} diff --git a/test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomCodeAnalyzer.java b/test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomCodeAnalyzer.java deleted file mode 100644 index 471674e4..00000000 --- a/test-compiler-plugins/invalid-rules-plugin/src/main/java/org/invalid/rules/CustomCodeAnalyzer.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com). - * - * WSO2 LLC. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.invalid.rules; - -import io.ballerina.compiler.syntax.tree.SyntaxKind; -import io.ballerina.projects.plugins.CodeAnalysisContext; -import io.ballerina.projects.plugins.CodeAnalyzer; -import io.ballerina.scan.ScannerContext; - -/** - * Represents a code analyzer for a custom module. - * - * @since 0.1.0 - * */ -public class CustomCodeAnalyzer extends CodeAnalyzer { - private final ScannerContext scannerContext; - - public CustomCodeAnalyzer(ScannerContext scannerContext) { - this.scannerContext = scannerContext; - } - - @Override - public void init(CodeAnalysisContext codeAnalysisContext) { - codeAnalysisContext.addSyntaxNodeAnalysisTask(new CustomAnalysisTask(scannerContext), SyntaxKind.MODULE_PART); - } -} diff --git a/test-compiler-plugins/invalid-rules-plugin/src/main/resources/rules.json b/test-compiler-plugins/invalid-rules-plugin/src/main/resources/rules.json deleted file mode 100644 index b1c10a54..00000000 --- a/test-compiler-plugins/invalid-rules-plugin/src/main/resources/rules.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "id": 1, - "kind": "CODE_SMELL", - "description": "rule 1" -} \ No newline at end of file From 5330dc29a956adb5c0f9ef9c27e5ddbdcd6043fe Mon Sep 17 00:00:00 2001 From: Nureka Rodrigo <127966714+nureka-rodrigo@users.noreply.github.com> Date: Fri, 27 Jun 2025 14:47:47 +0530 Subject: [PATCH 2/3] Update line format issue in `CustomRule` Co-authored-by: Tharik Kanaka --- .../io/ballerina/examplestaticcodeanalyzer/CustomRule.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java index 48da4a92..c0b17879 100644 --- a/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java +++ b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java @@ -9,8 +9,7 @@ public enum CustomRule implements Rule { RULE_1(1, "rule 1", CODE_SMELL), - RULE_2(2, "rule 2", - BUG), + RULE_2(2, "rule 2", BUG), RULE_3(3, "rule 3", VULNERABILITY); private final int id; From bceadb8a5a58a1250e03cd8f659fed52825d3b5a Mon Sep 17 00:00:00 2001 From: Nureka Rodrigo Date: Fri, 5 Sep 2025 11:39:37 +0530 Subject: [PATCH 3/3] Bump version to 0.12.0-SNAPSHOT and update related dependencies --- gradle.properties | 2 +- .../src/main/java/io/ballerina/scan/RuleProvider.java | 2 +- scan-command/tool-scan/BalTool.toml | 2 +- scan-command/tool-scan/Ballerina.toml | 2 +- scan-command/tool-scan/Dependencies.toml | 4 ++-- .../io/ballerina/examplestaticcodeanalyzer/CustomRule.java | 5 +++++ .../examplestaticcodeanalyzer/CustomRuleProvider.java | 5 +++++ .../io/ballerinax/examplestaticcodeanalyzer/CustomRule.java | 5 +++++ .../examplestaticcodeanalyzer/CustomRuleProvider.java | 5 +++++ .../org/example/examplestaticcodeanalyzer/CustomRule.java | 5 +++++ .../examplestaticcodeanalyzer/CustomRuleProvider.java | 5 +++++ 11 files changed, 36 insertions(+), 6 deletions(-) diff --git a/gradle.properties b/gradle.properties index fdfa4b60..ab6cb996 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ group=io.ballerina.scan -version=0.11.1-SNAPSHOT +version=0.12.0-SNAPSHOT # Plugin versions spotbugsPluginVersion=6.1.5 diff --git a/scan-command/src/main/java/io/ballerina/scan/RuleProvider.java b/scan-command/src/main/java/io/ballerina/scan/RuleProvider.java index 74c0af6d..23958d33 100644 --- a/scan-command/src/main/java/io/ballerina/scan/RuleProvider.java +++ b/scan-command/src/main/java/io/ballerina/scan/RuleProvider.java @@ -23,7 +23,7 @@ * This interface should be implemented by classes that extend CodeAnalyzer * and want to provide rules through the service loading mechanism. * - * @since 0.10.0 + * @since 0.12.0 */ public interface RuleProvider { /** diff --git a/scan-command/tool-scan/BalTool.toml b/scan-command/tool-scan/BalTool.toml index 7df0d6c1..d422560c 100644 --- a/scan-command/tool-scan/BalTool.toml +++ b/scan-command/tool-scan/BalTool.toml @@ -2,4 +2,4 @@ id = "scan" [[dependency]] -path = "../build/libs/scan-command-0.11.1-SNAPSHOT.jar" +path = "../build/libs/scan-command-0.12.0-SNAPSHOT.jar" diff --git a/scan-command/tool-scan/Ballerina.toml b/scan-command/tool-scan/Ballerina.toml index 6214f637..f80ded18 100644 --- a/scan-command/tool-scan/Ballerina.toml +++ b/scan-command/tool-scan/Ballerina.toml @@ -1,7 +1,7 @@ [package] org = "ballerina" name = "tool_scan" -version = "0.11.1-SNAPSHOT" +version = "0.12.0-SNAPSHOT" distribution = "2201.12.0" authors = ["Ballerina"] keywords = ["scan", "static code analysis"] diff --git a/scan-command/tool-scan/Dependencies.toml b/scan-command/tool-scan/Dependencies.toml index 48fa3206..cf8391c3 100644 --- a/scan-command/tool-scan/Dependencies.toml +++ b/scan-command/tool-scan/Dependencies.toml @@ -5,12 +5,12 @@ [ballerina] dependencies-toml-version = "2" -distribution-version = "2201.12.9" +distribution-version = "2201.12.7" [[package]] org = "ballerina" name = "tool_scan" -version = "0.11.1-SNAPSHOT" +version = "0.12.0-SNAPSHOT" modules = [ {org = "ballerina", packageName = "tool_scan", moduleName = "tool_scan"} ] diff --git a/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java index c0b17879..ae11f971 100644 --- a/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java +++ b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRule.java @@ -7,6 +7,11 @@ import static io.ballerina.scan.RuleKind.CODE_SMELL; import static io.ballerina.scan.RuleKind.VULNERABILITY; +/** + * Represents a custom rule for static code analysis. + * + * @since 0.12.0 + */ public enum CustomRule implements Rule { RULE_1(1, "rule 1", CODE_SMELL), RULE_2(2, "rule 2", BUG), diff --git a/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRuleProvider.java b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRuleProvider.java index 38a00d6e..58140910 100644 --- a/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRuleProvider.java +++ b/test-compiler-plugins/ballerina-plugin/src/main/java/io/ballerina/examplestaticcodeanalyzer/CustomRuleProvider.java @@ -23,6 +23,11 @@ import java.util.List; +/** + * Represents a custom rule provider for static code analysis. + * + * @since 0.12.0 + */ public class CustomRuleProvider implements RuleProvider { /** * Returns all Rule instances provided by this analyzer. diff --git a/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRule.java b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRule.java index 44df77dc..6ec7bfcb 100644 --- a/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRule.java +++ b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRule.java @@ -7,6 +7,11 @@ import static io.ballerina.scan.RuleKind.CODE_SMELL; import static io.ballerina.scan.RuleKind.VULNERABILITY; +/** + * Represents a custom rule for static code analysis. + * + * @since 0.12.0 + */ public enum CustomRule implements Rule { RULE_1(1, "rule 1", CODE_SMELL), RULE_2(2, "rule 2", diff --git a/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRuleProvider.java b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRuleProvider.java index 6fe75b93..374fe35b 100644 --- a/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRuleProvider.java +++ b/test-compiler-plugins/ballerinax-plugin/src/main/java/io/ballerinax/examplestaticcodeanalyzer/CustomRuleProvider.java @@ -23,6 +23,11 @@ import java.util.List; +/** + * Represents a custom rule provider for static code analysis. + * + * @since 0.12.0 + */ public class CustomRuleProvider implements RuleProvider { /** * Returns all Rule instances provided by this analyzer. diff --git a/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRule.java b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRule.java index 235938d3..1e33f071 100644 --- a/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRule.java +++ b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRule.java @@ -7,6 +7,11 @@ import static io.ballerina.scan.RuleKind.CODE_SMELL; import static io.ballerina.scan.RuleKind.VULNERABILITY; +/** + * Represents a custom rule for static code analysis. + * + * @since 0.12.0 + */ public enum CustomRule implements Rule { RULE_1(1, "rule 1", CODE_SMELL), RULE_2(2, "rule 2", diff --git a/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRuleProvider.java b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRuleProvider.java index 51624374..254cefa8 100644 --- a/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRuleProvider.java +++ b/test-compiler-plugins/exampleOrg-plugin/src/main/java/org/example/examplestaticcodeanalyzer/CustomRuleProvider.java @@ -23,6 +23,11 @@ import java.util.List; +/** + * Represents a custom rule provider for static code analysis. + * + * @since 0.12.0 + */ public class CustomRuleProvider implements RuleProvider { /** * Returns all Rule instances provided by this analyzer.