Create Java Annotation Wrapper
Asked Answered
U

4

17

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.

Underplot answered 24/10, 2013 at 22:17 Comment(0)
A
2

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

Aromaticity answered 24/10, 2013 at 22:32 Comment(0)
E
1

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(){}
Enos answered 25/12, 2020 at 18:25 Comment(0)
R
0

As far as I know, there are two options that can be currently used to do this:

  • Use the Daileon tool (http://daileon.sourceforge.net/) - since it is more of an experimental tool, its usability is a little bit cumbersome
  • 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.

Randalrandall answered 26/8, 2015 at 7:26 Comment(0)
K
0

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();
}
Kegler answered 14/10, 2016 at 20:18 Comment(1)
Please explain more. In your example is the @Page annotation is the @LibraryAnnotation? Does this only/ always work if the @LibraryAnnotation has @Target(value = {ElementType.ANNOTATION_TYPE})?Aromaticity

© 2022 - 2024 — McMap. All rights reserved.