如何使用 Eclipse 模板自动插入类符号?

发布于 2024-08-29 09:27:31 字数 865 浏览 5 评论 0原文

有谁知道如何使用 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;
    }
...
}

__

模板:

// 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;
    }

__ 感谢您的帮助!

Does anybody know how to insert a "@RunWith anotation" above the class signature, using eclipse templates?

Ex.:

@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;
    }
...
}

__

Template:

// 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;
    }

__ Thanks for the help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2024-09-05 09:27:31

不幸的是,您无法使用 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;
    }
}

要使用模板(假设其名为“Parameterized”):

  1. 在 Eclipse 中创建一个新类
  2. 在执行其他操作之前,请选择存根类声明,包括左大括号和右大括号。
  3. 输入模板的名称,然后按 Cntrl+Space 激活模板(您可能需要从模板列表中选择模板。我只有一个名为 Parameterized 的模板,因此 Eclipse 只使用它自动为我)。

类定义将替换为包含 @RunWith 注释的定义。我使用 ${id:newName(reference)} 模板变量来使 Eclipse 自动添加所有必需的导入(的导入除外) ${baseCollectionType}${concreteCollectionType},您必须手动添加它们...谢天谢地 Cntrl-Shift-M

这真的很难描述。您必须尝试一下才能确切地了解它是如何工作的。如果我的指示需要澄清,请发表评论。

Unfortunately, you cannot use Eclipse templates to add an annotation to an existing enclosing class (at least, not that I know of). However, there is a workaround. Here is a modified version of your template:

@${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;
    }
}

To use the template (assuming its named "Parameterized"):

  1. Create a new class in Eclipse
  2. Before doing anything else, select the stub class declaration, including the opening and closing braces.
  3. Type the name of the template, and press Cntrl+Space to activate the template (you may have to select the template from the list of templates. I only have one template called Parameterized, so Eclipse just uses it automatically for me).

The class definition will be replaced with a definition that includes the @RunWith annotation. I used the ${id:newName(reference)} template variable to cause Eclipse to automatically add all of the necessary imports (with the exception of the imports for ${baseCollectionType} and the ${concreteCollectionType}, you'll have to add those manually...thank goodness for Cntrl-Shift-M)

This is really hard to describe. You'll have to try it to see exactly how it works. Post a comment if my instructions need to be clarified.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文