- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用 CDT ASTParser 来解析 C/C++ 源文件的一些内容。示例:
//Docs for function min
int min(int a[], int n) {
//Comment here
}
int min1(){}
/*
Docs for function min2
*/
int min2(){}
通过使用 ASTVisitor,我可以解析代码中的函数定义列表,但我不知道如何“链接”定义代码上方的 IASTComment:
void handle(IASTTranslationUnit unit){
unit.accept(new ASTVisitor() {
{ shouldVisitDeclarations = true; }
@Override
public int visit(IASTDeclaration declaration) {
if (declaration instanceof IASTFunctionDefinition) {
IASTFunctionDefinition fnDefine =
(IASTFunctionDefinition) declaration;
IASTFunctionDeclarator fnDeclare = fnDefine.getDeclarator();
System.out.printf("Function: %s, type: %s, comment: %s\n",
fnDeclare.getName().getRawSignature(),
fnDefine.getDeclSpecifier().getRawSignature(),
"Somehow get the comment above function define???"
);
}
return PROCESS_SKIP;
}
@Override
public int visit(IASTComment comment) {
System.out.println("Comment: " + comment.getRawSignature());
return PROCESS_CONTINUE;
}
});
for (IASTComment cmt: unit.getComments())
System.out.println("Comment: " + cmt.getRawSignature());
}
ASTVisitor 类中的visit(IASTComment comment)
已弃用,IASTTranslationUnit.getComments()
方法只返回整个源文件中的注释列表,此处没有结构.
那么,如何获取链接到函数定义的 IASTComment 对象(如果有)?
最佳答案
您可以在 org.eclipse.cdt.internal.core.dom.rewrite.commenthandler
包中使用 ASTCommenter.getCommentedNodeMap(IASTTranslationUnit)
。该方法返回一个 NodeCommentMap
,它将注释映射到 AST 中的节点。可以将三种类型的评论分配给节点。在下面的示例中,方法声明是节点:
//this is a leading comment
void method() //this is a trailing comment
//this is a freestanding comment
{
...
NodeCommentMap.getOffsetIncludingComments(IASTNode)
返回节点的偏移量,包括其分配的前导注释。 NodeCommentMap.getEndOffsetIncludingComments(IASTNode)
返回节点的结束偏移量,包括其分配的尾随注释。分配的独立评论必须由您自己处理。您可以使用 NodeCommentMap.getFreestandingCommentsForNode(IASTNode)
获取独立注释。至少我在 cdt 包中找不到任何默认方法。
如果您想了解更多信息,建议阅读以下类中的文档:
org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.ASTCommenter
org.eclipse.cdt.core.parser.tests.rewrite.commenthandler.CommentHandlingTest
org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommenter
注意:大多数包/类都是内部的。
关于java - 如何在 CDT ASTParser 中将 IASTComment 链接到 IASTDeclaration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31583886/
我正在尝试在 this 的帮助下学习 ASTParser教程。当我编译第一段代码时: ASTParser parser = ASTParser.newParser(AST.JLS2); parser.
我正在使用 ASTParser 解析项目中的 Java 源代码。我设法获取了 Java 项目的不同类中所有方法的名称和返回类型。我现在想知道 main 中的方法之间是否有可能存在依赖关系。事实上,我还
我有一个访问者,它应该输出调用者类型、其名称、参数类型、方法声明中的相应类型,如果它是常量变量,则输出其值 public boolean visit(MethodInvocation e) {
例如,我有这样的代码 SomeObject1 obj1 = new SomeObject1(); SomeObject2 obj2 = new SomeObject2(); ... obj1.foo(
示例代码: String.valueOf("test"); 此代码的访问者: cu.accept(new ASTVisitor() { public boolean visit(MethodI
我想打印类的所有方法中的所有方法调用。我正在使用 ASTParser。以下是我的代码 import org.eclipse.jdt.core.dom.AST; import org.eclipse.j
我经常使用 Eclipse 中的 ASTParser,但我不明白我是否得到了 AST 图或已经有了 CFG。如果只是AST,请说一下应该如何修改为CFG。非常感谢 最佳答案 ASTParser 为您提
我目前正在使用 JDT 的 AST,以便能够解析 Java 源代码。我需要识别参数的类型绑定(bind)。例如,对于 String 参数,我需要 java.lang.String 而不仅仅是 Stri
我只是想知道使用 org.eclipse.jdt.core.dom.ASTParser 是否可以只解析 java 函数? 这就是我尝试的方法:我将函数的代码传递给 ASTParser.setSourc
我正致力于使用 JDT 分析 Java 代码,并打算构建一个依赖于 org.eclipse.jdt.core 包而不是 eclipse 插件的独立分析工具。但是我发现我的工具无法在 Java 代码中出
引用 this上一个问题,我无法在 Eclipse 之外获得 IFile 或 ICompilationUnit。 我需要 org.eclipse.jdt.core.dom 库来解析和提取树结构。 我看
我想要做的是获取方法的类名。例如,我想获得一类“直到”和“搜索”方法。这是代码。 Query query = new Query(queryStr).until(dateStr); QueryResu
我正在编写一些代码来解析 java 源代码。我正在试验 Eclipse JDT AST Parser。我的代码如下。 (解析代码)。我正在针对我用 Java 编写的 Mailer 应用程序测试解析器(
我实现了以下访问方法来获取所使用的方法的名称及其相应的完全限定类名。 public boolean visit(MethodInvocation node)
背景 - 下面的问题 我正开始在 Java 中为 Java 实现度量套件,但我担心我的方法不合适。 目前我正在使用 JDT 的 ASTParser对于目录中的每个文件。这开始得很好,我能够收集有关每个
我正在使用 Java ASTParser 来解析我的 java 代码。我正在使用以下代码。使用此代码我可以提取“if”和“try”语句。但我无法提取“catch 条款”。有谁知道如何在此单独提取 ca
我正在使用 CDT ASTParser 来解析 C/C++ 源文件的一些内容。示例: //Docs for function min int min(int a[], int n) { //C
我需要检索java文件的SuperClass名称(它扩展了一个类,我需要该类名称)。为了做到这一点,我开始使用 ASTParser,但我是新手,所以任何示例或插图都会有所帮助。我使用 ASTView
我正在编写一个使用 JDT AST 的 ASTParser 的 Eclipse 插件。解析一个方法。我正在该方法中寻找创建特定类型对象的方法。 当我找到 ClassInstanceCreation ,
ASTParser.setSource 具有具有不同输入类型的多态方法。 但是,当我使用 ICompilationUnit 作为 setSource 方法的输入时, 我收到错误消息,指出我没有使用 c
我是一名优秀的程序员,十分优秀!