2014-04-18 25 views
0

我有接收功能的MethodDeclaration如何在MethodDeclaration類中返回迭代器類型?

public boolean visit(MethodDeclaration node){ } 

我需要通過樹(從node開始)進行迭代,所以我宣佈:

Iterator<MethodDeclaration> itr; 

for循環,並用它像這樣的:

for(Iterator<MethodDeclaration> itr = node; itr.hasNext();) 

所以我的功能看起來像這樣至今:

public boolean visit(MethodDeclaration node) 
{ 
    if (node != null) 
    { 
     for (Iterator<MethodDeclaration> itr = node; itr.hasNext();) 
     { 
      .... 
     } 
    } 
} 

itr聲明和itr.hasNext()正在工作(日食標識他們)。但行Iterator<MethodDeclaration> itr = node;不是。我顯然需要激活node中的某種方法,該方法返回iterator類型。但我找不到任何。

我該怎麼辦?

感謝

+0

你的'MethodDeclaration'是否實現'Iterator'?如果不是,你不能使用迭代器。您可能需要使用while來遍歷節點。 –

+0

你想重複什麼? –

回答

0

node是一個MethodDeclaration對象,我不認爲有一種方法可以從`MethodDeclaration本身得到MethodDeclarationiterator一個s。

因此,看起來你只需要與你正在接收的node對象一起工作......因爲迭代器在visit方法之外迭代。

0

假設你正試圖遍歷方法體的每個語句,你可以做到以下幾點:

MethodDeclaration node; 
//Get node from somewhere 

Block methodBody=node.getBody(); // getBody returns body of code as a Block 
for(Object a: methodBody.statements()){ //Block.statements returns a list of statements. 
    System.out.println(a); 
} 

您可以使用Block.statements返回的列表上的迭代器做同樣的事情() 。希望這可以幫助。