*/
package org.sonar.api.utils;
+import javax.annotation.Nullable;
+
public class Preconditions {
private Preconditions() {
// static only
}
}
+ public static void checkNotNull(@Nullable Object obj) {
+ if (obj == null) {
+ throw new NullPointerException();
+ }
+ }
+
public static void checkState(boolean condition, String format, Object... args) {
if (!condition) {
throw new IllegalStateException(String.format(format, args));
compile 'commons-codec:commons-codec'
compile 'commons-lang:commons-lang'
compile 'com.google.code.gson:gson'
- compile 'com.google.guava:guava'
compile 'org.apache.commons:commons-csv'
compile 'org.freemarker:freemarker'
compile 'org.slf4j:jcl-over-slf4j'
*/
package org.sonar.scanner;
-import com.google.common.base.MoreObjects;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableMap;
+import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.sonar.api.utils.KeyValueFormat;
import static java.util.stream.Collectors.toMap;
+import static org.sonar.api.utils.Preconditions.checkArgument;
public class DefaultFileLinesContext implements FileLinesContext {
private final InputFile inputFile;
}
private void checkLineRange(int line) {
- Preconditions.checkArgument(line > 0, "Line number should be positive for file %s.", inputFile);
- Preconditions.checkArgument(line <= inputFile.lines(), "Line %s is out of range for file %s. File has %s lines.", line, inputFile, inputFile.lines());
+ checkArgument(line > 0, "Line number should be positive for file %s.", inputFile);
+ checkArgument(line <= inputFile.lines(), "Line %s is out of range for file %s. File has %s lines.", line, inputFile, inputFile.lines());
}
@Override
.forMetric(metricFinder.findByKey(metricKey))
.withValue(data)
.save();
- entry.setValue(ImmutableMap.copyOf(lines));
+ entry.setValue(Collections.unmodifiableMap(lines));
}
}
}
* @see #save()
*/
private static boolean shouldSave(Map<Integer, Object> lines) {
- return !(lines instanceof ImmutableMap);
+ return lines instanceof HashMap;
}
@Override
public String toString() {
- return MoreObjects.toStringHelper(this)
- .add("map", map)
- .toString();
+ return this.getClass().getSimpleName() + "{" + map.toString() + "}";
}
}
*/
package org.sonar.scanner.issue.ignore;
-import com.google.common.collect.LinkedHashMultimap;
-import com.google.common.collect.Multimap;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
import org.sonar.api.batch.fs.InputComponent;
+import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.api.scan.issue.filter.FilterableIssue;
import org.sonar.api.scan.issue.filter.IssueFilter;
import org.sonar.api.scan.issue.filter.IssueFilterChain;
import org.sonar.api.utils.WildcardPattern;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
-import org.sonar.api.batch.fs.internal.DefaultInputFile;
import org.sonar.scanner.issue.DefaultFilterableIssue;
public class IgnoreIssuesFilter implements IssueFilter {
- private Multimap<InputComponent, WildcardPattern> rulePatternByComponent = LinkedHashMultimap.create();
+ private Map<InputComponent, List<WildcardPattern>> rulePatternByComponent = new HashMap<>();
private static final Logger LOG = Loggers.get(IgnoreIssuesFilter.class);
if ("*".equals(rulePattern.toString())) {
inputFile.setIgnoreAllIssues(true);
} else {
- rulePatternByComponent.put(inputFile, rulePattern);
+ rulePatternByComponent.computeIfAbsent(inputFile, x -> new LinkedList<>()).add(rulePattern);
}
}
private boolean hasRuleMatchFor(InputComponent component, FilterableIssue issue) {
- for (WildcardPattern pattern : rulePatternByComponent.get(component)) {
+ for (WildcardPattern pattern : rulePatternByComponent.getOrDefault(component, Collections.emptyList())) {
if (pattern.match(issue.ruleKey().toString())) {
LOG.debug("Issue {} ignored by exclusion pattern {}", issue, pattern);
return true;
import org.sonar.api.config.Configuration;
import org.sonar.api.utils.MessageException;
-import static com.google.common.base.MoreObjects.firstNonNull;
-
public abstract class AbstractPatternInitializer {
private Configuration settings;
private List<IssuePattern> multicriteriaPatterns;
if (StringUtils.isBlank(ruleKeyPattern)) {
throw MessageException.of("Issue exclusions are misconfigured. Rule key pattern is mandatory for each entry of '" + getMulticriteriaConfigurationKey() + "'");
}
- IssuePattern pattern = new IssuePattern(firstNonNull(filePathPattern, "*"), firstNonNull(ruleKeyPattern, "*"));
+ IssuePattern pattern = new IssuePattern(filePathPattern != null ? filePathPattern : "*", ruleKeyPattern != null ? ruleKeyPattern : "*");
multicriteriaPatterns.add(pattern);
}
*/
package org.sonar.scanner.rule;
-import com.google.common.base.MoreObjects;
import java.util.Date;
-
import javax.annotation.concurrent.Immutable;
@Immutable
@Override
public String toString() {
- return MoreObjects.toStringHelper(this)
- .add("key", key)
- .add("name", name)
- .add("language", language)
- .add("rulesUpdatedAt", rulesUpdatedAt)
+ return new StringBuilder()
+ .append(this.getClass().getSimpleName())
+ .append("{")
+ .append("key=").append(key)
+ .append("name=").append(name)
+ .append("language=").append(language)
+ .append("rulesUpdatedAt=").append(rulesUpdatedAt)
+ .append("}")
.toString();
}
*/
package org.sonar.scanner.scan;
-import com.google.common.collect.ImmutableMultimap;
import java.nio.file.Path;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
-import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.api.batch.fs.internal.AbstractProjectOrModule;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
+import org.sonar.api.scan.filesystem.PathResolver;
import org.sonar.scanner.fs.InputModuleHierarchy;
@Immutable
public class DefaultInputModuleHierarchy implements InputModuleHierarchy {
private final DefaultInputModule root;
private final Map<DefaultInputModule, DefaultInputModule> parents;
- private final ImmutableMultimap<DefaultInputModule, DefaultInputModule> children;
+ private final Map<DefaultInputModule, List<DefaultInputModule>> children;
public DefaultInputModuleHierarchy(DefaultInputModule root) {
- this.children = new ImmutableMultimap.Builder<DefaultInputModule, DefaultInputModule>().build();
+ this.children = Collections.emptyMap();
this.parents = Collections.emptyMap();
this.root = root;
}
* Map of child->parent. Neither the Keys or values can be null.
*/
public DefaultInputModuleHierarchy(DefaultInputModule root, Map<DefaultInputModule, DefaultInputModule> parents) {
- ImmutableMultimap.Builder<DefaultInputModule, DefaultInputModule> childrenBuilder = new ImmutableMultimap.Builder<>();
+ Map<DefaultInputModule, List<DefaultInputModule>> childrenBuilder = new HashMap<>();
for (Map.Entry<DefaultInputModule, DefaultInputModule> e : parents.entrySet()) {
- childrenBuilder.put(e.getValue(), e.getKey());
+ childrenBuilder.computeIfAbsent(e.getValue(), x -> new ArrayList<>()).add(e.getKey());
}
- this.children = childrenBuilder.build();
+ this.children = Collections.unmodifiableMap(childrenBuilder);
this.parents = Collections.unmodifiableMap(new HashMap<>(parents));
this.root = root;
}
@Override
public Collection<DefaultInputModule> children(DefaultInputModule component) {
- return children.get(component);
+ return children.getOrDefault(component, Collections.emptyList());
}
@Override
*/
package org.sonar.scanner.scan;
-import com.google.common.base.Joiner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
validateLegacyBranch(validationMessages, deprecatedBranchName);
if (!validationMessages.isEmpty()) {
- throw MessageException.of("Validation of project reactor failed:\n o " + Joiner.on("\n o ").join(validationMessages));
+ throw MessageException.of("Validation of project reactor failed:\n o " +
+ String.join("\n o ", validationMessages));
}
}
*/
package org.sonar.scanner.scan.filesystem;
-import com.google.common.base.Preconditions;
-import com.google.common.collect.LinkedHashMultimap;
-import com.google.common.collect.SetMultimap;
-import com.google.common.collect.Table;
-import com.google.common.collect.TreeBasedTable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Optional;
+import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Stream;
import org.sonar.api.batch.fs.internal.predicates.FileExtensionPredicate;
import org.sonar.scanner.scan.branch.BranchConfiguration;
+import static org.sonar.api.utils.Preconditions.checkNotNull;
+import static org.sonar.api.utils.Preconditions.checkState;
+
/**
* Store of all files and dirs. Inclusion and
* exclusion patterns are already applied.
private final SortedSet<String> globalLanguagesCache = new TreeSet<>();
private final Map<String, SortedSet<String>> languagesCache = new HashMap<>();
private final Map<String, InputFile> globalInputFileCache = new HashMap<>();
- private final Table<String, String, InputFile> inputFileByModuleCache = TreeBasedTable.create();
+ private final Map<String, Map<String, InputFile>> inputFileByModuleCache = new LinkedHashMap<>();
private final Map<InputFile, String> inputModuleKeyByFileCache = new HashMap<>();
private final Map<String, DefaultInputModule> inputModuleCache = new HashMap<>();
private final Map<String, InputComponent> inputComponents = new HashMap<>();
- private final SetMultimap<String, InputFile> filesByNameCache = LinkedHashMultimap.create();
- private final SetMultimap<String, InputFile> filesByExtensionCache = LinkedHashMultimap.create();
+ private final Map<String, Set<InputFile>> filesByNameCache = new HashMap<>();
+ private final Map<String, Set<InputFile>> filesByExtensionCache = new HashMap<>();
private final BranchConfiguration branchConfiguration;
public InputComponentStore(BranchConfiguration branchConfiguration) {
}
public Iterable<InputFile> filesByModule(String moduleKey) {
- return inputFileByModuleCache.row(moduleKey).values();
+ return inputFileByModuleCache.getOrDefault(moduleKey, Collections.emptyMap()).values();
}
public InputComponentStore put(String moduleKey, InputFile inputFile) {
DefaultInputFile file = (DefaultInputFile) inputFile;
addToLanguageCache(moduleKey, file);
- inputFileByModuleCache.put(moduleKey, file.getModuleRelativePath(), inputFile);
+ inputFileByModuleCache.computeIfAbsent(moduleKey, x -> new HashMap<>()).put(file.getModuleRelativePath(), inputFile);
inputModuleKeyByFileCache.put(inputFile, moduleKey);
globalInputFileCache.put(file.getProjectRelativePath(), inputFile);
inputComponents.put(inputFile.key(), inputFile);
- filesByNameCache.put(inputFile.filename(), inputFile);
- filesByExtensionCache.put(FileExtensionPredicate.getExtension(inputFile), inputFile);
+ filesByNameCache.computeIfAbsent(inputFile.filename(), x -> new LinkedHashSet<>()).add(inputFile);
+ filesByExtensionCache.computeIfAbsent(FileExtensionPredicate.getExtension(inputFile), x -> new LinkedHashSet<>()).add(inputFile);
return this;
}
@CheckForNull
public InputFile getFile(String moduleKey, String relativePath) {
- return inputFileByModuleCache.get(moduleKey, relativePath);
+ return inputFileByModuleCache.getOrDefault(moduleKey, Collections.emptyMap())
+ .get(relativePath);
}
@Override
public void put(DefaultInputModule inputModule) {
String key = inputModule.key();
- Preconditions.checkNotNull(inputModule);
- Preconditions.checkState(!inputComponents.containsKey(key), "Module '%s' already indexed", key);
- Preconditions.checkState(!inputModuleCache.containsKey(key), "Module '%s' already indexed", key);
+ checkNotNull(inputModule);
+ checkState(!inputComponents.containsKey(key), "Module '%s' already indexed", key);
+ checkState(!inputModuleCache.containsKey(key), "Module '%s' already indexed", key);
inputComponents.put(key, inputModule);
inputModuleCache.put(key, inputModule);
}
@Override
public Iterable<InputFile> getFilesByName(String filename) {
- return filesByNameCache.get(filename);
+ return filesByNameCache.getOrDefault(filename, Collections.emptySet());
}
@Override
public Iterable<InputFile> getFilesByExtension(String extension) {
- return filesByExtensionCache.get(extension);
+ return filesByExtensionCache.getOrDefault(extension, Collections.emptySet());
}
@Override
*/
package org.sonar.scanner.scan.filesystem;
-import com.google.common.base.Joiner;
import java.nio.file.Path;
import java.text.MessageFormat;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.ThreadSafe;
import org.apache.commons.lang.StringUtils;
}
private static String getDetails(String detectedLanguage, PathPattern[] patterns) {
- return getFileLangPatternPropKey(detectedLanguage) + " : " + Joiner.on(",").join(patterns);
+ return getFileLangPatternPropKey(detectedLanguage) + " : " +
+ Arrays.stream(patterns).map(PathPattern::toString).collect(Collectors.joining(","));
}
static String sanitizeExtension(String suffix) {
private DefaultInputFile file;
@Before
- public void setUp() throws Exception {
+ public void setUp() {
MetricFinder metricFinder = mock(MetricFinder.class);
org.sonar.api.batch.measure.Metric<String> hitsMetric = mock(org.sonar.api.batch.measure.Metric.class);
when(hitsMetric.valueType()).thenReturn(String.class);
fileLineMeasures.setIntValue(HITS_METRIC_KEY, 3, 0);
fileLineMeasures.save();
- assertThat(fileLineMeasures.toString()).isEqualTo("DefaultFileLinesContext{map={hits={1=2, 3=0}}}");
+ assertThat(fileLineMeasures.toString()).isEqualTo("DefaultFileLinesContext{{hits={1=2, 3=0}}}");
ArgumentCaptor<DefaultMeasure> captor = ArgumentCaptor.forClass(DefaultMeasure.class);
verify(sensorStorage).store(captor.capture());