Skip to content

Commit

Permalink
fix bug when classes from the same package are instantiated
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Knoedlseder committed Oct 8, 2024
1 parent 034cc5d commit f8481c5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class AssertAllDetector implements Detector<Perfume> {

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<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) {
Expand All @@ -52,7 +53,7 @@ public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {
private List<MethodCallExpr> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,7 +48,14 @@ public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {

private List<ObjectCreationExpr> 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);
});
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/detectors/SwingTimerDetectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
1 change: 1 addition & 0 deletions src/test/resources/detectors/swing/SwingTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit f8481c5

Please sign in to comment.