Eclipse抽象语法树程序化访问

时间:2022-02-20 11:10:48

Could you provide an example of accessing the Eclipse Abstract Syntax Tree programmatically for a given piece of code?

您能否提供一个以编程方式为给定代码段访问Eclipse Abstract Syntax Tree的示例?

eg getting the AST for:

例如,获取AST:


Class1.java

package parseable;

public class Class1 {

/**
 * @param args
 */
public static void main(String[] args) {
    System.out.println("Hello world!");
}

}

2 个解决方案

#1


3  

It is not an exact answer, that may give you a place where to start:

这不是一个确切的答案,可能会给你一个开始的地方:

As said in this question,

正如这个问题所说,

A full example is available in this eclipse corner article, also more details in the eclipse help. And in the slide 59 of this presentation, you see how to apply a change to your source code.

这个eclipse角文章中提供了一个完整的例子,还有eclipse帮助中的更多细节。在本演示文稿的幻灯片59中,您将了解如何对源代码应用更改。

#2


1  

// get an ICompilationUnit by some means
// you might drill down from an IJavaProject, for instance 
ICompilationUnit iunit = ...

// create a new parser for the latest Java Language Spec
ASTParser parser = ASTParser.newParser(AST.JLS3);

// tell the parser you are going to pass it some code where the type level is a source file
// you might also just want to parse a block, or a method ("class body declaration"), etc
parser.setKind(ASTParser.K_COMPILATION_UNIT);

// set the source to be parsed to the ICompilationUnit
// we could also use a character array
parser.setSource(iunit);

// parse it.
// the output will be a CompilationUnit (also an ASTNode)
// the null is because we're not using a progress monitor
CompilationUnit unit = (CompilationUnit) parser.createAST(null);

Don't be confused by the ICompilationUnit vs CompilationUnit distinction, which seems to be just the result of uncreative naming on their part. CompilationUnit is a type of ASTNode. ICompilationUnit in this context resembles a file handle. For more info on the distinction, see here: http://wiki.eclipse.org/FAQ_How_do_I_manipulate_Java_code%3F

不要被ICompilationUnit与CompilationUnit区分混淆,这似乎只是他们自己命名的命名的结果。 CompilationUnit是一种ASTNode。此上下文中的ICompilationUnit类似于文件句柄。有关区别的更多信息,请参见此处:http://wiki.eclipse.org/FAQ_How_do_I_manipulate_Java_code%3F

#1


3  

It is not an exact answer, that may give you a place where to start:

这不是一个确切的答案,可能会给你一个开始的地方:

As said in this question,

正如这个问题所说,

A full example is available in this eclipse corner article, also more details in the eclipse help. And in the slide 59 of this presentation, you see how to apply a change to your source code.

这个eclipse角文章中提供了一个完整的例子,还有eclipse帮助中的更多细节。在本演示文稿的幻灯片59中,您将了解如何对源代码应用更改。

#2


1  

// get an ICompilationUnit by some means
// you might drill down from an IJavaProject, for instance 
ICompilationUnit iunit = ...

// create a new parser for the latest Java Language Spec
ASTParser parser = ASTParser.newParser(AST.JLS3);

// tell the parser you are going to pass it some code where the type level is a source file
// you might also just want to parse a block, or a method ("class body declaration"), etc
parser.setKind(ASTParser.K_COMPILATION_UNIT);

// set the source to be parsed to the ICompilationUnit
// we could also use a character array
parser.setSource(iunit);

// parse it.
// the output will be a CompilationUnit (also an ASTNode)
// the null is because we're not using a progress monitor
CompilationUnit unit = (CompilationUnit) parser.createAST(null);

Don't be confused by the ICompilationUnit vs CompilationUnit distinction, which seems to be just the result of uncreative naming on their part. CompilationUnit is a type of ASTNode. ICompilationUnit in this context resembles a file handle. For more info on the distinction, see here: http://wiki.eclipse.org/FAQ_How_do_I_manipulate_Java_code%3F

不要被ICompilationUnit与CompilationUnit区分混淆,这似乎只是他们自己命名的命名的结果。 CompilationUnit是一种ASTNode。此上下文中的ICompilationUnit类似于文件句柄。有关区别的更多信息,请参见此处:http://wiki.eclipse.org/FAQ_How_do_I_manipulate_Java_code%3F