Java 适配器注释

发布于 2024-12-07 20:11:11 字数 1004 浏览 0 评论 0原文

为了设计尽可能可重用的组件,我最近开始思考所谓的“适配器注释”的可能性。我指的是经典Adapter OO 模式在Java 注释中的应用。例如,假设我有一个完全基于 JUnit 的单元测试套件。我需要按如下方式注释我的所有测试方法:

public class WidgetTest
{
    @Test
    public void test_WidgetConstructor()
    {
        // ...
    }
}

但是,如果在创建包含 1000 个测试类的测试套件后,我决定使用一些全新的超酷测试框架开始单元测试,该框架需要所有要注释的测试方法如下:

public class WidgetTest
{
    @SuperCoolUnitTest(someParam="true")
    public void test_WidgetConstructor()
    {
        // ...
    }
}

现在,在这个特定示例中,搜索并替换旧注释为新注释可能是完全可行的,但在实际应用中,这将是不可行的。一个可行的解决方案。

那么我是否可以用自己开发的东西“包装”我的单元测试注释,例如:

public class WidgetTest
{
    @HomegrownUnitTestAnnotation
    public void test_WidgetConstructor()
    {
        // ...
    }
}

然后构建我自己的注释处理器,将 @HomegrownUnitTestAnnotation 的实例转换为我当前需要的任何注释(<代码>@Test 或@SuperCoolUnitTest(someParam="true"))?

显然,这个问题适用于所有注释,而不仅仅是 JUnit 提供的注释。我基本上是在问是否可以为了可重用性/关注点分离等而包装第 3 方注释。提前致谢!

In an effort to design components that are as reusable as possible, I got to thinking recently about the possibility of so-called "adapter annotations." By this, I mean the application of the classic Adapter OO pattern to Java annotations. So for example, let's say I have a unit testing suite that is entirely JUnit-based. I would need to annotate all of my test methods as follows:

public class WidgetTest
{
    @Test
    public void test_WidgetConstructor()
    {
        // ...
    }
}

But what if, after creating a test suite with, say, 1000 test classes, I decide I want to begin unit testing with some brand new super-cool testing framework that requires all test methods to be annotated as follows:

public class WidgetTest
{
    @SuperCoolUnitTest(someParam="true")
    public void test_WidgetConstructor()
    {
        // ...
    }
}

Now, in this particular example it might be perfectly feasible to search-n-replace the old annotations for the new ones, but in a practicle application, this would not be a feasible solution.

So would it be possible for me "wrap" my unit test annotations with something homegrown, like:

public class WidgetTest
{
    @HomegrownUnitTestAnnotation
    public void test_WidgetConstructor()
    {
        // ...
    }
}

And then build my own annotation processor that would convert instances of @HomegrownUnitTestAnnotation to whichever annotation I currently need (either @Test or @SuperCoolUnitTest(someParam="true"))?

Obviously, this question applies to all annotations, not just those provided by JUnit. I'm basically asking if it is possible to wrap 3rd party annotations for the sake of reusability/separation of concerns/etc. Thanks in advance!

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

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

发布评论

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

评论(1

佼人 2024-12-14 20:11:11

看一下 Spring 的元注释。在我看来,这就是您正在寻找的概念: http://blog.springsource.com/2009/05/06/spring-framework-30-m3-released/

您也许可以将相同的概念应用到您自己的应用程序中。

Have a look at Spring's meta-annotations. It seems to me that this is the concept you're looking for: http://blog.springsource.com/2009/05/06/spring-framework-30-m3-released/.

You may be able to apply the same concept to your own application.

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