001/*
002 * SonarQube
003 * Copyright (C) 2009-2017 SonarSource SA
004 * mailto:info AT sonarsource DOT com
005 *
006 * This program is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * This program is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020package org.sonar.api.batch.sensor;
021
022import java.io.Serializable;
023import org.sonar.api.SonarRuntime;
024import org.sonar.api.batch.fs.FileSystem;
025import org.sonar.api.batch.fs.InputFile;
026import org.sonar.api.batch.fs.InputModule;
027import org.sonar.api.batch.rule.ActiveRules;
028import org.sonar.api.batch.sensor.coverage.NewCoverage;
029import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
030import org.sonar.api.batch.sensor.error.NewAnalysisError;
031import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
032import org.sonar.api.batch.sensor.internal.SensorContextTester;
033import org.sonar.api.batch.sensor.issue.Issue;
034import org.sonar.api.batch.sensor.issue.NewIssue;
035import org.sonar.api.batch.sensor.measure.Measure;
036import org.sonar.api.batch.sensor.measure.NewMeasure;
037import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
038import org.sonar.api.config.Settings;
039import org.sonar.api.utils.Version;
040
041/**
042 * See {@link Sensor#execute(SensorContext)}
043 * In order to write unit tests you can use {@link SensorContextTester}
044 * @since 5.1
045 */
046public interface SensorContext {
047
048  /**
049   * Get settings of the current module.
050   */
051  Settings settings();
052
053  /**
054   * Get filesystem of the current module.
055   */
056  FileSystem fileSystem();
057
058  /**
059   * Get list of active rules.
060   */
061  ActiveRules activeRules();
062
063  /**
064   * @since 5.5
065   */
066  InputModule module();
067
068  /**
069   * Version of API at runtime, not at compilation time. It's a shortcut on
070   * {@code runtime().getApiVersion()} since 6.0.
071   * @since 5.5
072   * @see #runtime() since version 6.0.
073   */
074  Version getSonarQubeVersion();
075
076  /**
077   * Runtime information, mainly:
078   * <ul>
079   *   <li>to be able to have different behaviours between SonarQube and SonarLint</li>
080   *   <li>to enable new features depending on version of API available at runtime</li>
081   * </ul>
082   * @since 6.0
083   */
084  SonarRuntime runtime();
085
086  /**
087   * Test if a cancellation of the analysis was requested. Sensors should periodically test this flag
088   * and gracefully stop if value is {@code true}. For example it could be tested between each processed file.
089   * @since 6.0
090   */
091  boolean isCancelled();
092
093  // ----------- MEASURES --------------
094
095  /**
096   * Fluent builder to create a new {@link Measure}. Don't forget to call {@link NewMeasure#save()} once all parameters are provided.
097   */
098  <G extends Serializable> NewMeasure<G> newMeasure();
099
100  // ----------- ISSUES --------------
101
102  /**
103   * Fluent builder to create a new {@link Issue}. Don't forget to call {@link NewIssue#save()} once all parameters are provided.
104   */
105  NewIssue newIssue();
106
107  // ------------ HIGHLIGHTING ------------
108
109  /**
110   * Builder to define highlighting of a file. Don't forget to call {@link NewHighlighting#save()} once all elements are provided.
111   */
112  NewHighlighting newHighlighting();
113
114  // ------------ SYMBOL TABLE ------------
115
116  /**
117   * Builder to define symbol table of a file. Don't forget to call {@link NewSymbolTable#save()} once all symbols are provided.
118   * @since 5.6
119   */
120  NewSymbolTable newSymbolTable();
121
122  // ------------ COVERAGE ------------
123
124  /**
125   * Builder to define coverage in a file.
126   * Don't forget to call {@link NewCoverage#save()}.
127   */
128  NewCoverage newCoverage();
129
130  // ------------ CPD ------------
131
132  /**
133   * Builder to define CPD tokens in a file.
134   * Don't forget to call {@link NewCpdTokens#save()}.
135   * @since 5.5
136   */
137  NewCpdTokens newCpdTokens();
138
139  // ------------ ANALYSIS ERROR ------------
140
141  /**
142   * Builder to declare errors that happened while processing a source file.
143   * Don't forget to call {@link NewAnalysisError#save()}.
144   * @since 6.0
145   */
146  NewAnalysisError newAnalysisError();
147
148  /**
149   * Add a property to the scanner context. This context is available
150   * in Compute Engine when processing the report.
151   * <br/>
152   * The properties starting with {@code "sonar.analysis."} are included to the
153   * payload of webhooks.
154   *
155   * @throws IllegalArgumentException if key or value parameter is null
156   * @see org.sonar.api.ce.posttask.PostProjectAnalysisTask.ProjectAnalysis#getScannerContext()
157   * @since 6.1
158   */
159  void addContextProperty(String key, String value);
160
161  /**
162   * Indicate that a file should be published in the report sent to SonarQube.
163   * Files are automatically marked if any data is created for it (issues, highlighting, coverage, etc.).
164   * @since 6.3
165   */
166  void markForPublishing(InputFile inputFile);
167
168}