Search in sources :

Example 1 with BlockChunker

use of org.sonar.duplications.block.BlockChunker 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 2 with BlockChunker

use of org.sonar.duplications.block.BlockChunker 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

Block (org.sonar.duplications.block.Block)2 BlockChunker (org.sonar.duplications.block.BlockChunker)2 Statement (org.sonar.duplications.statement.Statement)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 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)1 StatementChunker (org.sonar.duplications.statement.StatementChunker)1 TokenChunker (org.sonar.duplications.token.TokenChunker)1