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.

AbstractSensorWrapper.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.scanner.sensor;
  21. import org.sonar.api.batch.sensor.SensorContext;
  22. import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
  23. import org.sonar.api.scanner.sensor.ProjectSensor;
  24. import org.sonar.api.utils.log.Logger;
  25. import org.sonar.api.utils.log.Loggers;
  26. import org.sonar.scanner.scan.branch.BranchConfiguration;
  27. import org.sonar.scanner.scan.branch.BranchType;
  28. import org.sonar.scanner.scan.filesystem.MutableFileSystem;
  29. public abstract class AbstractSensorWrapper<G extends ProjectSensor> {
  30. private static final Logger LOGGER = Loggers.get(AbstractSensorWrapper.class);
  31. private final G wrappedSensor;
  32. private final SensorContext context;
  33. private final MutableFileSystem fileSystem;
  34. private final DefaultSensorDescriptor descriptor;
  35. private final AbstractSensorOptimizer optimizer;
  36. private final boolean isPullRequest;
  37. public AbstractSensorWrapper(G sensor, SensorContext context, AbstractSensorOptimizer optimizer, MutableFileSystem fileSystem, BranchConfiguration branchConfiguration) {
  38. this.wrappedSensor = sensor;
  39. this.optimizer = optimizer;
  40. this.context = context;
  41. this.descriptor = new DefaultSensorDescriptor();
  42. this.fileSystem = fileSystem;
  43. this.isPullRequest = branchConfiguration.branchType() == BranchType.PULL_REQUEST;
  44. sensor.describe(this.descriptor);
  45. if (descriptor.name() == null) {
  46. descriptor.name(sensor.getClass().getName());
  47. }
  48. }
  49. public boolean shouldExecute() {
  50. return optimizer.shouldExecute(descriptor);
  51. }
  52. public void analyse() {
  53. boolean sensorIsRestricted = descriptor.isProcessesFilesIndependently() && isPullRequest;
  54. if (sensorIsRestricted) {
  55. LOGGER.info("Sensor {} is restricted to changed files only", descriptor.name());
  56. }
  57. fileSystem.setRestrictToChangedFiles(sensorIsRestricted);
  58. wrappedSensor.execute(context);
  59. }
  60. public G wrappedSensor() {
  61. return wrappedSensor;
  62. }
  63. @Override
  64. public String toString() {
  65. return descriptor.name();
  66. }
  67. public boolean isGlobal() {
  68. return descriptor.isGlobal();
  69. }
  70. }