From: Julien HENRY Date: Fri, 10 Oct 2014 09:17:24 +0000 (+0200) Subject: SONAR-5256 Fix NPE and add warning when sonar.importSources=false X-Git-Tag: 4.5.1-RC1~38 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=0d2a019b933e3a90de9c8365bfa4037b9d44d943;p=sonarqube.git SONAR-5256 Fix NPE and add warning when sonar.importSources=false --- diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/SourceHashHolder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/SourceHashHolder.java index 8caec63784f..c10586fda05 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/SourceHashHolder.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/SourceHashHolder.java @@ -19,8 +19,7 @@ */ package org.sonar.plugins.core.issue; -import java.util.Collection; - +import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.SonarIndex; import org.sonar.api.resources.Resource; import org.sonar.batch.scan.LastSnapshots; @@ -28,7 +27,7 @@ import org.sonar.plugins.core.issue.tracking.HashedSequence; import org.sonar.plugins.core.issue.tracking.StringText; import org.sonar.plugins.core.issue.tracking.StringTextComparator; - +import java.util.Collection; public class SourceHashHolder { @@ -66,15 +65,15 @@ public class SourceHashHolder { } public String getSource() { - if (! sourceInitialized) { - source = index.getSource(resource); + if (!sourceInitialized) { + source = StringUtils.defaultString(index.getSource(resource), ""); sourceInitialized = true; } return source; } public String getReferenceSource() { - if (! referenceSourceInitialized) { + if (!referenceSourceInitialized) { if (resource != null) { referenceSource = lastSnapshots.getSource(resource); } @@ -88,7 +87,7 @@ public class SourceHashHolder { } private void initHashesIfNull(Object required) { - if(required == null) { + if (required == null) { initHashes(); } } @@ -97,4 +96,3 @@ public class SourceHashHolder { return getHashedSource().getLinesForHash(getHashedReference().getHash(originLine)); } } - diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java index 1e185487a05..65a7585555e 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ComponentIndexer.java @@ -22,6 +22,8 @@ package org.sonar.batch.scan.filesystem; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.CharMatcher; import com.google.common.io.Files; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; import org.sonar.api.CoreProperties; import org.sonar.api.batch.SonarIndex; @@ -44,6 +46,8 @@ import org.sonar.batch.util.DeprecatedKeyUtils; */ public class ComponentIndexer implements BatchComponent { + private static final Logger LOG = LoggerFactory.getLogger(ComponentIndexer.class); + private final Languages languages; private final Settings settings; private final SonarIndex sonarIndex; @@ -62,6 +66,9 @@ public class ComponentIndexer implements BatchComponent { migration.migrateIfNeeded(module, fs); boolean shouldImportSource = settings.getBoolean(CoreProperties.CORE_IMPORT_SOURCES_PROPERTY); + if (!shouldImportSource) { + LOG.warn("Not importing source will prevent issues to be properly tracked between consecutive analyses"); + } for (InputFile inputFile : fs.inputFiles(fs.predicates().all())) { String languageKey = inputFile.language(); boolean unitTest = InputFile.Type.TEST == inputFile.type(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java index f52fcb98f0a..cbe29014b2f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java @@ -100,9 +100,11 @@ public abstract class SonarIndex implements DirectedGraphAccessornull if not available + * @return source code associated with a specified resource, null if not available + * (for example when sonar.importSources=false) * @since 2.9 */ + @CheckForNull public abstract String getSource(Resource resource); public abstract Project getProject();