]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21195 Fix issues raised by SonarQube
authorMatteo Mara <matteo.mara@sonarsource.com>
Mon, 18 Dec 2023 15:26:38 +0000 (16:26 +0100)
committersonartech <sonartech@sonarsource.com>
Thu, 4 Jan 2024 20:02:48 +0000 (20:02 +0000)
sonar-core/src/main/java/org/sonar/classloader/ClassloaderBuilder.java
sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/fs/FileSystemMediumIT.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/SpringScannerContainer.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/repository/language/DefaultLanguagesRepository.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/DirectoryFileVisitor.java
sonar-scanner-engine/src/main/java/org/sonar/scanner/scan/filesystem/ProjectFilePreprocessor.java

index 076865924dd497978477bc00990a2fb97b54e753..5a843212fc9d8025662ce2fcf72b3c2e77499feb 100644 (file)
@@ -117,13 +117,7 @@ public class ClassloaderBuilder {
       throw new IllegalStateException(String.format("The classloader '%s' already exists in the list of previously created classloaders."
         + " Can not create it twice.", key));
     }
-    //TODO: to be checked, the other version of the code is not building
-    ClassRealm realm = AccessController.doPrivileged(new PrivilegedAction<ClassRealm>() {
-      @Override
-      public ClassRealm run() {
-        return new ClassRealm(key, baseClassloader);
-      }
-    });
+    ClassRealm realm = AccessController.doPrivileged((PrivilegedAction<ClassRealm>) () -> new ClassRealm(key, baseClassloader));
     realm.setStrategy(LoadingOrder.PARENT_FIRST.strategy);
     newRealmsByKey.put(key, new NewRealm(realm));
     return this;
index 1b3ac84f4dc48afe9d42f1469dc751e8723ab1e7..0229752156fab10a46d50c40967cd81995a2ff00 100644 (file)
@@ -133,7 +133,7 @@ public class FileSystemMediumIT {
         .put("sonar.sources", "src")
         .build())
       .execute())
-        .isInstanceOf(MessageException.class);
+      .isInstanceOf(MessageException.class);
   }
 
   @Test
@@ -695,8 +695,8 @@ public class FileSystemMediumIT {
         .put("sonar.sources", "src,src/sample.xoo")
         .build())
       .execute())
-        .isInstanceOf(MessageException.class)
-        .hasMessage("File src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files");
+      .isInstanceOf(MessageException.class)
+      .hasMessage("File src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files");
   }
 
   // SONAR-9574
@@ -714,8 +714,8 @@ public class FileSystemMediumIT {
         .put("module1.sonar.sources", "src")
         .build())
       .execute())
-        .isInstanceOf(MessageException.class)
-        .hasMessage("File module1/src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files");
+      .isInstanceOf(MessageException.class)
+      .hasMessage("File module1/src/sample.xoo can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files");
   }
 
   // SONAR-5330
@@ -870,7 +870,7 @@ public class FileSystemMediumIT {
     File srcDir = new File(baseDir, "src");
     srcDir.mkdir();
 
-    tester.addLanguage("xoo3", "xoo3",false,  ".xoo3");
+    tester.addLanguage("xoo3", "xoo3", false, ".xoo3");
 
     writeFile(srcDir, "sample.xoo3", "Sample xoo\ncontent");
     writeFile(srcDir, "sample2.xoo3", "Sample xoo 2\ncontent");
@@ -1236,7 +1236,7 @@ public class FileSystemMediumIT {
         .build())
       .execute();
 
-    assertAnalysedFiles(result,  "src/srcSubDir/srcSub.xoo");
+    assertAnalysedFiles(result, "src/srcSubDir/srcSub.xoo");
   }
 
   @Test
index d7cecd16ed3cd97e7df44998dc2a9ffeed279d73..efee457d5a15b6222abdf7ec73b214afbf5e57c4 100644 (file)
@@ -330,10 +330,10 @@ public class SpringScannerContainer extends SpringComponentContainer {
     });
 
     BranchConfiguration branchConfig = getComponentByType(BranchConfiguration.class);
-    if (branchConfig.branchType() == BranchType.PULL_REQUEST) {
+    if (branchConfig.branchType() == BranchType.PULL_REQUEST && LOG.isInfoEnabled()) {
       LOG.info("Pull request {} for merge into {} from {}", branchConfig.pullRequestKey(), pullRequestBaseToDisplayName(branchConfig.targetBranchName()),
         branchConfig.branchName());
-    } else if (branchConfig.branchName() != null) {
+    } else if (branchConfig.branchName() != null && LOG.isInfoEnabled()) {
       LOG.info("Branch name: {}", branchConfig.branchName());
     }
 
index 607587950b6c4e9f78fac8350b055d0f2c2011bc..0dd80451fa2dedc66fe46fa04502f6f74d894ab4 100644 (file)
@@ -30,6 +30,7 @@ import java.util.stream.Collectors;
 import javax.annotation.CheckForNull;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import javax.annotation.concurrent.Immutable;
 import org.sonar.api.Startable;
 import org.sonar.api.config.Configuration;
 import org.sonar.api.resources.Languages;
@@ -40,6 +41,7 @@ import org.sonarqube.ws.client.GetRequest;
  * Languages repository using {@link Languages}
  * @since 4.4
  */
+@Immutable
 public class DefaultLanguagesRepository implements LanguagesRepository, Startable {
   private static final Logger LOG = LoggerFactory.getLogger(DefaultLanguagesRepository.class);
   private static final String LANGUAGES_WS_URL = "/api/languages/list";
@@ -75,9 +77,8 @@ public class DefaultLanguagesRepository implements LanguagesRepository, Startabl
   }
 
   private Language populateFileSuffixesAndPatterns(SupportedLanguageDto lang) {
-    String propertyFragment = PROPERTY_FRAGMENT_MAP.getOrDefault(lang.getKey(), lang.getKey());
-    lang.setFileSuffixes(properties.getStringArray(String.format("sonar.%s.file.suffixes", propertyFragment)));
-    lang.setFilenamePatterns(properties.getStringArray(String.format("sonar.%s.file.patterns", propertyFragment)));
+    lang.setFileSuffixes(getFileSuffixes(lang.getKey()));
+    lang.setFilenamePatterns(getFilenamePatterns(lang.getKey()));
     if (lang.filenamePatterns() == null && lang.getFileSuffixes() == null) {
       LOG.debug("Language '{}' cannot be detected as it has neither suffixes nor patterns.", lang.getName());
     }
@@ -85,7 +86,15 @@ public class DefaultLanguagesRepository implements LanguagesRepository, Startabl
   }
 
   private String[] getFileSuffixes(String languageKey) {
-    String propName = String.format("sonar.%s.file.suffixes", PROPERTY_FRAGMENT_MAP.getOrDefault(languageKey, languageKey));
+    return getPropertyForLanguage("sonar.%s.file.suffixes", languageKey);
+  }
+
+  private String[] getFilenamePatterns(String languageKey) {
+    return getPropertyForLanguage("sonar.%s.file.patterns", languageKey);
+  }
+
+  private String[] getPropertyForLanguage(String propertyPattern, String languageKey) {
+    String propName = String.format(propertyPattern, PROPERTY_FRAGMENT_MAP.getOrDefault(languageKey, languageKey));
     return properties.getStringArray(propName);
   }
 
index 57720692eb0558a8effdce5cb5476811b3a4f049..ea0bacb9a594709d860e670fde9020b2411b70bd 100644 (file)
@@ -47,7 +47,8 @@ public class DirectoryFileVisitor implements FileVisitor<Path> {
   private final InputModuleHierarchy inputModuleHierarchy;
   private final InputFile.Type type;
 
-  DirectoryFileVisitor(FileVisitAction fileVisitAction, DefaultInputModule module, ModuleExclusionFilters moduleExclusionFilters, InputModuleHierarchy inputModuleHierarchy, InputFile.Type type) {
+  DirectoryFileVisitor(FileVisitAction fileVisitAction, DefaultInputModule module, ModuleExclusionFilters moduleExclusionFilters,
+    InputModuleHierarchy inputModuleHierarchy, InputFile.Type type) {
     this.fileVisitAction = fileVisitAction;
     this.module = module;
     this.moduleExclusionFilters = moduleExclusionFilters;
index 31a467fd5ce75e496a3887b41cb97656e10ef9bb..4f2b95708609fed5fdb7fe6a6a84b11ac1d430ff 100644 (file)
@@ -54,7 +54,6 @@ import static java.util.Collections.singletonList;
 public class ProjectFilePreprocessor {
 
   private static final Logger LOG = LoggerFactory.getLogger(ProjectFilePreprocessor.class);
-  private static final String TELEMETRY_STEP_NAME = "file.preprocessing";
 
   private final AnalysisWarnings analysisWarnings;
   private final IgnoreCommand ignoreCommand;
@@ -113,17 +112,13 @@ public class ProjectFilePreprocessor {
       pluralizeWithCount("preprocessed file", totalFilesPreprocessed)));
 
     int excludedFileByPatternCount = exclusionCounter.getByPatternsCount();
-    if (projectExclusionFilters.hasPattern() || excludedFileByPatternCount > 0) {
-      if (LOG.isInfoEnabled()) {
-        LOG.info("{} ignored because of inclusion/exclusion patterns", pluralizeWithCount("file", excludedFileByPatternCount));
-      }
+    if ((projectExclusionFilters.hasPattern() || excludedFileByPatternCount > 0) && LOG.isInfoEnabled()) {
+      LOG.info("{} ignored because of inclusion/exclusion patterns", pluralizeWithCount("file", excludedFileByPatternCount));
     }
 
     int excludedFileByScmCount = exclusionCounter.getByScmCount();
-    if (useScmExclusion) {
-      if (LOG.isInfoEnabled()) {
-        LOG.info("{} ignored because of scm ignore settings", pluralizeWithCount("file", excludedFileByScmCount));
-      }
+    if (useScmExclusion && LOG.isInfoEnabled()) {
+      LOG.info("{} ignored because of scm ignore settings", pluralizeWithCount("file", excludedFileByScmCount));
     }
   }