Skip to content

Commit

Permalink
fix for cast exception
Browse files Browse the repository at this point in the history
  • Loading branch information
bcolvin committed May 13, 2019
1 parent 1828c45 commit 794c8f4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@
import org.apache.tools.ant.DirectoryScanner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.batch.sensor.SensorDescriptor;
import org.sonar.api.component.ResourcePerspectives;
import org.sonar.api.config.Settings;

import java.io.File;

Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@
<logback.version>1.0.13</logback.version>
<mockito.version>1.9.0</mockito.version>
<slf4j.version>1.7.21</slf4j.version>
<sonar.version>7.5</sonar.version>
<sonar-orchestrator.version>3.22.0.1791</sonar-orchestrator.version>
<sonarlint.version>4.0.0.2052</sonarlint.version>
<sonar.version>7.7</sonar.version>
<sonar-orchestrator.version>3.24.0.1993</sonar-orchestrator.version>
<sonarlint.version>4.1.0.2218</sonarlint.version>
<sslr.version>1.23</sslr.version>
<sslr-squid-bridge.version>2.6.1</sslr-squid-bridge.version>
<ant.version>1.6</ant.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.InputModule;
import org.sonar.api.batch.fs.internal.DefaultInputComponent;
import org.sonar.api.batch.sensor.SensorContext;
import org.sonar.api.measures.CoreMetrics;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -115,23 +116,63 @@ private void parseMeasure(String type, NodeList itemList) {

NodeList values = itemElement.getElementsByTagName(VALUE);
if (FILE_MEASURE.equalsIgnoreCase(type)) {
addComplexityFileMeasures(name, values);
InputFile inputFile = getFile(name);
addComplexityFileMeasures(inputFile, values);
} else if (FUNCTION_MEASURE.equalsIgnoreCase(type)) {
addComplexityFunctionMeasures(new SwiftFunction(name), values);
addComplexityFunctionMeasures(new SwiftFunction(0,name), values);
}
}
}
}

private void addComplexityFileMeasures(String fileName, NodeList values) {
LOGGER.debug("File measures for {}",fileName);
private InputFile getFile(String fileName){
FilePredicate fp = context.fileSystem().predicates().hasRelativePath(fileName);
if (!context.fileSystem().hasFiles(fp)) {
LOGGER.warn("file not included in sonar {}", fileName);
return;
return null;
}
InputFile component = context.fileSystem().inputFile(fp);
return context.fileSystem().inputFile(fp);
}

static class SwiftFunction extends DefaultInputComponent implements InputComponent {
private String name;
private String key;
private String file;
private int lineNumber;
SwiftFunction(int scannerId, String name) {
super(scannerId);
String[] vals = name.split(" at ");
if (vals.length >= 2) {
this.name = vals[0].replaceAll("\\W","");

if (vals[1].contains(":")) {
String[] sp = vals[1].split(":");
this.file = sp[0].substring(0,sp[0].lastIndexOf("."));
this.lineNumber = Integer.parseInt(sp[1]);
} else {
this.file = vals[1];
this.lineNumber = 0;
}

this.key = String.format("%s.%s:%d", this.file, this.name, this.lineNumber);
} else {
this.key = name;
}
}
@Override
public String key() {
return key;
}
@Override
public boolean isFile() {
return false;
}
}

private void addComplexityFileMeasures(InputFile component, NodeList values) {
LOGGER.debug("File measures for {}",component.toString());
int complexity = Integer.parseInt(values.item(cyclomaticComplexityIndex).getTextContent());

context.<Integer>newMeasure()
.on(component)
.forMetric(CoreMetrics.COMPLEXITY)
Expand All @@ -153,8 +194,8 @@ private void addComplexityFileMeasures(String fileName, NodeList values) {
.save();
}

private void addComplexityFunctionMeasures(SwiftFunction component, NodeList values) {
LOGGER.debug("Function measures for {}",component.key);
private void addComplexityFunctionMeasures(InputComponent component, NodeList values) {
LOGGER.debug("Function measures for {}",component.key());
int complexity = Integer.parseInt(values.item(cyclomaticComplexityIndex).getTextContent());
context.<Integer>newMeasure()
.on(component)
Expand All @@ -169,53 +210,4 @@ private void addComplexityFunctionMeasures(SwiftFunction component, NodeList val
.withValue(numberOfLines)
.save();
}

private static class SwiftFunction implements InputModule {
private String name;
private String key;
private String file;
private int lineNumber;

public SwiftFunction(String name) {
String[] vals = name.split(" at ");
if (vals.length >= 2) {
this.name = vals[0].replaceAll("\\W","");

if (vals[1].contains(":")) {
String[] sp = vals[1].split(":");
this.file = sp[0].substring(0,sp[0].lastIndexOf("."));
this.lineNumber = Integer.parseInt(sp[1]);
} else {
this.file = vals[1];
this.lineNumber = 0;
}

this.key = String.format("%s.%s:%d", this.file, this.name, this.lineNumber);
} else {
this.key = name;
}
}

@Override
public String key() {
return key;
}

public String getName() {
return name;
}

public String getFile() {
return file;
}

public int getLineNumber() {
return lineNumber;
}

@Override
public boolean isFile() {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@
package com.backelite.sonarqube.swift.lang;

import com.backelite.sonarqube.swift.lang.core.Swift;
import org.sonar.api.batch.ScannerSide;
import org.sonar.api.batch.fs.FilePredicate;
import org.sonar.api.batch.fs.FilePredicates;
import org.sonar.api.batch.fs.FileSystem;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.scanner.ScannerSide;

import javax.annotation.CheckForNull;
import java.io.File;
import java.util.ArrayList;
import java.util.List;


@ScannerSide
public class SwiftFileSystem {

Expand All @@ -49,10 +48,10 @@ public boolean hasSwiftFiles() {
return fileSystem.hasFiles(isSwiftLanguage);
}

public List<File> sourceFiles() {
Iterable<File> files = fileSystem.files(predicates.and(isSwiftLanguage, isMainTypeFile));
List<File> list = new ArrayList<>();
files.iterator().forEachRemaining(list::add);
public List<InputFile> sourceFiles() {
Iterable<InputFile> inputFiles = fileSystem.inputFiles(predicates.and(isSwiftLanguage, isMainTypeFile));
List<InputFile> list = new ArrayList<>();
inputFiles.iterator().forEachRemaining(list::add);
return list;
}

Expand Down

0 comments on commit 794c8f4

Please sign in to comment.