001/*
002 * SonarQube
003 * Copyright (C) 2009-2016 SonarSource SA
004 * mailto:contact 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.InputModule;
026import org.sonar.api.batch.rule.ActiveRules;
027import org.sonar.api.batch.sensor.coverage.NewCoverage;
028import org.sonar.api.batch.sensor.cpd.NewCpdTokens;
029import org.sonar.api.batch.sensor.error.NewAnalysisError;
030import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
031import org.sonar.api.batch.sensor.internal.SensorContextTester;
032import org.sonar.api.batch.sensor.issue.Issue;
033import org.sonar.api.batch.sensor.issue.NewIssue;
034import org.sonar.api.batch.sensor.measure.Measure;
035import org.sonar.api.batch.sensor.measure.NewMeasure;
036import org.sonar.api.batch.sensor.symbol.NewSymbolTable;
037import org.sonar.api.config.Settings;
038import org.sonar.api.utils.Version;
039
040/**
041 * See {@link Sensor#execute(SensorContext)}
042 * In order to write unit tests you can use {@link SensorContextTester}
043 * @since 5.1
044 */
045public interface SensorContext {
046
047  /**
048   * Get settings of the current module.
049   */
050  Settings settings();
051
052  /**
053   * Get filesystem of the current module.
054   */
055  FileSystem fileSystem();
056
057  /**
058   * Get list of active rules.
059   */
060  ActiveRules activeRules();
061
062  /**
063   * @since 5.5
064   */
065  InputModule module();
066
067  /**
068   * Version of API at runtime, not at compilation time. It's a shortcut on
069   * {@code runtime().getApiVersion()} since 6.0.
070   * @since 5.5
071   * @see #runtime() since version 6.0.
072   */
073  Version getSonarQubeVersion();
074
075  /**
076   * Runtime information, mainly:
077   * <ul>
078   *   <li>to be able to have different behaviours between SonarQube and SonarLint</li>
079   *   <li>to enable new features depending on version of API available at runtime</li>
080   * </ul>
081   * @since 6.0
082   */
083  SonarRuntime runtime();
084
085  /**
086   * Test if a cancellation of the analysis was requested. Sensors should periodically test this flag
087   * and gracefully stop if value is {@code true}. For example it could be tested between each processed file.
088   * @since 6.0
089   */
090  boolean isCancelled();
091
092  // ----------- MEASURES --------------
093
094  /**
095   * Fluent builder to create a new {@link Measure}. Don't forget to call {@link NewMeasure#save()} once all parameters are provided.
096   */
097  <G extends Serializable> NewMeasure<G> newMeasure();
098
099  // ----------- ISSUES --------------
100
101  /**
102   * Fluent builder to create a new {@link Issue}. Don't forget to call {@link NewIssue#save()} once all parameters are provided.
103   */
104  NewIssue newIssue();
105
106  // ------------ HIGHLIGHTING ------------
107
108  /**
109   * Builder to define highlighting of a file. Don't forget to call {@link NewHighlighting#save()} once all elements are provided.
110   */
111  NewHighlighting newHighlighting();
112
113  // ------------ SYMBOL TABLE ------------
114
115  /**
116   * Builder to define symbol table of a file. Don't forget to call {@link NewSymbolTable#save()} once all symbols are provided.
117   * @since 5.6
118   */
119  NewSymbolTable newSymbolTable();
120
121  // ------------ COVERAGE ------------
122
123  /**
124   * Builder to define coverage in a file.
125   * Don't forget to call {@link NewCoverage#save()}.
126   */
127  NewCoverage newCoverage();
128
129  // ------------ CPD ------------
130
131  /**
132   * Builder to define CPD tokens in a file.
133   * Don't forget to call {@link NewCpdTokens#save()}.
134   * @since 5.5
135   */
136  NewCpdTokens newCpdTokens();
137
138  // ------------ ANALYSIS ERROR ------------
139
140  /**
141   * Builder to declare errors that happened while processing a source file.
142   * Don't forget to call {@link NewAnalysisError#save()}.
143   * @since 6.0
144   */
145  NewAnalysisError newAnalysisError();
146
147  /**
148   * Add a property to the scanner context. This context is available
149   * in Compute Engine when processing the report.
150   * <br/>
151   * The properties starting with {@code "sonar.analysis."} are included to the
152   * payload of webhooks.
153   *
154   * @throws IllegalArgumentException if key or value parameter is null
155   * @see org.sonar.api.ce.posttask.PostProjectAnalysisTask.ProjectAnalysis#getScannerContext()
156   * @since 6.1
157   */
158  void addContextProperty(String key, String value);
159
160}