2010-04-19 36 views
4

有沒有人知道如何在類簽名上面使用eclipse模板插入「@RunWith anotation」?如何使用eclipse模板自動插入類表示法?

例:

@RunWith(Parameterized.class) 
public class MyClassTest { 
... 
    @Parameters 
    public static Collection<Object[]> parameters() { 
     List<Object[]> list = new ArrayList<Object[]>(); 
     list.add(new Object[] { "mind!", "find!" }); 
     list.add(new Object[] { "misunderstood", "understood" }); 
     return list; 
    } 
... 
} 

__

模板:

// TODO: move this '@RunWith(Parameterized.class)' to class anotation 
    @Parameters 
    public static Collection<Object[]> parameters() { 
     ${type:elemType(collection)}<Object[]> parametersList = new ${type:elemType(collection)}<Object[]>(); 
     ${cursor}// TODO: populate collection 
     return parametersList; 
    } 

__感謝您的幫助!

回答

3

不幸的是,您不能使用Eclipse模板來向現有的封閉類添加註釋(至少,我不知道)。但是,有一個解決方法。這是你的模板的修改版本:

@${runnerType:newType(org.junit.runner.RunWith)}(${paramterizedType:newType(org.junit.runners.Parameterized)}.class) 
public class ${primary_type_name} { 
    @${parametersType:newType(org.junit.runners.Parameterized.Parameters)} 
    public static ${collectionType:newType(java.util.Collection)}<Object[]> parameters() { 
     ${baseCollectionType}<Object[]> parametersList = new ${concreteCollectionType}<Object[]>(); 
     ${cursor}// TODO: populate collection 
     return parametersList; 
    } 
} 

要使用的模板(假設其命名爲「參數」):

  1. 在Eclipse
  2. 創建一個新類做其他事情之前,先選擇存根類聲明,包括開頭和結束括號。
  3. 輸入模板的名稱,然後按Cntrl+Space激活模板(您可能必須從模板列表中選擇模板,我只有一個名爲Parameterized的模板,所以Eclipse會自動爲我使用它)。

該類的定義將被包含@RunWith註釋的定義替換。我用$ {ID:了newName(參考)}模板變量引起的Eclipse自動添加所有必要的進口(與進口的例外${baseCollectionType}${concreteCollectionType}的,你必須手動添加這些...謝天謝地Cntrl-Shift-M

這實在很難形容。你必須嘗試看看它是如何工作的。發表評論,如果我的指示需要澄清。

相關問題