diff --git a/src/main/java/click/kamil/springstatemachineexporter/ast/Main.java b/src/main/java/click/kamil/springstatemachineexporter/ast/Main.java index a29dcf7..98e1ca3 100644 --- a/src/main/java/click/kamil/springstatemachineexporter/ast/Main.java +++ b/src/main/java/click/kamil/springstatemachineexporter/ast/Main.java @@ -29,6 +29,7 @@ public class Main { private static final List TARGET_ANNOTATIONS = List.of("EnableStateMachineFactory", "EnableStateMachine"); public static void main(String[] args) throws IOException { + AstFileFinder finder = new AstFileFinder(); List javaFiles = FileUtils.findFilesWithExtension(Paths.get(START_DIR), EXTENSION); // List of output handlers @@ -40,7 +41,6 @@ public class Main { for (Path javaFile : javaFiles) { String source = Files.readString(javaFile); - AstFileFinder finder = new AstFileFinder(source); if (finder.hasClassWithAnnotation(source, TARGET_ANNOTATIONS)) { System.out.println("Found annotation in: " + javaFile.toAbsolutePath()); diff --git a/src/main/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinder.java b/src/main/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinder.java index 0616184..5a83136 100644 --- a/src/main/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinder.java +++ b/src/main/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinder.java @@ -6,17 +6,17 @@ import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; public class AstFileFinder { - private final CompilationUnit cu; + private final ASTParser parser; - public AstFileFinder(String source) { - ASTParser parser = ASTParser.newParser(AST.getJLSLatest()); + public AstFileFinder() { + this.parser = ASTParser.newParser(AST.getJLSLatest()); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setResolveBindings(false); - parser.setSource(source.toCharArray()); - this.cu = (CompilationUnit) parser.createAST(null); } public boolean hasClassWithAnnotation(String source, List annotationNames) { + parser.setSource(source.toCharArray()); + CompilationUnit cu = (CompilationUnit) parser.createAST(null); AtomicBoolean found = new AtomicBoolean(false); cu.accept(new ASTVisitor() { @Override diff --git a/src/test/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinderTest.java b/src/test/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinderTest.java index 7a83747..d6fa820 100644 --- a/src/test/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinderTest.java +++ b/src/test/java/click/kamil/springstatemachineexporter/ast/in/AstFileFinderTest.java @@ -96,7 +96,7 @@ class AstFileFinderTest { @Test void shouldReturnTrue_whenClassHasMatchingAnnotation() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); @@ -106,7 +106,7 @@ class AstFileFinderTest { @Test void shouldReturnTrue_whenClassHasMultipleMatchingAnnotations() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_MULTIPLE_ANNOTATIONS); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service", "Component"); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_MULTIPLE_ANNOTATIONS, annotationNames); @@ -116,7 +116,7 @@ class AstFileFinderTest { @Test void shouldReturnFalse_whenClassHasNoAnnotation() { - AstFileFinder finder = new AstFileFinder(CLASS_WITHOUT_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(CLASS_WITHOUT_ANNOTATION, annotationNames); @@ -126,7 +126,7 @@ class AstFileFinderTest { @Test void shouldReturnFalse_whenClassHasDifferentAnnotation() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_DIFFERENT_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_DIFFERENT_ANNOTATION, annotationNames); @@ -136,7 +136,7 @@ class AstFileFinderTest { @Test void shouldReturnTrue_whenClassHasFullyQualifiedAnnotation() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_FULLY_QUALIFIED_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("org.springframework.stereotype.Service"); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_FULLY_QUALIFIED_ANNOTATION, annotationNames); @@ -146,7 +146,7 @@ class AstFileFinderTest { @Test void shouldReturnTrue_whenMultipleClassesAndOneHasAnnotation() { - AstFileFinder finder = new AstFileFinder(MULTIPLE_CLASSES); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(MULTIPLE_CLASSES, annotationNames); @@ -156,7 +156,7 @@ class AstFileFinderTest { @Test void shouldReturnFalse_whenInterfaceHasAnnotation() { - AstFileFinder finder = new AstFileFinder(INTERFACE_WITH_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("FunctionalInterface"); boolean result = finder.hasClassWithAnnotation(INTERFACE_WITH_ANNOTATION, annotationNames); @@ -166,7 +166,7 @@ class AstFileFinderTest { @Test void shouldReturnFalse_whenEmptyAnnotationList() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); List annotationNames = Collections.emptyList(); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); @@ -176,7 +176,7 @@ class AstFileFinderTest { @Test void shouldReturnFalse_whenEmptySource() { - AstFileFinder finder = new AstFileFinder(EMPTY_SOURCE); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(EMPTY_SOURCE, annotationNames); @@ -186,7 +186,7 @@ class AstFileFinderTest { @Test void shouldThrowException_whenNullAnnotationList() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); // when & then assertThatThrownBy(() -> @@ -196,7 +196,7 @@ class AstFileFinderTest { @Test void shouldReturnTrue_whenClassHasAnnotationAndFields() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION_AND_FIELDS); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION_AND_FIELDS, annotationNames); @@ -206,7 +206,7 @@ class AstFileFinderTest { @Test void shouldHandleInvalidJavaSyntax() { - AstFileFinder finder = new AstFileFinder(INVALID_JAVA_SYNTAX); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(INVALID_JAVA_SYNTAX, annotationNames); @@ -218,7 +218,7 @@ class AstFileFinderTest { @Test void shouldReturnTrue_whenAnnotationNameMatchesCaseInsensitive() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("service"); // lowercase boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); @@ -228,7 +228,7 @@ class AstFileFinderTest { @Test void shouldReturnTrue_whenMultipleAnnotationNamesProvided() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Component", "Service", "Repository"); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); @@ -238,7 +238,7 @@ class AstFileFinderTest { @Test void shouldReturnFalse_whenNoMatchingAnnotationNames() { - AstFileFinder finder = new AstFileFinder(CLASS_WITH_ANNOTATION); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Component", "Repository", "Controller"); boolean result = finder.hasClassWithAnnotation(CLASS_WITH_ANNOTATION, annotationNames); @@ -253,7 +253,7 @@ class AstFileFinderTest { /* Another comment */ /** JavaDoc comment */ """; - AstFileFinder finder = new AstFileFinder(sourceWithComments); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(sourceWithComments, annotationNames); @@ -264,7 +264,7 @@ class AstFileFinderTest { @Test void shouldReturnFalse_whenSourceContainsOnlyPackageDeclaration() { String sourceWithPackage = "package com.example;"; - AstFileFinder finder = new AstFileFinder(sourceWithPackage); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(sourceWithPackage, annotationNames); @@ -282,7 +282,7 @@ class AstFileFinderTest { } } """; - AstFileFinder finder = new AstFileFinder(classWithAnnotatedParams); + AstFileFinder finder = new AstFileFinder(); List annotationNames = List.of("Service"); boolean result = finder.hasClassWithAnnotation(classWithAnnotatedParams, annotationNames);