51 lines
1.7 KiB
Java
51 lines
1.7 KiB
Java
package com.example.statemachinedemo;
|
|
import org.eclipse.jdt.core.dom.*;
|
|
import java.io.*;
|
|
import java.nio.file.*;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
|
|
public class AstFileFinder {
|
|
|
|
public static List<Path> findJavaFiles(Path startDir, String extension) throws IOException {
|
|
try (Stream<Path> paths = Files.walk(startDir)) {
|
|
return paths
|
|
.filter(p -> Files.isRegularFile(p) && p.toString().endsWith(extension))
|
|
.collect(Collectors.toList());
|
|
}
|
|
}
|
|
|
|
public static boolean hasClassWithAnnotation(String source, String annotationName) {
|
|
ASTParser parser = ASTParser.newParser(AST.JLS17);
|
|
parser.setSource(source.toCharArray());
|
|
parser.setKind(ASTParser.K_COMPILATION_UNIT);
|
|
parser.setResolveBindings(false);
|
|
|
|
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
|
|
|
|
final boolean[] found = {false};
|
|
|
|
cu.accept(new ASTVisitor() {
|
|
@Override
|
|
public boolean visit(TypeDeclaration node) {
|
|
if (node.isInterface()) return true;
|
|
|
|
for (Object modifier : node.modifiers()) {
|
|
if (modifier instanceof Annotation) {
|
|
Annotation annotation = (Annotation) modifier;
|
|
String name = annotation.getTypeName().getFullyQualifiedName();
|
|
if (name.equals(annotationName)) {
|
|
found[0] = true;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
});
|
|
|
|
return found[0];
|
|
}
|
|
}
|