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