dimanche 11 avril 2021

How to get list of custom annotations declared in class

I have created custom annotations to get the list of classes which are having these markers on runtime (Working as expected).

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Marker {
  @interface Only{}
  @interface Marker1{}
  @interface Marker2{}
}

I need a mechanism to get the list of the markers declared in these test classes. Below is the code which I am trying to run to get these, but no luck!!

@Marker.Only
public class Sample {

    @Test
    public void main() {
        Assert.assertTrue(true);
    }

    @AfterMethod
    public void m2(ITestResult result) {
        Annotation[] acc = result.getMethod().getRealClass().getAnnotations();
        // Getting blank array as acc
        System.out.println(acc);
    }
}




Aucun commentaire:

Enregistrer un commentaire