使用javaparser获取方法语句 [英] get method statements using javaparser

查看:1081
本文介绍了使用javaparser获取方法语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获取没有注释的方法语句列表,我使用 method.getBody()这是输出

Is it possible to get list of method statements without comments, i used method.getBody() and this is the output

/*
set the value of the age integer to 32
*/
int age = 32;

我想只发表声明就是这样的结果

I want to make statements only are the outcome like this

int age = 32;


推荐答案

.getBody()方法返回BlockStmt对象,它是{和}之间的语句,所以下面的代码执行我想要的操作

.getBody() method return BlockStmt object which is Statements in between { and } so the following code do what i want

Optional<BlockStmt> block = method.getBody();
NodeList<Statement> statements = block.get().getStatements();

for (Statement tmp : statements) {
    tmp.removeComment();
    System.out.println(tmp.toString());
}

这篇关于使用javaparser获取方法语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆