2010-04-19 62 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

这实在很难形容。你必须尝试看看它是如何工作的。发表评论,如果我的指示需要澄清。