private static void checkInterrupted(Exception e) { Throwable cause = Throwables.getRootCause(e); if (cause instanceof InterruptedException || cause instanceof InterruptedIOException) { throw new AnalysisException("Analysis cancelled", e); } }
private static void checkInterrupted(Exception e) { Throwable cause = Throwables.getRootCause(e); if (cause instanceof InterruptedException || cause instanceof InterruptedIOException) { throw new AnalysisException("Analysis cancelled", e); } }
private static void checkCurrentReportFormat(@Nullable File jacocoExecutionData) { if (jacocoExecutionData == null) { return; } 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(); if (version != ExecutionDataWriter.FORMAT_VERSION) { throw new AnalysisException("You are not using the latest JaCoCo binary format version, please consider upgrading to latest JaCoCo version."); } } catch (IOException | IllegalStateException e) { throw new AnalysisException(String.format("Unable to read %s to determine JaCoCo binary format.", jacocoExecutionData.getAbsolutePath()), e); } }
public List<String> fileLines(File file) { List<String> lines = new ArrayList<>(); try(Scanner scanner = new Scanner(getInputStream(file), getCharset(file).name())) { while (scanner.hasNextLine()) { lines.add(scanner.nextLine()); } } catch (IOException e) { throw new AnalysisException("Unable to read file "+file, e); } return lines; }
public List<String> fileLines(File file) { List<String> lines = new ArrayList<>(); try(Scanner scanner = new Scanner(getInputStream(file), getCharset(file).name())) { while (scanner.hasNextLine()) { lines.add(scanner.nextLine()); } } catch (IOException e) { throw new AnalysisException("Unable to read file "+file, e); } return lines; }
private static void parseFiles(List<File> reports, UnitTestIndex index) { StaxParser parser = new StaxParser(index); for (File report : reports) { try { parser.parse(report); } catch (XMLStreamException e) { throw new AnalysisException("Fail to parse the Surefire report: " + report, e); } } }
/** * Read bytes representing class with name passed as an argument. Modify the class version in bytecode so ASM can read * returned array without issues. * * @param className canonical name of the class (e.g. org.acme.Foo ) * @return bytes or null if class is not found */ @CheckForNull public byte[] getBytesForClass(String className) { try (InputStream is = getResourceAsStream(Convert.bytecodeName(className) + ".class")) { if (is == null) { return null; } return ByteStreams.toByteArray(is); } catch (IOException e) { throw new AnalysisException("An IOException occurred in SonarJava classLoader.",e); } }
private void parseErrorWalkAndVisit(RecognitionException e, File file) { try { // Process the exception visitor.processRecognitionException(e, file); } catch (Exception e2) { throw new AnalysisException(getAnalysisExceptionMessage(file), e2); } }
public String fileContent(File file) { try { return inputFromIOFile(file).contents(); } catch (IOException e) { throw new AnalysisException("Unable to read file "+file, e); } }
private void parseErrorWalkAndVisit(RecognitionException e, File file) { try { // Process the exception visitor.processRecognitionException(e, file); } catch (Exception e2) { throw new AnalysisException(getAnalysisExceptionMessage(file), e2); } }
/** * Read bytes representing class with name passed as an argument. Modify the class version in bytecode so ASM can read * returned array without issues. * * @param className canonical name of the class (e.g. org.acme.Foo ) * @return bytes or null if class is not found */ @CheckForNull public byte[] getBytesForClass(String className) { try (InputStream is = getResourceAsStream(Convert.bytecodeName(className) + ".class")) { if (is == null) { return null; } return ByteStreams.toByteArray(is); } catch (IOException e) { throw new AnalysisException("An IOException occurred in SonarJava classLoader.",e); } }
public String fileContent(File file) { try { return inputFromIOFile(file).contents(); } catch (IOException e) { throw new AnalysisException("Unable to read file "+file, e); } }
private static void parseFiles(List<File> reports, UnitTestIndex index) { StaxParser parser = new StaxParser(index); for (File report : reports) { try { parser.parse(report); } catch (XMLStreamException e) { throw new AnalysisException("Fail to parse the Surefire report: " + report, e); } } }
/** * 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(); loadSourceFiles(edv, reports); try (BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(reportOverall))) { Object visitor = new 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); } }
/** * 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))) { ExecutionDataReader reader = new 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; }
Set<File> libraries = getFilesFromProperty(JavaClasspathProperties.SONAR_JAVA_LIBRARIES); if (binaries.isEmpty() && libraries.isEmpty() && useDeprecatedProperties()) { throw new AnalysisException( "sonar.binaries and sonar.libraries are not supported since version 4.0 of sonar-java-plugin, please use sonar.java.binaries and sonar.java.libraries instead"); LOG.warn("sonar.java.binaries is empty, please double check your configuration"); } else { throw new AnalysisException("Please provide compiled classes of your project with sonar.java.binaries property");
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(), error); throw error; } }
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(), error); throw error; } }
@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); } } }
@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); } } }