use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class IntegrateCrossProjectDuplications method addDuplication.
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));
}
}
use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class CloneGroupMatcher method describeTo.
public void describeTo(Description description) {
StringBuilder builder = new StringBuilder();
for (ClonePart part : expectedParts) {
builder.append(part).append(" - ");
}
builder.append(expectedLen);
description.appendText(builder.toString());
}
use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class DuplicationsCollector method endOfGroup.
/**
* Constructs CloneGroup and saves it.
*/
@Override
public void endOfGroup() {
ClonePart origin = null;
CloneGroup.Builder builder = CloneGroup.builder().setLength(length);
List<ClonePart> parts = new ArrayList<>(count);
for (int[] b : blockNumbers) {
Block firstBlock = text.getBlock(b[0]);
Block lastBlock = text.getBlock(b[1]);
ClonePart part = new ClonePart(firstBlock.getResourceId(), firstBlock.getIndexInFile(), firstBlock.getStartLine(), lastBlock.getEndLine());
// TODO Godin: maybe use FastStringComparator here ?
if (originResourceId.equals(part.getResourceId())) {
// part from origin
if (origin == null) {
origin = part;
// To calculate length important to use the origin, because otherwise block may come from DB without required data
builder.setLengthInUnits(lastBlock.getEndUnit() - firstBlock.getStartUnit() + 1);
} else if (part.getUnitStart() < origin.getUnitStart()) {
origin = part;
}
}
parts.add(part);
}
Collections.sort(parts, ContainsInComparator.CLONEPART_COMPARATOR);
builder.setOrigin(origin).setParts(parts);
filter(builder.build());
reset();
}
use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class JavaDuplicationsFunctionalTest method type2_literals.
/**
* 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));
}
use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class JavaDuplicationsFunctionalTest method type1.
@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));
}
Aggregations