javac.exe AST 编程访问示例 [英] javac.exe AST programmatic access example

查看:35
本文介绍了javac.exe AST 编程访问示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式访问 javac.exe 中的抽象语法树(AST)?你能举个例子吗?

Is it possible to access the Abstract Syntax Tree(AST) inside the javac.exe programmatically? Could you provide an example?

推荐答案

是的,这是可能的,但仅限于 Java 6.Peter von der Ahé 谈到了 本次采访.JSR 199:

Yes, it is possible, but only since Java 6. Peter von der Ahé talks about the two JSRs in this interview. Of JSR 199:

JSR 199 编译器 API 由三部分组成:第一个基本上允许您通过 API 调用编译器.其次,API 允许您自定义编译器查找和写出文件的方式.我的意思是抽象意义上的文件,因为编译器处理的文件不一定在文件系统上.例如,JSR 199 的文件抽象允许您将文件保存在数据库中,并将输出直接生成到内存中.最后,JSR 199 API 允许您以结构化的方式从编译器收集诊断信息,以便您可以轻松地将错误消息(例如,转换为 IDE 编辑器中的行).

The JSR 199 Compiler API consists of three things: The first one basically allows you to invoke a compiler via the API. Second, the API allows you to customize how the compiler finds and writes out files. I mean files in the abstract sense, since the files the compiler deals with aren't necessarily on the file system. JSR 199's file abstraction allows you to have files in a database, and to generate output directly to memory, for example. Finally, the JSR 199 API lets you collect diagnostics from the compiler in a structured way so that you can easily transform error messages, for instance, into lines in an IDE's editor.

JSR 269 是注解处理 API.

JSR 269 is the annotation processing API.

本文 对访问 编译器树 API.部分访问抽象语法树:编译器树 API" 似乎特别适合您的问题.

This article gives an excellent overview of accessing the Compiler Tree API. The section "Accessing the Abstract Syntax Tree: The Compiler Tree API" seems particularly suitable for your question.

根据您在做什么,您可能还想查看 Jackpot规则语言,这是一个独立的重构引擎,可插入到编译器树中.

Depending on what you're doing, you may also want to look at the Jackpot Rule Language, which is a standalone refactoring engine that plugins into the Compiler Tree.

这篇关于javac.exe AST 编程访问示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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