AST的节点调用的API
2015-11-24 21:16
309 查看
AST的节点调用的API总结,这些都是我在写项目的时候用到的关于AST的API,现总结整理如下,当然这些不全,以后还要做补充修改。
TypeDeclaration 类声明
TypeDeclaration是关于类的声明,可以调用getName得到类名,通过调用getSuperclassType()得到父类名,通过调用getSuperclassType()得到父类路径。
Expression 类是所有 Eclipse AST 中表达式节点类的基类,由它派生出许多类。在表示SimpleMiniJOOL 程 序 时
, 只 涉 及 到 MethodInvocation 、 Assignment 、 InfixExpression 、PrefixExpression、ParenthesizedExpression、NumberLiteral、Name 这些表达式节点类。
1、MethodInvocation 类(方法调用)
MethodInvocation它用来表示 Java 程序中的方法调用。
========================================================================================
MethodDeclaration方法声明
MethodDeclaration 它表示类中方法的声明
==============================================================================================
FieldDeclaration 变量声明
FieldDeclaration是变量的声明,VariableDeclarationFragment是变量名,可以通过for循环遍历得到。
========================================================================================
SwitchStatement
SwitchStatement是得到代码中关于Switch case相关语句块的逻辑。
TypeDeclaration 类声明
TypeDeclaration是关于类的声明,可以调用getName得到类名,通过调用getSuperclassType()得到父类名,通过调用getSuperclassType()得到父类路径。
public boolean visit(TypeDeclaration node) { System.out.println("Class:\t" + node.getName());//得到类名 if(node.getSuperclassType()!=null){ System.out.println("superClass:"+node.getSuperclassType());//得到父类名 String SuperclassName=""+node.getSuperclassType();//父类路径 return true; }
Expression 类是所有 Eclipse AST 中表达式节点类的基类,由它派生出许多类。在表示SimpleMiniJOOL 程 序 时
, 只 涉 及 到 MethodInvocation 、 Assignment 、 InfixExpression 、PrefixExpression、ParenthesizedExpression、NumberLiteral、Name 这些表达式节点类。
1、MethodInvocation 类(方法调用)
MethodInvocation它用来表示 Java 程序中的方法调用。
public boolean visit(MethodInvocation node) { <span style="white-space:pre"> </span>System.out.println(node.getName());//测试每个方法里所调用的方法 <span style="white-space:pre"> </span>System.out.println(node.getExpression());//输出调用方法的对象,例如commandline.createArgument().setValue("-root_dir"); <span style="white-space:pre"> </span>//总共有三个调用commandline.createArgument(),commandline,null <span style="white-space:pre"> </span>return true; <span style="white-space:pre"> </span>}
========================================================================================
MethodDeclaration方法声明
MethodDeclaration 它表示类中方法的声明
public boolean visit(MethodDeclaration node) { System.out.println("Method:\t" + node.getName());//得到方法名 System.out.println("the character length of the method is:"+node.getLength());//节点的长度,不过是以字符长度来计算的,不是以行数来计//算的 System.out.println("Parameter list of Method:\t" + node.parameters());//得到方法的参数列表 return true;}
==============================================================================================
FieldDeclaration 变量声明
FieldDeclaration是变量的声明,VariableDeclarationFragment是变量名,可以通过for循环遍历得到。
@Override public boolean visit(FieldDeclaration node) { for (Object obj: node.fragments()) { VariableDeclarationFragment v = (VariableDeclarationFragment)obj; System.out.println("Field:\t" + v.getName()); } return true; }
========================================================================================
SwitchStatement
SwitchStatement是得到代码中关于Switch case相关语句块的逻辑。
public boolean visit(SwitchStatement node) { System.out.println("SwitchStatement "+node.getStartPosition());//得到switch的起始位置 System.out.println("SwitchStatement "+node.getExpression());//得到switch的case中的变量比如switch(x) x就是变量 //System.out.println("SwitchStatement "+node.getRoot());//得到switch所在类的所有语句 System.out.println("SwitchStatement "+node.getParent());//到确切的语句块,这个是所需要的方法 return true; }
相关文章推荐
- 内部排序-交换式排序-选择排序
- 利用遍历思想求解图问题(输出所有路径)
- NYOJ--975--关于521
- String类的常用方法
- 关于Javascript函数的一些思考(一)
- 非单位时间任务安排问题
- sed
- WordPress主题制作入门(3)-后台制作
- qt/c++ 实现二维码生成(利用qrencode)
- Android使用XMPP协议、Openfire服务器和Smack类库实现即时通信
- Linux重定向:将标准输出stdout和标准错误stderr重定向到一个文件
- 机器视觉入门知识总结
- 转载:java集合类数据结构分析
- 【IOS】UILabel 属性及方法
- 搜索 Arithmetic Puzzles
- 对Python中文分词模块结巴分词算法过程的理解和分析
- hud 2586 How far away ?
- Hadoop学习笔记(一)Hadoop的单节点安装
- Hadoop学习笔记(一)Hadoop的单节点安装
- 选夫婿1