use of org.sonar.duplications.statement.Statement in project sonarqube by SonarSource.
the class BlockChunker method chunk.
public List<Block> chunk(String resourceId, List<Statement> statements) {
List<Statement> filtered = new ArrayList<>();
int i = 0;
while (i < statements.size()) {
Statement first = statements.get(i);
int j = i + 1;
while (j < statements.size() && statements.get(j).getValue().equals(first.getValue())) {
j++;
}
filtered.add(statements.get(i));
if (i < j - 1) {
filtered.add(statements.get(j - 1));
}
i = j;
}
statements = filtered;
if (statements.size() < blockSize) {
return Collections.emptyList();
}
Statement[] statementsArr = statements.toArray(new Statement[statements.size()]);
List<Block> blocks = new ArrayList<>(statementsArr.length - blockSize + 1);
long hash = 0;
int first = 0;
int last = 0;
for (; last < blockSize - 1; last++) {
hash = hash * PRIME_BASE + statementsArr[last].getValue().hashCode();
}
Block.Builder blockBuilder = Block.builder().setResourceId(resourceId);
for (; last < statementsArr.length; last++, first++) {
Statement firstStatement = statementsArr[first];
Statement lastStatement = statementsArr[last];
// add last statement to hash
hash = hash * PRIME_BASE + lastStatement.getValue().hashCode();
// create block
Block block = blockBuilder.setBlockHash(new ByteArray(hash)).setIndexInFile(first).setLines(firstStatement.getStartLine(), lastStatement.getEndLine()).build();
blocks.add(block);
// remove first statement from hash
hash -= power * firstStatement.getValue().hashCode();
}
return blocks;
}
use of org.sonar.duplications.statement.Statement in project sonarqube by SonarSource.
the class JavaCpdBlockIndexer method createIndex.
private void createIndex(Iterable<InputFile> sourceFiles) {
TokenChunker tokenChunker = JavaTokenProducer.build();
StatementChunker statementChunker = JavaStatementBuilder.build();
BlockChunker blockChunker = new BlockChunker(BLOCK_SIZE);
for (InputFile inputFile : sourceFiles) {
LOG.debug("Populating index from {}", inputFile);
String resourceEffectiveKey = ((DefaultInputFile) inputFile).key();
List<Statement> statements;
try (InputStream is = new FileInputStream(inputFile.file());
Reader reader = new InputStreamReader(is, fs.encoding())) {
statements = statementChunker.chunk(tokenChunker.chunk(reader));
} catch (FileNotFoundException e) {
throw new IllegalStateException("Cannot find file " + inputFile.file(), e);
} catch (IOException e) {
throw new IllegalStateException("Exception handling file: " + inputFile.file(), e);
}
List<Block> blocks = blockChunker.chunk(resourceEffectiveKey, statements);
index.insert(inputFile, blocks);
}
}
use of org.sonar.duplications.statement.Statement in project sonarqube by SonarSource.
the class JavaDuplicationsFunctionalTest method detect.
private List<CloneGroup> detect(String... lines) {
String sourceCode = Joiner.on('\n').join(lines);
MemoryCloneIndex index = new MemoryCloneIndex();
List<Statement> statements = STATEMENT_CHUNKER.chunk(TOKEN_CHUNKER.chunk(sourceCode));
List<Block> blocks = BLOCK_CHUNKER.chunk("resourceId", statements);
for (Block block : blocks) {
index.insert(block);
}
return detect(index, blocks);
}
use of org.sonar.duplications.statement.Statement in project sonarqube by SonarSource.
the class JavaDuplicationsFunctionalTest method addToIndex.
private static void addToIndex(CloneIndex index, String resourceId, String sourceCode) {
List<Statement> statements = STATEMENT_CHUNKER.chunk(TOKEN_CHUNKER.chunk(sourceCode));
BlockChunker blockChunker = new BlockChunker(2);
List<Block> blocks = blockChunker.chunk(resourceId, statements);
for (Block block : blocks) {
index.insert(block);
}
}
Aggregations