Skip to content

Commit

Permalink
add swing perfumes and many improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Christoph Knoedlseder committed Oct 6, 2024
1 parent 28c9d57 commit b735623
Show file tree
Hide file tree
Showing 30 changed files with 750 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.Problem;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.resolution.typesolvers.CombinedTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JarTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.JavaParserTypeSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;
import com.github.javaparser.symbolsolver.utils.SymbolSolverCollectionStrategy;
import de.jsilbereisen.perfumator.model.AnalysisResult;
import de.jsilbereisen.perfumator.model.StatisticsSummary;
Expand Down Expand Up @@ -119,6 +121,7 @@ public static JavaParser getConfiguredJavaParser() {
ParserConfiguration config = new ParserConfiguration();

config.setLanguageLevel(ParserConfiguration.LanguageLevel.JAVA_17);
config.setSymbolResolver(new JavaSymbolSolver(new ReflectionTypeSolver(false)));
config.setDoNotAssignCommentsPrecedingEmptyLines(false);

return new JavaParser(config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import de.jsilbereisen.perfumator.engine.detector.Detector;
import de.jsilbereisen.perfumator.engine.visitor.MethodCallByNameVisitor;
import de.jsilbereisen.perfumator.model.DetectedInstance;
import de.jsilbereisen.perfumator.model.perfume.Perfume;
import lombok.EqualsAndHashCode;
Expand All @@ -14,14 +16,19 @@
import java.util.ArrayList;
import java.util.List;

/**
* {@link Detector} for the "Assert all" {@link Perfume}.
* Detects the perfume only if the method is part of the {@link org.junit.jupiter.api.Assertions} class.
*/
@EqualsAndHashCode
public class AssertAllDetector implements Detector<Perfume> {

private Perfume perfume;

private JavaParserFacade analysisContext;

private static final String ASSERT_ALL_METHOD_NAME = "assertAll";
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";

@Override
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) {
Expand All @@ -43,14 +50,22 @@ public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {
}

private List<MethodCallExpr> getAssertAllMethodCalls(@NotNull CompilationUnit astRoot) {
MethodCallByNameVisitor methodCallByNameVisitor = new MethodCallByNameVisitor();
astRoot.accept(methodCallByNameVisitor, null);
List<MethodCallExpr> assertAllMethodCallExpressions = new ArrayList<>();
for (MethodCallExpr methodCallExpr : methodCallByNameVisitor.getMethodCalls()) {
if (ASSERT_ALL_METHOD_NAME.equals(methodCallExpr.getNameAsString())) {
assertAllMethodCallExpressions.add(methodCallExpr);
return astRoot.findAll(MethodCallExpr.class, expr -> {
// contains instead of equals because of possible 'Assertions.assertAll' calls
if (!expr.getNameAsString().contains("assertAll")) {
return false;
}
}
return assertAllMethodCallExpressions;
if (expr.getScope().isPresent()) {
// for non-static imports
ResolvedType resolvedType = expr.getScope().get().calculateResolvedType();
return resolvedType instanceof ReferenceTypeImpl referenceType
&& referenceType.getQualifiedName().equals(ASSERTIONS_IMPORT_NAME);
} else {
// for static imports
ResolvedMethodDeclaration resolvedMethodDeclaration = expr.resolve();
String qualifiedName = resolvedMethodDeclaration.getQualifiedName();
return qualifiedName.equals(QUALIFIED_ASSERT_ALL_METHOD_NAME);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package de.jsilbereisen.perfumator.engine.detector.perfume;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import de.jsilbereisen.perfumator.engine.detector.Detector;
import de.jsilbereisen.perfumator.model.DetectedInstance;
import de.jsilbereisen.perfumator.model.perfume.Perfume;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
* {@link Detector} for the "JFrame dispose" {@link Perfume}.
* Detects the perfume only if the method is part of the {@link javax.swing.JFrame} class.
*/
public class JFrameDisposeDetector implements Detector<Perfume> {

private Perfume perfume;

private JavaParserFacade analysisContext;

private static final String DISPOSE_METHOD_NAME = "dispose";
private static final String QUALIFIED_JFRAME_CLASS_NAME = "javax.swing.JFrame";

@Override
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) {
List<DetectedInstance<Perfume>> detectedInstances = new ArrayList<>();
List<MethodCallExpr> disposeMethodCallExpressions = getJFrameDisposeMethodCalls(astRoot);
disposeMethodCallExpressions
.forEach(expr -> detectedInstances.add(DetectedInstance.from(expr, perfume, astRoot)));
return detectedInstances;
}

@Override
public void setConcreteDetectable(@NotNull Perfume concreteDetectable) {
this.perfume = concreteDetectable;
}

@Override
public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {
this.analysisContext = analysisContext;
}

private List<MethodCallExpr> getJFrameDisposeMethodCalls(@NotNull CompilationUnit astRoot) {
return astRoot.findAll(MethodCallExpr.class, expr -> {
if (!expr.getNameAsString().equals(DISPOSE_METHOD_NAME)) {
return false;
}
var scope = expr.getScope();
if (scope.isPresent()) {
var resolvedType = scope.get().calculateResolvedType();
if (resolvedType instanceof ReferenceTypeImpl referenceType) {
return referenceType.getQualifiedName().equals(QUALIFIED_JFRAME_CLASS_NAME);
}
}
return false;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import de.jsilbereisen.perfumator.engine.detector.Detector;
import de.jsilbereisen.perfumator.engine.visitor.MethodDeclarationVisitor;
import de.jsilbereisen.perfumator.model.DetectedInstance;
import de.jsilbereisen.perfumator.model.perfume.Perfume;
import lombok.EqualsAndHashCode;
Expand All @@ -15,15 +15,19 @@
import java.util.ArrayList;
import java.util.List;

/**
* {@link Detector} for the "Parameterized Test" {@link Perfume}.
* Detects the perfume only if the annotation is part of JUnit 5 ({@link org.junit.jupiter.params.ParameterizedTest}).
*/
@EqualsAndHashCode
public class ParameterizedTestDetector implements Detector<Perfume> {

private Perfume perfume;

private JavaParserFacade analysisContext;

private static final String PARAMETERIZED_TEST_IDENTIFIER = "ParameterizedTest";
private static final String PARAMETERIZED_TEST_IDENTIFIER = "org.junit.jupiter.params.ParameterizedTest";

@Override
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) {
List<DetectedInstance<Perfume>> detectedInstances = new ArrayList<>();
Expand All @@ -42,21 +46,11 @@ public void setConcreteDetectable(@NotNull Perfume concreteDetectable) {
public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {
this.analysisContext = analysisContext;
}

private List<MethodDeclaration> getParameterizedTestMethodDeclarations(@NotNull CompilationUnit astRoot) {
MethodDeclarationVisitor methodDeclarationVisitor = new MethodDeclarationVisitor();
astRoot.accept(methodDeclarationVisitor, null);
List<MethodDeclaration> parameterizedTestMethodDeclarations = new ArrayList<>();
for (MethodDeclaration declaration : methodDeclarationVisitor.getMethodDeclarations()) {
boolean hasParameterizedTestAnnotation = declaration.getAnnotations()
.stream()
.map(AnnotationExpr::getNameAsString)
.anyMatch(id -> id.equals(PARAMETERIZED_TEST_IDENTIFIER));

if (hasParameterizedTestAnnotation) {
parameterizedTestMethodDeclarations.add(declaration);
}
}
return parameterizedTestMethodDeclarations;
return astRoot.findAll(MethodDeclaration.class, methodDeclaration -> methodDeclaration.getAnnotations().stream()
.map(AnnotationExpr::resolve)
.map(ResolvedTypeDeclaration::getQualifiedName)
.anyMatch(name -> name.equals(PARAMETERIZED_TEST_IDENTIFIER)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,31 @@
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.expr.AnnotationExpr;
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import de.jsilbereisen.perfumator.engine.detector.Detector;
import de.jsilbereisen.perfumator.engine.visitor.MethodDeclarationVisitor;
import de.jsilbereisen.perfumator.model.DetectedInstance;
import de.jsilbereisen.perfumator.model.perfume.Perfume;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
* {@link Detector} for the "Setup or teardown method" {@link Perfume}.
* Detects the perfume only if the annotation is part of the JUnit 5 package {@link org.junit.jupiter.api}.
*/
public class SetupAndTeardownMethodDetector implements Detector<Perfume> {

private Perfume perfume;

private JavaParserFacade analysisContext;

public static final List<String> TEST_ANNOTATIONS = List.of("BeforeAll", "BeforeEach", "AfterAll", "AfterEach");
public static final String IMPORT_QUALIFIER = "org.junit.jupiter.api.";
public static final Set<String> TEST_ANNOTATIONS = Set.of("BeforeAll", "BeforeEach", "AfterAll", "AfterEach");

@Override
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) {
Expand All @@ -41,17 +48,14 @@ public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {
this.analysisContext = analysisContext;
}

private Set<String> getQualifiedAnnotations() {
return TEST_ANNOTATIONS.stream().map(annotation -> IMPORT_QUALIFIER + annotation).collect(Collectors.toSet());
}

private List<MethodDeclaration> getSetupAndTeardownMethodDeclarations(@NotNull CompilationUnit astRoot) {
MethodDeclarationVisitor methodDeclarationVisitor = new MethodDeclarationVisitor();
astRoot.accept(methodDeclarationVisitor, null);
List<MethodDeclaration> setupAndTeardownMethodDeclarations = new ArrayList<>();
for (MethodDeclaration declaration : methodDeclarationVisitor.getMethodDeclarations()) {
for (AnnotationExpr annotation : declaration.getAnnotations()) {
if (TEST_ANNOTATIONS.contains(annotation.getNameAsString())) {
setupAndTeardownMethodDeclarations.add(declaration);
}
}
}
return setupAndTeardownMethodDeclarations;
return astRoot.findAll(MethodDeclaration.class, methodDeclaration -> methodDeclaration.getAnnotations().stream()
.map(AnnotationExpr::resolve)
.map(ResolvedTypeDeclaration::getQualifiedName)
.anyMatch(name -> getQualifiedAnnotations().contains(name)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package de.jsilbereisen.perfumator.engine.detector.perfume;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.expr.ObjectCreationExpr;
import com.github.javaparser.resolution.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import de.jsilbereisen.perfumator.engine.detector.Detector;
import de.jsilbereisen.perfumator.model.DetectedInstance;
import de.jsilbereisen.perfumator.model.perfume.Perfume;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

/**
* {@link Detector} for the "Swing timer" {@link Perfume}.
* Detects the perfume only if an object of type {@link javax.swing.Timer} is created.
*/
public class SwingTimerDetector implements Detector<Perfume> {

private Perfume perfume;

private JavaParserFacade analysisContext;

private static final String QUALIFIED_TIMER_NAME = "javax.swing.Timer";

@Override
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) {
List<DetectedInstance<Perfume>> detectedInstances = new ArrayList<>();
List<ObjectCreationExpr> newTimerExpressions = getNewTimerExpressions(astRoot);
newTimerExpressions
.forEach(callExpr -> detectedInstances.add(DetectedInstance.from(callExpr, perfume, astRoot)));
return detectedInstances;
}

@Override
public void setConcreteDetectable(@NotNull Perfume concreteDetectable) {
this.perfume = concreteDetectable;
}

@Override
public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {
this.analysisContext = analysisContext;
}

private List<ObjectCreationExpr> getNewTimerExpressions(@NotNull CompilationUnit astRoot) {
return astRoot.findAll(ObjectCreationExpr.class, expr -> {
ResolvedType resolvedType = expr.calculateResolvedType();
return resolvedType instanceof ReferenceTypeImpl referenceType
&& referenceType.getQualifiedName().equals(QUALIFIED_TIMER_NAME);
});
}
}
Loading

0 comments on commit b735623

Please sign in to comment.