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.

AbstractDefaultIssue.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.api.batch.sensor.issue.internal;
  21. import com.google.common.base.Preconditions;
  22. import java.nio.file.Path;
  23. import java.util.ArrayList;
  24. import java.util.Collections;
  25. import java.util.List;
  26. import java.util.Objects;
  27. import java.util.Optional;
  28. import javax.annotation.Nullable;
  29. import org.sonar.api.batch.fs.InputComponent;
  30. import org.sonar.api.batch.fs.internal.DefaultInputDir;
  31. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  32. import org.sonar.api.batch.sensor.internal.DefaultStorable;
  33. import org.sonar.api.batch.sensor.internal.SensorStorage;
  34. import org.sonar.api.batch.sensor.issue.Issue.Flow;
  35. import org.sonar.api.batch.sensor.issue.IssueLocation;
  36. import org.sonar.api.batch.sensor.issue.NewIssueLocation;
  37. import org.sonar.api.utils.PathUtils;
  38. import static com.google.common.base.Preconditions.checkState;
  39. import static com.google.common.base.Strings.isNullOrEmpty;
  40. import static java.util.Collections.unmodifiableList;
  41. import static java.util.stream.Collectors.toList;
  42. public abstract class AbstractDefaultIssue<T extends AbstractDefaultIssue> extends DefaultStorable {
  43. protected IssueLocation primaryLocation;
  44. protected List<List<IssueLocation>> flows = new ArrayList<>();
  45. protected DefaultInputModule projectRoot;
  46. protected AbstractDefaultIssue(DefaultInputModule projectRoot) {
  47. this(projectRoot, null);
  48. }
  49. public AbstractDefaultIssue(DefaultInputModule projectRoot, @Nullable SensorStorage storage) {
  50. super(storage);
  51. this.projectRoot = projectRoot;
  52. }
  53. public IssueLocation primaryLocation() {
  54. return primaryLocation;
  55. }
  56. public List<Flow> flows() {
  57. return this.flows.stream()
  58. .<Flow>map(l -> () -> unmodifiableList(new ArrayList<>(l)))
  59. .collect(toList());
  60. }
  61. public NewIssueLocation newLocation() {
  62. return new DefaultIssueLocation();
  63. }
  64. public T at(NewIssueLocation primaryLocation) {
  65. Preconditions.checkArgument(primaryLocation != null, "Cannot use a location that is null");
  66. checkState(this.primaryLocation == null, "at() already called");
  67. this.primaryLocation = rewriteLocation((DefaultIssueLocation) primaryLocation);
  68. Preconditions.checkArgument(this.primaryLocation.inputComponent() != null, "Cannot use a location with no input component");
  69. return (T) this;
  70. }
  71. public T addLocation(NewIssueLocation secondaryLocation) {
  72. flows.add(Collections.singletonList(rewriteLocation((DefaultIssueLocation) secondaryLocation)));
  73. return (T) this;
  74. }
  75. public T addFlow(Iterable<NewIssueLocation> locations) {
  76. List<IssueLocation> flowAsList = new ArrayList<>();
  77. for (NewIssueLocation issueLocation : locations) {
  78. flowAsList.add(rewriteLocation((DefaultIssueLocation) issueLocation));
  79. }
  80. flows.add(flowAsList);
  81. return (T) this;
  82. }
  83. private DefaultIssueLocation rewriteLocation(DefaultIssueLocation location) {
  84. InputComponent component = location.inputComponent();
  85. Optional<Path> dirOrModulePath = Optional.empty();
  86. if (component instanceof DefaultInputDir) {
  87. DefaultInputDir dirComponent = (DefaultInputDir) component;
  88. dirOrModulePath = Optional.of(projectRoot.getBaseDir().relativize(dirComponent.path()));
  89. } else if (component instanceof DefaultInputModule && !Objects.equals(projectRoot.key(), component.key())) {
  90. DefaultInputModule moduleComponent = (DefaultInputModule) component;
  91. dirOrModulePath = Optional.of(projectRoot.getBaseDir().relativize(moduleComponent.getBaseDir()));
  92. }
  93. if (dirOrModulePath.isPresent()) {
  94. String path = PathUtils.sanitize(dirOrModulePath.get().toString());
  95. DefaultIssueLocation fixedLocation = new DefaultIssueLocation();
  96. fixedLocation.on(projectRoot);
  97. StringBuilder fullMessage = new StringBuilder();
  98. if (!isNullOrEmpty(path)) {
  99. fullMessage.append("[").append(path).append("] ");
  100. }
  101. fullMessage.append(location.message());
  102. fixedLocation.message(fullMessage.toString());
  103. return fixedLocation;
  104. } else {
  105. return location;
  106. }
  107. }
  108. }