You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NoSonarSensor.java 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.xoo.rule;
  21. import java.io.IOException;
  22. import java.nio.file.Files;
  23. import java.util.HashSet;
  24. import java.util.Set;
  25. import java.util.stream.Stream;
  26. import org.sonar.api.batch.Phase;
  27. import org.sonar.api.batch.fs.InputFile;
  28. import org.sonar.api.batch.sensor.Sensor;
  29. import org.sonar.api.batch.sensor.SensorContext;
  30. import org.sonar.api.batch.sensor.SensorDescriptor;
  31. import org.sonar.api.issue.NoSonarFilter;
  32. import org.sonar.xoo.Xoo;
  33. @Phase(name = Phase.Name.PRE)
  34. public class NoSonarSensor implements Sensor {
  35. private NoSonarFilter noSonarFilter;
  36. public NoSonarSensor(NoSonarFilter noSonarFilter) {
  37. this.noSonarFilter = noSonarFilter;
  38. }
  39. @Override
  40. public void describe(SensorDescriptor descriptor) {
  41. descriptor
  42. .onlyOnLanguage(Xoo.KEY);
  43. }
  44. @Override
  45. public void execute(SensorContext context) {
  46. for (InputFile inputFile : context.fileSystem().inputFiles(context.fileSystem().predicates().hasLanguage(Xoo.KEY))) {
  47. processFile(inputFile);
  48. }
  49. }
  50. private void processFile(InputFile inputFile) {
  51. try {
  52. Set<Integer> noSonarLines = new HashSet<>();
  53. int[] lineCounter = {1};
  54. try (Stream<String> stream = Files.lines(inputFile.path(), inputFile.charset())) {
  55. stream.forEachOrdered(lineStr -> {
  56. if (lineStr.contains("//NOSONAR")) {
  57. noSonarLines.add(lineCounter[0]);
  58. }
  59. lineCounter[0]++;
  60. });
  61. }
  62. noSonarFilter.noSonarInFile(inputFile, noSonarLines);
  63. } catch (IOException e) {
  64. throw new IllegalStateException("Fail to process " + inputFile, e);
  65. }
  66. }
  67. }