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
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/*
* SonarQube
* Copyright (C) 2009-2025 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.sensor;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.sensor.internal.DefaultSensorDescriptor;
import org.sonar.api.config.Configuration;
import org.sonar.scanner.config.DefaultConfiguration;
public abstract class AbstractSensorOptimizer {
private static final Logger LOG = LoggerFactory.getLogger(AbstractSensorOptimizer.class);
private final FileSystem fs;
private final ActiveRules activeRules;
private final Configuration originalConfiguration;
private final ConfigurationReadsInterceptor config;
public AbstractSensorOptimizer(FileSystem fs, ActiveRules activeRules, Configuration config) {
this.fs = fs;
this.activeRules = activeRules;
this.originalConfiguration = config;
this.config = new ConfigurationReadsInterceptor(config);
}
/**
* Decide if the given Sensor should be executed.
*/
public boolean shouldExecute(DefaultSensorDescriptor descriptor) {
if (!fsCondition(descriptor)) {
LOG.debug("'{}' skipped because there is no related file in current project", descriptor.name());
return false;
}
if (!activeRulesCondition(descriptor)) {
LOG.debug("'{}' skipped because there is no related rule activated in the quality profile", descriptor.name());
return false;
}
if (!settingsCondition(descriptor)) {
String accessedConfiguration = config.getAccessedKeys().stream()
.map(key -> "- " + key + ": " + getConfigurationValue(key).orElse("<empty>"))
.collect(Collectors.joining("\n"));
LOG.debug("""
'{}' skipped because of missing configuration requirements.
Accessed configuration:
{}""", descriptor.name(), accessedConfiguration);
return false;
}
return true;
}
private Optional<String> getConfigurationValue(String key) {
if (originalConfiguration instanceof DefaultConfiguration configuration) {
return Optional.ofNullable(configuration.getOriginalProperties().get(key));
} else {
return config.get(key);
}
}
private boolean settingsCondition(DefaultSensorDescriptor descriptor) {
if (descriptor.configurationPredicate() != null) {
return descriptor.configurationPredicate().test(config);
}
return true;
}
private boolean activeRulesCondition(DefaultSensorDescriptor descriptor) {
if (!descriptor.ruleRepositories().isEmpty()) {
for (String repoKey : descriptor.ruleRepositories()) {
if (!activeRules.findByRepository(repoKey).isEmpty()) {
return true;
}
}
return false;
}
return true;
}
private boolean fsCondition(DefaultSensorDescriptor descriptor) {
if (!descriptor.languages().isEmpty() || descriptor.type() != null) {
FilePredicate langPredicate = descriptor.languages().isEmpty() ? fs.predicates().all() : fs.predicates().hasLanguages(descriptor.languages());
FilePredicate typePredicate = descriptor.type() == null ? fs.predicates().all() : fs.predicates().hasType(descriptor.type());
return fs.hasFiles(fs.predicates().and(langPredicate, typePredicate));
}
return true;
}
private static class ConfigurationReadsInterceptor implements Configuration {
private final Configuration configuration;
private final Set<String> accessedKeys;
private ConfigurationReadsInterceptor(Configuration configuration) {
this.configuration = configuration;
this.accessedKeys = new HashSet<>();
}
Set<String> getAccessedKeys() {
return accessedKeys;
}
@Override
public boolean hasKey(String key) {
boolean hasKey = configuration.hasKey(key);
accessedKeys.add(key);
return hasKey;
}
@Override
public Optional<String> get(String key) {
Optional<String> value = configuration.get(key);
accessedKeys.add(key);
return value;
}
@Override
public String[] getStringArray(String key) {
String[] values = configuration.getStringArray(key);
accessedKeys.add(key);
return values;
}
}
}
|