我正在解析Java源文件以收集有关我的类的各种信息。因此我使用JavaParser,因为我找不到一个好的替代方案(好的建议有机会成为“答案”)来解析源文件。
我已经设法从我的课程中获取所有方法的注释。代码如下所示:
package de.mackaz;
import japa.parser.JavaParser;
import japa.parser.ParseException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.expr.AnnotationExpr;
import japa.parser.ast.expr.MarkerAnnotationExpr;
import japa.parser.ast.expr.MemberValuePair;
import japa.parser.ast.expr.NormalAnnotationExpr;
import japa.parser.ast.visitor.VoidVisitorAdapter;
import java.io.FileInputStream;
public class JavaSourceUtils {
public static void main(String[] args) throws Exception {
File f = new File("/home/mackaz/SourceFile.java");
inspectJavaFile(f);
}
public static void inspectJavaFile(File pFile)
throws FileNotFoundException, ParseException, IOException {
CompilationUnit cu;
FileInputStream in = new FileInputStream(pFile);
try {
cu = JavaParser.parse(in);
} finally {
in.close();
}
new MethodVisitor().visit(cu, null);
}
/**
* Simple visitor implementation for visiting MethodDeclaration nodes.
*/
private static class MethodVisitor extends VoidVisitorAdapter {
@Override
public void visit(MethodDeclaration n, Object arg) {
System.out.println(n.getName());
if (n.getAnnotations() != null) {
for (AnnotationExpr annotation : n.getAnnotations()) {
System.out.println(annotation.getClass());
// MarkerAnnotations, for example @Test
if (annotation.getClass().equals(MarkerAnnotationExpr.class)) {
System.out.println("MarkerAnnotation:" + ((MarkerAnnotationExpr)annotation).getName());
}
if (annotation.getClass().equals(NormalAnnotationExpr.class)) {
for (MemberValuePair pair : ((NormalAnnotationExpr)annotation).getPairs()) {
if (pair.getName().equals("groups"))
System.out.println("Group:\"" + pair.getValue() + "\"");
}
}
}
}
}
}
}
现在我怎样才能获得类本身的注释?
答案 0 :(得分:4)
您最重要的是public void visit(MethodDeclaration n, Object arg)
访问方法。您还可以覆盖public void visit(ClassOrInterfaceDeclaration n, A arg)
或public void visit(ClassOrInterfaceType n, A arg)
,这样您就可以访问所需的信息。
答案 1 :(得分:2)
这就是我最终解决的问题 - 我添加了另一个访客“ClassVisitor”:
private static class ClassVisitor extends VoidVisitorAdapter {
@Override
public void visit(ClassOrInterfaceDeclaration n, Object arg) {
for (AnnotationExpr ann: n.getAnnotations()) {
System.out.println(ann.toString());
}
}
}
答案 2 :(得分:1)
看起来您需要扩展ModifierVisitorAdapter并实施
public Node visit(ClassOrInterfaceDeclaration n, A arg) {
请查看此处的实施,了解您可能想要做的事情:
public Node visit(ClassOrInterfaceDeclaration n, A arg) {
if (n.getJavaDoc() != null) {
n.setJavaDoc((JavadocComment) n.getJavaDoc().accept(this, arg));
}
List<AnnotationExpr> annotations = n.getAnnotations();
if (annotations != null) {
for (int i = 0; i < annotations.size(); i++) {
annotations.set(i, (AnnotationExpr) annotations.get(i).accept(this, arg));
}
removeNulls(annotations);
}
List<TypeParameter> typeParameters = n.getTypeParameters();
if (typeParameters != null) {
for (int i = 0; i < typeParameters.size(); i++) {
typeParameters.set(i, (TypeParameter) typeParameters.get(i).accept(this, arg));
}
removeNulls(typeParameters);
}
List<ClassOrInterfaceType> extendz = n.getExtends();
if (extendz != null) {
for (int i = 0; i < extendz.size(); i++) {
extendz.set(i, (ClassOrInterfaceType) extendz.get(i).accept(this, arg));
}
removeNulls(extendz);
}
List<ClassOrInterfaceType> implementz = n.getImplements();
if (implementz != null) {
for (int i = 0; i < implementz.size(); i++) {
implementz.set(i, (ClassOrInterfaceType) implementz.get(i).accept(this, arg));
}
removeNulls(implementz);
}
List<BodyDeclaration> members = n.getMembers();
if (members != null) {
for (int i = 0; i < members.size(); i++) {
members.set(i, (BodyDeclaration) members.get(i).accept(this, arg));
}
removeNulls(members);
}
return n;
}