3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.scanner.cpd.deprecated;
22 import com.google.common.annotations.VisibleForTesting;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.sonar.api.CoreProperties;
26 import org.sonar.api.batch.CpdMapping;
27 import org.sonar.api.batch.Phase;
28 import org.sonar.api.batch.fs.FileSystem;
29 import org.sonar.api.batch.sensor.Sensor;
30 import org.sonar.api.batch.sensor.SensorContext;
31 import org.sonar.api.batch.sensor.SensorDescriptor;
32 import org.sonar.api.config.Settings;
35 * Feed block index using deprecated {@link CpdMapping} extension point if not already
36 * fed by another Sensor using {@link SensorContext#newCpdTokens()}. Special case for Java
37 * that use a dedicated block indexer.
38 * Can be removed when {@link CpdMapping} extension is removed and Java specific part moved to Java plugin.
40 @Phase(name = Phase.Name.POST)
41 public class DeprecatedCpdBlockIndexerSensor implements Sensor {
43 private static final Logger LOG = LoggerFactory.getLogger(DeprecatedCpdBlockIndexerSensor.class);
45 private CpdBlockIndexer javaCpdBlockIndexer;
46 private CpdBlockIndexer defaultCpdBlockIndexer;
47 private Settings settings;
48 private FileSystem fs;
50 public DeprecatedCpdBlockIndexerSensor(JavaCpdBlockIndexer javaCpdBlockIndexer, DefaultCpdBlockIndexer defaultCpdBlockIndexer, Settings settings, FileSystem fs) {
51 this.javaCpdBlockIndexer = javaCpdBlockIndexer;
52 this.defaultCpdBlockIndexer = defaultCpdBlockIndexer;
53 this.settings = settings;
58 public void describe(SensorDescriptor descriptor) {
59 descriptor.name("CPD Block Indexer");
63 CpdBlockIndexer getBlockIndexer(String language) {
64 if (javaCpdBlockIndexer.isLanguageSupported(language)) {
65 return javaCpdBlockIndexer;
67 return defaultCpdBlockIndexer;
71 boolean isSkipped(String language) {
72 String key = "sonar.cpd." + language + ".skip";
73 if (settings.hasKey(key)) {
74 return settings.getBoolean(key);
76 return settings.getBoolean(CoreProperties.CPD_SKIP_PROPERTY);
80 public void execute(SensorContext context) {
81 if (settings.hasKey(CoreProperties.CPD_SKIP_PROPERTY)) {
82 LOG.warn("\"sonar.cpd.skip\" property is deprecated and will be removed. Please set \"sonar.cpd.exclusions=**\" instead to disable duplication mechanism.");
85 for (String language : fs.languages()) {
86 if (settings.hasKey("sonar.cpd." + language + ".skip")) {
88 .warn("\"sonar.cpd." + language + ".skip\" property is deprecated and will be removed. Please set \"sonar.cpd.exclusions=**\" instead to disable duplication mechanism.");
91 if (isSkipped(language)) {
92 LOG.info("Detection of duplicated code is skipped for {}", language);
96 CpdBlockIndexer blockIndexer = getBlockIndexer(language);
97 if (!blockIndexer.isLanguageSupported(language)) {
98 LOG.debug("Detection of duplicated code is not supported for {}", language);
101 LOG.info("{} is used for {}", blockIndexer, language);
102 blockIndexer.index(language);