Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Java Annotation Wrapper

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.

like image 580
user1751547 Avatar asked Oct 24 '13 22:10

user1751547


2 Answers

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(){}
like image 101
Krizsán Balazs Avatar answered Oct 11 '22 11:10

Krizsán Balazs


Annotations are quite limited. Unfortunately, I don't see a way, but I might be wrong.

like image 2
Puce Avatar answered Oct 11 '22 10:10

Puce