Skip to content

Commit

Permalink
fix(spoon): Fix wrong filepath for badsmells (#855)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinWitt authored Jul 16, 2023
1 parent b7245a9 commit e208bbe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.github.martinwitt.spoon_analyzer.badsmells.unnecessary_tostring.UnnecessaryTostring;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Optional;
Expand All @@ -30,12 +32,16 @@

class AnalyzerResultVisitor implements BadSmellVisitor<AnalyzerResult> {

private static final AnalyzerResultVisitor analyzerResultVisitor = new AnalyzerResultVisitor();
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private final Path rootPath;

public static Optional<AnalyzerResult> toAnalyzerResult(BadSmell badSmell) {
public AnalyzerResultVisitor(Path rootPath) {
this.rootPath = rootPath;
}

Optional<AnalyzerResult> toAnalyzerResult(BadSmell badSmell) {
try {
return Optional.ofNullable(badSmell.accept(analyzerResultVisitor));
return Optional.ofNullable(badSmell.accept(this));

} catch (Exception e) {
logger.atWarning().withStackTrace(StackSize.NONE).log(
Expand All @@ -45,8 +51,6 @@ public static Optional<AnalyzerResult> toAnalyzerResult(BadSmell badSmell) {
}
}

private AnalyzerResultVisitor() {}

@Override
public AnalyzerResult visit(IndexOfReplaceableByContains badSmell) {
return toSpoonAnalyzerResult(
Expand All @@ -58,8 +62,23 @@ public AnalyzerResult visit(IndexOfReplaceableByContains badSmell) {
.toString()));
}

private String getAbsolutePath(BadSmell badSmell) {
return badSmell.getAffectedType().getPosition().getFile().getAbsolutePath();
private Optional<String> getAbsolutePath(BadSmell badSmell) {
return getRelativeFilePath(badSmell, rootPath.toAbsolutePath().toString());
}

private Optional<String> getRelativeFilePath(BadSmell badSmell, String rootPath) {
try {
File file = badSmell.getAffectedType().getPosition().getFile();
Path filePath = Paths.get(file.getAbsolutePath());
Path rootPathObj = Paths.get(rootPath);

// Get the relative path of the file relative to the root path
Path relativePath = rootPathObj.relativize(filePath);

return Optional.ofNullable(relativePath.toString());
} catch (Exception e) {
return Optional.empty();
}
}

private Position toPosition(SourcePosition position) {
Expand All @@ -73,7 +92,7 @@ private Position toPosition(SourcePosition position) {
}

public AnalyzerResult toSpoonAnalyzerResult(BadSmell badSmell, SourcePosition position, String snippet) {
String absolutePath = getAbsolutePath(badSmell);
String absolutePath = getAbsolutePath(badSmell).orElse("unknown");
RuleId ruleId = new RuleId(badSmell.getName());
return new SpoonAnalyzerResult(
ruleId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ public class SpoonBasedAnalyzer {

public List<AnalyzerResult> analyze(Path sourceRoot) {
SpoonAnalyzer analyzer = new SpoonAnalyzer();
AnalyzerResultVisitor analyzerResultVisitor = new AnalyzerResultVisitor(sourceRoot);
List<BadSmell> analyze = analyzer.analyze(sourceRoot.toAbsolutePath().toString());
return analyze.stream()
.map(AnalyzerResultVisitor::toAnalyzerResult)
.map(analyzerResultVisitor::toAnalyzerResult)
.filter(v -> v.isPresent())
.map(v -> v.get())
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private void migrateDataBase(Promise<Object> promise) {
removeProjectsWithOutHashes();
removeDuplicatedProjects();
removeRuleIdsWithSpaces();
removeBadSmellsWithWrongFolder();
logger.atInfo().log("Finished migrating database");
promise.complete();
}
Expand Down Expand Up @@ -129,4 +130,11 @@ private void removeRuleIdsWithSpaces() {
.deleteMany(Filters.and(Filters.regex("ruleID", ".*\s.*"), Filters.eq("analyzer", "Spoon")));
logger.atInfo().log("Removed %d bad smells with ruleId containing spaces", deleteMany.getDeletedCount());
}

private void removeBadSmellsWithWrongFolder() {
DeleteResult deleteMany = badSmellRepositoryImpl
.mongoCollection()
.deleteMany(Filters.and(Filters.regex("filePath", ".*/tmp/.*"), Filters.eq("analyzer", "Spoon")));
logger.atInfo().log("Removed %d bad smells with ruleId containing spaces", deleteMany.getDeletedCount());
}
}

0 comments on commit e208bbe

Please sign in to comment.