Search in sources :

Example 1 with AnalysisException

use of org.sonar.java.AnalysisException in project sonar-java by SonarSource.

the class PostAnalysisIssueFilter method scanFile.

@Override
public void scanFile(JavaFileScannerContext context) {
    InputFile component = fileSystem.inputFile(fileSystem.predicates().is(context.getFile()));
    if (component == null) {
        throw new AnalysisException("Component not found: " + context.getFileKey());
    }
    String componentKey = component.key();
    for (JavaIssueFilter javaIssueFilter : getIssueFilters()) {
        javaIssueFilter.setComponentKey(componentKey);
        javaIssueFilter.scanFile(context);
    }
}
Also used : AnalysisException(org.sonar.java.AnalysisException) InputFile(org.sonar.api.batch.fs.InputFile)

Example 2 with AnalysisException

use of org.sonar.java.AnalysisException in project sonar-java by SonarSource.

the class JavaAstScanner method simpleScan.

private void simpleScan(File file) {
    visitor.setCurrentFile(file);
    try {
        String fileContent = getFileContent(file);
        Tree ast;
        if (fileContent.isEmpty()) {
            ast = parser.parse(file);
        } else {
            ast = parser.parse(fileContent);
        }
        visitor.visitFile(ast);
    } catch (RecognitionException e) {
        checkInterrupted(e);
        LOG.error("Unable to parse source file : " + file.getAbsolutePath());
        LOG.error(e.getMessage());
        parseErrorWalkAndVisit(e, file);
    } catch (Exception e) {
        checkInterrupted(e);
        throw new AnalysisException(getAnalysisExceptionMessage(file), e);
    } catch (StackOverflowError error) {
        LOG.error("A stack overflow error occured while analyzing file: " + file.getAbsolutePath());
        throw error;
    }
}
Also used : AnalysisException(org.sonar.java.AnalysisException) Tree(org.sonar.plugins.java.api.tree.Tree) RecognitionException(com.sonar.sslr.api.RecognitionException) AnalysisException(org.sonar.java.AnalysisException) InterruptedIOException(java.io.InterruptedIOException) RecognitionException(com.sonar.sslr.api.RecognitionException)

Example 3 with AnalysisException

use of org.sonar.java.AnalysisException in project sonar-java by SonarSource.

the class JacocoReportReader method readJacocoReport.

/**
 * Read JaCoCo report determining the format to be used.
 * @param executionDataVisitor visitor to store execution data.
 * @param sessionInfoStore visitor to store info session.
 * @return true if binary format is the latest one.
 * @throws IOException in case of error or binary format not supported.
 */
public JacocoReportReader readJacocoReport(IExecutionDataVisitor executionDataVisitor, ISessionInfoVisitor sessionInfoStore) {
    if (jacocoExecutionData == null) {
        return this;
    }
    LOG.info("Analysing {}", jacocoExecutionData);
    try (InputStream inputStream = new BufferedInputStream(new FileInputStream(jacocoExecutionData))) {
        if (useCurrentBinaryFormat) {
            ExecutionDataReader reader = new ExecutionDataReader(inputStream);
            reader.setSessionInfoVisitor(sessionInfoStore);
            reader.setExecutionDataVisitor(executionDataVisitor);
            reader.read();
        } else {
            org.jacoco.previous.core.data.ExecutionDataReader reader = new org.jacoco.previous.core.data.ExecutionDataReader(inputStream);
            reader.setSessionInfoVisitor(sessionInfoStore);
            reader.setExecutionDataVisitor(executionDataVisitor);
            reader.read();
        }
    } catch (IOException e) {
        throw new AnalysisException(String.format("Unable to read %s", jacocoExecutionData.getAbsolutePath()), e);
    }
    return this;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) AnalysisException(org.sonar.java.AnalysisException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) ExecutionDataReader(org.jacoco.core.data.ExecutionDataReader)

Example 4 with AnalysisException

use of org.sonar.java.AnalysisException in project sonar-java by SonarSource.

the class JaCoCoReportMerger method mergeReports.

/**
 * Merge all reports in reportOverall.
 * @param reportOverall destination file of merge.
 * @param reports files to be merged.
 */
public static void mergeReports(File reportOverall, File... reports) {
    ExecutionDataVisitor edv = new ExecutionDataVisitor();
    boolean isCurrentVersionFormat = loadSourceFiles(edv, reports);
    try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(reportOverall))) {
        Object visitor;
        if (isCurrentVersionFormat) {
            visitor = new ExecutionDataWriter(outputStream);
        } else {
            visitor = new org.jacoco.previous.core.data.ExecutionDataWriter(outputStream);
        }
        for (Map.Entry<String, ExecutionDataStore> entry : edv.getSessions().entrySet()) {
            SessionInfo sessionInfo = new SessionInfo(entry.getKey(), 0, 0);
            ((ISessionInfoVisitor) visitor).visitSessionInfo(sessionInfo);
            entry.getValue().accept((IExecutionDataVisitor) visitor);
        }
    } catch (IOException e) {
        throw new AnalysisException(String.format("Unable to write overall coverage report %s", reportOverall.getAbsolutePath()), e);
    }
}
Also used : ISessionInfoVisitor(org.jacoco.core.data.ISessionInfoVisitor) SessionInfo(org.jacoco.core.data.SessionInfo) IOException(java.io.IOException) IExecutionDataVisitor(org.jacoco.core.data.IExecutionDataVisitor) ExecutionDataStore(org.jacoco.core.data.ExecutionDataStore) FileOutputStream(java.io.FileOutputStream) AnalysisException(org.sonar.java.AnalysisException) ExecutionDataWriter(org.jacoco.core.data.ExecutionDataWriter) BufferedOutputStream(java.io.BufferedOutputStream) Map(java.util.Map)

Example 5 with AnalysisException

use of org.sonar.java.AnalysisException in project sonar-java by SonarSource.

the class JacocoReportReader method isCurrentReportFormat.

private static boolean isCurrentReportFormat(@Nullable File jacocoExecutionData) {
    if (jacocoExecutionData == null) {
        return true;
    }
    try (DataInputStream dis = new DataInputStream(new FileInputStream(jacocoExecutionData))) {
        byte firstByte = dis.readByte();
        Preconditions.checkState(firstByte == ExecutionDataWriter.BLOCK_HEADER);
        Preconditions.checkState(dis.readChar() == ExecutionDataWriter.MAGIC_NUMBER);
        char version = dis.readChar();
        boolean isCurrentFormat = version == ExecutionDataWriter.FORMAT_VERSION;
        if (!isCurrentFormat) {
            LOG.warn("You are not using the latest JaCoCo binary format version, please consider upgrading to latest JaCoCo version.");
        }
        return isCurrentFormat;
    } catch (IOException | IllegalStateException e) {
        throw new AnalysisException(String.format("Unable to read %s to determine JaCoCo binary format.", jacocoExecutionData.getAbsolutePath()), e);
    }
}
Also used : AnalysisException(org.sonar.java.AnalysisException) IOException(java.io.IOException) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream)

Aggregations

AnalysisException (org.sonar.java.AnalysisException)5 IOException (java.io.IOException)3 DataInputStream (java.io.DataInputStream)2 FileInputStream (java.io.FileInputStream)2 RecognitionException (com.sonar.sslr.api.RecognitionException)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 InterruptedIOException (java.io.InterruptedIOException)1 Map (java.util.Map)1 ExecutionDataReader (org.jacoco.core.data.ExecutionDataReader)1 ExecutionDataStore (org.jacoco.core.data.ExecutionDataStore)1 ExecutionDataWriter (org.jacoco.core.data.ExecutionDataWriter)1 IExecutionDataVisitor (org.jacoco.core.data.IExecutionDataVisitor)1 ISessionInfoVisitor (org.jacoco.core.data.ISessionInfoVisitor)1 SessionInfo (org.jacoco.core.data.SessionInfo)1 InputFile (org.sonar.api.batch.fs.InputFile)1 Tree (org.sonar.plugins.java.api.tree.Tree)1