diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-25 16:31:45 +0200 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2014-07-30 18:03:36 +0200 |
commit | 6074164392edd3db2dfdfd21d05cd56c19e2b0e6 (patch) | |
tree | b9314796d68c4c396dcf45a1ab689b06490fd4a2 /sonar-batch/src/main/java/org/sonar/batch/scan | |
parent | 12f243728f42a5eb1e714ff15f0240109193f1d8 (diff) | |
download | sonarqube-6074164392edd3db2dfdfd21d05cd56c19e2b0e6.tar.gz sonarqube-6074164392edd3db2dfdfd21d05cd56c19e2b0e6.zip |
SONAR-5389 New duplication API
Diffstat (limited to 'sonar-batch/src/main/java/org/sonar/batch/scan')
6 files changed, 158 insertions, 19 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java index c5e21b89a66..3a209b91961 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/ProjectScanContainer.java @@ -42,6 +42,8 @@ import org.sonar.batch.bootstrap.MetricProvider; import org.sonar.batch.components.PeriodsDefinition; import org.sonar.batch.debt.DebtModelProvider; import org.sonar.batch.debt.IssueChangelogDebtCalculator; +import org.sonar.batch.duplication.BlockCache; +import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.index.Caches; import org.sonar.batch.index.ComponentDataCache; import org.sonar.batch.index.ComponentDataPersister; @@ -194,6 +196,10 @@ public class ProjectScanContainer extends ComponentContainer { new RulesProvider(), new DebtModelProvider(), + // Duplications + BlockCache.class, + DuplicationCache.class, + ProjectSettings.class); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/SensorContextAdaptor.java b/sonar-batch/src/main/java/org/sonar/batch/scan/SensorContextAdaptor.java index 1de8fd7201c..d285a6c8646 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/SensorContextAdaptor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/SensorContextAdaptor.java @@ -27,6 +27,8 @@ import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.measure.Metric; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.sensor.SensorContext; +import org.sonar.api.batch.sensor.duplication.DuplicationBuilder; +import org.sonar.api.batch.sensor.duplication.TokenBuilder; import org.sonar.api.batch.sensor.highlighting.HighlightingBuilder; import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.batch.sensor.issue.IssueBuilder; @@ -49,9 +51,14 @@ import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; import org.sonar.api.resources.Scopes; import org.sonar.api.rule.RuleKey; +import org.sonar.batch.duplication.BlockCache; +import org.sonar.batch.duplication.DefaultDuplicationBuilder; +import org.sonar.batch.duplication.DefaultTokenBuilder; +import org.sonar.batch.duplication.DuplicationCache; import org.sonar.batch.highlighting.DefaultHighlightingBuilder; import org.sonar.batch.index.ComponentDataCache; import org.sonar.batch.symbol.DefaultSymbolTableBuilder; +import org.sonar.duplications.internal.pmd.PmdBlockChunker; import java.io.Serializable; @@ -61,17 +68,20 @@ import java.io.Serializable; */ public class SensorContextAdaptor implements SensorContext { - private org.sonar.api.batch.SensorContext sensorContext; - private MetricFinder metricFinder; - private Project project; - private ResourcePerspectives perspectives; - private Settings settings; - private FileSystem fs; - private ActiveRules activeRules; - private ComponentDataCache componentDataCache; + private final org.sonar.api.batch.SensorContext sensorContext; + private final MetricFinder metricFinder; + private final Project project; + private final ResourcePerspectives perspectives; + private final Settings settings; + private final FileSystem fs; + private final ActiveRules activeRules; + private final ComponentDataCache componentDataCache; + private final BlockCache blockCache; + private final DuplicationCache duplicationCache; public SensorContextAdaptor(org.sonar.api.batch.SensorContext sensorContext, MetricFinder metricFinder, Project project, ResourcePerspectives perspectives, - Settings settings, FileSystem fs, ActiveRules activeRules, ComponentDataCache componentDataCache) { + Settings settings, FileSystem fs, ActiveRules activeRules, ComponentDataCache componentDataCache, BlockCache blockCache, + DuplicationCache duplicationCache) { this.sensorContext = sensorContext; this.metricFinder = metricFinder; this.project = project; @@ -80,6 +90,8 @@ public class SensorContextAdaptor implements SensorContext { this.fs = fs; this.activeRules = activeRules; this.componentDataCache = componentDataCache; + this.blockCache = blockCache; + this.duplicationCache = duplicationCache; } @Override @@ -254,4 +266,33 @@ public class SensorContextAdaptor implements SensorContext { return new DefaultSymbolTableBuilder(((DefaultInputFile) inputFile).key(), componentDataCache); } + @Override + public TokenBuilder tokenBuilder(InputFile inputFile) { + PmdBlockChunker blockChunker = new PmdBlockChunker(getBlockSize(inputFile.language())); + return new DefaultTokenBuilder(inputFile, blockCache, blockChunker); + } + + @Override + public DuplicationBuilder duplicationBuilder(InputFile inputFile) { + return new DefaultDuplicationBuilder(inputFile, duplicationCache); + } + + private int getBlockSize(String languageKey) { + int blockSize = settings.getInt("sonar.cpd." + languageKey + ".minimumLines"); + if (blockSize == 0) { + blockSize = getDefaultBlockSize(languageKey); + } + return blockSize; + } + + private static int getDefaultBlockSize(String languageKey) { + if ("cobol".equals(languageKey)) { + return 30; + } else if ("abap".equals(languageKey) || "natur".equals(languageKey)) { + return 20; + } else { + return 10; + } + } + } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/SensorWrapper.java b/sonar-batch/src/main/java/org/sonar/batch/scan/SensorWrapper.java index b5929008269..32a49a16492 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/SensorWrapper.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/SensorWrapper.java @@ -33,19 +33,23 @@ import java.util.List; public class SensorWrapper implements org.sonar.api.batch.Sensor { - private Sensor analyzer; + private Sensor wrappedSensor; private SensorContext adaptor; private DefaultSensorDescriptor descriptor; private AnalyzerOptimizer optimizer; public SensorWrapper(Sensor newSensor, SensorContext adaptor, AnalyzerOptimizer optimizer) { - this.analyzer = newSensor; + this.wrappedSensor = newSensor; this.optimizer = optimizer; descriptor = new DefaultSensorDescriptor(); newSensor.describe(descriptor); this.adaptor = adaptor; } + public Sensor wrappedSensor() { + return wrappedSensor; + } + @DependedUpon public List<Metric> provides() { return Arrays.asList(descriptor.provides()); @@ -63,7 +67,7 @@ public class SensorWrapper implements org.sonar.api.batch.Sensor { @Override public void analyse(Project module, org.sonar.api.batch.SensorContext context) { - analyzer.execute(adaptor); + wrappedSensor.execute(adaptor); } @Override diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultInputFileValueCoder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultInputFileValueCoder.java new file mode 100644 index 00000000000..d60cbac0088 --- /dev/null +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/DefaultInputFileValueCoder.java @@ -0,0 +1,76 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.batch.scan.filesystem; + +import com.persistit.Value; +import com.persistit.encoding.CoderContext; +import com.persistit.encoding.ValueCoder; +import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; + +import javax.annotation.Nullable; + +import java.io.File; + +class DefaultInputFileValueCoder implements ValueCoder { + + @Override + public void put(Value value, Object object, CoderContext context) { + DeprecatedDefaultInputFile f = (DeprecatedDefaultInputFile) object; + putUTFOrNull(value, f.relativePath()); + putUTFOrNull(value, f.getFileBaseDir().toString()); + putUTFOrNull(value, f.deprecatedKey()); + putUTFOrNull(value, f.sourceDirAbsolutePath()); + putUTFOrNull(value, f.pathRelativeToSourceDir()); + putUTFOrNull(value, f.absolutePath()); + putUTFOrNull(value, f.language()); + putUTFOrNull(value, f.type().name()); + putUTFOrNull(value, f.status().name()); + putUTFOrNull(value, f.hash()); + value.put(f.lines()); + putUTFOrNull(value, f.key()); + } + + private void putUTFOrNull(Value value, @Nullable String utfOrNull) { + if (utfOrNull != null) { + value.putUTF(utfOrNull); + } else { + value.putNull(); + } + } + + @Override + public Object get(Value value, Class clazz, CoderContext context) { + DeprecatedDefaultInputFile file = new DeprecatedDefaultInputFile(value.getString()); + file.setBasedir(new File(value.getString())); + file.setDeprecatedKey(value.getString()); + file.setSourceDirAbsolutePath(value.getString()); + file.setPathRelativeToSourceDir(value.getString()); + file.setAbsolutePath(value.getString()); + file.setLanguage(value.getString()); + file.setType(InputFile.Type.valueOf(value.getString())); + file.setStatus(InputFile.Status.valueOf(value.getString())); + file.setHash(value.getString()); + file.setLines(value.getInt()); + file.setKey(value.getString()); + return file; + } + +} diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java index 5d901a517ae..39672d7f7c5 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/InputPathCache.java @@ -23,6 +23,7 @@ import org.sonar.api.BatchComponent; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputPath; +import org.sonar.api.batch.fs.internal.DeprecatedDefaultInputFile; import org.sonar.batch.index.Cache; import org.sonar.batch.index.Caches; @@ -44,6 +45,7 @@ public class InputPathCache implements BatchComponent { private final Cache<InputPath> cache; public InputPathCache(Caches caches) { + caches.registerValueCoder(DeprecatedDefaultInputFile.class, new DefaultInputFileValueCoder()); cache = caches.createCache("inputFiles"); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureValueCoder.java b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureValueCoder.java index 7f010d97d0a..c0906b51d6d 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureValueCoder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scan/measure/MeasureValueCoder.java @@ -30,6 +30,8 @@ import org.sonar.api.technicaldebt.batch.Characteristic; import org.sonar.api.technicaldebt.batch.Requirement; import org.sonar.api.technicaldebt.batch.TechnicalDebtModel; +import javax.annotation.Nullable; + class MeasureValueCoder implements ValueCoder { private final MetricFinder metricFinder; @@ -42,12 +44,12 @@ class MeasureValueCoder implements ValueCoder { public void put(Value value, Object object, CoderContext context) { Measure<?> m = (Measure) object; - value.putString(m.getMetricKey()); + value.putUTF(m.getMetricKey()); value.put(m.getValue()); - value.putString(m.getData()); - value.putString(m.getDescription()); - value.putString(m.getAlertStatus() != null ? m.getAlertStatus().name() : null); - value.putString(m.getAlertText()); + putUTFOrNull(value, m.getData()); + putUTFOrNull(value, m.getDescription()); + putUTFOrNull(value, m.getAlertStatus() != null ? m.getAlertStatus().name() : null); + putUTFOrNull(value, m.getAlertText()); value.put(m.getTendency()); value.putDate(m.getDate()); value.put(m.getVariation1()); @@ -55,7 +57,7 @@ class MeasureValueCoder implements ValueCoder { value.put(m.getVariation3()); value.put(m.getVariation4()); value.put(m.getVariation5()); - value.putString(m.getUrl()); + putUTFOrNull(value, m.getUrl()); Characteristic characteristic = m.getCharacteristic(); value.put(characteristic != null ? characteristic.id() : null); Requirement requirement = m.getRequirement(); @@ -63,7 +65,15 @@ class MeasureValueCoder implements ValueCoder { Integer personId = m.getPersonId(); value.put(personId != null ? personId.intValue() : null); PersistenceMode persistenceMode = m.getPersistenceMode(); - value.putString(persistenceMode != null ? persistenceMode.name() : null); + putUTFOrNull(value, persistenceMode != null ? persistenceMode.name() : null); + } + + private void putUTFOrNull(Value value, @Nullable String utfOrNull) { + if (utfOrNull != null) { + value.putUTF(utfOrNull); + } else { + value.putNull(); + } } public Object get(Value value, Class clazz, CoderContext context) { |