用于获取有关 Java 源代码的元数据的 Java API

发布于 2024-10-25 17:43:19 字数 1037 浏览 2 评论 0 原文

我想创建一些基于Java代码(而不是字节码)的反向工程设计文档,而不是编写自己的解释器,有哪些工具和API可用于使用Java代码遍历Java代码?

反射是针对字节码的,并且仅限于方法级别,我也想“对象化”方法代码。

Java doc 忽略代码本身,仅基于注释,自动 UML 序列过于

严格像这样的 API(请原谅我对官方编程语言结构术语的无知):

JavaCodeDom jcd = new JavaCodeDom(new File(pathToJavaSource), CompilerEnum.Java16)
List <ClassSrc> classes = jcd.getClasses();
ClassSrc cls = classes.get(0);
Map<MethodSignatureSrc,MethodSrc> methods = cls.getMethodsMap();
MethodSrc main = mothds.get(new MethodSignatureSrc(Modifiers.Public, Modifiers.Static, ReturnTypes.Void, "main", new MethodParams(String[].class))
List<StatementSrc> statements = main.getStatements();
for(StatementSrc statement : statements){
   if(statement.getType()==StatementTypes.Assignment()){
        AssignmentStatementSrc assignment = (AssignmentStatementSrc)statement;
        Identifier src = assignment.getAssigneeVariable();
        ExpressinoSrc = assignment.getAssignmentValue();
   }
}
List<AnnotationsSrc> annotations = cls.getAnnotations();

I would like to create some reverse egineered design docs based on Java code (not bytecode), instead of writing my own interpreter, what tools and APIs are available to traverse Java code, using Java code?

Reflection is on bytecode, and is limited to the method level, I want to "objectize" also the method code.

Java doc is ignoring the code itself and only based on comments, automatic UML sequnces are too strict

E.g. an API like this (forgive my ignorance of official Programming Languages Structure terms):

JavaCodeDom jcd = new JavaCodeDom(new File(pathToJavaSource), CompilerEnum.Java16)
List <ClassSrc> classes = jcd.getClasses();
ClassSrc cls = classes.get(0);
Map<MethodSignatureSrc,MethodSrc> methods = cls.getMethodsMap();
MethodSrc main = mothds.get(new MethodSignatureSrc(Modifiers.Public, Modifiers.Static, ReturnTypes.Void, "main", new MethodParams(String[].class))
List<StatementSrc> statements = main.getStatements();
for(StatementSrc statement : statements){
   if(statement.getType()==StatementTypes.Assignment()){
        AssignmentStatementSrc assignment = (AssignmentStatementSrc)statement;
        Identifier src = assignment.getAssigneeVariable();
        ExpressinoSrc = assignment.getAssignmentValue();
   }
}
List<AnnotationsSrc> annotations = cls.getAnnotations();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

走走停停 2024-11-01 17:43:20

存在多个此类 API(并随 JDK 一起提供),其中一些内置于 Java 编译器 (javac)。

  • 最广泛的是编译器树 API,它使您可以访问 Java 源代码中的各个表达式(和子表达式)。
  • 语言模型 API 模型类型和类型成员(构造函数、方法、字段) - 它由编译器树 API 使用,也用于注释处理。它不允许访问方法的内容。
  • 当然,在运行时您可以使用 Reflection API(java.lang.Classjava.lang.reflect.*,以及 java.lang.annotation< /代码>)。
  • 要使用编译器树 API,您必须使用 编译器 API
  • 此外,还有 Doclet API对于 Javadoc,它为您提供了与语言模型 API 类似的视图,但另外还包含文档注释(和解析的标签)。

我曾经使用 Doclet API 和 Compiler Tree API 的组合来漂亮地格式化源代码(遗憾的是,这不是在线的)。

There are several such APIs in existence (and delivered with the JDK), some of them build in in the Java Compiler (javac).

  • The most extensive is the Compiler Tree API, which gets you access to individual expressions (and subexpressions) in the Java source.
  • The language model API models types and members of types (constructors, methods, fields) - it is used by the compiler tree API and also for annotation processing. It does not give access to the contents of the methods.
  • Of course, on runtime you have the Reflection API (java.lang.Class and java.lang.reflect.*, together with java.lang.annotation).
  • To use the compiler tree API, you have to invoke the compiler, with the compiler API.
  • Additionally, there is the Doclet API for Javadoc, which gives you a similar view like the language model API, but additionally with the documentation comments (and parsed tags).

I once used a combination of Doclet API and Compiler Tree API to format source code beautifully (this is not online, sadly).

稚然 2024-11-01 17:43:20

BCEL 支持读取操作 Java 类文件。 (我自己没有使用过它,但看到它在第三方产品中成功使用。)

    The Byte Code Engineering Library is intended to give users a convenient
    possibility to analyze, create, and manipulate (binary) Java class files
    (those ending with .class). Classes are represented by objects which 
    contain all the symbolic information of the given class: methods, 
    fields and byte code instructions, in particular. 

如果您只是对反编译感兴趣,您可能会发现反编译为源代码就足够了。这是Java 的几个选项的比较< /a>.

BCEL supports reading an manipulating Java class files. (I have not used it myself, but saw it used successfully in a third-party product.)

    The Byte Code Engineering Library is intended to give users a convenient
    possibility to analyze, create, and manipulate (binary) Java class files
    (those ending with .class). Classes are represented by objects which 
    contain all the symbolic information of the given class: methods, 
    fields and byte code instructions, in particular. 

If you're just interested in decompiling, you might find it sufficient to decompile to source code. Here's a comparison of several options for Java.

聆听风音 2024-11-01 17:43:20

我似乎 ANTLR 是一种选择,但我没有使用它

I seems ANTLR is one option, but I haven't used it

忆梦 2024-11-01 17:43:20

这似乎回答了我的问题: 如何从 Java 源代码生成 AST ? ( 勺子 )

This seems to answer my question: How to generate AST from Java source-code? ( Spoon )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文