Skip to content

Commit cbd5462

Browse files
Add default ModuleMetadata implementation
1 parent 5c443ed commit cbd5462

File tree

6 files changed

+71
-24
lines changed

6 files changed

+71
-24
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+
}

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.sonar.java.AnalysisWarningsWrapper;
3030
import org.sonar.java.DefaultClassPathResolver;
3131
import org.sonar.java.DefaultJavaResourceLocator;
32+
import org.sonar.java.DefaultModuleMetadata;
3233
import org.sonar.java.JavaConstants;
3334
import org.sonar.java.SonarComponents;
3435
import org.sonar.java.classpath.ClasspathForMain;
@@ -64,6 +65,7 @@ public void define(Context context) {
6465
list.add(JavaSonarWayProfile.class);
6566
list.add(ClasspathForMain.class);
6667
list.add(DefaultClassPathResolver.class);
68+
list.add(DefaultModuleMetadata.class);
6769

6870
ExternalReportExtensions.define(context);
6971
}

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void sonarqube_9_9_extensions() {
5050
Plugin.Context context = new Plugin.Context(sqCommunity);
5151
javaPlugin.define(context);
5252
assertThat(context.getExtensions())
53-
.hasSize(36)
53+
.hasSize(37)
5454
.doesNotContain(Jasper.class);
5555
}
5656

@@ -60,7 +60,7 @@ void sonarqube_9_9_commercial_extensions() {
6060
Plugin.Context context = new Plugin.Context(sqEnterprise);
6161
javaPlugin.define(context);
6262
assertThat(context.getExtensions())
63-
.hasSize(37)
63+
.hasSize(38)
6464
.contains(Jasper.class);
6565
}
6666

0 commit comments

Comments
 (0)