简体   繁体   English

Javadoc源文件解析

[英]Javadoc source file parsing

in my program I dynamically get the name of the .java file. 在我的程序中,我动态地获取.java文件的名称。 In this file I need to find all methods, foreach method also all parameters (also with their annotations). 在此文件中,我需要查找所有方法,foreach方法以及所有参数(以及它们的注释)。

I read through the discussions here and found this https://code.google.com/p/javaparser/ javaparser, that seems pretty easy to use, but the problem is, that it is just for 1.5. 我通读了这里的讨论,发现这个https://code.google.com/p/javaparser/ javaparser看起来很容易使用,但是问题是,它仅适用于1.5。

Than you mentioned, that Java 1.6 has already got built-in parser (javax.lang.model). 值得一提的是,Java 1.6已经内置了解析器(javax.lang.model)。 But I can not figure out, how it works. 但是我不知道它是如何工作的。 Do you know any good tutorial/example of it? 您知道任何好的教程/示例吗?

Do you know any other way to parse java source file? 您知道解析Java源文件的任何其他方法吗?

How about using Doclet API ? 如何使用Doclet API
Normally, This API is used from bat file, but you can invoke programmatically like the following. 通常,此API是从bat文件中使用的,但是您可以像下面这样以编程方式调用。

IMPORTANT : This API exists in not rt.jar(JRE) but tools.jar (JDK). 重要说明 :该API不存在于rt.jar(JRE)中,而存在于tools.jar(JDK)中。 So you need to add tool.jar into classpath. 因此,您需要将tool.jar添加到类路径中。

import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.Doclet;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.RootDoc;
import com.sun.tools.javadoc.Main;

public class DocletTest {

    public static void main(String[] args) {
        Main.execute("", Analyzer.class.getName(), new String[] {"path/to/your/file.java"});
    }

    public static class Analyzer extends Doclet {

        public static boolean start(RootDoc root) {
            for (ClassDoc classDoc : root.classes()) {
                System.out.println("Class: " + classDoc.qualifiedName());

                for (MethodDoc methodDoc : classDoc.methods()) {
                    System.out.println("  " + methodDoc.returnType() + " " + methodDoc.name() + methodDoc.signature());
                }
            }
            return false;
        }
    }
}

Take another look at the javaparser project . 再看一下javaparser项目 It has been updated to support all modern Java versions. 它已更新为支持所有现代Java版本。

The Doclet API is really hard to use and is badly documented. Doclet API确实很难使用,并且记录严重。 It will be either going away or be replaced with something better, hopefully even in Java 1.9 . 它要么消失,要么被更好的东西所取代, 甚至在Java 1.9中也是如此

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM