AST的节点调用的API

AST的节点调用的API总结,这些都是我在写项目的时候用到的关于AST的API,现总结整理如下,当然这些不全,以后还要做补充修改。

TypeDeclaration 类声明
TypeDeclaration是关于类的声明,可以调用getName得到类名,通过调用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) {
 	System.out.println(node.getName());//测试每个方法里所调用的方法
	System.out.println(node.getExpression());//输出调用方法的对象,例如commandline.createArgument().setValue("-root_dir"); //总共有三个调用commandline.createArgument(),commandline,null
 return true;  
}
========================================================================================
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;
	 }



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值