aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DeprecatedIssueBuilderWrapper.java
blob: 4db09b503d7d2a4f3b706eb5b1372073bf7261d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * SonarQube
 * Copyright (C) 2009-2017 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.issue;

import com.google.common.base.Preconditions;
import javax.annotation.Nullable;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.issue.NewIssueLocation;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssue;
import org.sonar.api.batch.sensor.issue.internal.DefaultIssueLocation;
import org.sonar.api.issue.Issuable;
import org.sonar.api.issue.Issuable.IssueBuilder;
import org.sonar.api.issue.Issue;
import org.sonar.api.rule.RuleKey;

public class DeprecatedIssueBuilderWrapper implements Issuable.IssueBuilder {

  private final DefaultIssue newIssue;
  private final InputComponent primaryComponent;
  private TextRange primaryRange = null;
  private String primaryMessage = null;

  public DeprecatedIssueBuilderWrapper(InputComponent primaryComponent, DefaultIssue newIssue) {
    this.primaryComponent = primaryComponent;
    this.newIssue = newIssue;
  }

  @Override
  public IssueBuilder ruleKey(RuleKey ruleKey) {
    newIssue.forRule(ruleKey);
    return this;
  }

  @Override
  public IssueBuilder line(@Nullable Integer line) {
    Preconditions.checkState(newIssue.primaryLocation() == null, "Do not use line() and at() for the same issue");
    if (primaryComponent.isFile()) {
      if (line != null) {
        this.primaryRange = ((InputFile) primaryComponent).selectLine(line.intValue());
      }
      return this;
    } else {
      throw new IllegalArgumentException("Unable to set line for issues on project or directory");
    }
  }

  @Override
  public IssueBuilder message(String message) {
    Preconditions.checkState(newIssue.primaryLocation() == null, "Do not use message() and at() for the same issue");
    this.primaryMessage = message;
    return this;
  }

  @Override
  public NewIssueLocation newLocation() {
    return new DefaultIssueLocation();
  }

  @Override
  public IssueBuilder at(NewIssueLocation primaryLocation) {
    Preconditions.checkState(primaryMessage == null && primaryRange == null, "Do not use message() or line() and at() for the same issue");
    newIssue.at(primaryLocation);
    return this;
  }

  @Override
  public IssueBuilder addLocation(NewIssueLocation secondaryLocation) {
    newIssue.addLocation(secondaryLocation);
    return this;
  }

  @Override
  public IssueBuilder addFlow(Iterable<NewIssueLocation> flowLocations) {
    newIssue.addFlow(flowLocations);
    return this;
  }

  @Override
  public IssueBuilder severity(String severity) {
    newIssue.overrideSeverity(Severity.valueOf(severity));
    return this;
  }

  @Override
  public IssueBuilder reporter(String reporter) {
    throw new UnsupportedOperationException("Not supported during sensor phase");
  }

  @Override
  public IssueBuilder effortToFix(Double d) {
    newIssue.effortToFix(d);
    return this;
  }

  @Override
  public IssueBuilder attribute(String key, String value) {
    throw new UnsupportedOperationException("Not supported during sensor phase");
  }

  @Override
  public Issue build() {
    if (newIssue.primaryLocation() == null) {
      NewIssueLocation newLocation = newIssue.newLocation().on(primaryComponent);
      if (primaryMessage != null) {
        newLocation.message(primaryMessage);
      }
      if (primaryComponent.isFile() && primaryRange != null) {
        newLocation.at(primaryRange);
      }
      newIssue.at(newLocation);
    }
    return new DeprecatedIssueWrapper(newIssue);
  }

}