Skip to content
This repository has been archived by the owner on Aug 24, 2019. It is now read-only.

Improve implementation for parsing Lizard reports #60

Open
wants to merge 34 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
1068cdb
update version for sonarqube
raatiniemi Feb 25, 2018
8ff0fd9
remove use of PersistenceMode since its been removed
raatiniemi Feb 25, 2018
bf3d1b0
implement LizardSensor using new Sensor-interface
raatiniemi Feb 21, 2018
0a2e918
remove unused method from LizardSensor
raatiniemi Feb 25, 2018
5fe7519
hide analyse method from public API of LizardSensor
raatiniemi Feb 25, 2018
afd577b
remove use of deprecated Project from LizardSensor
raatiniemi Feb 25, 2018
98af16d
remove use of deprecated CoreMetrics.COMPLEXITY_IN_FUNCTIONS
raatiniemi Feb 25, 2018
c44cc35
remove use of deprecated CoreMetrics.FUNCTION_COMPLEXITY
raatiniemi Feb 25, 2018
f19cca6
remove use of deprecated CoreMetrics.FILE_COMPLEXITY
raatiniemi Feb 25, 2018
a6c1129
remove use of deprecated CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION
raatiniemi Feb 25, 2018
39a1cf8
combine lizard metric and value into LizardMeasure
raatiniemi Mar 2, 2018
8de7e5f
replace use of deprecated SensorContext
raatiniemi Mar 2, 2018
cacf3af
hide constructor for LizardMeasurePersistor
raatiniemi Mar 2, 2018
49916af
annotate LizardReportParser with Nonnull
raatiniemi Mar 2, 2018
11deec6
combine catches for lizard processing errors
raatiniemi Mar 2, 2018
8b9968f
improve building of complexity report path
raatiniemi Mar 4, 2018
7893d4f
ensure that report file is available before parsing
raatiniemi Mar 4, 2018
d987789
group methods based on calling points
raatiniemi Mar 4, 2018
6c047cf
inline analyse method to reduce unnecessary methods
raatiniemi Mar 4, 2018
62f8190
move lizard report parser to instance field
raatiniemi Mar 4, 2018
0bbc124
use static import for Assert-methods
raatiniemi Mar 4, 2018
0e4d9dc
mark LizardMeasure class as final
raatiniemi Mar 11, 2018
c72a392
hide LizardMeasurePersistor from outside package
raatiniemi Mar 11, 2018
f3fceae
mark LizardMeasurePersistor class as final
raatiniemi Mar 11, 2018
a7e6a6e
convert for into forEach
raatiniemi Mar 11, 2018
e564fdf
improve building of InputFile for LizardMeasurePersistor
raatiniemi Mar 11, 2018
beb8fc5
annotate LizardMeasurePersistor constructor with @Nonnull
raatiniemi Mar 11, 2018
897d562
extract building of LizardMeasure to separate method
raatiniemi Mar 11, 2018
de9690a
perform early exit while iterating of lizard elements
raatiniemi Mar 11, 2018
dab3178
update documentation for LizardReportParser
raatiniemi Mar 11, 2018
6046847
hide LizardReportParser from outside package
raatiniemi Mar 11, 2018
0211b7a
declare LizardReportParser class as final
raatiniemi Mar 11, 2018
e3cd4b0
annotate LizardReportParser with @Nonnull
raatiniemi Mar 11, 2018
a35365a
downgrade version of sonarqube plugin to 5.6
raatiniemi Mar 12, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* backelite-sonar-objective-c-plugin - Enables analysis of Objective-C projects into SonarQube.
* Copyright © 2012 OCTO Technology, Backelite (${email})
*
* 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, see <http://www.gnu.org/licenses/>.
*/
package org.sonar.plugins.objectivec.complexity;

import org.sonar.api.batch.measure.Metric;

import java.io.Serializable;

final class LizardMeasure<T extends Serializable> {
private final Metric<T> metric;
private final T value;

private LizardMeasure(Metric<T> metric, T value) {
this.metric = metric;
this.value = value;
}

static <T extends Serializable> LizardMeasure of(Metric<T> metric, T value) {
return new LizardMeasure<>(metric, value);
}

public Metric<T> getMetric() {
return metric;
}

public T getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,33 +19,33 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.batch.measure.Metric;
import org.sonar.api.batch.sensor.SensorContext;

import javax.annotation.Nonnull;
import java.io.File;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
* This class is used to save the measures created by the lizardReportParser in the sonar database
*
* @author Andres Gil Herrera
* @since 28/05/15.
*/
public class LizardMeasurePersistor {
final class LizardMeasurePersistor {

private static final Logger LOGGER = LoggerFactory.getLogger(LizardMeasurePersistor.class);

private final Project project;
private final SensorContext sensorContext;
private final FileSystem fileSystem;

public LizardMeasurePersistor(final Project project, final SensorContext sensorContext, final FileSystem fileSystem) {
this.project = project;
LizardMeasurePersistor(@Nonnull final SensorContext sensorContext, @Nonnull final FileSystem fileSystem) {
this.sensorContext = sensorContext;
this.fileSystem = fileSystem;
}
Expand All @@ -54,33 +54,38 @@ public LizardMeasurePersistor(final Project project, final SensorContext sensorC
*
* @param measures Map containing as key the name of the file and as value a list containing the measures for that file
*/
public void saveMeasures(final Map<String, List<Measure>> measures) {
<T extends Serializable> void saveMeasures(@Nonnull final Map<String, List<LizardMeasure<T>>> measures) {
for (Map.Entry<String, List<LizardMeasure<T>>> entry : measures.entrySet()) {
Optional<InputFile> value = buildInputFile(entry.getKey());
if (value.isPresent()) {
entry.getValue().forEach(measure -> saveMeasure(value.get(), measure));
continue;
}

if (measures == null) {
return;
LOGGER.warn("File not included {}", entry.getKey());
}
}

for (Map.Entry<String, List<Measure>> entry : measures.entrySet()) {
File file = new File(fileSystem.baseDir(), entry.getKey());
InputFile inputFile = fileSystem.inputFile(fileSystem.predicates().hasAbsolutePath(file.getAbsolutePath()));
@Nonnull
private Optional<InputFile> buildInputFile(@Nonnull String relativeFilePath) {
File file = new File(fileSystem.baseDir(), relativeFilePath);
FilePredicate predicate = fileSystem.predicates().hasAbsolutePath(file.getAbsolutePath());

if (inputFile == null) {
LOGGER.warn("file not included in sonar {}", entry.getKey());
continue;
}
return Optional.ofNullable(fileSystem.inputFile(predicate));
}

Resource resource = sensorContext.getResource(inputFile);
private <T extends Serializable> void saveMeasure(@Nonnull InputFile inputFile, @Nonnull LizardMeasure<T> measure) {
Metric<T> metric = measure.getMetric();

if (resource != null) {
for (Measure measure : entry.getValue()) {
try {
LOGGER.debug("Save measure {} for file {}", measure.getMetric().getName(), file);
sensorContext.saveMeasure(resource, measure);
} catch (Exception e) {
LOGGER.error(" Exception -> {} -> {}", entry.getKey(), measure.getMetric().getName(), e);
}
}
}
try {
LOGGER.debug("Save measure {} for file {}", metric.key(), inputFile.relativePath());
sensorContext.<T>newMeasure()
.on(inputFile)
.forMetric(metric)
.withValue(measure.getValue())
.save();
} catch (Exception e) {
LOGGER.error(" Exception -> {} -> {}", inputFile.relativePath(), metric.key(), e);
}
}

Expand Down
Loading