获得Java源文件类注解 [英] Get Class Annotations from Java Source File

查看:965
本文介绍了获得Java源文件类注解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析Java源文件,以收集有关我的课的各种信息。因此,我使用的是 JavaParser类,因为我无法找到一个很好的选择(好的建议都成为答案)来解析源文件的机会。

I'm parsing Java Source Files to collect various informations about my classes. Therefore I'm using the JavaParser, since I could not find a good alternative (good suggestions have the chance to become "answers") to parse Source Files.

我已经设法从我的类的所有方法的注释。在code是这样的:

I already managed to get Annotations of all methods from my class. The code looks like this:

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() + "\"");
                        }
                    }
                }
            }
        }
    }
}

现在我怎么能获得类本身的注释?

Now how can I get the Annotations of the class itself?

推荐答案

您要重写公共无效访问(MethodDeclaration N,对象ARG),其访问方法。您还可以覆盖公共无效访问(ClassOrInterfaceDeclaration N,A ARG)公共无效访问(ClassOrInterfaceType N,A ARG),这应该给你访问你正在寻找的信息。

You are overriding public void visit(MethodDeclaration n, Object arg), which visits methods. You can also override public void visit(ClassOrInterfaceDeclaration n, A arg) or public void visit(ClassOrInterfaceType n, A arg), which should give you access to the information you are looking for.

这篇关于获得Java源文件类注解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆