Eclipse JDT核心解析器无法解析注释(Eclipse JDT core parser not parsing the comments)

编程入门 行业动态 更新时间:2024-10-25 04:24:30
Eclipse JDT核心解析器无法解析注释(Eclipse JDT core parser not parsing the comments)

我们一直在尝试在我们的项目中使用Eclipse JDT核心解析器来解析Java源文件,但我们遇到的一个问题是我们无法从文件中获取注释列表。 我们尝试在CompilationUnit上调用getComments方法,但它向我们显示了一个空注释列表。

这是来自Test1.java源代码的测试项目:

public class Test1 { public static void main(String[] args) throws IOException { File file = new File("src/main/java/DummyFile.java"); FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); StringBuilder source = new StringBuilder(); String line = ""; while ((line = bufferedReader.readLine()) != null) { source.append(line + System.getProperty("line.separator")); } bufferedReader.close(); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source.toString().toCharArray()); parser.setResolveBindings(true); Hashtable<String, String> options = JavaCore.getOptions(); options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); parser.setCompilerOptions(options); CompilationUnit cu = (CompilationUnit) parser.createAST(null); List comments = cu.getCommentList(); System.out.println(cu); // breakpoint is here. for (Object comment : comments) { System.out.println(comment.toString()); } }

DummyFile.java如下:

public class DummyFile { // 1 private String str; public static int number; public DummyFile() { // 2 // 3 str = "something"; number = 2; // 4 // 5 } // 6 }

这是输出:

public class DummyFile { private String str; public static int number; public DummyFile(){ str="something"; number=2; } } // // // // // //

当我们查看compilationUnit并查看注释列表时,这就是调试器中的内容:

[// , // , // , // , // , // ]

所以我们想知道这是否是正常行为,如果是,那么获取评论的内容或我们做错了什么呢?

如果这不是正常行为,那么可能有人对什么是错误的以及要寻找什么有任何建议。

We have been trying to use the Eclipse JDT core parser in our project to parse Java source files, but one of the problems we have is that we cannot get the list of comments from a file. We have tried calling the getComments method on the CompilationUnit but it shows us a list of empty comments.

This is from a test project whose Test1.java source is here:

public class Test1 { public static void main(String[] args) throws IOException { File file = new File("src/main/java/DummyFile.java"); FileReader fileReader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(fileReader); StringBuilder source = new StringBuilder(); String line = ""; while ((line = bufferedReader.readLine()) != null) { source.append(line + System.getProperty("line.separator")); } bufferedReader.close(); ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(source.toString().toCharArray()); parser.setResolveBindings(true); Hashtable<String, String> options = JavaCore.getOptions(); options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); parser.setCompilerOptions(options); CompilationUnit cu = (CompilationUnit) parser.createAST(null); List comments = cu.getCommentList(); System.out.println(cu); // breakpoint is here. for (Object comment : comments) { System.out.println(comment.toString()); } }

The DummyFile.java is as follows:

public class DummyFile { // 1 private String str; public static int number; public DummyFile() { // 2 // 3 str = "something"; number = 2; // 4 // 5 } // 6 }

This is the output:

public class DummyFile { private String str; public static int number; public DummyFile(){ str="something"; number=2; } } // // // // // //

This is what see in the debugger when we look inside the compilationUnit and look at the list of comments:

[// , // , // , // , // , // ]

So we were wondering if this is normal behaviour and if so, what do to get the content of the comments or what are we doing wrong?

If this is not normal behaviour then maybe someone has any suggestions as to what is going wrong and what to look for.

最满意答案

通过CompilationUnit的getCommentList()方法获得的注释将不具有注释主体。 AST访问期间也不会访问评论。 为了访问评论,我们必须为评论列表中的每个评论调用“ accept ”。

请参阅以下问题的答案,了解如何从CompilationUnit中提取注释:

如何从java编译器树api生成注释生成的ast?

The comments obtained through the getCommentList() method of CompilationUnit will not have the comment body. Also the comments will not be visited during an AST Visit. In order to visit the comments, we have to call 'accept' for each comment in the Comment List.

See the answer to the below question on how to extract comments from a CompilationUnit:

How to access comments from the java compiler tree api generated ast?

更多推荐

本文发布于:2023-07-25 19:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1265232.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   核心   JDT   Eclipse   comments

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!