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