Skip to content

Commit

Permalink
Merge pull request #3 from se2p/provide-dependencies-and-better-error…
Browse files Browse the repository at this point in the history
…-handling

Provide dependencies and better error handling
  • Loading branch information
chrisknedl authored Oct 27, 2024
2 parents 62461b2 + f0a517d commit a6e59c3
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 31 deletions.
6 changes: 1 addition & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<groupId>de.jsilbereisen</groupId>
<artifactId>perfumator-java</artifactId>
<version>0.4.0</version>
<version>0.4.1</version>
<packaging>jar</packaging>

<developers>
Expand Down Expand Up @@ -127,29 +127,25 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public static Builder builder(@NotNull Locale locale) {
* as it is used by the <i><b>Perfumator</b></i> by default.<br/>
* List of settings that are changed, compared to the default {@link ParserConfiguration}:
* <ul>
* <li>Language level: 11 (default) -&gt; 17</li>
* <li>Language level: 21</li>
* <li>Do NOT Capture empty line comments: {@code true} -&gt; {@code false}</li>
* </ul>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
import de.jsilbereisen.perfumator.engine.detector.Detector;
import de.jsilbereisen.perfumator.model.DetectedInstance;
import de.jsilbereisen.perfumator.model.perfume.Perfume;
import de.jsilbereisen.perfumator.util.NodeUtil;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
* {@link Detector} for the "Assert all" {@link Perfume}.
Expand Down Expand Up @@ -58,14 +60,22 @@ private List<MethodCallExpr> getAssertAllMethodCalls(@NotNull CompilationUnit as
}
if (expr.getScope().isPresent()) {
// for non-static imports
ResolvedType resolvedType = expr.getScope().get().calculateResolvedType();
ResolvedType resolvedType;
try {
resolvedType = expr.getScope().get().calculateResolvedType();
} catch (Exception e) {
System.out.println(expr.getNameAsString());
System.out.println(e.getMessage());
return false;
}
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);
Optional<ResolvedMethodDeclaration> resolvedMethodDeclaration
= NodeUtil.resolveSafely(expr, this, expr.getNameAsString());
return resolvedMethodDeclaration.map(methodDeclaration -> methodDeclaration.getQualifiedName().equals(QUALIFIED_ASSERT_ALL_METHOD_NAME))
.orElse(false);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import de.jsilbereisen.perfumator.engine.detector.Detector;
import de.jsilbereisen.perfumator.model.DetectedInstance;
Expand Down Expand Up @@ -52,7 +53,14 @@ private List<MethodCallExpr> getJFrameDisposeMethodCalls(@NotNull CompilationUni
}
var scope = expr.getScope();
if (scope.isPresent()) {
var resolvedType = scope.get().calculateResolvedType();
ResolvedType resolvedType;
try {
resolvedType = scope.get().calculateResolvedType();
} catch (Exception e) {
System.out.println(expr.getNameAsString());
System.out.println(e.getMessage());
return false;
}
if (resolvedType instanceof ReferenceTypeImpl referenceType) {
return referenceType.getQualifiedName().equals(QUALIFIED_JFRAME_CLASS_NAME);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

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.model.DetectedInstance;
import de.jsilbereisen.perfumator.model.perfume.Perfume;
import de.jsilbereisen.perfumator.util.NodeUtil;
import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

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

/**
* {@link Detector} for the "Parameterized Test" {@link Perfume}.
Expand All @@ -26,7 +24,8 @@ public class ParameterizedTestDetector implements Detector<Perfume> {

private JavaParserFacade analysisContext;

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

@Override
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) {
Expand All @@ -49,8 +48,12 @@ public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {

private List<MethodDeclaration> getParameterizedTestMethodDeclarations(@NotNull CompilationUnit astRoot) {
return astRoot.findAll(MethodDeclaration.class, methodDeclaration -> methodDeclaration.getAnnotations().stream()
.map(AnnotationExpr::resolve)
.map(ResolvedTypeDeclaration::getQualifiedName)
.anyMatch(name -> name.equals(PARAMETERIZED_TEST_IDENTIFIER)));
// filter out annotations that do not contain 'ParameterizedTest'
.filter(annotation -> annotation.getNameAsString().contains(PARAMETERIZED_TEST_IDENTIFIER))
// try to resolve the symbol in order to get the declaration
.map(paramTestAnnotation -> NodeUtil.resolveSafely(paramTestAnnotation, this, paramTestAnnotation.getNameAsString()))
.filter(Optional::isPresent)
.map(resolvedAnnotationDeclaration -> resolvedAnnotationDeclaration.get().getQualifiedName())
.anyMatch(qualifiedName -> qualifiedName.equals(PARAMETERIZED_TEST_PACKAGE + PARAMETERIZED_TEST_IDENTIFIER)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@

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.model.DetectedInstance;
import de.jsilbereisen.perfumator.model.perfume.Perfume;
import de.jsilbereisen.perfumator.util.NodeUtil;
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.*;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -47,15 +44,25 @@ public void setConcreteDetectable(@NotNull Perfume concreteDetectable) {
public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {
this.analysisContext = analysisContext;
}


/**
* Shorthand to return the fully qualified names of the annotations.
*
* @return The Set containing {org.junit.jupiter.api.BeforeEach, org.junit.jupiter.api.BeforeAll,
* org.junit.jupiter.api.AfterEach, org.junit.jupiter.api.AfterAll}.
*/
private Set<String> getQualifiedAnnotations() {
return TEST_ANNOTATIONS.stream().map(annotation -> IMPORT_QUALIFIER + annotation).collect(Collectors.toSet());
}

private List<MethodDeclaration> getSetupAndTeardownMethodDeclarations(@NotNull CompilationUnit astRoot) {
return astRoot.findAll(MethodDeclaration.class, methodDeclaration -> methodDeclaration.getAnnotations().stream()
.map(AnnotationExpr::resolve)
.map(ResolvedTypeDeclaration::getQualifiedName)
.anyMatch(name -> getQualifiedAnnotations().contains(name)));
// filter out annotations that do not contain any of the four relevant annotations
.filter(annotation -> TEST_ANNOTATIONS.stream().anyMatch(testAnnotation -> annotation.getNameAsString().contains(testAnnotation)))
// try to resolve the symbol in order to get the declaration
.map(testAnnotation -> NodeUtil.resolveSafely(testAnnotation, this, testAnnotation.getNameAsString()))
.filter(Optional::isPresent)
.map(resolvedAnnotationDeclaration -> resolvedAnnotationDeclaration.get().getQualifiedName())
.anyMatch(qualifiedName -> getQualifiedAnnotations().contains(qualifiedName)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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 All @@ -26,6 +25,7 @@ public class SwingTimerDetector implements Detector<Perfume> {
private JavaParserFacade analysisContext;

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

@Override
public @NotNull List<DetectedInstance<Perfume>> detect(@NotNull CompilationUnit astRoot) {
Expand All @@ -48,12 +48,17 @@ public void setAnalysisContext(@Nullable JavaParserFacade analysisContext) {

private List<ObjectCreationExpr> getNewTimerExpressions(@NotNull CompilationUnit astRoot) {
return astRoot.findAll(ObjectCreationExpr.class, expr -> {
if (!expr.getType().getNameAsString().equals(TIMER_IDENTIFIER)) {
return false;
}
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) {
} catch (Exception e) {
System.out.println(expr);
System.out.println(e.getMessage());
return false;
}
return resolvedType instanceof ReferenceTypeImpl referenceType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,14 @@ private List<MethodCallExpr> getInvokeLaterInvokeAndWaitMethodCalls(@NotNull Com
}
if (expr.getScope().isPresent()) {
// for non-static imports
ResolvedType resolvedType = expr.getScope().get().calculateResolvedType();
ResolvedType resolvedType;
try {
resolvedType = expr.getScope().get().calculateResolvedType();
} catch (Exception e) {
System.out.println(expr.getNameAsString());
System.out.println(e.getMessage());
return false;
}
return resolvedType instanceof ReferenceTypeImpl referenceType
&& referenceType.getQualifiedName().equals(IMPORT);
} else {
Expand Down

0 comments on commit a6e59c3

Please sign in to comment.