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);
}
}
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;
}
}
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;
}
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);
}
}
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);
}
}
Aggregations