-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add swing perfumes and many improvements
- Loading branch information
Christoph Knoedlseder
committed
Oct 6, 2024
1 parent
28c9d57
commit b735623
Showing
30 changed files
with
750 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/JFrameDisposeDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/de/jsilbereisen/perfumator/engine/detector/perfume/SwingTimerDetector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
Oops, something went wrong.