refactoring
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package com.example.statemachinedemo.in;
|
||||
|
||||
import org.eclipse.jdt.core.dom.*;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
public class AstFileFinder {
|
||||
private final CompilationUnit cu;
|
||||
|
||||
public AstFileFinder(String source) {
|
||||
ASTParser 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, String annotationName) {
|
||||
AtomicBoolean found = new AtomicBoolean(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.getTypeName().getFullyQualifiedName().equals(annotationName)) {
|
||||
found.set(true);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
});
|
||||
return found.get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user