}
public boolean shouldDecorateResource(Resource resource) {
- return ResourceUtils.isUnitTestClass(resource) || !ResourceUtils.isEntity(resource);
+ return ResourceUtils.isUnitTestFile(resource) || !ResourceUtils.isEntity(resource);
}
@Override
this.fs = fs;
}
+ @Override
public final void tokenize(SourceCode source, Tokens cpdTokens) {
String fileName = source.getFileName();
int lineIdx = 1;
public final class CpdComponents {
+ private CpdComponents() {
+ }
+
public static List all() {
return ImmutableList.of(
CpdSensor.class,
populateIndex(languageKey, sourceFiles, mapping, index);
// Detect
+ runCpdAnalysis(languageKey, context, sourceFiles, index);
+ }
+
+ private void runCpdAnalysis(String languageKey, SensorContext context, List<InputFile> sourceFiles, SonarDuplicationsIndex index) {
Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(languageKey));
ExecutorService executorService = Executors.newSingleThreadExecutor();
} catch (TimeoutException e) {
filtered = null;
LOG.warn("Timeout during detection of duplications for " + inputFile, e);
- } catch (InterruptedException e) {
- throw new SonarException("Fail during detection of duplication for " + inputFile, e);
- } catch (ExecutionException e) {
+ } catch (InterruptedException | ExecutionException e) {
throw new SonarException("Fail during detection of duplication for " + inputFile, e);
}
for (int i = 1; i <= inputFile.lines(); i++) {
duplicationByLine.put(i, duplicatedLines.contains(i) ? 1 : 0);
}
+ saveMeasures(context, inputFile, duplicatedLines, duplicatedBlocks, duplicationByLine);
+
+ saveDuplications(context, inputFile, duplications);
+ }
+
+ private static void saveMeasures(org.sonar.api.batch.sensor.SensorContext context, InputFile inputFile, Set<Integer> duplicatedLines, int duplicatedBlocks,
+ Map<Integer, Integer> duplicationByLine) {
((DefaultMeasure<String>) context.<String>newMeasure()
.forMetric(CoreMetrics.DUPLICATION_LINES_DATA)
.onFile(inputFile)
.withValue(duplicatedBlocks))
.setFromCore()
.save();
+ }
+ private static void saveDuplications(org.sonar.api.batch.sensor.SensorContext context, InputFile inputFile, Iterable<CloneGroup> duplications) {
int cloneGroupCount = 0;
for (CloneGroup duplication : duplications) {
cloneGroupCount++;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
if (CoreMetrics.DUPLICATIONS_DATA_KEY.equals(metricKey)) {
// Hack for SONAR-5765
Iterable<DefaultDuplication> group = duplicationCache.byComponent(resource.getEffectiveKey());
- if (group != null) {
- unfiltered = Arrays.asList(new Measure(CoreMetrics.DUPLICATIONS_DATA, DuplicationUtils.toXml(group)));
- } else {
- unfiltered = Collections.<Measure>emptyList();
- }
+ unfiltered = Arrays.asList(new Measure(CoreMetrics.DUPLICATIONS_DATA, DuplicationUtils.toXml(group)));
} else {
// optimization
unfiltered = measuresByMetric.get(metricKey);
}
public DuplicationCache put(String effectiveKey, DefaultDuplication duplication) {
- cache.put(effectiveKey, sequence++, duplication);
+ cache.put(effectiveKey, sequence, duplication);
+ sequence++;
return this;
}
return new ValueIterable<V>(exchange, firstKey, secondKey);
}
- private IllegalStateException failToGetValues(Exception e) {
- return new IllegalStateException("Fail to get values from cache " + name, e);
- }
-
/**
* Lazy-loading values for a given key
*/
*/
package org.sonar.batch.sensor;
+import com.google.common.base.Preconditions;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputDir;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.test.MutableTestable;
import org.sonar.api.test.Testable;
import org.sonar.batch.duplication.DuplicationCache;
+import org.sonar.batch.index.BatchResource;
import org.sonar.batch.index.ComponentDataCache;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.index.ResourceCache;
@Override
public void store(org.sonar.api.batch.sensor.dependency.Dependency dep) {
- File fromResource = (File) resourceCache.get(dep.fromKey()).resource();
- File toResource = (File) resourceCache.get(dep.toKey()).resource();
+ BatchResource fromBatchResource = resourceCache.get(dep.fromKey());
+ BatchResource toBatchResource = resourceCache.get(dep.toKey());
+ Preconditions.checkNotNull(fromBatchResource, "Unable to find from resource " + dep.fromKey());
+ Preconditions.checkNotNull(toBatchResource, "Unable to find from resource " + dep.toKey());
+ File fromResource = (File) fromBatchResource.resource();
+ File toResource = (File) toBatchResource.resource();
if (sonarIndex.getEdge(fromResource, toResource) != null) {
throw new IllegalStateException("Dependency between " + dep.fromKey() + " and " + dep.toKey() + " was already saved.");
}
}
}
- public Block originBlock();
+ Block originBlock();
- public List<Block> duplicates();
+ List<Block> duplicates();
}