@Override public boolean apply(@Nonnull ClonePart input) { if (counter == MAX_CLONE_PART_PER_GROUP) { LOGGER.warn("Too many duplication references on file {} for block at line {}. Keeping only the first {} references.", file.getDbKey(), originPart.getStartLine(), MAX_CLONE_PART_PER_GROUP); } boolean res = counter <= MAX_CLONE_GROUP_PER_FILE; counter++; return res; } }
@Override @Nonnull public Duplicate apply(@Nonnull ClonePart input) { return new CrossProjectDuplicate( input.getResourceId(), new TextBlock(input.getStartLine(), input.getEndLine())); } }
private void addDuplication(Component file, CloneGroup duplication) { ClonePart originPart = duplication.getOriginPart(); Iterable<Duplicate> duplicates = convertClonePartsToDuplicates(file, duplication); if (!Iterables.isEmpty(duplicates)) { duplicationRepository.add( file, new Duplication(new TextBlock(originPart.getStartLine(), originPart.getEndLine()), duplicates)); } }
@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); }
@Test public void type1() { String fragment0 = source( "if (a >= b) {", " c = d + b; // Comment1", " d = d + 1;}", "else", " c = d - a; // Comment2"); String fragment1 = source( "if (a>=b) {", " // Comment1", " c=d+b;", " d=d+1;", "} else // Comment2", " c=d-a;"); List<CloneGroup> duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(1)); ClonePart part = duplications.get(0).getOriginPart(); assertThat(part.getStartLine(), is(1)); assertThat(part.getEndLine(), is(5)); }
/** * Does not support Type 3, however able to detect inner parts. */ @Test public void type3() { String fragment0 = source( "public int getSoLinger() throws SocketException {", " Object o = impl.getOption( SocketOptions.SO_LINGER);", " if (o instanceof Integer) {", " return((Integer) o).intValue();", " }", " else return -1;", "}"); String fragment1 = source( "public synchronized int getSoTimeout() throws SocketException {", " Object o = impl.getOption( SocketOptions.SO_TIMEOUT);", " if (o instanceof Integer) {", " return((Integer) o).intValue();", " }", " else return -0;", "}"); List<CloneGroup> duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(1)); ClonePart part = duplications.get(0).getOriginPart(); assertThat(part.getStartLine(), is(3)); assertThat(part.getEndLine(), is(6)); }
/** * Supports only subset of Type 2. * * @see #type2 * @see #literalsNormalization() */ @Test public void type2_literals() { String fragment0 = source( "if (a >= b) {", " c = b + 1; // Comment1", " d = '1';}", "else", " c = d - a; // Comment2"); String fragment1 = source( "if (a >= b) {", " c = b + 2; // Comment1", " d = '2';}", "else", " c = d - a; // Comment2"); List<CloneGroup> duplications = detect2(fragment0, fragment1); assertThat(duplications.size(), is(1)); ClonePart part = duplications.get(0).getOriginPart(); assertThat(part.getStartLine(), is(1)); assertThat(part.getEndLine(), is(5)); }
blockBuilder.clear(); dupBuilder.setOriginPosition(ScannerReport.TextRange.newBuilder() .setStartLine(originBlock.getStartLine()) .setEndLine(originBlock.getEndLine()) .build()); if (clonePartCount > MAX_CLONE_PART_PER_GROUP) { LOG.warn("Too many duplication references on file " + component + " for block at line " + originBlock.getStartLine() + ". Keep only the first " + MAX_CLONE_PART_PER_GROUP + " references."); break; .setStartLine(duplicate.getStartLine()) .setEndLine(duplicate.getEndLine()) .build())
@Override public boolean apply(@Nonnull ClonePart input) { if (counter == MAX_CLONE_PART_PER_GROUP) { LOGGER.warn("Too many duplication references on file {} for block at line {}. Keeping only the first {} references.", file.getKey(), originPart.getStartLine(), MAX_CLONE_PART_PER_GROUP); } boolean res = counter <= MAX_CLONE_GROUP_PER_FILE; counter++; return res; } }
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 addDuplication(Component file, CloneGroup duplication) { ClonePart originPart = duplication.getOriginPart(); Iterable<Duplicate> duplicates = convertClonePartsToDuplicates(file, duplication); if (!Iterables.isEmpty(duplicates)) { duplicationRepository.add( file, new Duplication(new TextBlock(originPart.getStartLine(), originPart.getEndLine()), duplicates)); } }
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(); } }
blockBuilder.clear(); dupBuilder.setOriginPosition(ScannerReport.TextRange.newBuilder() .setStartLine(originBlock.getStartLine()) .setEndLine(originBlock.getEndLine()) .build()); if (clonePartCount > MAX_CLONE_PART_PER_GROUP) { LOG.warn("Too many duplication references on file " + component + " for block at line " + originBlock.getStartLine() + ". Keep only the first " + MAX_CLONE_PART_PER_GROUP + " references."); break; .setStartLine(duplicate.getStartLine()) .setEndLine(duplicate.getEndLine()) .build())
blockBuilder.clear(); dupBuilder.setOriginPosition(BatchReport.TextRange.newBuilder() .setStartLine(originBlock.getStartLine()) .setEndLine(originBlock.getEndLine()) .build()); if (clonePartCount > MAX_CLONE_PART_PER_GROUP) { LOG.warn("Too many duplication references on file " + component.inputComponent() + " for block at line " + originBlock.getStartLine() + ". Keep only the first " + MAX_CLONE_PART_PER_GROUP + " references."); break; .setStartLine(duplicate.getStartLine()) .setEndLine(duplicate.getEndLine()) .build())