Skip to content

Commit 95ccc08

Browse files
Add default ModuleMetadata implementation
1 parent ea5799a commit 95ccc08

File tree

7 files changed

+106
-28
lines changed

7 files changed

+106
-28
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.java;
18+
19+
import org.sonar.api.config.Configuration;
20+
import org.sonar.java.model.JavaVersionImpl;
21+
import org.sonar.plugins.java.api.JavaVersion;
22+
import org.sonar.plugins.java.api.internal.ModuleMetadata;
23+
24+
public class DefaultModuleMetadata implements ModuleMetadata {
25+
26+
private final JavaVersion javaVersion;
27+
private final String moduleKey;
28+
29+
public DefaultModuleMetadata(SonarComponents sonarComponents, Configuration configuration) {
30+
this.javaVersion = JavaVersionImpl.readFromConfiguration(configuration);
31+
this.moduleKey = sonarComponents.getModuleKey();
32+
}
33+
34+
@Override
35+
public JavaVersion javaVersion() {
36+
return javaVersion;
37+
}
38+
39+
@Override
40+
public String moduleKey() {
41+
return moduleKey;
42+
}
43+
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* SonarQube Java
3+
* Copyright (C) 2012-2025 SonarSource SA
4+
* mailto:info AT sonarsource DOT com
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
* See the Sonar Source-Available License for more details.
13+
*
14+
* You should have received a copy of the Sonar Source-Available License
15+
* along with this program; if not, see https://sonarsource.com/license/ssal/
16+
*/
17+
package org.sonar.java.annotations;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
/**
26+
* This annotation should be placed on api elements that are meant to only be consumed by plugins maintained by SonarSource.
27+
* Elements with this annotation are not stable and can change at any time.
28+
*/
29+
@Retention(RetentionPolicy.CLASS)
30+
@Target({ElementType.ANNOTATION_TYPE, ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD, ElementType.TYPE})
31+
@Documented
32+
public @interface Internal {
33+
}

java-frontend/src/main/java/org/sonar/java/model/JavaVersionImpl.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
package org.sonar.java.model;
1818

1919
import java.util.Locale;
20+
import java.util.Optional;
2021
import org.slf4j.Logger;
2122
import org.slf4j.LoggerFactory;
23+
import org.sonar.api.config.Configuration;
2224
import org.sonar.plugins.java.api.JavaVersion;
2325

2426
public class JavaVersionImpl implements JavaVersion {
@@ -230,4 +232,22 @@ private static int convertJavaVersionString(String javaVersion) {
230232
return Integer.parseInt(cleanedVersion);
231233
}
232234

235+
public static JavaVersion readFromConfiguration(Configuration config) {
236+
Optional<String> javaVersionAsString = config.get(SOURCE_VERSION);
237+
if (!javaVersionAsString.isPresent()) {
238+
return new JavaVersionImpl();
239+
}
240+
String enablePreviewAsString = config.get(ENABLE_PREVIEW).orElse("false");
241+
242+
JavaVersion javaVersion = fromString(javaVersionAsString.get(), enablePreviewAsString);
243+
if (javaVersion.arePreviewFeaturesEnabled() && javaVersion.asInt() < MAX_SUPPORTED) {
244+
LOG.warn("sonar.java.enablePreview is set but will be discarded as the Java version is less than the max" +
245+
" supported version ({} < {})", javaVersion.asInt(), MAX_SUPPORTED);
246+
javaVersion = new JavaVersionImpl(javaVersion.asInt(), false);
247+
}
248+
LOG.info("Configured Java source version ({}): {}, preview features enabled ({}): {}",
249+
SOURCE_VERSION, javaVersion.asInt(), ENABLE_PREVIEW, javaVersion.arePreviewFeaturesEnabled());
250+
return javaVersion;
251+
}
252+
233253
}

java-frontend/src/main/java/org/sonar/plugins/java/api/internal/ModuleMetadata.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
package org.sonar.plugins.java.api.internal;
1818

1919
import org.sonar.api.scanner.ScannerSide;
20-
import org.sonar.java.annotations.Beta;
20+
import org.sonar.java.annotations.Internal;
2121
import org.sonar.plugins.java.api.JavaVersion;
2222

2323

2424
/**
2525
* Interface to access metadata about the module being analyzed by a Sensor.
26-
* For internal use only, this API will not be supported for custom plugins.
2726
*/
28-
@Beta
27+
@Internal
2928
@ScannerSide
3029
public interface ModuleMetadata {
3130

sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaPlugin.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.sonar.api.config.PropertyDefinition;
2929
import org.sonar.java.AnalysisWarningsWrapper;
3030
import org.sonar.java.DefaultJavaResourceLocator;
31+
import org.sonar.java.DefaultModuleMetadata;
3132
import org.sonar.java.JavaConstants;
3233
import org.sonar.java.SonarComponents;
3334
import org.sonar.java.classpath.ClasspathForMain;
@@ -36,8 +37,8 @@
3637
import org.sonar.java.classpath.ClasspathProperties;
3738
import org.sonar.java.filters.PostAnalysisIssueFilter;
3839
import org.sonar.java.jsp.Jasper;
39-
import org.sonar.java.telemetry.NoOpTelemetry;
4040
import org.sonar.java.telemetry.DefaultTelemetry;
41+
import org.sonar.java.telemetry.NoOpTelemetry;
4142
import org.sonar.plugins.java.api.JavaVersion;
4243
import org.sonar.plugins.java.api.caching.SonarLintCache;
4344
import org.sonar.plugins.surefire.SurefireExtensions;
@@ -62,6 +63,7 @@ public void define(Context context) {
6263
list.add(DroppedPropertiesSensor.class);
6364
list.add(JavaSonarWayProfile.class);
6465
list.add(ClasspathForMain.class);
66+
list.add(DefaultModuleMetadata.class);
6567

6668
ExternalReportExtensions.define(context);
6769
}
@@ -105,8 +107,7 @@ public void define(Context context) {
105107
" When a package or class is found in both the unnamed module and a named one, modularization is broken." +
106108
" As a result, the parser may be unable to build the project semantic successfully, leading to analysis failure." +
107109
" This parameter allows users to bypass Java platform modularity enforcement to prevent analysis failure." +
108-
"</p>"
109-
)
110+
"</p>")
110111
.category(JavaConstants.JAVA_CATEGORY)
111112
.subCategory("Language")
112113
.onConfigScopes(Set.of(PropertyDefinition.ConfigScope.PROJECT))

sonar-java-plugin/src/main/java/org/sonar/plugins/java/JavaSensor.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void execute(SensorContext context) {
111111

112112
Measurer measurer = new Measurer(context, noSonarFilter);
113113

114-
JavaVersion javaVersion = getJavaVersion();
114+
JavaVersion javaVersion = JavaVersionImpl.readFromConfiguration(settings);
115115
telemetry.aggregateAsSortedSet(JAVA_LANGUAGE_VERSION, javaVersion.toString());
116116
telemetry.aggregateAsCounter(JAVA_MODULE_COUNT, 1L);
117117

@@ -181,24 +181,6 @@ private Iterable<InputFile> javaFiles(InputFile.Type type) {
181181
return fs.inputFiles(fs.predicates().and(fs.predicates().hasLanguage(Java.KEY), fs.predicates().hasType(type)));
182182
}
183183

184-
private JavaVersion getJavaVersion() {
185-
Optional<String> javaVersionAsString = settings.get(JavaVersion.SOURCE_VERSION);
186-
if (!javaVersionAsString.isPresent()) {
187-
return new JavaVersionImpl();
188-
}
189-
String enablePreviewAsString = settings.get(JavaVersion.ENABLE_PREVIEW).orElse("false");
190-
191-
JavaVersion javaVersion = JavaVersionImpl.fromString(javaVersionAsString.get(), enablePreviewAsString);
192-
if (javaVersion.arePreviewFeaturesEnabled() && javaVersion.asInt() < JavaVersionImpl.MAX_SUPPORTED) {
193-
LOG.warn("sonar.java.enablePreview is set but will be discarded as the Java version is less than the max" +
194-
" supported version ({} < {})", javaVersion.asInt(), JavaVersionImpl.MAX_SUPPORTED);
195-
javaVersion = new JavaVersionImpl(javaVersion.asInt(), false);
196-
}
197-
LOG.info("Configured Java source version ({}): {}, preview features enabled ({}): {}",
198-
JavaVersion.SOURCE_VERSION, javaVersion.asInt(), JavaVersion.ENABLE_PREVIEW, javaVersion.arePreviewFeaturesEnabled());
199-
return javaVersion;
200-
}
201-
202184
@Override
203185
public String toString() {
204186
return getClass().getSimpleName();

sonar-java-plugin/src/test/java/org/sonar/plugins/java/JavaPluginTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,13 @@ void sonarLint_9_9_extensions() {
4444
.contains(SonarLintCache.class);
4545
}
4646

47-
4847
@Test
4948
void sonarqube_9_9_extensions() {
5049
SonarRuntime sqCommunity = SonarRuntimeImpl.forSonarQube(VERSION_9_9, SonarQubeSide.SCANNER, SonarEdition.COMMUNITY);
5150
Plugin.Context context = new Plugin.Context(sqCommunity);
5251
javaPlugin.define(context);
5352
assertThat(context.getExtensions())
54-
.hasSize(35)
53+
.hasSize(36)
5554
.doesNotContain(Jasper.class);
5655
}
5756

@@ -61,7 +60,7 @@ void sonarqube_9_9_commercial_extensions() {
6160
Plugin.Context context = new Plugin.Context(sqEnterprise);
6261
javaPlugin.define(context);
6362
assertThat(context.getExtensions())
64-
.hasSize(36)
63+
.hasSize(37)
6564
.contains(Jasper.class);
6665
}
6766

0 commit comments

Comments
 (0)