Java 如何使用eclipse模板自动插入类表示法?
有人知道如何使用eclipse模板在类签名上方插入“@RunWith anotation”吗 例:Java 如何使用eclipse模板自动插入类表示法?,java,eclipse,code-generation,eclipse-templates,Java,Eclipse,Code Generation,Eclipse Templates,有人知道如何使用eclipse模板在类签名上方插入“@RunWith anotation”吗 例: @RunWith(参数化的.class) 公共类MyClassTest{ ... @参数 公共静态集合参数(){ 列表=新的ArrayList(); 添加(新对象[]{“mind!”,“find!”}); 添加(新对象[]{“误解”、“理解”}); 退货清单; } ... } __ 模板: // TODO: move this '@RunWith(Parameterized.class)' to
@RunWith(参数化的.class)
公共类MyClassTest{
...
@参数
公共静态集合参数(){
列表=新的ArrayList();
添加(新对象[]{“mind!”,“find!”});
添加(新对象[]{“误解”、“理解”});
退货清单;
}
...
}
__
模板:
// 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;
}
//TODO:将此“@RunWith(Parameterized.class)”移动到类Anotion
@参数
公共静态集合参数(){
${type:elemType(collection)}参数列表=新的${type:elemType(collection)}();
${cursor}//TODO:填充集合
返回参数列表;
}
__谢谢你的帮助 不幸的是,您不能使用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;
}
}
@${runnerType:newType(org.junit.runner.RunWith)}(${ParameterizedType:newType(org.junit.runners.Parameterized)}.class)
公共类${primary\u type\u name}{
@${parametersType:newType(org.junit.runners.Parameterized.Parameters)}
公共静态${collectionType:newType(java.util.Collection)}参数(){
${baseCollectionType}参数列表=新的${concreteCollectionType}();
${cursor}//TODO:填充集合
返回参数列表;
}
}
要使用模板(假定其命名为“参数化”):
Cntrl+Space
激活模板(您可能需要从模板列表中选择模板。我只有一个名为Parameterized的模板,因此Eclipse会自动为我使用它)@RunWith
注释的定义。我使用${id:newName(reference)}模板变量使Eclipse自动添加所有必要的导入(除了${baseCollectionType}
和${concreteCollectionType}
的导入之外,您必须手动添加这些导入…谢天谢地Cntrl-Shift-M
)
这真的很难描述。你得试试看它到底是怎么工作的。如果我的指示需要澄清,请发表评论
@${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;
}
}