removal of a not used argument

This commit is contained in:
2025-07-20 13:48:44 +02:00
parent b8f5562f18
commit 31e19981af
3 changed files with 24 additions and 24 deletions

View File

@@ -29,6 +29,7 @@ public class Main {
private static final List<String> TARGET_ANNOTATIONS = List.of("EnableStateMachineFactory", "EnableStateMachine"); private static final List<String> TARGET_ANNOTATIONS = List.of("EnableStateMachineFactory", "EnableStateMachine");
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
AstFileFinder finder = new AstFileFinder();
List<Path> javaFiles = FileUtils.findFilesWithExtension(Paths.get(START_DIR), EXTENSION); List<Path> javaFiles = FileUtils.findFilesWithExtension(Paths.get(START_DIR), EXTENSION);
// List of output handlers // List of output handlers
@@ -40,7 +41,6 @@ public class Main {
for (Path javaFile : javaFiles) { for (Path javaFile : javaFiles) {
String source = Files.readString(javaFile); String source = Files.readString(javaFile);
AstFileFinder finder = new AstFileFinder(source);
if (finder.hasClassWithAnnotation(source, TARGET_ANNOTATIONS)) { if (finder.hasClassWithAnnotation(source, TARGET_ANNOTATIONS)) {
System.out.println("Found annotation in: " + javaFile.toAbsolutePath()); System.out.println("Found annotation in: " + javaFile.toAbsolutePath());

View File

@@ -6,17 +6,17 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
public class AstFileFinder { public class AstFileFinder {
private final CompilationUnit cu; private final ASTParser parser;
public AstFileFinder(String source) { public AstFileFinder() {
ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); this.parser = ASTParser.newParser(AST.getJLSLatest());
parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setResolveBindings(false); parser.setResolveBindings(false);
parser.setSource(source.toCharArray());
this.cu = (CompilationUnit) parser.createAST(null);
} }
public boolean hasClassWithAnnotation(String source, List<String> annotationNames) { public boolean hasClassWithAnnotation(String source, List<String> annotationNames) {
parser.setSource(source.toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
AtomicBoolean found = new AtomicBoolean(false); AtomicBoolean found = new AtomicBoolean(false);
cu.accept(new ASTVisitor() { cu.accept(new ASTVisitor() {
@Override @Override

View File

@@ -96,7 +96,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnTrue_whenClassHasMatchingAnnotation() { void shouldReturnTrue_whenClassHasMatchingAnnotation() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames);
@@ -106,7 +106,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnTrue_whenClassHasMultipleMatchingAnnotations() { void shouldReturnTrue_whenClassHasMultipleMatchingAnnotations() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_MULTIPLE_ANNOTATIONS); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service", "Component"); List<String> annotationNames = List.of("Service", "Component");
boolean result = finder.hasClassWithAnnotation(CLASS_WITH_MULTIPLE_ANNOTATIONS, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_MULTIPLE_ANNOTATIONS, annotationNames);
@@ -116,7 +116,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnFalse_whenClassHasNoAnnotation() { void shouldReturnFalse_whenClassHasNoAnnotation() {
AstFileFinder finder = new AstFileFinder(CLASS_WITHOUT_ANNOTATION); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(CLASS_WITHOUT_ANNOTATION, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITHOUT_ANNOTATION, annotationNames);
@@ -126,7 +126,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnFalse_whenClassHasDifferentAnnotation() { void shouldReturnFalse_whenClassHasDifferentAnnotation() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_DIFFERENT_ANNOTATION); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(CLASS_WITH_DIFFERENT_ANNOTATION, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_DIFFERENT_ANNOTATION, annotationNames);
@@ -136,7 +136,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnTrue_whenClassHasFullyQualifiedAnnotation() { void shouldReturnTrue_whenClassHasFullyQualifiedAnnotation() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_FULLY_QUALIFIED_ANNOTATION); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("org.springframework.stereotype.Service"); List<String> annotationNames = List.of("org.springframework.stereotype.Service");
boolean result = finder.hasClassWithAnnotation(CLASS_WITH_FULLY_QUALIFIED_ANNOTATION, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_FULLY_QUALIFIED_ANNOTATION, annotationNames);
@@ -146,7 +146,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnTrue_whenMultipleClassesAndOneHasAnnotation() { void shouldReturnTrue_whenMultipleClassesAndOneHasAnnotation() {
AstFileFinder finder = new AstFileFinder(MULTIPLE_CLASSES); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(MULTIPLE_CLASSES, annotationNames); boolean result = finder.hasClassWithAnnotation(MULTIPLE_CLASSES, annotationNames);
@@ -156,7 +156,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnFalse_whenInterfaceHasAnnotation() { void shouldReturnFalse_whenInterfaceHasAnnotation() {
AstFileFinder finder = new AstFileFinder(INTERFACE_WITH_ANNOTATION); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("FunctionalInterface"); List<String> annotationNames = List.of("FunctionalInterface");
boolean result = finder.hasClassWithAnnotation(INTERFACE_WITH_ANNOTATION, annotationNames); boolean result = finder.hasClassWithAnnotation(INTERFACE_WITH_ANNOTATION, annotationNames);
@@ -166,7 +166,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnFalse_whenEmptyAnnotationList() { void shouldReturnFalse_whenEmptyAnnotationList() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = Collections.emptyList(); List<String> annotationNames = Collections.emptyList();
boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames);
@@ -176,7 +176,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnFalse_whenEmptySource() { void shouldReturnFalse_whenEmptySource() {
AstFileFinder finder = new AstFileFinder(EMPTY_SOURCE); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(EMPTY_SOURCE, annotationNames); boolean result = finder.hasClassWithAnnotation(EMPTY_SOURCE, annotationNames);
@@ -186,7 +186,7 @@ class AstFileFinderTest {
@Test @Test
void shouldThrowException_whenNullAnnotationList() { void shouldThrowException_whenNullAnnotationList() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); AstFileFinder finder = new AstFileFinder();
// when & then // when & then
assertThatThrownBy(() -> assertThatThrownBy(() ->
@@ -196,7 +196,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnTrue_whenClassHasAnnotationAndFields() { void shouldReturnTrue_whenClassHasAnnotationAndFields() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION_AND_FIELDS); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION_AND_FIELDS, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION_AND_FIELDS, annotationNames);
@@ -206,7 +206,7 @@ class AstFileFinderTest {
@Test @Test
void shouldHandleInvalidJavaSyntax() { void shouldHandleInvalidJavaSyntax() {
AstFileFinder finder = new AstFileFinder(INVALID_JAVA_SYNTAX); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(INVALID_JAVA_SYNTAX, annotationNames); boolean result = finder.hasClassWithAnnotation(INVALID_JAVA_SYNTAX, annotationNames);
@@ -218,7 +218,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnTrue_whenAnnotationNameMatchesCaseInsensitive() { void shouldReturnTrue_whenAnnotationNameMatchesCaseInsensitive() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("service"); // lowercase List<String> annotationNames = List.of("service"); // lowercase
boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames);
@@ -228,7 +228,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnTrue_whenMultipleAnnotationNamesProvided() { void shouldReturnTrue_whenMultipleAnnotationNamesProvided() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Component", "Service", "Repository"); List<String> annotationNames = List.of("Component", "Service", "Repository");
boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames);
@@ -238,7 +238,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnFalse_whenNoMatchingAnnotationNames() { void shouldReturnFalse_whenNoMatchingAnnotationNames() {
AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Component", "Repository", "Controller"); List<String> annotationNames = List.of("Component", "Repository", "Controller");
boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames);
@@ -253,7 +253,7 @@ class AstFileFinderTest {
/* Another comment */ /* Another comment */
/** JavaDoc comment */ /** JavaDoc comment */
"""; """;
AstFileFinder finder = new AstFileFinder(sourceWithComments); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(sourceWithComments, annotationNames); boolean result = finder.hasClassWithAnnotation(sourceWithComments, annotationNames);
@@ -264,7 +264,7 @@ class AstFileFinderTest {
@Test @Test
void shouldReturnFalse_whenSourceContainsOnlyPackageDeclaration() { void shouldReturnFalse_whenSourceContainsOnlyPackageDeclaration() {
String sourceWithPackage = "package com.example;"; String sourceWithPackage = "package com.example;";
AstFileFinder finder = new AstFileFinder(sourceWithPackage); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(sourceWithPackage, annotationNames); boolean result = finder.hasClassWithAnnotation(sourceWithPackage, annotationNames);
@@ -282,7 +282,7 @@ class AstFileFinderTest {
} }
} }
"""; """;
AstFileFinder finder = new AstFileFinder(classWithAnnotatedParams); AstFileFinder finder = new AstFileFinder();
List<String> annotationNames = List.of("Service"); List<String> annotationNames = List.of("Service");
boolean result = finder.hasClassWithAnnotation(classWithAnnotatedParams, annotationNames); boolean result = finder.hasClassWithAnnotation(classWithAnnotatedParams, annotationNames);