Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify the AST using Java 9+ [duplicate]

I've been trying to modify the AST using annotation processors. I tried extending Lombok, but that seemed too hard, so I decided to use things from com.sun.source.* and com.sun.tools.javac.* However, I am using java 11, and the document I was learning from, "The Hacker's Guide to Javac" http://scg.unibe.ch/archive/projects/Erni08b.pdf, uses Java 6. The api they used is now internal and my module cannot read it.

In IntelliJ, it gave me a few errors, but I clicked on the suggestions (which said things like "Add --Xxx-xxx to xxx" without paying attention to them. When I tried compiling with Maven, it failed, since the module does not read the internals of jdk.compiler.

These are some of my imports:

    import com.sun.source.util.Trees;
    import com.sun.tools.javac.tree.JCTree;
    import com.sun.tools.javac.tree.TreeMaker;
    import com.sun.tools.javac.tree.TreeTranslator;
    import com.sun.tools.javac.util.Context;

My module-info file contains

    requires jdk.compiler;
    requires java.compiler;

I got messages like "[ERROR]package com.sun.tools.javac.util is declared in module jdk.compiler, which does not export it to module OtherAnnot" and "[ERROR] (package com.sun.tools.javac.tree is declared in module jdk.compiler, which does not export it to module OtherAnnot)"

Edit: I guess this is a duplicate, but I wanted to know if there was some alternative API for AST transformations in java 9.

like image 955
user Avatar asked Aug 08 '19 20:08

user


People also ask

How do you use AST?

How to do using ast library, a = b + 3 or a = 3+b , both have same node type i.e. BinOp, you can validate variable “a” value and its node type. For each line of code, create AST node then compare value, node type and other parameters as well like operator, operand, function name, class name, index, etc… if required.

What is Java AST?

The AST is a detailed tree representation of the Java source code. The AST defines an API to modify, create, read and delete source code. The main package for the AST is the org. eclipse. jdt.

Is an AST a binary tree?

Binary trees have many uses. In fact, compilers often use them to build what is known as an abstract syntax tree (or AST) - an intermediate representation of the code that is not yet fully compiled. The JavaScript parser uses an AST as well, though it's a general tree, not a binary tree.

Is AST part of Python standard library?

ast is a module in the python standard library. Python codes need to be converted to an Abstract Syntax Tree (AST) before becoming “byte code”(. pyc files). Generating AST is the most important function of ast, but there are more ways one can use the module.


1 Answers

With the introduction of Project Jigsaw, the JDK has been modularized, allowing users to create their own modules as well. These modules allows you to export packages of yours, allowing programs that require your module (in their module-info.java) to use the exported packages.

Ideally, you'd be prohibited from using classes that reside in packages that are not exported. However, to not break backwards compatibility, VM flags were introduced that allow you to forcefully export packages (that don't belong to you) to your module.

Given your error message, the respective VM flag to add is:

--add-exports jdk.compiler/com.sun.tools.javac.tree=OtherAnnot

The pattern here is:

--add-exports THEIR_MODULE/THEIR_PACKAGE=YOUR_MODULE

If the compiler complains that packages aren't exported to the unnamed module, then you can use the following:

--add-exports THEIR_MODULE/THEIR_PACKAGE=ALL-UNNAMED
like image 111
Jacob G. Avatar answered Sep 20 '22 14:09

Jacob G.