- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 ANTLR 4.2 解析 Java 源文件。
我的目标是在类中找到具有特定注释的方法。
我面临的问题是 Java.g4语法不包括annotation
规则作为 methodDeclaration
的一部分规则(这是有道理的),这意味着当我覆盖 enterMethodDeclaration
时,它不能直接从上下文对象中使用的 JavaBaseListener
:
@Override
public void enterMethodDeclaration(@NotNull JavaParser.MethodDeclarationContext ctx) {
// How do I access the annotation on the method in 'ctx'?
}
到目前为止,我唯一的想法是获取 token 流并尝试从方法声明上下文的位置向后遍历它。这样做的问题是它非常麻烦(方法上的注释可能出现在修饰符之前,修饰符之后,也就是说,如果修饰符确实存在)并且我觉得它缺少听众/访问者方法的要点。
是否有一种干净的方法来获取方法上的注释?
最佳答案
与其尝试遍历向上 解析树,不如考虑向下遍历。您可以从 classBodyDeclaration
生产开始,检查您是否可以进入 memberDeclaration
并从那里进入 methodDeclaration
。如果是这样,您就知道您目前正处于一种方法中。然后只需遍历 classBodyDeclaration
中的 modifier
列表即可找到注释。
快速演示:
public class Main {
public static void main(String[] args) throws Exception {
String input = "@Annotation(\"class\")\n" +
"class Mu {\n" +
"\n" +
" @Annotation(\"field\")\n" +
" int x;\n" +
" \n" +
" @Annotation(\"method\")\n" +
" void withAnnotation(){}\n" +
" \n" +
" void withoutAnnotation(){}\n" +
"}";
JavaLexer lexer = new JavaLexer(new ANTLRInputStream(input));
JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
// classBodyDeclaration
// : ...
// | modifier* memberDeclaration
// ;
//
// memberDeclaration
// : methodDeclaration
// | ...
// ;
//
// methodDeclaration
// : (type|'void') Identifier formalParameters ...
// ;
//
// modifier
// : classOrInterfaceModifier
// | ...
// ;
//
// classOrInterfaceModifier
// : annotation
// | ...
// ;
ParseTreeWalker.DEFAULT.walk(new JavaBaseListener(){
@Override
public void enterClassBodyDeclaration(@NotNull JavaParser.ClassBodyDeclarationContext ctx) {
if (!(ctx.memberDeclaration() != null && ctx.memberDeclaration().methodDeclaration() != null)) {
// No method declaration.
return;
}
String methodName = ctx.memberDeclaration().methodDeclaration().Identifier().getText();
for (JavaParser.ModifierContext mctx : ctx.modifier()) {
if (mctx.classOrInterfaceModifier() != null && mctx.classOrInterfaceModifier().annotation() != null) {
System.out.println(methodName + " -> " + mctx.classOrInterfaceModifier().annotation().getText());
}
}
}
}, parser.compilationUnit());
}
}
打印:
withAnnotation -> @Annotation("method")
关于java - 使用 antlr4 解析 java 代码时导航到不属于上下文的标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24677858/
我正在运行一个带有 while 约束的 SQL 查询,其中包含一些“id”。例如: SELECT table.id FROM TableOne table WHERE table.id IN (1,
假设我有以下类型声明: declare type Point2D = { x: number, y: number } 我从服务器获取一些数据并得到以下信息: const response = { x
根据 Angular 文档,Angular 的指令有 3 种类型: 组件 结构化 属性 根据以下官方链接:https://angular.io/guide/attribute-directives#d
在我正在处理的 Spring Boot 应用程序中,我有一个未注释为 bean (@Component) 的类,但包含一个 Autowiring 字段: public class One{ @
我有一个问题,我正在学习 swift 编程,我已经学会了毫无问题地处理表格和集合,包括使用 alamofire 的 cosumo 服务,但我遇到了一个问题,我在个性化表格 View 中有一个集合单元格
我已经在我们办公场所的实时服务器上配置了 TFS。 现在,我们可以访问它,即 windows 域 用户/事件目录 用户但是当我把我的用户名密码 给别人时我们的办公场所,并要求他通过 Web 链接从 v
我的 .plist 的 UIStatusBarHidden 是 false,所以我的应用程序不显示 iOS 状态栏。 连接到自定义 UIView 顶部的 UI 元素在 iPhone 6/7/8 设备上
执行某些 SP 时开始出现以下错误。与此错误相关的代码非常简单,将#temp 表连接到真实表 错误全文: Msg 605, Level 21, State 3, Procedure spSSRSRPT
我有一段代码调用 LogonUser(),然后调用 CreateProcessAsUser()。在 Win32 中,生成的进程属于属于 LOCAL 组的用户(例如,TESTDOMAIN\user1)。
Xcode 6.1 在组件安装完成后出现错误。 Xcode 安装程序是从其他 mac 复制的。请帮助我如何解决 dyld_sim 错误....谢谢 /Applications/Xcode.app/Co
我是一名优秀的程序员,十分优秀!