From d45f8580633ce65da1e48522df9f08ad24dd463e Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Sat, 15 Jul 2023 20:47:16 +0200 Subject: [PATCH] fix(spoon): Make conversion of spoon-analyzer more robust (#849) --- .../analyzer/spoon/AnalyzerResultVisitor.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/AnalyzerResultVisitor.java b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/AnalyzerResultVisitor.java index 5917b3b2d..d793fbed9 100644 --- a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/AnalyzerResultVisitor.java +++ b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/analyzer/spoon/AnalyzerResultVisitor.java @@ -1,5 +1,7 @@ package xyz.keksdose.spoon.code_solver.analyzer.spoon; +import com.google.common.flogger.FluentLogger; +import com.google.common.flogger.StackSize; import io.github.martinwitt.laughing_train.domain.entity.AnalyzerResult; import io.github.martinwitt.laughing_train.domain.value.Position; import io.github.martinwitt.laughing_train.domain.value.RuleId; @@ -29,9 +31,17 @@ class AnalyzerResultVisitor implements BadSmellVisitor { private static final AnalyzerResultVisitor analyzerResultVisitor = new AnalyzerResultVisitor(); + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); public static Optional toAnalyzerResult(BadSmell badSmell) { - return Optional.ofNullable(badSmell.accept(analyzerResultVisitor)); + try { + return Optional.ofNullable(badSmell.accept(analyzerResultVisitor)); + + } catch (Exception e) { + logger.atWarning().withCause(e).withStackTrace(StackSize.SMALL).log( + "Could not convert bad smell to analyzer result"); + return Optional.empty(); + } } private AnalyzerResultVisitor() {} @@ -106,7 +116,7 @@ public AnalyzerResult visit(InnerClassMayBeStatic badSmell) { CtType clone = badSmell.getAffectedType().clone(); clone.setTypeMembers(new ArrayList<>()); String snippet = clone.toString(); - return toSpoonAnalyzerResult(badSmell, badSmell.getAffectedType().getPosition(), snippet); + return toSpoonAnalyzerResult(badSmell, badSmell.getInnerClass().getPosition(), snippet); } @Override