Java:解析java源代码,提取方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2206065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:43:59  来源:igfitidea点击:

Java : parse java source code, extract methods

javaparsing

提问by glmxndr

I wish to parse java source code files, and extract the methods source code.

我想解析java源代码文件,并提取方法源代码。

I would need a method like this :

我需要一个这样的方法:

/** Returns a map with key = method name ; value = method source code */
Map<String,String> getMethods(File javaFile);

Is there a simple way to achieve this, a library to help me build my method, etc. ?

有没有一个简单的方法来实现这一点,一个库来帮助我构建我的方法等?

采纳答案by arcticfox

Download the java parser from https://javaparser.org/

https://javaparser.org/下载 java 解析器

You'll have to write some code. This code will invoke the parser... it will return you a CompilationUnit:

您将不得不编写一些代码。此代码将调用解析器...它将返回一个 CompilationUnit:

            InputStream in = null;
            CompilationUnit cu = null;
            try
            {
                    in = new SEDInputStream(filename);
                    cu = JavaParser.parse(in);
            }
            catch(ParseException x)
            {
                 // handle parse exceptions here.
            }
            finally
            {
                  in.close();
            }
            return cu;

Note: SEDInputStream is a subclass of input stream. You can use a FileInputStream if you want.

注意:SEDInputStream 是输入流的子类。如果需要,您可以使用 FileInputStream。



You'll have to create a visitor. Your visitor will be easy because you're only interested in methods:

您必须创建一个访问者。您的访问者会很容易,因为您只对方法感兴趣:

  public class MethodVisitor extends VoidVisitorAdapter
  {
        public void visit(MethodDeclaration n, Object arg)
        {
             // extract method information here.
             // put in to hashmap
        }
  }


To invoke the visitor, do this:

要调用访问者,请执行以下操作:

  MethodVisitor visitor = new MethodVisitor();
  visitor.visit(cu, null);

回答by Rich

Could you use a custom doclet with JavaDoc?

您可以在 JavaDoc 中使用自定义 doclet 吗?

Doclet.commay provide more information.

Doclet.com可能会提供更多信息。

回答by unwind

JavaCCis a "compiler-compiler" (parser generator) for Java. There are many grammar files for it, including one for Java 1.5.

JavaCC是 Java 的“编译器-编译器”(解析器生成器)。它有许多语法文件,其中包括一个用于 Java 1.5 的语法文件。

回答by Upul Bandara

I think you can use ANTLRparser generator.

我认为您可以使用ANTLR解析器生成器。

It is very easy to use and ANTLRsite has a lot of examples written in Java.

它非常易于使用,ANTLR站点有很多用 Java 编写的示例。

Also it comes with a plug-in for eclipse.

它还带有一个eclipse插件。

回答by Rorick

You can build your parser with one of parser generators:

您可以使用解析器生成器之一构建解析器:

  1. ANTLR
  2. JavaCC
  3. SableCC
  1. ANTLR
  2. JavaCC
  3. SableCC

Also, you can use (or study how it works) something ready-made. There are Java Tree Builderwhich uses JavaCC and RefactorItwhich uses ANTLR.

此外,您可以使用(或研究它是如何工作的)现成的东西。有使用 JavaCC 的Java Tree Builder和使用 ANTLR 的RefactorIt

回答by Felix Leipold

QDOXis a more lightweight parser, that does only parse down to the method level, i.e. the method body is not being parsed into statements. It gives you more or less, what you ask for, even though the you would have navigate the model to find the right name, as it doesn't index classes and methods by name.

QDOX是一个更轻量级的解析器,它只解析到方法级别,即方法体不被解析为语句。它或多或少地为您提供所需的内容,即使您需要导航模型以找到正确的名称,因为它不会按名称对类和方法进行索引。