From f8481c5df1c4da55e5951a493364ab037958694b Mon Sep 17 00:00:00 2001 From: Christoph Knoedlseder Date: Tue, 8 Oct 2024 19:45:14 +0200 Subject: [PATCH] fix bug when classes from the same package are instantiated --- .../engine/detector/perfume/AssertAllDetector.java | 3 ++- .../engine/detector/perfume/SwingTimerDetector.java | 10 +++++++++- src/test/java/detectors/SwingTimerDetectorTest.java | 6 +++--- src/test/resources/detectors/swing/SwingTimer.java | 1 + 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/AssertAllDetector.java b/src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/AssertAllDetector.java index 394ff62..02e9dfd 100644 --- a/src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/AssertAllDetector.java +++ b/src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/AssertAllDetector.java @@ -29,6 +29,7 @@ public class AssertAllDetector implements Detector { private static final String QUALIFIED_ASSERT_ALL_METHOD_NAME = "org.junit.jupiter.api.Assertions.assertAll"; private static final String ASSERTIONS_IMPORT_NAME = "org.junit.jupiter.api.Assertions"; + private static final String ASSERT_ALL = "assertAll"; @Override public @NotNull List> detect(@NotNull CompilationUnit astRoot) { @@ -52,7 +53,7 @@ public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) { private List getAssertAllMethodCalls(@NotNull CompilationUnit astRoot) { return astRoot.findAll(MethodCallExpr.class, expr -> { // contains instead of equals because of possible 'Assertions.assertAll' calls - if (!expr.getNameAsString().contains("assertAll")) { + if (!expr.getNameAsString().contains(ASSERT_ALL)) { return false; } if (expr.getScope().isPresent()) { diff --git a/src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/SwingTimerDetector.java b/src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/SwingTimerDetector.java index 1fe29d4..1ec6e9d 100644 --- a/src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/SwingTimerDetector.java +++ b/src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/SwingTimerDetector.java @@ -2,6 +2,7 @@ import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.ast.expr.ObjectCreationExpr; +import com.github.javaparser.resolution.UnsolvedSymbolException; import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl; import com.github.javaparser.resolution.types.ResolvedType; import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade; @@ -47,7 +48,14 @@ public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) { private List getNewTimerExpressions(@NotNull CompilationUnit astRoot) { return astRoot.findAll(ObjectCreationExpr.class, expr -> { - ResolvedType resolvedType = expr.calculateResolvedType(); + ResolvedType resolvedType; + // when classes from the same package are instantiated, the javaparser cannot resolve the type, therefore + // we simply skip such occurrences of object creation expressions + try { + resolvedType = expr.calculateResolvedType(); + } catch (UnsolvedSymbolException e) { + return false; + } return resolvedType instanceof ReferenceTypeImpl referenceType && referenceType.getQualifiedName().equals(QUALIFIED_TIMER_NAME); }); diff --git a/src/test/java/detectors/SwingTimerDetectorTest.java b/src/test/java/detectors/SwingTimerDetectorTest.java index 97c8503..a4edfc1 100644 --- a/src/test/java/detectors/SwingTimerDetectorTest.java +++ b/src/test/java/detectors/SwingTimerDetectorTest.java @@ -50,16 +50,16 @@ void detect() { detection = detections.get(1); assertThat(detection.getDetectable()).isEqualTo(perfume); assertThat(detection.getTypeName()).isEqualTo("SwingTimer"); - assertThat(detection.getCodeRanges()).containsExactly(CodeRange.of(13, 18, 18, 10)); + assertThat(detection.getCodeRanges()).containsExactly(CodeRange.of(14, 18, 19, 10)); detection = detections.get(2); assertThat(detection.getDetectable()).isEqualTo(perfume); assertThat(detection.getTypeName()).isEqualTo("SwingTimer"); - assertThat(detection.getCodeRanges()).containsExactly(CodeRange.of(19, 22, 19, 60)); + assertThat(detection.getCodeRanges()).containsExactly(CodeRange.of(20, 22, 20, 60)); detection = detections.get(3); assertThat(detection.getDetectable()).isEqualTo(perfume); assertThat(detection.getTypeName()).isEqualTo("SwingTimer"); - assertThat(detection.getCodeRanges()).containsExactly(CodeRange.of(20, 36, 20, 71)); + assertThat(detection.getCodeRanges()).containsExactly(CodeRange.of(21, 36, 21, 71)); } } diff --git a/src/test/resources/detectors/swing/SwingTimer.java b/src/test/resources/detectors/swing/SwingTimer.java index 0a3ea24..9ab6492 100644 --- a/src/test/resources/detectors/swing/SwingTimer.java +++ b/src/test/resources/detectors/swing/SwingTimer.java @@ -7,6 +7,7 @@ public class SwingTimer { Timer t = new Timer(1000, new ActionListener() {}); + SomeClass c = new SomeClass(); public static void main(String[] args) { Timer timer1;