简体   繁体   English

Eclipse ASTParser仅解析Java函数?

[英]Eclipse ASTParser to parse only a java function?

I just wanna know using org.eclipse.jdt.core.dom.ASTParser if it is possible to parse only a java function? 我只是想知道使用org.eclipse.jdt.core.dom.ASTParser是否可以仅解析Java函数?

This is how I tried: I passed the code of a function to the ASTParser.setSource(char[] s) as follows: 这是我尝试的方法:我将函数的代码传递给ASTParser.setSource(char [] s),如下所示:

    ASTParser parser = ASTParser.newParser(AST.JLS3);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(unit); //set source
    CompilationUnit cu = (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse
    List list = node.types();
    for(int i = 0; i < list.size(); i++){
       ASTNode typeNode = (ASTNode) list.get(i);
       System.out.println(ASTNode.nodeClassForType(typeNode.getNodeType()));
    }

But I see that the list of types contains nothing (size = 0). 但是我看到类型列表不包含任何内容(大小= 0)。

Please suggest. 请提出建议。 Thanks. 谢谢。 Fahim 法希姆

Just a small typo, in the line List list = node.types(); List list = node.types();行中只是一个小的错字List list = node.types(); it should be List list = cu.types(); 它应该是List list = cu.types(); . You cannot pass a function only. 您不能仅传递功能。 It needs to be valid Java compilation unit, so it must have a type definition. 它必须是有效的Java编译单元,因此必须具有类型定义。 Make sure you wrap your function with a class. 确保用一个类包装函数。 Any class. 任何课。 It should work just fine. 它应该工作正常。

If it doesn't work, remember that you can ask for cu.getProblems() and see where it failed. 如果它不起作用,请记住您可以要求cu.getProblems()并查看失败的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM