Joã*_*ini 5 java eclipse code-generation eclipse-templates
有人知道如何使用 Eclipse 模板在类签名上方插入“@RunWith 注释”吗?
前任。:
@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;
}
...
}
Run Code Online (Sandbox Code Playgroud)
__
模板:
// 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;
}
Run Code Online (Sandbox Code Playgroud)
__ 谢谢您的帮助!
不幸的是,您无法使用 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;
}
}
Run Code Online (Sandbox Code Playgroud)
要使用模板(假设其名为“Parameterized”):
Cntrl+Space激活该模板(您可能需要从模板列表中选择模板。我只有一个名为 Parameterized 的模板,因此 Eclipse 会自动为我使用它)。类定义将替换为包含注释的定义@RunWith。我使用${ id :newName(reference)}模板变量来使 Eclipse 自动添加所有必需的导入(除了${baseCollectionType}和 的导入之外${concreteCollectionType},您必须手动添加这些...谢天谢地Cntrl-Shift-M)
这实在是很难形容。您必须尝试一下才能确切地了解它是如何工作的。如果我的指示需要澄清,请发表评论。