Reputation: 2321
I am currently using an annotation provided by a 3rd party library and I'm wondering if there is a way to create another 'wrapper annotation' around it so that I don't have to require all parameters.
For example I can use the library annotation like this:
@LibraryAnnotation(Parameter1, Parameter2, Parameter3)
But in my case Parameter2 and Parameter3 are always the same so I want to create an annotation that will only take in Parameter1
@MyAnnotation(Parameter1)
But will call the other annotation with all Parameters, similar to how you might create a wrapper for a 3rd party method.
Upvotes: 17
Views: 8027
Reputation: 385
My original tests was like this:
@Test
@SqlGroup(
{
@Sql(
executionPhase = BEFORE_TEST_METHOD,
config = @SqlConfig(transactionMode = ISOLATED),
scripts = {"classpath:test/sqls/_truncate_tables.sql"}
),
@Sql(
executionPhase = AFTER_TEST_METHOD,
config = @SqlConfig(transactionMode = ISOLATED),
scripts = {"classpath:test/sqls/_truncate_tables.sql"}
)
}
)
public void countTeams_countOnEmptyTable_returnsWithEmptyList() {}
And whit this base annotation I cleaned up the test files:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SqlGroup(
{
@Sql(
executionPhase = BEFORE_TEST_METHOD,
config = @SqlConfig(transactionMode = ISOLATED),
scripts = {"classpath:test/sqls/_truncate_tables.sql"}
),
@Sql(
executionPhase = AFTER_TEST_METHOD,
config = @SqlConfig(transactionMode = ISOLATED),
scripts = {"classpath:test/sqls/_truncate_tables.sql"}
)
}
)
And finally I got this clean version:
@Test
@BaseSqlGroup
public void countTeams_countOnEmptyTable_returnsWithEmptyList(){}
Upvotes: 1
Reputation: 6997
You can annotate your annotation with base annotation like in this sample:
@Target(value = {ElementType.TYPE})
@Page(method3="someValue")
public @interface Move {
String method1();
String method2();
}
@Target(value = {ElementType.ANNOTATION_TYPE})
public @interface Page{
String method3();
}
Upvotes: 0
Reputation: 19737
As far as I know, there are two options that can be currently used to do this:
Use Aspect-oriented Programming and Inter-Type Declarations to inject a new annotation, e.g.:
declare @type : @MyAnnotation package.* : @LibraryAnnotation(..);
However, both options are quite limiting.
Upvotes: 0
Reputation: 38132
Annotations are quite limited. Unfortunately, I don't see a way, but I might be wrong.
Upvotes: 2