@Override public int compare(ClonePart o1, ClonePart o2) { return FastStringComparator.INSTANCE.compare(o1.getResourceId(), o2.getResourceId()); } };
@Override public boolean apply(@Nonnull ClonePart part) { return !part.getResourceId().equals(componentKey); } }
private static Iterable<Duplicate> convertClonePartsToDuplicates(final Component file, CloneGroup duplication) { final ClonePart originPart = duplication.getOriginPart(); return from(duplication.getCloneParts()) .filter(new DoesNotMatchSameComponentKey(originPart.getResourceId())) .filter(new DuplicateLimiter(file, originPart)) .transform(ClonePartToCrossProjectDuplicate.INSTANCE); }
@Override @Nonnull public Duplicate apply(@Nonnull ClonePart input) { return new CrossProjectDuplicate( input.getResourceId(), new TextBlock(input.getStartLine(), input.getEndLine())); } }
@Test public void testDuplicationInSingleFile() throws IOException { File file = new File("test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile3.java"); addToIndex(file); List<CloneGroup> duplications = detect(file); assertThat(duplications.size()).isEqualTo(1); CloneGroup duplication = duplications.get(0); assertThat(duplication.getOriginPart().getResourceId()).isEqualTo(file.getAbsolutePath()); assertThat(duplication.getCloneParts().size()).isEqualTo(2); assertThat(duplication.getLengthInUnits()).as("length in tokens").isEqualTo(157); ClonePart part = duplication.getCloneParts().get(0); assertThat(part.getResourceId()).isEqualTo(file.getAbsolutePath()); assertThat(part.getStartLine()).isEqualTo(30); assertThat(part.getEndLine()).isEqualTo(44); }
private void reportClones(BlocksGroup beginGroup, BlocksGroup endGroup, int cloneLength) { List<Block[]> pairs = beginGroup.pairs(endGroup, cloneLength); ClonePart origin = null; List<ClonePart> parts = new ArrayList<>(); for (int i = 0; i < pairs.size(); i++) { Block[] pair = pairs.get(i); Block firstBlock = pair[0]; Block lastBlock = pair[1]; ClonePart part = new ClonePart(firstBlock.getResourceId(), firstBlock.getIndexInFile(), firstBlock.getStartLine(), lastBlock.getEndLine()); if (originResourceId.equals(part.getResourceId())) { if (origin == null || part.getUnitStart() < origin.getUnitStart()) { origin = part; } } parts.add(part); } filter.add(CloneGroup.builder().setLength(cloneLength).setOrigin(origin).setParts(parts).build()); }
@Test public void testDuplicationBetweenTwoFiles() throws IOException { File file1 = new File("test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile1.java"); File file2 = new File("test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile2.java"); addToIndex(file1); addToIndex(file2); List<CloneGroup> duplications = detect(file1); assertThat(duplications.size()).isEqualTo(1); CloneGroup duplication = duplications.get(0); assertThat(duplication.getOriginPart().getResourceId()).isEqualTo(file1.getAbsolutePath()); ClonePart part1 = new ClonePart(file1.getAbsolutePath(), 1, 18, 41); ClonePart part2 = new ClonePart(file2.getAbsolutePath(), 1, 18, 41); assertThat(duplication.getCloneParts()).containsOnly(part1, part2); assertThat(duplication.getLengthInUnits()).as("length in tokens").isEqualTo(115); }
if (originResourceId.equals(part.getResourceId())) {
String componentKey = duplicate.getResourceId(); if (!component.key().equals(componentKey)) { DefaultInputComponent sameProjectComponent = (DefaultInputComponent) componentStore.getByKey(componentKey);
@Override public int compare(ClonePart o1, ClonePart o2) { return FastStringComparator.INSTANCE.compare(o1.getResourceId(), o2.getResourceId()); } };
@Override public boolean apply(@Nonnull ClonePart part) { return !part.getResourceId().equals(componentKey); } }
private static Iterable<Duplicate> convertClonePartsToDuplicates(final Component file, CloneGroup duplication) { final ClonePart originPart = duplication.getOriginPart(); return from(duplication.getCloneParts()) .filter(new DoesNotMatchSameComponentKey(originPart.getResourceId())) .filter(new DuplicateLimiter(file, originPart)) .transform(ClonePartToCrossProjectDuplicate.INSTANCE); }
private static int computeBlockAndLineCount(Iterable<CloneGroup> duplications, Set<Integer> duplicatedLines) { int duplicatedBlocks = 0; for (CloneGroup clone : duplications) { ClonePart origin = clone.getOriginPart(); for (ClonePart part : clone.getCloneParts()) { if (part.getResourceId().equals(origin.getResourceId())) { duplicatedBlocks++; for (int duplicatedLine = part.getStartLine(); duplicatedLine < part.getStartLine() + part.getLines(); duplicatedLine++) { duplicatedLines.add(duplicatedLine); } } } } return duplicatedBlocks; }
@Override @Nonnull public Duplicate apply(@Nonnull ClonePart input) { return new CrossProjectDuplicate( input.getResourceId(), new TextBlock(input.getStartLine(), input.getEndLine())); } }
private void reportClones(BlocksGroup beginGroup, BlocksGroup endGroup, int cloneLength) { List<Block[]> pairs = beginGroup.pairs(endGroup, cloneLength); ClonePart origin = null; List<ClonePart> parts = Lists.newArrayList(); for (int i = 0; i < pairs.size(); i++) { Block[] pair = pairs.get(i); Block firstBlock = pair[0]; Block lastBlock = pair[1]; ClonePart part = new ClonePart(firstBlock.getResourceId(), firstBlock.getIndexInFile(), firstBlock.getStartLine(), lastBlock.getEndLine()); if (originResourceId.equals(part.getResourceId())) { if (origin == null) { origin = part; } else if (part.getUnitStart() < origin.getUnitStart()) { origin = part; } } parts.add(part); } filter.add(CloneGroup.builder().setLength(cloneLength).setOrigin(origin).setParts(parts).build()); }
private static void saveDuplications(org.sonar.api.batch.sensor.SensorContext context, InputFile inputFile, Iterable<CloneGroup> duplications) { int cloneGroupCount = 0; for (CloneGroup duplication : duplications) { cloneGroupCount++; if (cloneGroupCount > MAX_CLONE_GROUP_PER_FILE) { LOG.warn("Too many duplication groups on file " + inputFile.relativePath() + ". Keep only the first " + MAX_CLONE_GROUP_PER_FILE + " groups."); break; } NewDuplication builder = context.newDuplication(); ClonePart originPart = duplication.getOriginPart(); builder.originBlock(inputFile, originPart.getStartLine(), originPart.getEndLine()); int clonePartCount = 0; for (ClonePart part : duplication.getCloneParts()) { if (!part.equals(originPart)) { clonePartCount++; if (clonePartCount > MAX_CLONE_PART_PER_GROUP) { LOG.warn("Too many duplication references on file " + inputFile.relativePath() + " for block at line " + originPart.getStartLine() + ". Keep only the first " + MAX_CLONE_PART_PER_GROUP + " references."); break; } ((DefaultDuplication) builder).isDuplicatedBy(part.getResourceId(), part.getStartLine(), part.getEndLine()); } } builder.save(); } }
if (originResourceId.equals(part.getResourceId())) {
String componentKey = duplicate.getResourceId(); if (!component.key().equals(componentKey)) { DefaultInputComponent sameProjectComponent = (DefaultInputComponent) componentStore.getByKey(componentKey);
String componentKey = duplicate.getResourceId(); if (!component.key().equals(componentKey)) { BatchComponent sameProjectComponent = batchComponentCache.get(componentKey);