Skip to content

Commit

Permalink
Fix some quality flaws
Browse files Browse the repository at this point in the history
  • Loading branch information
henryju committed Jan 3, 2017
1 parent 8f429bd commit 806e0ea
Show file tree
Hide file tree
Showing 18 changed files with 70 additions and 73 deletions.
Expand Up @@ -89,7 +89,7 @@ public DefaultMeasure<G> setFromCore() {
public void doSave() {
Preconditions.checkNotNull(this.value, "Measure value can't be null");
Preconditions.checkNotNull(this.metric, "Measure metric can't be null");
Preconditions.checkState(this.metric.valueType().equals(this.value.getClass()), "Measure value should be of type " + this.metric.valueType());
Preconditions.checkState(this.metric.valueType().equals(this.value.getClass()), "Measure value should be of type %s", this.metric.valueType());
storage.store(this);
}

Expand Down
Expand Up @@ -91,14 +91,14 @@ public Collection<PluginInfo> getPluginInfos() {
@Override
public PluginInfo getPluginInfo(String key) {
PluginInfo info = infosByKeys.get(key);
Preconditions.checkState(info != null, String.format("Plugin [%s] does not exist", key));
Preconditions.checkState(info != null, "Plugin [%s] does not exist", key);
return info;
}

@Override
public Plugin getPluginInstance(String key) {
Plugin instance = pluginInstancesByKeys.get(key);
Preconditions.checkState(instance != null, String.format("Plugin [%s] does not exist", key));
Preconditions.checkState(instance != null, "Plugin [%s] does not exist", key);
return instance;
}

Expand Down
Expand Up @@ -19,31 +19,29 @@
*/
package org.sonar.scanner.cpd.deprecated;

import org.slf4j.Logger;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;

@ScannerSide
public abstract class CpdBlockIndexer {

private static final Logger LOG = Loggers.get(CpdBlockIndexer.class);

abstract boolean isLanguageSupported(String language);

abstract void index(String language);

protected void logExclusions(String[] exclusions, Logger logger) {
protected void logExclusions(String[] exclusions) {
if (exclusions.length > 0) {
StringBuilder message = new StringBuilder("Copy-paste detection exclusions:");
for (String exclusion : exclusions) {
message.append("\n ");
message.append(exclusion);
}

logger.info(message.toString());
LOG.info(message.toString());
}
}

@Override
public String toString() {
return getClass().getSimpleName();
}

}
Expand Up @@ -22,22 +22,22 @@
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Lists;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.CpdMapping;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.internal.pmd.TokenizerBridge;
import org.sonar.scanner.cpd.index.SonarCpdBlockIndex;

public class DefaultCpdBlockIndexer extends CpdBlockIndexer {

private static final Logger LOG = LoggerFactory.getLogger(DefaultCpdBlockIndexer.class);
private static final Logger LOG = Loggers.get(DefaultCpdBlockIndexer.class);

private final CpdMappings mappings;
private final FileSystem fs;
Expand All @@ -60,12 +60,12 @@ public boolean isLanguageSupported(String language) {
public void index(String languageKey) {
CpdMapping mapping = mappings.getMapping(languageKey);
if (mapping == null) {
LOG.debug("No CpdMapping for language " + languageKey);
LOG.debug("No CpdMapping for language {}", languageKey);
return;
}

String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
logExclusions(cpdExclusions, LOG);
logExclusions(cpdExclusions);
FilePredicates p = fs.predicates();
List<InputFile> sourceFiles = Lists.newArrayList(fs.inputFiles(p.and(
p.hasType(InputFile.Type.MAIN),
Expand Down
Expand Up @@ -27,14 +27,14 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.duplications.block.Block;
import org.sonar.duplications.block.BlockChunker;
import org.sonar.duplications.java.JavaStatementBuilder;
Expand All @@ -46,7 +46,7 @@

public class JavaCpdBlockIndexer extends CpdBlockIndexer {

private static final Logger LOG = LoggerFactory.getLogger(JavaCpdBlockIndexer.class);
private static final Logger LOG = Loggers.get(JavaCpdBlockIndexer.class);

private static final int BLOCK_SIZE = 10;

Expand All @@ -68,7 +68,7 @@ public boolean isLanguageSupported(String language) {
@Override
public void index(String languageKey) {
String[] cpdExclusions = settings.getStringArray(CoreProperties.CPD_EXCLUSIONS);
logExclusions(cpdExclusions, LOG);
logExclusions(cpdExclusions);
FilePredicates p = fs.predicates();
List<InputFile> sourceFiles = Lists.newArrayList(fs.inputFiles(p.and(
p.hasType(InputFile.Type.MAIN),
Expand Down
Expand Up @@ -315,15 +315,15 @@ private Bucket doIndex(Resource resource, @Nullable Resource parentReference) {
}

if (StringUtils.isBlank(resource.getKey())) {
LOG.warn("Unable to index a resource without key " + resource);
LOG.warn("Unable to index a resource without key: {}", resource);
return null;
}

Resource parent = (Resource) ObjectUtils.defaultIfNull(parentReference, currentProject);

Bucket parentBucket = getBucket(parent);
if (parentBucket == null && parent != null) {
LOG.warn("Resource ignored, parent is not indexed: " + resource);
LOG.warn("Resource ignored, parent is not indexed: {}", resource);
return null;
}

Expand Down
Expand Up @@ -20,21 +20,21 @@
package org.sonar.scanner.phases;

import com.google.common.collect.Lists;
import java.util.Collection;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.PostJob;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.scanner.bootstrap.BatchExtensionDictionnary;
import org.sonar.scanner.events.EventBus;
import org.sonar.scanner.util.BatchUtils;
import java.util.Collection;

@ScannerSide
public class PostJobsExecutor {
private static final Logger LOG = LoggerFactory.getLogger(PostJobsExecutor.class);
private static final Logger LOG = Loggers.get(PostJobsExecutor.class);

private final BatchExtensionDictionnary selector;
private final Project project;
Expand Down Expand Up @@ -67,7 +67,7 @@ private void execute(SensorContext context, Collection<PostJob> postJobs) {

private static void logPostJobs(Collection<PostJob> postJobs) {
if (LOG.isDebugEnabled()) {
LOG.debug("Post-jobs : {}", StringUtils.join(postJobs, " -> "));
LOG.debug(() -> "Post-jobs : " + StringUtils.join(postJobs, " -> "));
}
}
}
Expand Up @@ -56,7 +56,7 @@ void execute(Logger logger) {
for (String lang : fs.languages()) {
QProfile profile = profiles.findByLanguage(lang);
if (profile == null) {
logger.warn("No Quality profile found for language " + lang);
logger.warn("No Quality profile found for language {}", lang);
} else {
logger.info("Quality profile for {}: {}", lang, profile.getName());
if (isNotEmpty(defaultName) && defaultName.equals(profile.getName())) {
Expand Down
Expand Up @@ -62,7 +62,7 @@ private static void log(String title, PathPattern[] patterns) {
if (patterns.length > 0) {
LOG.info(title);
for (PathPattern pattern : patterns) {
LOG.info(" " + pattern);
LOG.info(" {}", pattern);
}
}
}
Expand Down
Expand Up @@ -103,7 +103,7 @@ void index(DefaultModuleFileSystem fileSystem) {
progressReport.stop(progress.count() + " files indexed");

if (exclusionFilters.hasPattern()) {
LOG.info(progress.excludedByPatternsCount() + " files ignored because of inclusion/exclusion patterns");
LOG.info("{} files ignored because of inclusion/exclusion patterns", progress.excludedByPatternsCount());
}
}

Expand Down Expand Up @@ -162,6 +162,11 @@ private void indexFile(final InputFileBuilder inputFileBuilder, final DefaultMod
tasks.add(executorService.submit(() -> {
DefaultInputFile completedInputFile = inputFileBuilder.completeAndComputeMetadata(inputFile, type);
if (completedInputFile != null && accept(completedInputFile)) {
LOG.debug("'{}' indexed {}with language '{}' and charset '{}'",
inputFile.relativePath(),
type == Type.TEST ? "as test " : "",
inputFile.language(),
inputFile.charset());
fs.add(completedInputFile);
status.markAsIndexed(completedInputFile);
File parentDir = completedInputFile.file().getParentFile();
Expand All @@ -180,6 +185,7 @@ private boolean accept(InputFile inputFile) {
// InputFileFilter extensions
for (InputFileFilter filter : filters) {
if (!filter.accept(inputFile)) {
LOG.debug("'{}' excluded by {}", inputFile.relativePath(), filter.getClass().getName());
return false;
}
}
Expand Down
Expand Up @@ -56,9 +56,9 @@ void doLog(Logger logger) {

private void logEncoding(Logger logger, Charset charset) {
if (!fs.isDefaultJvmEncoding()) {
logger.info("Source encoding: " + charset.displayName() + ", default locale: " + Locale.getDefault());
logger.info("Source encoding: {}, default locale: {}", charset.displayName(), Locale.getDefault());
} else {
logger.warn("Source encoding is platform dependent (" + charset.displayName() + "), default locale: " + Locale.getDefault());
logger.warn("Source encoding is platform dependent ({}), default locale: {}", charset.displayName(), Locale.getDefault());
}
}

Expand Down
Expand Up @@ -105,6 +105,8 @@ DefaultInputFile completeAndComputeMetadata(DefaultInputFile inputFile, InputFil

String lang = langDetection.language(inputFile);
if (lang == null && !settings.getBoolean(CoreProperties.IMPORT_UNKNOWN_FILES_KEY)) {
// Return fast to skip costly metadata computation
LOG.debug("'{}' language is not supported by any analyzer. Skipping it.", inputFile.relativePath());
return null;
}
inputFile.setLanguage(lang);
Expand Down
Expand Up @@ -22,6 +22,10 @@
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -32,11 +36,6 @@
import org.sonar.api.utils.MessageException;
import org.sonar.scanner.repository.language.Language;
import org.sonar.scanner.repository.language.LanguagesRepository;
import javax.annotation.CheckForNull;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

/**
* Detect language of a source file based on its suffix and configured patterns.
Expand Down Expand Up @@ -68,7 +67,7 @@ class LanguageDetection {
}
PathPattern[] defaultLanguagePatterns = PathPattern.create(patterns);
patternsByLanguage.put(language.key(), defaultLanguagePatterns);
LOG.debug("Declared extensions of language " + language + " were converted to " + getDetails(language.key()));
LOG.debug("Declared extensions of language {} were converted to {}", language, getDetails(language.key()));
}
}

Expand Down Expand Up @@ -103,7 +102,6 @@ String language(InputFile inputFile) {
}
}
if (detectedLanguage != null) {
LOG.debug(String.format("Language of file '%s' is detected to be '%s'", inputFile.relativePath(), detectedLanguage));
return detectedLanguage;
}

Expand All @@ -127,7 +125,7 @@ private boolean isCandidateForLanguage(InputFile inputFile, String languageKey)
return false;
}

private String getFileLangPatternPropKey(String languageKey) {
private static String getFileLangPatternPropKey(String languageKey) {
return "sonar.lang.patterns." + languageKey;
}

Expand Down
Expand Up @@ -110,7 +110,7 @@ private File getReportFileDir() {
reportFileDir = new File(fs.workDir(), reportFileDirStr);
}
if (StringUtils.endsWith(reportFileDirStr, ".html")) {
LOG.warn(HTML_REPORT_LOCATION_KEY + " should indicate a directory. Using parent folder.");
LOG.warn("{} should indicate a directory. Using parent folder.", HTML_REPORT_LOCATION_KEY);
reportFileDir = reportFileDir.getParentFile();
}
try {
Expand Down
Expand Up @@ -32,10 +32,9 @@
import javax.annotation.CheckForNull;
import org.apache.commons.io.input.BOMInputStream;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
import org.sonar.api.utils.log.Loggers;
import org.sonar.api.web.CodeColorizerFormat;
import org.sonar.colorizer.JavaTokenizers;
import org.sonar.colorizer.Tokenizer;
Expand All @@ -46,7 +45,7 @@
@ScannerSide
public class CodeColorizers {

private static final Logger LOG = LoggerFactory.getLogger(CodeColorizers.class);
private static final org.sonar.api.utils.log.Logger LOG = Loggers.get(CodeColorizers.class);

private final Map<String, CodeColorizerFormat> byLang;

Expand All @@ -56,7 +55,7 @@ public CodeColorizers(List<CodeColorizerFormat> formats) {
byLang.put(format.getLanguageKey(), format);
}

LOG.debug("Code colorizer, supported languages: " + StringUtils.join(byLang.keySet(), ","));
LOG.debug(() -> "Code colorizer, supported languages: " + StringUtils.join(byLang.keySet(), ","));
}

/**
Expand All @@ -72,7 +71,7 @@ public void toSyntaxHighlighting(File file, Charset charset, String language, Ne
List<Tokenizer> tokenizers;
if (format == null) {
// Workaround for Java test code since Java plugin only provides highlighting for main source and no colorizer
// TODO can be dropped when Java plugin embed its own CodeColorizerFormat of (better) provides highlighting for tests
// TODO can be dropped when Java plugin embed its own CodeColorizerFormat or (better) provides highlighting for tests
// See SONARJAVA-830
if ("java".equals(language)) {
tokenizers = JavaTokenizers.forHtml();
Expand Down
Expand Up @@ -19,15 +19,14 @@
*/
package org.sonar.scanner.source;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
import org.sonar.api.batch.sensor.highlighting.TypeOfText;
import org.sonar.colorizer.HtmlCodeBuilder;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HighlightingCodeBuilder extends HtmlCodeBuilder {

private static final Logger LOG = LoggerFactory.getLogger(HighlightingCodeBuilder.class);
Expand Down Expand Up @@ -65,15 +64,15 @@ public void appendWithoutTransforming(String htmlTag) {
startOffset = currentOffset;
cssClass = startMatcher.group(1);
} else {
LOG.warn("Expected to match highlighting start html tag but was: " + htmlTag);
LOG.warn("Expected to match highlighting start html tag but was: {}", htmlTag);
}
} else {
Matcher endMatcher = END_TAG_PATTERN.matcher(htmlTag);
if (endMatcher.matches()) {
highlighting.highlight(startOffset, currentOffset, TypeOfText.forCssClass(cssClass));
startOffset = -1;
} else {
LOG.warn("Expected to match highlighting end html tag but was: " + htmlTag);
LOG.warn("Expected to match highlighting end html tag but was: {}", htmlTag);
}
}
}
Expand Down

0 comments on commit 806e0ea

Please sign in to comment.