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")开发者_StackOverflow中文版
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!
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.
精彩评论