Search in sources :

Example 1 with Statement

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;
}
Also used : Statement(org.sonar.duplications.statement.Statement) ArrayList(java.util.ArrayList)

Example 2 with Statement

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);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) TokenChunker(org.sonar.duplications.token.TokenChunker) Statement(org.sonar.duplications.statement.Statement) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) StatementChunker(org.sonar.duplications.statement.StatementChunker) Block(org.sonar.duplications.block.Block) BlockChunker(org.sonar.duplications.block.BlockChunker)

Example 3 with Statement

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);
}
Also used : Statement(org.sonar.duplications.statement.Statement) Block(org.sonar.duplications.block.Block) MemoryCloneIndex(org.sonar.duplications.index.MemoryCloneIndex)

Example 4 with Statement

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);
    }
}
Also used : Statement(org.sonar.duplications.statement.Statement) Block(org.sonar.duplications.block.Block) BlockChunker(org.sonar.duplications.block.BlockChunker)

Aggregations

Statement (org.sonar.duplications.statement.Statement)4 Block (org.sonar.duplications.block.Block)3 BlockChunker (org.sonar.duplications.block.BlockChunker)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 ArrayList (java.util.ArrayList)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)1 StatementChunker (org.sonar.duplications.statement.StatementChunker)1 TokenChunker (org.sonar.duplications.token.TokenChunker)1