Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用eclipse模板自动插入类表示法?_Java_Eclipse_Code Generation_Eclipse Templates - Fatal编程技术网

Java 如何使用eclipse模板自动插入类表示法?

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

有人知道如何使用eclipse模板在类签名上方插入“@RunWith anotation”吗

例:

@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:填充集合
返回参数列表;
}
}
要使用模板(假定其命名为“参数化”):

  • 在Eclipse中创建一个新类
  • 在执行任何其他操作之前,请选择存根类声明,包括开始和结束大括号
  • 键入模板名称,然后按
    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;
        }
    }