-
Notifications
You must be signed in to change notification settings - Fork 706
SONARJAVA-5845 Add new ModuleMetadata public API #5343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
73192ad
SONARJAVA-5845 Add new ModuleMetadata public API
leonardo-pilastri-sonarsource 42902e9
Improve coverage
leonardo-pilastri-sonarsource 30fef41
Avoid accessing SonarComponents at injection time to prevent NPE
leonardo-pilastri-sonarsource 77f09c9
Improve coverage
leonardo-pilastri-sonarsource dd13053
Review changes
leonardo-pilastri-sonarsource File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
java-frontend/src/main/java/org/sonar/java/DefaultModuleMetadata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) 2012-2025 SonarSource SA | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.sonar.api.batch.bootstrap.ProjectDefinition; | ||
| import org.sonar.api.config.Configuration; | ||
| import org.sonar.java.model.JavaVersionImpl; | ||
| import org.sonar.java.utils.ModuleMetadataUtils; | ||
| import org.sonar.plugins.java.api.JavaVersion; | ||
| import org.sonar.plugins.java.api.internal.ModuleMetadata; | ||
|
|
||
| import static org.sonar.java.SonarComponents.SONAR_IGNORE_UNNAMED_MODULE_FOR_SPLIT_PACKAGE; | ||
|
|
||
| public class DefaultModuleMetadata implements ModuleMetadata { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(DefaultModuleMetadata.class); | ||
|
|
||
| private final JavaVersion javaVersion; | ||
| private final ProjectDefinition projectDefinition; | ||
| private final boolean ignoreUnnamedModuleForSplitPackage; | ||
|
|
||
| public DefaultModuleMetadata(ProjectDefinition projectDefinition, Configuration configuration) { | ||
| this.javaVersion = JavaVersionImpl.readFromConfiguration(configuration); | ||
| this.projectDefinition = projectDefinition; | ||
| this.ignoreUnnamedModuleForSplitPackage = configuration.getBoolean(SONAR_IGNORE_UNNAMED_MODULE_FOR_SPLIT_PACKAGE).orElse(false); | ||
| } | ||
|
|
||
| @Override | ||
| public JavaVersion javaVersion() { | ||
| return javaVersion; | ||
| } | ||
|
|
||
| @Override | ||
| public String moduleKey() { | ||
| var moduleKey = ModuleMetadataUtils.getModuleKey(projectDefinition); | ||
| if (moduleKey.isEmpty()) { | ||
| LOG.warn("Unable to determine module key, using empty string as fallback"); | ||
| } | ||
| return moduleKey; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean shouldIgnoreUnnamedModuleForSplitPackage() { | ||
| return ignoreUnnamedModuleForSplitPackage; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
java-frontend/src/main/java/org/sonar/java/utils/ModuleMetadataUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) 2012-2025 SonarSource SA | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java.utils; | ||
|
|
||
| import javax.annotation.CheckForNull; | ||
| import javax.annotation.Nullable; | ||
| import org.sonar.api.batch.bootstrap.ProjectDefinition; | ||
|
|
||
| public class ModuleMetadataUtils { | ||
|
|
||
| private ModuleMetadataUtils() { | ||
| // utility class | ||
| } | ||
|
|
||
| public static String getModuleKey(@Nullable ProjectDefinition projectDefinition) { | ||
| var root = getRootProject(projectDefinition); | ||
| if (root != null && projectDefinition != null) { | ||
| var rootBase = root.getBaseDir().toPath(); | ||
| var moduleBase = projectDefinition.getBaseDir().toPath(); | ||
| return rootBase.relativize(moduleBase).toString().replace('\\', '/'); | ||
| } | ||
| return ""; | ||
| } | ||
|
|
||
| @CheckForNull | ||
| public static ProjectDefinition getRootProject(@Nullable ProjectDefinition projectDefinition) { | ||
| ProjectDefinition current = projectDefinition; | ||
| if (current == null) { | ||
| return null; | ||
| } | ||
| while (current.getParent() != null) { | ||
| current = current.getParent(); | ||
| } | ||
| return current; | ||
| } | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
java-frontend/src/main/java/org/sonar/java/utils/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) 2012-2025 SonarSource SA | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| @ParametersAreNonnullByDefault | ||
| package org.sonar.java.utils; | ||
|
|
||
| import javax.annotation.ParametersAreNonnullByDefault; | ||
|
|
48 changes: 48 additions & 0 deletions
48
java-frontend/src/main/java/org/sonar/plugins/java/api/internal/ModuleMetadata.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) 2012-2025 SonarSource SA | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.plugins.java.api.internal; | ||
|
|
||
| import org.sonar.api.batch.ScannerSide; | ||
| import org.sonar.java.annotations.Beta; | ||
| import org.sonar.plugins.java.api.JavaVersion; | ||
|
|
||
|
|
||
| /** | ||
| * Interface to access metadata about the module being analyzed by a Sensor. | ||
| * For internal use only, this API will not be supported for custom plugins. | ||
| */ | ||
| @Beta | ||
| @ScannerSide | ||
| public interface ModuleMetadata { | ||
|
|
||
| /** | ||
| * Returns the Java version of the module being analyzed. | ||
| */ | ||
| JavaVersion javaVersion(); | ||
|
|
||
| /** | ||
| * Returns the module key of the module being analyzed. | ||
| */ | ||
| String moduleKey(); | ||
|
|
||
| /** | ||
| * Describes whether input files should be parsed while ignoring unnamed split modules. | ||
| */ | ||
| boolean shouldIgnoreUnnamedModuleForSplitPackage(); | ||
|
|
||
| } | ||
|
|
||
82 changes: 82 additions & 0 deletions
82
java-frontend/src/test/java/org/sonar/java/DefaultModuleMetadataTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| /* | ||
| * SonarQube Java | ||
| * Copyright (C) 2012-2025 SonarSource SA | ||
| * mailto:info AT sonarsource DOT com | ||
| * | ||
| * This program is free software; you can redistribute it and/or | ||
| * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
| * See the Sonar Source-Available License for more details. | ||
| * | ||
| * You should have received a copy of the Sonar Source-Available License | ||
| * along with this program; if not, see https://sonarsource.com/license/ssal/ | ||
| */ | ||
| package org.sonar.java; | ||
|
|
||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.sonar.api.config.Configuration; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.Mockito.doReturn; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.sonar.java.TestUtils.mockProjectDefinition; | ||
|
|
||
| class DefaultModuleMetadataTest { | ||
|
|
||
| @Test | ||
| void test() { | ||
| var projectDefinition = mockProjectDefinition(); | ||
| var config = mockConfiguration(); | ||
| var defaultModuleMetadata = new DefaultModuleMetadata(projectDefinition, config); | ||
|
|
||
| assertThat(defaultModuleMetadata.moduleKey()).isEqualTo("pmodule/cmodule"); | ||
| assertThat(defaultModuleMetadata.javaVersion().asInt()).isEqualTo(-1); | ||
| assertThat(defaultModuleMetadata.shouldIgnoreUnnamedModuleForSplitPackage()).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void testNullProjectDefinition() { | ||
| var config = mockConfiguration(); | ||
| var defaultModuleMetadata = new DefaultModuleMetadata(null, config); | ||
|
|
||
| assertThat(defaultModuleMetadata.moduleKey()).isEmpty(); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithJavaVersion() { | ||
| var projectDefinition = mockProjectDefinition(); | ||
| var config = mockConfiguration("sonar.java.source", "11"); | ||
| var defaultModuleMetadata = new DefaultModuleMetadata(projectDefinition, config); | ||
|
|
||
| assertThat(defaultModuleMetadata.moduleKey()).isEqualTo("pmodule/cmodule"); | ||
| assertThat(defaultModuleMetadata.javaVersion().asInt()).isEqualTo(11); | ||
| } | ||
|
|
||
| @Test | ||
| void testWithShouldIgnoreUnnamed() { | ||
| var projectDefinition = mockProjectDefinition(); | ||
| var config = mockConfiguration("sonar.java.ignoreUnnamedModuleForSplitPackage", "true"); | ||
| var defaultModuleMetadata = new DefaultModuleMetadata(projectDefinition, config); | ||
|
|
||
| assertThat(defaultModuleMetadata.moduleKey()).isEqualTo("pmodule/cmodule"); | ||
| assertThat(defaultModuleMetadata.shouldIgnoreUnnamedModuleForSplitPackage()).isTrue(); | ||
| } | ||
|
|
||
| private Configuration mockConfiguration(String... keysAndValues) { | ||
| Configuration configuration = mock(Configuration.class); | ||
| for (int i = 0; i < keysAndValues.length; i++) { | ||
| String key = keysAndValues[i++]; | ||
| String value = keysAndValues[i]; | ||
| doReturn(Optional.of(value)).when(configuration).get(key); | ||
| if (value.equals("true") || value.equals("false")) { | ||
| doReturn(Optional.of(Boolean.valueOf(value))).when(configuration).getBoolean(key); | ||
| } | ||
| } | ||
| return configuration; | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.