您的位置:首页 > 其它

AST的节点调用的API

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

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;
	 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: