Search in sources :

Example 1 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.

the class DefaultCpdBlockIndexer method populateIndex.

private void populateIndex(String languageKey, List<InputFile> sourceFiles, CpdMapping mapping) {
    TokenizerBridge bridge = new TokenizerBridge(mapping.getTokenizer(), fs.encoding().name(), getBlockSize(languageKey));
    for (InputFile inputFile : sourceFiles) {
        if (!index.isIndexed(inputFile)) {
            LOG.debug("Populating index from {}", inputFile.absolutePath());
            String resourceEffectiveKey = ((DefaultInputFile) inputFile).key();
            List<Block> blocks = bridge.chunk(resourceEffectiveKey, inputFile.file());
            index.insert(inputFile, blocks);
        }
    }
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) TokenizerBridge(org.sonar.duplications.internal.pmd.TokenizerBridge) Block(org.sonar.duplications.block.Block) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 2 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile 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 DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.

the class IssueExclusionsLoader method execute.

/**
   * {@inheritDoc}
   */
public void execute() {
    Charset sourcesEncoding = fileSystem.encoding();
    for (InputFile inputFile : fileSystem.inputFiles(fileSystem.predicates().all())) {
        try {
            String componentEffectiveKey = ((DefaultInputFile) inputFile).key();
            if (componentEffectiveKey != null) {
                String path = inputFile.relativePath();
                inclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey);
                exclusionPatternInitializer.initializePatternsForPath(path, componentEffectiveKey);
                if (exclusionPatternInitializer.hasFileContentPattern()) {
                    regexpScanner.scan(componentEffectiveKey, inputFile.file(), sourcesEncoding);
                }
            }
        } catch (Exception e) {
            throw new IllegalStateException("Unable to read the source file : '" + inputFile.absolutePath() + "' with the charset : '" + sourcesEncoding.name() + "'.", e);
        }
    }
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Charset(java.nio.charset.Charset) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 4 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.

the class DefaultSensorContext method markForPublishing.

@Override
public void markForPublishing(InputFile inputFile) {
    DefaultInputFile file = (DefaultInputFile) inputFile;
    file.setPublish(true);
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 5 with DefaultInputFile

use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.

the class DefaultSensorStorage method store.

@Override
public void store(DefaultCoverage defaultCoverage) {
    DefaultInputFile inputFile = (DefaultInputFile) defaultCoverage.inputFile();
    inputFile.setPublish(true);
    if (coverageExclusions.isExcluded(inputFile)) {
        return;
    }
    if (defaultCoverage.linesToCover() > 0) {
        saveCoverageMetricInternal(inputFile, LINES_TO_COVER, new DefaultMeasure<Integer>().forMetric(LINES_TO_COVER).withValue(defaultCoverage.linesToCover()));
        saveCoverageMetricInternal(inputFile, UNCOVERED_LINES, new DefaultMeasure<Integer>().forMetric(UNCOVERED_LINES).withValue(defaultCoverage.linesToCover() - defaultCoverage.coveredLines()));
        saveCoverageMetricInternal(inputFile, COVERAGE_LINE_HITS_DATA, new DefaultMeasure<String>().forMetric(COVERAGE_LINE_HITS_DATA).withValue(KeyValueFormat.format(defaultCoverage.hitsByLine())));
    }
    if (defaultCoverage.conditions() > 0) {
        saveCoverageMetricInternal(inputFile, CONDITIONS_TO_COVER, new DefaultMeasure<Integer>().forMetric(CONDITIONS_TO_COVER).withValue(defaultCoverage.conditions()));
        saveCoverageMetricInternal(inputFile, UNCOVERED_CONDITIONS, new DefaultMeasure<Integer>().forMetric(UNCOVERED_CONDITIONS).withValue(defaultCoverage.conditions() - defaultCoverage.coveredConditions()));
        saveCoverageMetricInternal(inputFile, COVERED_CONDITIONS_BY_LINE, new DefaultMeasure<String>().forMetric(COVERED_CONDITIONS_BY_LINE).withValue(KeyValueFormat.format(defaultCoverage.coveredConditionsByLine())));
        saveCoverageMetricInternal(inputFile, CONDITIONS_BY_LINE, new DefaultMeasure<String>().forMetric(CONDITIONS_BY_LINE).withValue(KeyValueFormat.format(defaultCoverage.conditionsByLine())));
    }
}
Also used : DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Aggregations

DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)173 Test (org.junit.Test)117 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)75 File (java.io.File)63 InputFile (org.sonar.api.batch.fs.InputFile)46 Path (java.nio.file.Path)25 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)18 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)17 BlameOutput (org.sonar.api.batch.scm.BlameCommand.BlameOutput)16 FileMetadata (org.sonar.api.batch.fs.internal.FileMetadata)14 IOException (java.io.IOException)13 DefaultIndexedFile (org.sonar.api.batch.fs.internal.DefaultIndexedFile)11 Metadata (org.sonar.api.batch.fs.internal.Metadata)11 Before (org.junit.Before)10 DefaultInputModule (org.sonar.api.batch.fs.internal.DefaultInputModule)10 BlameLine (org.sonar.api.batch.scm.BlameLine)9 List (java.util.List)8 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)8 ProjectDefinition (org.sonar.api.batch.bootstrap.ProjectDefinition)7 TextRange (org.sonar.api.batch.fs.TextRange)7